import numpy as np
import scipy as sp
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
%matplotlib inline
#三角函数
def real_func(x: np.array):
return np.sin(2 * np.pi * x)
#多项式
def fit_func(p, x: np.array):
#print("=======p======", type(p))
#print(p)
_func = np.poly1d(p)
return _func(x)
#残差
def residuals_func(p, x, y):
return fit_func(p, x) - y
def rediduals_func_regual(p, x, y):
regularization = 0.0001
ret = fit_func(p, x) - y
return np.append(ret, np.sqrt(0.5 * regularization * np.square(p)))
def fitting(M=0):
"""
M 为 多项式的次数
"""
# 随机初始化多项式参数
p_init = np.random.rand(M + 1)
print("======随机参数====", p_init)
# 最小二乘法
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()
return p_lsq
x = np.linspace(0, 1, 10)
x_points = np.linspace(0, 1, 1000)
y_ = real_func(x)
#随机产生的噪声
y = [np.random.normal(0, 0.1) + y1 for y1 in y_]
def fitting_regual(M=9):
"""
M 为 多项式的次数
"""
# 随机初始化多项式参数
p_init = np.random.rand(M + 1)
print("======随机参数====", p_init)
# 最小二乘法
p_lsq_regual = leastsq(rediduals_func_regual, p_init, args=(x, y))
print(‘Fitting Parameters:‘, p_lsq_regual[0])
# 真实
plt.plot(x_points, real_func(x_points), label=‘real‘)
#带正则项
plt.plot(x_points, fit_func(p_lsq_regual[0], x_points), label=‘regularization‘)
#噪声
plt.plot(x, y, ‘bo‘, label=‘noise‘)
plt.legend()
return p_lsq_regual
原文:https://www.cnblogs.com/spray1982/p/14364547.html