logisticpython
A. 您好,請問python運行二元logistics回歸怎樣設置對照呢
邏輯回歸裡面像男女這種類別類型的特徵,都要轉換成兩個特徵,如果是男,就是01,如專果是女,就是10。你說的屬啞變數,也就是大家常說的獨熱編碼,在sklearn里有onehotencoder,可以去查查怎麼用。拿著個男女屬性被轉化成獨熱編碼以後,通過邏輯回歸,可以算出那個特徵的權重。
B. python邏輯回歸調用哪個包
可以使用機器學習,使用很方便(相當於別人早已經把具體過程做好了,像公式、模板一樣自己代入數據就可以得到結果)
from sklearn.linear_model import LogisticRegression
C. R語言logistic回歸模型
R語言logistic回歸模型
logistic回歸模型為:
對上面的模型進行變換,得到線性形式的logistic回歸模型:
在二項分布族中,logistic回歸是重要的模型。在某些回歸問題中,響應變數是分類的,經常是要麼成功,要麼失敗。
在R語言構建數據框時,應輸入一列成功(響應)的次數和一列不成功(不響應)的次數,例如:
[python] view plain
norell<-data.frame(
x=0:5,n=rep(70,6),success=c(0,9,21,47,60,63)
)
norell$Ymat<-cbind(norell$success,norell$n-norell$success)
glm.sol<-glm(Ymat~x,family=binomial,data=norell)
summary(glm.sol)
#預測並畫出回歸曲線
d<-seq(0, 5, len=100)
pre<-predict(glm.sol, data.frame(x = d))
p<-exp(pre)/(1+exp(pre))
norell$y<-norell$success/norell$n
plot(norell$x, norell$y); lines(d, p)
得到回歸方程(變換後的)右側為:-3.3010+1.2459X
於是回歸方程為:
D. 怎麼用python做logistic回歸
Logistic回歸主要分為三類,一種是因變數為二分類得logistic回歸,這種回歸叫做二項專logistic回歸,一種是因變數屬為無序多分類得logistic回歸,比如傾向於選擇哪種產品,這種回歸叫做多項logistic回歸。還有一種是因變數為有序多分類的logistic回...
E. 誰會多項式logistic回歸分析
不知道你想基於什麼軟體進行 多項式logistic回歸分析,這里提供兩種:
1、Python
使用statsmodels包中的MNLogit模塊
2、Minitab
F. Python3.4機器學習的Logistic回歸演算法的stocGradAscent1(dataMatrix, classLabels, numIter=150)問題求解
把del那句改成del(list(dataIndex)[randIndex])
G. 怎麼看邏輯回歸的python代碼
你把大於0,改成大於等於0,再重新試試。 另外你的邏輯弄得復雜了,好好想想,把邏輯簡化一下。
如果你會畫狀態圖,可以畫個圖給自己看看,好多邏輯是重復的。
比如if H3MRRFlag == 1: 象這樣的語句是一需要的,直接刪除。因為從python語法角度看,可能會有runtime error, 因為你沒有初始化變數
H. GDA和Logistic方法的區別及相應的python代碼
GDA方法與Logistic方法的主要區別在於這兩個模型的假設不同:GDA方法假設p(x|y)服從多元高斯分布,並且輸入特徵是連續的;Logistic方法並沒有GDA那麼強的假設,它既沒有要求p(x|y)服從多元高斯分布,也沒有要求輸入特徵是連續的。因此Logistic的適用范圍比GDA更加廣泛。例如:如果輸入特徵符合泊松分布,則Logistic得到的結果會比GDA更加准確。如果輸入特徵滿足GDA的要求時,既可以用Logistic方法也可以用GDA,但是在這種情況下GDA得到的結果會比Logistic方法得到的結果准確些。下面給出GDA和Logistic方法的簡要說明,最後給出相應的 python代碼。
GDA是一種生成學習法,主要利用貝葉斯准則得到後驗分布律,然後通過最大後驗分布對輸入數據進行分類。簡單地說,也就是在給定某個特徵情況下,擁有此特徵的數據屬於哪個類的概率大 就屬於哪個類。GDA的優勢:由於有高斯分布的先驗信息,如果確實符合實際數據,則只需要少量的樣本就可以得到較好的模型。
Logistic是一種判別想學習法,判別學習法通過建立輸入數據與輸出信息之間的映射關系學得p(y|x),這個與生成學習法是不同的。在生成學習法中首先要確定p(x|y)和p(y)。Logistic主要是通過sigmoid函數來確定輸入數據及是將如何進行分類的。Logistic的優勢:具有更高的魯棒性和對數據的分布不明感(不想GDA那樣需要特徵服從高斯分布)。
下面是具體的python代碼:
一、GDA模型的python代碼:
點擊(此處)折疊或打開
def GDA(dataIn, classLabel):
m = len(classLabel);
sum_1 = sum(classLabel);
q = sum_1/(float(m));
notLabel = ones((len(classLabel),),dtype=int)-array(classLabel);
row,col = shape(dataIn);
y0x = y1x = mat(zeros(col));
for i in range(m):
y0x += mat(dataIn[i])*notLabel[i];
y1x += mat(dataIn[i])*classLabel[i];
mean_0 = y0x/(m-sum_1);
mean_1 = y1x/sum_1;
correlation = 0;
for i in range(m):
correlation += (mat(dataIn[i]-mean_0)).T*(mat(dataIn[i]-mean_0))*notLabel[i] \
+(mat(dataIn[i]-mean_1)).T*(mat(dataIn[i]-mean_1))*classLabel[i];
correlation = correlation/m;
return q,mean_0,mean_1,correlation;
def calculate_pxy0(x,n=2):
return ((2*math.pi)**(-n/2))*(linalg.det(correlation)**(-0.5))*exp(-0.5*(x-mean_0).T*correlation.I*(x-mean_0));
def calculate_pxy1(n=2):
return ((2*math.pi)**(-n/2))*(linalg.det(correlation)**(-0.5))*exp(-0.5*(x-mean_1).T*correlation.I*(x-mean_1));
def GDAClass(testPoint,dataIn,classLabel):
import math;
x = testPoint;
q,mean_0,mean_1,correlation = GDA(dataIn,classLabel);
n=shape(dataIn)[0];
py0 = 1-q;
py1 = q;
pxy0 = calculate_pxy0(x,n);
pxy1 = calculate_pxy1(x,n);
if pxy0*py0 > pxy1*py1:
return 0;
return 1;
二、Logistic模型的python代碼:
點擊(此處)折疊或打開
def sigmoid(w,x):
return 1/(1+exp(-w*x))
def logisticRegression(xMat,yMat,maxCycles = 500):
'''
ones((m,n)): 產生m維的向量,且每個值為n
'''
col = shape(xMat)[1];
weight = ones((col,1));
alpha = 0.001;
for j in range(maxCycles):
h = sigmoid(weight,xMat);
err = (yMat-h);
weight += alpha*xMat.transpose*err;
return weight;
I. 怎麼看python中邏輯回歸輸出的解釋
以下為python代碼,由於訓練數據比較少,這邊使用了批處理梯度下降法,沒有使用增量梯度下降法。
##author:lijiayan##data:2016/10/27
##name:logReg.pyfrom numpy import *import matplotlib.pyplot as pltdef loadData(filename):
data = loadtxt(filename)
m,n = data.shape print 'the number of examples:',m print 'the number of features:',n-1 x = data[:,0:n-1]
y = data[:,n-1:n] return x,y#the sigmoid functiondef sigmoid(z): return 1.0 / (1 + exp(-z))#the cost functiondef costfunction(y,h):
y = array(y)
h = array(h)
J = sum(y*log(h))+sum((1-y)*log(1-h)) return J# the batch gradient descent algrithmdef gradescent(x,y):
m,n = shape(x) #m: number of training example; n: number of features x = c_[ones(m),x] #add x0 x = mat(x) # to matrix y = mat(y)
a = 0.0000025 # learning rate maxcycle = 4000 theta = zeros((n+1,1)) #initial theta J = [] for i in range(maxcycle):
h = sigmoid(x*theta)
theta = theta + a * (x.T)*(y-h)
cost = costfunction(y,h)
J.append(cost)
plt.plot(J)
plt.show() return theta,cost#the stochastic gradient descent (m should be large,if you want the result is good)def stocGraddescent(x,y):
m,n = shape(x) #m: number of training example; n: number of features x = c_[ones(m),x] #add x0 x = mat(x) # to matrix y = mat(y)
a = 0.01 # learning rate theta = ones((n+1,1)) #initial theta J = [] for i in range(m):
h = sigmoid(x[i]*theta)
theta = theta + a * x[i].transpose()*(y[i]-h)
cost = costfunction(y,h)
J.append(cost)
plt.plot(J)
plt.show() return theta,cost#plot the decision boundarydef plotbestfit(x,y,theta):
plt.plot(x[:,0:1][where(y==1)],x[:,1:2][where(y==1)],'ro')
plt.plot(x[:,0:1][where(y!=1)],x[:,1:2][where(y!=1)],'bx')
x1= arange(-4,4,0.1)
x2 =(-float(theta[0])-float(theta[1])*x1) /float(theta[2])
plt.plot(x1,x2)
plt.xlabel('x1')
plt.ylabel(('x2'))
plt.show()def classifyVector(inX,theta):
prob = sigmoid((inX*theta).sum(1)) return where(prob >= 0.5, 1, 0)def accuracy(x, y, theta):
m = shape(y)[0]
x = c_[ones(m),x]
y_p = classifyVector(x,theta)
accuracy = sum(y_p==y)/float(m) return accuracy
調用上面代碼:
from logReg import *
x,y = loadData("horseColicTraining.txt")
theta,cost = gradescent(x,y)print 'J:',cost
ac_train = accuracy(x, y, theta)print 'accuracy of the training examples:', ac_train
x_test,y_test = loadData('horseColicTest.txt')
ac_test = accuracy(x_test, y_test, theta)print 'accuracy of the test examples:', ac_test
學習速率=0.0000025,迭代次數=4000時的結果:
似然函數走勢(J = sum(y*log(h))+sum((1-y)*log(1-h))),似然函數是求最大值,一般是要穩定了才算最好。
從上面這個例子,我們可以看到對特徵進行歸一化操作的重要性。
J. 機器學習中,使用邏輯回歸(python)做二分類時,recall,f1_score,support的含義是
假設預測目標為0和1
數據中1的個數為a,預測1的次數為b,預測1命中的次數為c
准確率 precision = c / b
召回率 recall = c / a
f1_score = 2 * precision * recall / (precision + recall)