Python 確定多項(xiàng)式擬合/回歸的階數(shù)實(shí)例
通過 1至10 階來擬合對(duì)比 均方誤差及R評(píng)分,可以確定最優(yōu)的“最大階數(shù)”。
import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression,Perceptron from sklearn.metrics import mean_squared_error,r2_score from sklearn.model_selection import train_test_split X = np.array([-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]).reshape(-1, 1) y = np.array(2*(X**4) + X**2 + 9*X + 2) #y = np.array([300,500,0,-10,0,20,200,300,1000,800,4000,5000,10000,9000,22000]).reshape(-1, 1) x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3) rmses = [] degrees = np.arange(1, 10) min_rmse, min_deg,score = 1e10, 0 ,0 for deg in degrees: # 生成多項(xiàng)式特征集(如根據(jù)degree=3 ,生成 [[x,x**2,x**3]] ) poly = PolynomialFeatures(degree=deg, include_bias=False) x_train_poly = poly.fit_transform(x_train) # 多項(xiàng)式擬合 poly_reg = LinearRegression() poly_reg.fit(x_train_poly, y_train) #print(poly_reg.coef_,poly_reg.intercept_) #系數(shù)及常數(shù) # 測(cè)試集比較 x_test_poly = poly.fit_transform(x_test) y_test_pred = poly_reg.predict(x_test_poly) #mean_squared_error(y_true, y_pred) #均方誤差回歸損失,越小越好。 poly_rmse = np.sqrt(mean_squared_error(y_test, y_test_pred)) rmses.append(poly_rmse) # r2 范圍[0,1],R2越接近1擬合越好。 r2score = r2_score(y_test, y_test_pred) # degree交叉驗(yàn)證 if min_rmse > poly_rmse: min_rmse = poly_rmse min_deg = deg score = r2score print('degree = %s, RMSE = %.2f ,r2_score = %.2f' % (deg, poly_rmse,r2score)) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(degrees, rmses) ax.set_yscale('log') ax.set_xlabel('Degree') ax.set_ylabel('RMSE') ax.set_title('Best degree = %s, RMSE = %.2f, r2_score = %.2f' %(min_deg, min_rmse,score)) plt.show()
因?yàn)橐蜃兞?Y = 2*(X**4) + X**2 + 9*X + 2 ,自變量和因變量是完整的公式,看圖很明顯,degree >=4 的都符合,擬合函數(shù)都正確。(RMSE 最小,R平方非負(fù)且接近于1,則模型最好)
如果將 Y 值改為如下:
y = np.array([300,500,0,-10,0,20,200,300,1000,800,4000,5000,10000,9000,22000]).reshape(-1, 1)
degree=3 是最好的,且 r 平方也最接近于1(注意:如果 R 平方為負(fù)數(shù),則不準(zhǔn)確,需再次測(cè)試。因樣本數(shù)據(jù)較少,可能也會(huì)判斷錯(cuò)誤)。
以上這篇Python 確定多項(xiàng)式擬合/回歸的階數(shù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python-opencv 中值濾波{cv2.medianBlur(src, ksize)}的用法
這篇文章主要介紹了python-opencv 中值濾波{cv2.medianBlur(src, ksize)}的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-06-06Python對(duì)象的生命周期源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python對(duì)象的生命周期源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05教你用Python實(shí)現(xiàn)簡(jiǎn)易版學(xué)生信息管理系統(tǒng)(含源碼)
學(xué)生管理信息系統(tǒng)主要用來日常查詢學(xué)生信息,以及及時(shí)更新數(shù)據(jù)和修改數(shù)據(jù).用python實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理信息系統(tǒng)不僅可以滿足以上要求,也可以鞏固之前學(xué)習(xí)的基礎(chǔ),需要的朋友可以參考下2021-06-06Python中str is not callable問題詳解及解決辦法
這篇文章主要介紹了Python中str is not callable問題詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02python開發(fā)中range()函數(shù)用法實(shí)例分析
這篇文章主要介紹了python開發(fā)中range()函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python中range()函數(shù)遍歷列表的相關(guān)技巧,需要的朋友可以參考下2015-11-11一起來學(xué)習(xí)一下python的數(shù)據(jù)類型
這篇文章主要為大家詳細(xì)介紹了python的數(shù)據(jù)類型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下希望能夠給你帶來幫助2022-01-01