第1章 统计学习方法概论
- 高斯于1823年在误差e1 ,… , en独立同分布的假定下,证明了最小二乘方法的一个最优性质: 在所有无偏的线性估计类中,最小二乘方法是其中方差最小的!
- 无偏估计是用样本统计量来估计总体参数时的一种无偏推断。 估计量的数学期望等于被估计参数的真实值,则称此此估计量为被估计参数的无偏估计,即具有无偏性,是一种用于评价估计量优良性的准则。 无偏估计的意义是:在多次重复下,它们的平均数接近所估计的参数真值。
使用最小二乘法拟和曲线
对于数据
拟合出函数
有误差,即残差:
此时L2范数(残差平方和)最小时,h(x) 和 y 相似度最高,更适合拟合
一般的H(x)为n次的多项式,
为参数
最小二乘法就是要找到一组 使得 (残差平方和) 最小
即,求
例如书本P11,例1.1中,我们用目标函数, 加上一个正太分布的噪音干扰,用多项式去拟合
代码:
import numpy as np
import scipy as sp
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
%matplotlib inline
ps: numpy.poly1d([1,2,3]) 生成
# 目标函数
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
# 十个点
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(M=0):
"""
n 为 多项式的次数
"""
# 随机初始化多项式参数
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()
return p_lsq
# M=0
p_lsq_0 = fitting(M=0)
# M=1
p_lsq_1 = fitting(M=1)
# M=9
p_lsq_9 = fitting(M=9)
当M=9时,多项式曲线通过了每个数据点,但是造成了过拟合
1.0.2 正则化
结果显示过拟合, 引入正则化项(regularizer),降低过拟合。
模型选择的典型方法是正则化,正则化是结构风险最小化策略的实现,是在经验风险上加一个正则化向或惩罚项。正则化项一般是模型复杂度的单调递减函数,模型月复杂,正则化值越大。
正则化一般具有如下形式:
。并使得Q(x)取得最小
$Q(x)
回归问题中,损失函数是平方损失,正则化可以是参数向量的L2范数,也可以是L1范数。
-
L1: regularization*abs§
-
L2: 0.5 * regularization * np.square§
regularization = 0.0001
def residuals_func_regularization(p, x, y):
ret = fit_func(p, x) - y
ret = np.append(ret, np.sqrt(0.5*regularization*np.square(p))) # L2范数作为正则化项
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()