统计学第一章--最小二乘拟合正弦函数,正则化
生活随笔
收集整理的這篇文章主要介紹了
统计学第一章--最小二乘拟合正弦函数,正则化
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#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)# 多項(xiàng)式
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# 十個(gè)點(diǎn)
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 為 多項(xiàng)式的次數(shù)"""# 隨機(jī)初始化多項(xiàng)式參數(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í)的多項(xiàng)式系數(shù)。?
?
M=0,即多項(xiàng)式為常數(shù)時(shí)?
M=1, 即多項(xiàng)式為一次項(xiàng)時(shí)
?M=3,即多項(xiàng)式為三次項(xiàng)時(shí),可看出擬合的比較不錯(cuò)
M=9時(shí),可看出過擬合了
引入正則化
#加入正則 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ù)作為正則化項(xiàng)return ret # 最小二乘法,加正則化項(xiàng) 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)容,希望文章能夠幫你解決所遇到的問題。