第1章 统计学习方法概论¶
1.统计学习是关于计算机基于数据构建概率统计模型并运用模型对数据进行分析与预测的一门学科。统计学习包括监督学习、非监督学习、半监督学习和强化学习。
2.统计学习方法三要素——模型、策略、算法,对理解统计学习方法起到提纲挈领的作用。
3.本书主要讨论监督学习,监督学习可以概括如下:从给定有限的训练数据出发, 假设数据是独立同分布的,而且假设模型属于某个假设空间,应用某一评价准则,从假设空间中选取一个最优的模型,使它对已给训练数据及未知测试数据在给定评价标准意义下有最准确的预测。
4.统计学习中,进行模型选择或者说提高学习的泛化能力是一个重要问题。如果只考虑减少训练误差,就可能产生过拟合现象。模型选择的方法有正则化与交叉验证。学习方法泛化能力的分析是统计学习理论研究的重要课题。
5.分类问题、标注问题和回归问题都是监督学习的重要问题。本书中介绍的统计学习方法包括感知机、k近邻法、朴素贝叶斯法、决策树、逻辑斯谛回归与最大熵模型、支持向量机、提升方法、EM算法、隐马尔可夫模型和条件随机场。这些方法是主要的分类、标注以及回归方法。它们又可以归类为生成方法与判别方法。
使用最小二乘法拟和曲线¶
高斯于1823年在误差e_1,…,e_n独立同分布的假定下,证明了最小二乘方法的一个最优性质: 在所有无偏的线性估计类中,最小二乘方法是其中方差最小的! 对于数据(x_i, y_i) (i=1, 2, 3...,m)
拟合出函数h(x)
有误差,即残差:r_i=h(x_i)-y_i
此时L2范数(残差平方和)最小时,h(x) 和 y 相似度最高,更拟合
一般的H(x)为n次的多项式,H(x)=w_0+w_1x+w_2x^2+...w_nx^n
w(w_0,w_1,w_2,...,w_n)为参数
最小二乘法就是要找到一组 w(w_0,w_1,w_2,...,w_n) ,使得\sum_{i=1}^n(h(x_i)-y_i)^2 (残差平方和) 最小
即,求 min\sum_{i=1}^n(h(x_i)-y_i)^2
举例:我们用目标函数y=sin2{\pi}x, 加上一个正态分布的噪音干扰,用多项式去拟合【例1.1 11页】
1 2 3 4 5 | 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]) 生成 1x^2+2x^1+3x^0*
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 目标函数 def real_func(x): return np.sin(2*np.pi*x) # 多项式 def fit_func(p, x): #p是一个列表 f = np.poly1d(p) return f(x) # 残差 def residuals_func(p, x, y): ret = fit_func(p, x) - y return ret |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 十个点 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): """ M 为 多项式的次数 """ # 随机初始化多项式参数 p_init = np.random.rand(M + 1) # 最小二乘法 p_lsq = leastsq(residuals_func, p_init, args=(x, y)) #Scipy中最小二乘函数leastsq().误差函数,参数,样本 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¶
1 2 | # M=0 p_lsq_0 = fitting(M=0) |
1 | Fitting Parameters: [0.01170132] |
M=1¶
1 2 | # M=1 p_lsq_1 = fitting(M=1) |
1 | Fitting Parameters: [-1.35280561 0.68810412] |
M=3¶
1 2 | # M=3 p_lsq_3 = fitting(M=3) |
1 | Fitting Parameters: [ 2.05388434e+01 -3.06589850e+01 1.01873054e+01 2.34964215e-04] |
M=9¶
1 2 | # M=9 p_lsq_9 = fitting(M=9) |
1 2 3 | Fitting Parameters: [-7.82939078e+03 3.69175896e+04 -7.35800852e+04 8.04229659e+04 -5.22984941e+04 2.05368707e+04 -4.69766563e+03 5.47021480e+02 -1.88778866e+01 5.93666711e-02] |
当M=9时,多项式曲线通过了每个数据点,但是造成了过拟合
正则化¶
结果显示过拟合, 引入正则化项(regularizer),降低过拟合
Q(x)=\sum_{i=1}^n(h(x_i)-y_i)^2+\lambda||w||^2。
回归问题中,损失函数是平方损失,正则化可以是参数向量的L2范数,也可以是L1范数。
-
L1: regularization*abs(p)
-
L2: 0.5 * regularization * np.square(p)
1 2 3 4 5 6 7 8 | 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 |
1 2 3 4 | # 最小二乘法,加正则化项 p_init = np.random.rand(9 + 1) p_lsq_regularization = leastsq( residuals_func_regularization, p_init, args=(x, y)) |
1 2 3 4 5 6 7 8 | 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() |
1 | <matplotlib.legend.Legend at 0x1a6b7b22da0> |