本文目錄一覽:
- 1、關於python簡單線性回歸
- 2、python邏輯回歸結果怎麼看
- 3、python 的LinearRegression包,怎麼導出回歸模型公式?
- 4、python回歸模型保存
- 5、python怎麼保存回歸結果
- 6、怎麼看python中邏輯回歸輸出的解釋
關於python簡單線性回歸
線性回歸:
設x,y分別為一組數據,代碼如下
import matplotlib.pyplot as plt
import numpy as np
ro=np.polyfit(x,y,deg=1) #deg為擬合的多項式的次數(線性回歸就選1)
ry=np.polyval(ro,x) #忘記x和ro哪個在前哪個在後了。。。
print ro #輸出的第一個數是斜率k,第二個數是縱截距b
plt.scatter(x,y)
plt.plot(x,ry)
python邏輯回歸結果怎麼看
假設預測目標為0和1 數據中1的個數為a,預測1的次數為b,預測1命中的次數為c 準確率 precision = c / b 召回率 recall = c / a f1_score = 2 * precision * recall / (precision + recall)
python 的LinearRegression包,怎麼導出回歸模型公式?
線性回歸是機器學習演算法中最簡單的演算法之一,它是監督學習的一種演算法,主要思想是在給定訓練集上學習得到一個線性函數,在損失函數的約束下,求解相關係數,最終在測試集上測試模型的回歸效果。
也就是說 LinearRegression 模型會構造一個線性回歸公式
y’ = w^T x + b
,其中 w 和 x 均為向量,w 就是係數,截距是 b,得分是根據真實的 y 值和預測值 y’ 計算得到的。
python回歸模型保存
1、首先需要使用公式將回歸結果計算出來。
2、其次選擇回歸。
3、最後將其另存為,另存為到word中就可以保存了。Python由荷蘭數學和計算機科學研究學會的吉多范羅蘇姆於1990年代初設計,作為一門叫做ABC語言的替代品。Python提供了高效的高級數據結構,還能簡單有效地面向對象編程。
python怎麼保存回歸結果
1、需要使用公式將回歸結果計算出來。
2、選擇回歸。
3、結果將其另存為,另存為到word中就可以保存了。
怎麼看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))),似然函數是求最大值,一般是要穩定了才算最好。
下圖為計算結果,可以看到訓練集的準確率為73%,測試集的準確率為78%。
這個時候,我去看了一下數據集,發現沒個特徵的數量級不一致,於是我想到要進行歸一化處理:
歸一化處理句修改列loadData(filename)函數:
def 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]
max = x.max(0)
min = x.min(0)
x = (x – min)/((max-min)*1.0) #scaling y = data[:,n-1:n] return x,y
在沒有歸一化的時候,我的學習速率取了0.0000025(加大就會震蕩,因為有些特徵的值很大,學習速率取的稍大,波動就很大),由於學習速率小,迭代了4000次也沒有完全穩定。現在當把特徵歸一化後(所有特徵的值都在0~1之間),這樣學習速率可以加大,迭代次數就可以大大減少,以下是學習速率=0.005,迭代次數=500的結果:
此時的訓練集的準確率為72%,測試集的準確率為73%
從上面這個例子,我們可以看到對特徵進行歸一化操作的重要性。
原創文章,作者:XXEG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139400.html