日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python最小二乘法拟合_Python 普通最小二乘法(OLS)进行多项式拟合

發布時間:2024/1/23 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python最小二乘法拟合_Python 普通最小二乘法(OLS)进行多项式拟合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多元函數擬合。如 電視機和收音機價格多銷售額的影響,此時自變量有兩個。

python 解法:

importnumpy as npimportpandas as pd#import statsmodels.api as sm #方法一

import statsmodels.formula.api as smf #方法二

importmatplotlib.pyplot as pltfrom mpl_toolkits.mplot3d importAxes3D

df= pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)

X= df[['TV', 'radio']]

y= df['sales']#est = sm.OLS(y, sm.add_constant(X)).fit() #方法一

est = smf.ols(formula='sales ~ TV + radio', data=df).fit() #方法二

y_pred =est.predict(X)

df['sales_pred'] =y_predprint(df)print(est.summary()) #回歸結果

print(est.params) #系數

fig=plt.figure()

ax= fig.add_subplot(111, projection='3d') #ax = Axes3D(fig)

ax.scatter(X['TV'], X['radio'], y, c='b', marker='o')

ax.scatter(X['TV'], X['radio'], y_pred, c='r', marker='+')

ax.set_xlabel('X Label')

ax.set_ylabel('Y Label')

ax.set_zlabel('Z Label')

plt.show()

擬合的各項評估結果和參數都打印出來了,其中結果函數為:

f(sales) = β0 + β1*[TV] + β2*[radio]

f(sales)? = 2.9211 + 0.0458?* [TV] + 0.188 *?[radio]

圖中,sales 方向上,藍色點為原 sales 實際值,紅色點為擬合函數計算出來的值。其實誤差并不大,部分數據如下。

同樣可擬合一元函數;

importnumpy as npimportpandas as pdimportstatsmodels.formula.api as smfimportmatplotlib.pyplot as pltfrom mpl_toolkits.mplot3d importAxes3D

df= pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)

X= df['TV']

y= df['sales']

est= smf.ols(formula='sales ~ TV', data=df).fit()

y_pred=est.predict(X)print(est.summary())

fig=plt.figure()

ax= fig.add_subplot(111)

ax.scatter(X, y, c='b')

ax.plot(X, y_pred, c='r')

plt.show()

Ridge Regression:(嶺回歸交叉驗證)

嶺回歸(ridge regression, Tikhonov regularization)是一種專用于共線性數據分析的有偏估計回歸方法,實質上是一種改良的最小二乘估計法,通過放棄最小二乘法的無偏性,以損失部分信息、降低精度為代價獲得回歸系數更為符合實際、更可靠的回歸方法,對病態數據的擬合要強于最小二乘法。通常嶺回歸方程的R平方值會稍低于普通回歸分析,但回歸系數的顯著性往往明顯高于普通回歸,在存在共線性問題和病態數據偏多的研究中有較大的實用價值。

importnumpy as npimportpandas as pdimportmatplotlib.pyplot as pltfrom sklearn importlinear_modelfrom mpl_toolkits.mplot3d importAxes3D

df= pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)

X= np.asarray(df[['TV', 'radio']])

y= np.asarray(df['sales'])

clf= linear_model.RidgeCV(alphas=[i+1 for i in np.arange(200.0)]).fit(X, y)

y_pred=clf.predict(X)

df['sales_pred'] =y_predprint(df)print("alpha=%s, 常數=%.2f, 系數=%s" %(clf.alpha_ ,clf.intercept_,clf.coef_))

fig=plt.figure()

ax= fig.add_subplot(111, projection='3d')

ax.scatter(df['TV'], df['radio'], y, c='b', marker='o')

ax.scatter(df['TV'], df['radio'], y_pred, c='r', marker='+')

ax.set_xlabel('TV')

ax.set_ylabel('radio')

ax.set_zlabel('sales')

plt.show()

輸出結果:alpha=150.0, 常數=2.94, 系數=[ 0.04575621? 0.18735312]

總結

以上是生活随笔為你收集整理的python最小二乘法拟合_Python 普通最小二乘法(OLS)进行多项式拟合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。