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

python回歸

發布時間: 2022-01-14 08:52:01

python做回歸用什麼庫

許多都可以,推薦numpy、matplotlib、pandas

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

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

㈢ 如何用python實現含有虛擬自變數的回歸



參考資料:
DataRobot | Ordinary Least Squares in Python

DataRoboe | Multiple Regression using Statsmodels

AnalyticsVidhya | 7 Types of Regression Techniques you should know!



㈣ python該如何得到ols回歸後的系數的t值

regstats(y1,x1,'linear','tstat');
c1=ans.tstat.beta(1,1);
beta1=ans.tstat.beta(2,1);
t1=ans.tstat.t(2,1);
c1是常數項,beta1是回歸系數,t1就是beta1的t值,這里是單變數線性回歸

㈤ 使用python進行回歸分析,如何利用Excel的數據生成結果

用pandas+numpy應該可以實現

㈥ python 嶺回歸



所求參數是alpha的函數,比如記為f(alpha),f(alpha)隨alpha的改變的軌跡就是嶺跡。
實際計算中可選非常多的alpha值,做出一個嶺跡圖,看看這個圖在取哪個值的時候變穩定了,
那就確定alpha值了,從而確定參數。

Ridge(alpha=1.0,fit_intercept=False)
model.fit(x,y)

這樣就等於你算的,因為你numpy是用增廣矩陣算的,所以應該將setfit_intercept=False
model.coef_
array([[1.06059732,0.48614918,0.44596739]])

㈦ python線性回歸散點圖怎麼做

import matplotlib.pyplot as plt
plt.scatter(xdata,ydata)
(xdata,ydata為兩個需要作圖的數據集)

㈧ 請教怎樣用Python做stepwise回歸

所說所有的變數都是對象。 對象在python里,其實是一個指針,指向一個數據結構,數據結構里有屬性,有方法。
對象通常就是指變數。從面向對象OO的概念來講,對象是類的一個實例。在python里很簡單,對象就是變數。
class A:
myname="class a"
上面就是一個類。不是對象
a=A()
這里變數a就是一個對象。
它有一個屬性(類屬性),myname,你可以顯示出來
print a.myname
所以,你看到一個變數後面跟點一個小數點。那麼小數點後面。

㈨ 回歸方程表達式:如y=b+a1x1+a2x2+a3x3用python編程如何實現

樣本中心點為橫坐標是x的平均值,縱坐標是y的平均值。
回歸方程所代表的直線經過樣本中心點,單單給出方程表達式,應該是沒法求樣本中心點的!

熱點內容
SQL寫序列 發布:2024-09-20 06:02:29 瀏覽:964
裝緩存下載 發布:2024-09-20 05:42:36 瀏覽:72
gon引擎自動回收腳本 發布:2024-09-20 05:39:39 瀏覽:247
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:99
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:758
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354