當前位置:首頁 » 編程語言 » python邏輯回歸

python邏輯回歸

發布時間: 2022-01-12 08:44:39

1. 怎麼看邏輯回歸的python代碼

你把大於0,改成大於等於0,再重新試試。 另外你的邏輯弄得復雜了,好好想想,把邏輯簡化一下。

如果你會畫狀態圖,可以畫個圖給自己看看,好多邏輯是重復的。

比如if H3MRRFlag == 1: 象這樣的語句是一需要的,直接刪除。因為從python語法角度看,可能會有runtime error, 因為你沒有初始化變數

2. python邏輯回歸調用哪個包

可以使用機器學習,使用很方便(相當於別人早已經把具體過程做好了,像公式、模板一樣自己代入數據就可以得到結果)

from sklearn.linear_model import LogisticRegression

3. Python sklearn 訓練完邏輯回歸模型之後,怎麼使用他做預判

from sklearn import linear_model
建立模型
model = linear_model.LinearRegression()
model.fit(x_train,y_train)
評估模型
score = model.score(x_test, y_test)
預測模型
result = model.predict(x_test)

4. 怎麼看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))),似然函數是求最大值,一般是要穩定了才算最好。

從上面這個例子,我們可以看到對特徵進行歸一化操作的重要性。

5. 機器學習中,使用邏輯回歸(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)

6. python怎麼實現邏輯回歸的梯度下降和梯度上升法有區別嗎

多數函數解不出導數得0的解析解.梯度下降法是種數值演算法,一般可以用計算機求出很好的近似解

7. python怎麼實現邏輯回歸的梯度下降法

import sys

#Training data set
#each element in x represents (x0,x1,x2)
x = [(1,0.,3) , (1,1.,3) ,(1,2.,3), (1,3.,2) , (1,4.,4)]
#y[i] is the output of y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
y = [95.364,97.217205,75.195834,60.105519,49.342380]

epsilon = 0.0001
#learning rate
alpha = 0.01
diff = [0,0]
max_itor = 1000
error1 = 0
error0 =0
cnt = 0
m = len(x)

#init the parameters to zero
theta0 = 0
theta1 = 0
theta2 = 0

while True:

cnt = cnt + 1

#calculate the parameters
for i in range(m):

diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )

theta0 = theta0 + alpha * diff[0] * x[i][0]
theta1 = theta1 + alpha * diff[0]* x[i][1]
theta2 = theta2 + alpha * diff[0]* x[i][2]

#calculate the cost function
error1 = 0
for lp in range(len(x)):
error1 += ( y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] ) )**2/2

if abs(error1-error0) < epsilon:
break
else:
error0 = error1

print ' theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f'%(theta0,theta1,theta2,error1)

print 'Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2)

8. python做邏輯回歸 怎麼把導入的數據分成x,y

簡介
本例子是通過對一組邏輯回歸映射進行輸出,使得網路的權重和偏置達到最理想狀態,最後再進行預測。其中,使用GD演算法對參數進行更新,損耗函數採取交叉商來表示,一共訓練10000次。
2.python代碼
#!/usr/bin/python

import numpy
import theano
import theano.tensor as T
rng=numpy.random

N=400
feats=784
# D[0]:generate rand numbers of size N,element between (0,1)
# D[1]:generate rand int number of size N,0 or 1
D=(rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
training_steps=10000

# declare symbolic variables
x=T.matrix('x')
y=T.vector('y')
w=theano.shared(rng.randn(feats),name='w') # w is shared for every input
b=theano.shared(0.,name='b') # b is shared too.

print('Initial model:')
print(w.get_value())
print(b.get_value())

# construct theano expressions,symbolic
p_1=1/(1+T.exp(-T.dot(x,w)-b)) # sigmoid function,probability of target being 1
prediction=p_1>0.5
xent=-y*T.log(p_1)-(1-y)*T.log(1-p_1) # cross entropy
cost=xent.mean()+0.01*(w**2).sum() # cost function to update parameters
gw,gb=T.grad(cost,[w,b]) # stochastic gradient descending algorithm

#compile
train=theano.function(inputs=[x,y],outputs=[prediction,xent],updates=((w,w-0.1*gw),(b,b-0.1*gb)))
predict=theano.function(inputs=[x],outputs=prediction)

# train
for i in range(training_steps):
pred,err=train(D[0],D[1])

print('Final model:')
print(w.get_value())
print(b.get_value())
print('target values for D:')
print(D[1])
print('prediction on D:')
print(predict(D[0]))

print('newly generated data for test:')
test_input=rng.randn(30,feats)
print('result:')
print(predict(test_input))

3.程序解讀
如上面所示,首先導入所需的庫,theano是一個用於科學計算的庫。然後這里我們隨機產生一個輸入矩陣,大小為400*784的隨機數,隨機產生一個輸出向量大小為400,輸出向量為二值的。因此,稱為邏輯回歸。
然後初始化權重和偏置,它們均為共享變數(shared),其中權重初始化為較小的數,偏置初始化為0,並且列印它們。
這里我們只構建一層網路結構,使用的激活函數為logistic sigmoid function,對輸入量乘以權重並考慮偏置以後就可以算出輸入的激活值,該值在(0,1)之間,以0.5為界限進行二值化,然後算出交叉商和損耗函數,其中交叉商是代表了我們的激活值與實際理論值的偏離程度。接著我們使用cost分別對w,b進行求解偏導,以上均為符號表達式運算。
接著我們使用theano.function進行編譯優化,提高計算效率。得到train函數和predict函數,分別進行訓練和預測。
接著,我們對數據進行10000次的訓練,每次訓練都會按照GD演算法進行更新參數,最後我們得到了想要的模型,產生一組新的輸入,即可進行預測。

9. python邏輯回歸結果怎麼看

假設預測目標為0和1 數據中1的個數為a,預測1的次數為b,預測1命中的次數為c 准確率 precision = c / b 召回率 recall = c / a f1_score = 2 * precision * recall / (precision + recall)

10. python sklearn邏輯回歸怎麼調參

from sklearn import linear_model#線性回歸clf = linear_model.LinearRegression()#訓練clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])#表達式參數clf.coef_#測試improt numpy as npx = np.array([1,1])y = x.dot(clf.coef_)

熱點內容
crv哪個配置性價比高2021 發布:2024-09-17 04:07:51 瀏覽:35
wincc圖形編譯在哪裡 發布:2024-09-17 03:58:26 瀏覽:977
androidubuntu 發布:2024-09-17 03:50:27 瀏覽:701
識夢源碼 發布:2024-09-17 03:50:18 瀏覽:26
諾基亞密碼忘了打什麼電話 發布:2024-09-17 03:27:09 瀏覽:555
樹深度優先演算法 發布:2024-09-17 03:26:58 瀏覽:472
跳轉頁源碼 發布:2024-09-17 03:13:05 瀏覽:543
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:785
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:726
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688