# encoding:utf-8
import numpy as np
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
"""我们用目标函数y=sin2πx,加上一个正态分布的噪音干扰,用多项式去拟合(例1.1 11页)"""
"""目标函数"""
def real_func(x):
return np.sin(2 * np.pi * x)
"""多项式"""
def fit_func(p, x):
"""生成一个多项式"""
f = np.poly1d(p)
return f(x)
"""残差"""
def residuals_func(p, x, y):
ret = fit_func(p, x) - y
return ret
"""生成十个点"""
"""linspace()函数是创建等差数列的函数 """
"""第一个参数表示起始点、第二个参数表示终止点,第三个参数表示数列的个数"""
x = np.linspace(0, 1, 10)
x_points = np.linspace(0, 1, 100)
"""加上正态分布噪音的目标函数的值"""
y_ = real_func(x)
y = [np.random.normal(0, 0.1) + y1 for y1 in y_]
def fitting(M=0):
"""M为多项式的次数"""
"""随机初始化多项式参数"""
p_init = np.random.rand(M + 1)
"""最小二乘法"""
"""最小二乘函数leastsq()"""
"""
residuals_func:误差函数
p_init:表示函数的参数
args()表示数据点
"""
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)
regularization = 0.0001
def residuals_func_regularization(p, x, y):
ret = fit_func(p, x) - y
"""L2范数作为正则化项"""
ret = np.append(ret, np.sqrt(0.5 * regularization * np.square(p)))
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()
- M=0
Fitting Parameters: [ 0.06225187]
- M=1
Fitting Parameters: [-1.52289456 0.82369915]
- M=3
Fitting Parameters: [ 19.78479794 -29.56849209 9.62863747 0.15588463]
- M=9
Fitting Parameters: [ 1.85650557e+04 -8.53696108e+04 1.66671311e+05
-1.79727475e+05 1.16312355e+05 -4.57586021e+04
1.05187764e+04 -1.27770807e+03 6.57051632e+01
1.94913510e-01]
-
最小二乘法,加正则化项(L2范数作为正则化项)