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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

统计学第一章--最小二乘拟合正弦函数,正则化

發(fā)布時間:2024/7/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 统计学第一章--最小二乘拟合正弦函数,正则化 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#coding:utf-8 import numpy as np import scipy as sp from scipy.optimize import leastsq import matplotlib.pyplot as plt # 目標(biāo)函數(shù) def real_func(x):return np.sin(2*np.pi*x)# 多項式 def fit_func(p, x):f = np.poly1d(p)# print('f=',f)return f(x)# 殘差 def residuals_func(p, x, y):ret = fit_func(p, x) - yreturn ret# 十個點 x = np.linspace(0, 1, 10) x_points = np.linspace(0, 1, 1000) # 加上正態(tài)分布噪音的目標(biāo)函數(shù)的值 y_ = real_func(x) y = [np.random.normal(0, 0.1) + y1 for y1 in y_]def fitting(M=0):"""M 為 多項式的次數(shù)"""# 隨機初始化多項式參數(shù)p_init = np.random.rand(M + 1)# 最小二乘法p_lsq = leastsq(residuals_func, p_init, args=(x, y))print('Fitting Parameters:', p_lsq[0])## 可視化plt.plot(x_points, real_func(x_points), label='real')plt.plot(x_points, fit_func(p_lsq[0], x_points), label='fitted curve')plt.plot(x, y, 'bo', label='noise')plt.legend()plt.show()return p_lsq # M=0 p_lsq_0 = fitting(M=0) # M=1 p_lsq_1 = fitting(M=1) # M=3 p_lsq_3 = fitting(M=3) # M=9 p_lsq_9 = fitting(M=9)

M分別為0,1,3,9時的多項式系數(shù)。?

?

M=0,即多項式為常數(shù)時?

M=1, 即多項式為一次項時

?M=3,即多項式為三次項時,可看出擬合的比較不錯

M=9時,可看出過擬合了

引入正則化

#加入正則 regularization = 0.0001 def residuals_func_regularization(p, x, y):ret = fit_func(p, x) - yret = np.append(ret, np.sqrt(0.5*regularization*np.square(p))) # L2范數(shù)作為正則化項return ret # 最小二乘法,加正則化項 p_init = np.random.rand(9+1) p_lsq_regularization = leastsq(residuals_func_regularization, p_init, args=(x, y)) plt.plot(x_points, real_func(x_points), label='real') plt.plot(x_points, fit_func(p_lsq_9[0], x_points), label='fitted curve') plt.plot(x_points, fit_func(p_lsq_regularization[0], x_points), label='regularization') plt.plot(x, y, 'bo', label='noise') plt.legend() plt.show()

可看出:正則化有效?

?

總結(jié)

以上是生活随笔為你收集整理的统计学第一章--最小二乘拟合正弦函数,正则化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。