4.3 欠拟合与过拟合
生活随笔
收集整理的這篇文章主要介紹了
4.3 欠拟合与过拟合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、數據生成
- 二、正常擬合
- 效果
- 代碼
- 三、欠擬合
- 效果
- 代碼
- 四、過擬合
- 效果
- 代碼
一、數據生成
#采用多項式回歸測試欠擬合與過擬合 import numpy as np import matplotlib.pyplot as plt#數據集生成 #數據集大小 start = 15 end = 85#x獲取 x = np.arange(start,end+1).reshape(-1,1) #階數 degree = 5 #1 x x^2 x^3 x^4 poly_degree_x = np.power(x,np.arange(degree))#多項式系數 w = np.array([3840000,-400000,14000,-200,1])#y值計算 向量點積(這兒是矩陣*向量) y = np.dot(poly_degree_x,w)#擾動添加 y_r = y + np.random.normal(-20000,20000, size=y.shape) x.shape,y.shape,y_r.shape二、正常擬合
效果
代碼
#過擬合 4次多項式 ret = np.polyfit(x.reshape(-1),y_r,4) ret_poly = np.poly1d(ret) ret_y = ret_poly(x.reshape(-1)) plt.plot(x,y,'*') plt.plot(x,ret_y,'r')三、欠擬合
效果
代碼
#欠擬合 2次多項式 ret = np.polyfit(x.reshape(-1),y_r,2) ret_poly = np.poly1d(ret) ret_y = ret_poly(x.reshape(-1)) plt.plot(x,y,'*') plt.plot(x,ret_y,'r')四、過擬合
效果
代碼
#過擬合 30次多項式 ret = np.polyfit(x.reshape(-1),y_r,30) ret_poly = np.poly1d(ret) ret_y = ret_poly(x.reshape(-1)) plt.plot(x,y,'*') plt.plot(x,ret_y,'r')總結
以上是生活随笔為你收集整理的4.3 欠拟合与过拟合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汇编语言练习_1_数字分解_显示
- 下一篇: 4.4 权重衰减