第一章 统计学习方法
基本知识
附上思维导图:
-
统计学习
- 对象
- 数字
- 文字
- 图像
- 视频
- 音频
- …
- 目的
- 预测
- 分析
- 方法
- Supervised learning
- 训练数据(training data)
- 模型(model) —假设空间 hypothesis
- 评价准则 evaluation criterion ---- 策略 strategy
- 算法 algorithm
- Unsupervised learning
- Semi-supervised learning
- Reinforcement learning
- Supervised learning
- 研究
- 方法
- 理论
- 统计学习方法的有效性和效率的基本理论
- 应用
- 对象
-
监督学习
-
特征向量
-
训练集
-
输入变量和输出变量
- 分类问题
- 回归问题
- 标注问题
-
联合概率分布
-
假设空间
-
问题的形式化

-
-
统计学习三要素
- 方法 = 模型 + 策略 + 算法
- 模型
- 决策函数
- 条件概率
- 策略
- 损失函数(一次预测的好坏)
- 0-1损失函数
- 平方损失函数
- 绝对损失函数
- 对数损失函数
- 风险函数(平均意义下模型预测的好坏)
- 经验风险最小化
- 结构风险最小化(正则化)
- 损失函数(一次预测的好坏)
- 算法
- 如果最优化问题有显式的解析式, 算法比较简单
- 但通常解析式不存在, 就需要数值计算的方法
-
模型评估与模型选择
- 训练误差
- 测试误差
- 准确率
-
正则化与交叉验证
- 正则化
- 回归问题
- 交叉验证
- 训练集
- 验证集
- 测试集
- 方法
- 简单交叉验证
- S折交叉验证
- 留一交叉验证
-
泛化能力
-
生成模型与判别模型
- 生成模型
- 朴素贝叶斯法
- 判别模型
- K近邻
- 感知机
- 决策树
- logistic回归模型
- 最大熵模型
- 支持向量机
- 提升方法
- 条件随机场
- 生成模型
-
分类问题
- 精确率
- 针对本人,意思你给出的结果有多少是正确的.
- 召回率
- 针对样本,意思正确的样本有多少是你给出来的.
- F1值
- 精确率
-
标注问题
-
回归问题
- 函数拟合
代码小练习
回归问题
用最小二乘法拟合曲线
对于数据
(
x
i
,
y
i
)
(
i
=
1
,
2
,
3...
,
m
)
(x_i, y_i)(i=1, 2, 3...,m)
(xi,yi)(i=1,2,3...,m)
假设函数: h ( x ) h(x) h(x), w ( w 0 , w 1 , w 2 , . . . , w n ) w(w_0,w_1,w_2,...,w_n) w(w0,w1,w2,...,wn)为参数.
求 m i n ∑ i = 1 n ( h ( x i ) − y i ) 2 min\sum_{i=1}^n(h(x_i)-y_i)^2 min∑i=1n(h(xi)−yi)2
举例:
我们用目标函数 y=sin2πxy=sin2πx , 加上一个正太分布的噪音干扰,用多项式去拟合
import numpy as np
import scipy as sp
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
%matplotlib inline
# 目标函数
def real_func(x):
return np.sin(2*np.pi*x)
numpy.polyld([1,2,3])生成 1 x 2 + 2 x 1 + 3 x 0 1x^2+2x^1+3x^0 1x2+2x1+3x0
# 定义多项式
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)+i for i in y_]
# 最小二次法拟合
def fitting(M=0):
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
# M=0
p_lsq_0 = fitting(M=0)
Fitting Parameters: [-0.03650779]

# M=1
p_lsq_1 = fitting(M=1)
Fitting Parameters: [-1.3831543 0.65506936]

# M=3
p_lsq_3 = fitting(M=3)
Fitting Parameters: [ 22.56376565 -33.92286776 11.53591186 -0.13635256]

# M=9
p_lsq_9 = fitting(M=9)
Fitting Parameters: [ -1.45300294e+04 7.00123796e+04 -1.41647259e+05 1.55934574e+05
-1.01185195e+05 3.91798680e+04 -8.70651951e+03 9.77069058e+02
-3.48259543e+01 -9.27935645e-02]

M = 9时,多项式曲线通过每个数据点,但是造成了过拟合
正则化
-
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
# 最小二次法拟合
def fitting1(M=0):
p_init = np.random.rand(M+1)
# print(p_init)
p_lsq = leastsq(residuals_func_regularization,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
fitting1(9)
Fitting Parameters: [ -8.85657048 -1.5809234 4.78738062 8.6677364 8.29833613
2.58889648 -7.25474857 -14.92464875 8.32084776 -0.07639554]
(array([ -8.85657048, -1.5809234 , 4.78738062, 8.6677364 ,
8.29833613, 2.58889648, -7.25474857, -14.92464875,
8.32084776, -0.07639554]), 1)




被折叠的 条评论
为什么被折叠?



