机器学习入门介绍

1. 机器学习介绍

1.1 基本术语

机器学习:利用经验、通过计算、构建模型,来改善系统自身的性能。
属性(特征):描述事物在特定方面的表现或性质的事项。
属性值:属性上的取值。
属性空间(输入空间):属性的所有可能取值构成的集合,若属性为多维特征则是由多维属性张成的空间,属性空间 X \mathcal{X} X

记录(样本、实例):一个具体事物的属性描述,由属性向量表示。

例如:第 j j j个记录 x j \mathbf{x}_j xj的属性向量,表示如下
x j = ( x j ( 1 ) , x j ( 2 ) , ⋯   , x j ( i ) , ⋯   , x j ( n ) ) ⊤ , j = 1 , 2 , ⋯   , N i = 1 , 2 , ⋯   , n x j ∈ X \mathbf{x} _ { j } = \left( x _ { j } ^ { ( 1 ) } , x _ { j } ^ { ( 2 ) } , \cdots , x _ { j } ^ { ( i ) } , \cdots , x _ { j } ^ { ( n ) } \right) ^ { \top } , \quad j = 1,2 , \cdots , N \quad i=1,2,\cdots,n \quad\mathbf{x}_j\in\mathcal{X} xj=(xj(1),xj(2),,xj(i),,xj(n)),j=1,2,,Ni=1,2,,nxjX

其中, x j ( i ) x_j^{\left(i\right)} xj(i)为第 j j j个记录的第 i i i个属性。(这里为啥要用 ⊤ \top 转置是因为机器学习中默认向量为列向量,由于列向量太占空间,所以这里用行向量加 ⊤ \top 转置表示列向量)

标记:描述事物某个特性或结果的事项。
标记值:标记上的取值。
标记空间(输出空间):标记的所有取值构成的集合,标记空间 Y \mathcal{Y} Y

样例:拥有了对应标记的记录,由(记录,标记)对表示。第 j j j个样例
( x j , y j ) , j = 1 , 2 , … , N , x j ∈ X , y j ∈ Y 。 \left(\mathbf{x}_j,y_j\right),\quad j=1,2,\dots,N,\quad\mathbf{x}_j\in\mathcal{X},y_j\in\mathcal{Y}。 (xj,yj),j=1,2,,N,xjX,yjY

数据集
记录的集合(无监督学习数据集), D = { x 1 , x 2 , … , x N } D=\{\mathbf{x}_1,\mathbf{x}_2,\dots,\mathbf{x}_N\} D={x1,x2,,xN}
样例的集合(有监督学习数据集), D = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x N , y N ) } D=\{\left(\mathbf{x}_1,y_1\right),\left(\mathbf{x}_2,y_2\right),\dots,\left(\mathbf{x}_N,y_N\right)\} D={(x1,y1),(x2,y2),,(xN,yN)}

有监督学习:

  1. 回归学习:有监督学习中,标记为连续值, Y = R \mathcal{Y}=\mathbb{R} Y=R
  2. 分类学习:有监督学习中,标记为离散值。其中,
    ∣ Y ∣ = 2 , Y = { 0 , 1 } |\mathcal{Y}|=2,\mathcal{Y}=\{0,1\} Y=2,Y={0,1} Y = { + 1 , − 1 } \mathcal{Y}=\{+1,-1\} Y={+1,1},则为二分类;
    ∣ Y ∣ > 2 , Y = { c 1 , c 2 , … , c m } |\mathcal{Y}|>2,\mathcal{Y}=\{c_1,c_2,\dots,c_m\} Y>2,Y={c1,c2,,cm},则为多分类。

训练数据集:用以训练模型的数据集的子集, D t r a i n i n g ⊆ D D_{training} \subseteq D DtrainingD
验证数据集:用以选择模型的数据集的子集, D v e r i f i c a t i o n ⊆ D D_{verification}\subseteq D DverificationD
测试数据集:应用测试模型的数据集的子集, D t e s t i n g ⊆ D D_{testing} \subseteq D DtestingD

1.2 假设空间与参数空间

有监督学习的目的在于学习一个从输入空间 X \mathcal{X} X到输出空间 Y \mathcal{Y} Y的映射 f f f,或是条件概率 P ( Y ∣ X ) P\left(Y|X\right) P(YX)

决策函数(非概率模型):从输入空间 X \mathcal{X} X到输出空间 Y \mathcal{Y} Y的映射 f : X → Y f:\mathcal{X}\to\mathcal{Y} f:XY
假设空间 F \mathcal{F} F定义为决策函数的集合
F = { f ∣ Y = f ( X ) } \mathcal{F} = \left\{ f | Y = f \left( \mathbf{X} \right) \right\} F={fY=f(X)}
其中, X \mathbf{X} X是定义在输入空间 X \mathcal{X} X上的变量, X ∈ X \mathbf{X}\in\mathcal{X} XX Y Y Y是定义在输出空间 Y \mathcal{Y} Y上的变量, Y ∈ Y Y\in\mathcal{Y} YY

假设空间 F \mathcal{F} F通常是由一个参数向量决定的函数族
F = { f ∣ Y = f w ( X ) , w ∈ R n } \mathcal{F} = \left\{ f | Y = f_{\mathbf{w}} \left( \mathbf{X} \right), \mathbf{w} \in \mathbb{R}^{n} \right\} F={fY=fw(X),wRn}
其中,参数向量 w = ( w ( 1 ) , ⋯   , w ( n ) ) ⊤ \mathbf{w}=\left(w^{\left(1\right)},\cdots,w^{\left(n\right)}\right)^\top w=(w(1),,w(n))取值于 n n n维向量空间 R n \mathbb{R}^{n} Rn,称为参数空间。

概率模型:假设空间 F \mathcal{F} F定义为条件概率的集合
F = { P ∣ P ( Y ∣ X ) } \mathcal{F} = \left\{ P | P \left( Y | \mathbf{X} \right) \right\} F={PP(YX)}
其中, X \mathbf{X} X是定义在输入空间 X \mathcal{X} X上的随机变量, Y Y Y是定义在输出空间 Y \mathcal{Y} Y上的随机变量。

假设空间 F \mathcal{F} F通常是由一个参数向量决定的概率分布族
F = { P ∣ P w ( Y ∣ X ) , w ∈ R n } \mathcal{F} = \left\{ P | P_{\mathbf{w}} \left( Y | \mathbf{X} \right), \mathbf{w} \in \mathbb{R}^{n} \right\} F={PPw(YX),wRn}
其中,参数向量 w \mathbf{w} w取值于 n n n维向量空间 R n \mathbb{R}^{n} Rn,称为参数空间。

1.3 模型策略

1.3.1 损失函数

损失函数:度量模型预测错误的程度,是模型的预测输出 f ( X ) f\left(\mathbf{X}\right) f(X)和实际输出 Y Y Y的非负实值函数,记作 L ( Y , f ( X ) ) L \left(Y, f \left( \mathbf{X} \right) \right) L(Y,f(X))

0 − 1 0-1 01 损失函数表示如下
L ( Y , f ( X ) ) = {   1 , Y ≠ f ( X ) 0 , Y = f ( X ) = I ( Y ≠ f ( X ) ) \begin{aligned} L \left(Y, f \left( \mathbf{X} \right) \right) &= \left\{ \begin{aligned} \ & 1, \quad Y \neq f \left( \mathbf{X} \right) \\ & 0, \quad Y = f \left( \mathbf{X} \right) \end{aligned} \right. \\ &=I\left(Y \neq f \left( \mathbf{X}\right)\right)\end{aligned} L(Y,f(X))={ 1,Y=f(X)0,Y=f(X)=I(Y=f(X))
其中, I ( ⋅ ) I\left(\cdot\right) I()是指示函数,当括号中条件成立返回 1 1 1,否则返回 0 0 0

1.3.2 平方损失函数

L ( Y , f ( X ) ) = ( Y − f ( X ) ) 2 \begin{aligned} L \left(Y, f \left( \mathbf{X} \right) \right) = \left( Y - f \left( \mathbf{X} \right) \right)^{2} \end{aligned} L(Y,f(X))=(Yf(X))2

1.3.3 绝对值损失函数

L ( Y , f ( X ) ) = ∣ Y − f ( X ) ∣ \begin{aligned} L \left(Y, f \left( \mathbf{X} \right) \right) = \left| Y - f \left( \mathbf{X} \right) \right| \end{aligned} L(Y,f(X))=Yf(X)

1.3.4 对数似然损失函数

L ( Y , P ( Y ∣ X ) ) = − log ⁡ P ( Y ∣ X ) \begin{aligned} L \left(Y, P \left( Y | \mathbf{X} \right) \right) = - \log P \left( Y | \mathbf{X} \right) \end{aligned} L(Y,P(YX))=logP(YX)

1.3.5 经验损失

经验风险(经验损失)是模型 f f f关于训练数据集 D D D
D = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , ⋯   , ( x N , y N ) } \begin{aligned} \\& D = \left\{ \left( \mathbf{x}_{1}, y_{1} \right), \left( \mathbf{x}_{2}, y_{2} \right), \cdots, \left( \mathbf{x}_{N}, y_{N} \right) \right\} \end{aligned} D={(x1,y1),(x2,y2),,(xN,yN)}
的平均损失
R e m p ( f ) = 1 N ∑ i = 1 N L ( y i , f ( x i ) ) \begin{aligned} R_{emp} \left( f \right) = \dfrac{1}{N} \sum_{i=1}^{N} L \left(y_{i}, f \left( \mathbf{x}_{i} \right) \right) \end{aligned} Remp(f)=N1i=1NL(yi,f(xi))

学习目标:经验风险最小化
f ∗ = arg ⁡ min ⁡ f ∈ F R e m p ( f ) f^*=\mathop{\arg\min}\limits_{f\in\mathcal{F}} R_{emp}\left(f\right) f=fFargminRemp(f)
其中, F \mathcal{F} F是假设空间。

在这里插入图片描述

1.3.6 过拟合

学习时选择的模型复杂,所包含的参数过多,以至于出现对已知数据预测得很好,但对未知数据预测得很差的现象。

1.3.7 欠拟合

学习时选择的模型简单,所包含的参数过少,以至于出现不能对数据预测很好的现象。

1.3.8 泛化能力

学习得到的模型对未知数据的预测能力。

1.3.8 结构风险

在经验风险上增加表示模型复杂度的正则化项。
R s t r = 1 N ∑ i = 1 N L ( y i , f ( x i ) ) + λ J ( f ) \begin{aligned} R_{str}= \dfrac{1}{N} \sum_{i=1}^{N} L \left(y_{i}, f \left( \mathbf{x}_{i} \right) \right) + \lambda J \left(f\right) \end{aligned} Rstr=N1i=1NL(yi,f(xi))+λJ(f)
其中, J ( f ) J \left(f\right) J(f)是模型复杂度,是正则化项,是定义在假设空间 F \mathcal{F} F上的泛函; λ ≥ 0 \lambda \geq 0 λ0是系数,用以权衡风险和模型复杂度。

学习目标:结构风险最小化
f ∗ = arg ⁡ min ⁡ f ∈ F R s t r ( f ) f^*=\mathop{\arg\min}_{f\in\mathcal{F}} R_{str}\left(f\right) f=argminfFRstr(f)
其中, F \mathcal{F} F是假设空间。

正则化项可以是参数向量的 L 2 L_{2} L2范数
J 2 = 1 2 ∥ w ∥ 2 2 = 1 2 ( ( w ( 1 ) ) 2 + ⋯ + ( w ( n ) ) 2 ) 2 = 1 2 ( ( w ( 1 ) ) 2 + ⋯ + ( w ( n ) ) 2 ) J_{2} = \frac{1}{2}\| \mathbf{w} \|_2^2=\frac{1}{2}\left(\sqrt{\left(w^{\left(1\right)}\right)^2+\cdots+\left(w^{\left(n\right)}\right)^2}\right)^2=\frac{1}{2}\left(\left(w^{\left(1\right)}\right)^2+\cdots+\left(w^{\left(n\right)}\right)^2\right) J2=21w22=21((w(1))2++(w(n))2 )2=21((w(1))2++(w(n))2)
其中, ∥ w ∥ 2 \|\mathbf{w}\|_2 w2表示参数向量 w \mathbf{w} w L 2 L_{2} L2范数。
正则化项可以是参数向量的 L 1 L_{1} L1范数
J 1 = ∥ w ∥ 1 = ∣ w ( 1 ) ∣ + ⋯ + ∣ w ( n ) ∣ J_{1} = \| \mathbf{w} \|_{1}=|w^{\left(1\right)}|+\cdots+|w^{\left(n\right)}| J1=w1=w(1)++w(n)
其中, ∥ w ∥ 1 \|\mathbf{w}\|_{1} w1表示参数向量 w \mathbf{w} w L 1 L_{1} L1范数。

1.4 优化算法

机器学习的训练过程就是使用训练数据集 D D D,按照模型策略在假设空间 F \mathcal{F} F中寻找最优模型 f ∗ f^* f的最优化求解过程
f ∗ = arg ⁡ min ⁡ f ∈ F R s t r ( f ) f^* = \mathop{\arg\min}_{f\in\mathcal{F}}R_{str}\left(f\right) f=argminfFRstr(f)或者在参数空间中寻找最优参数 w ∗ \mathbf{w}^* w的最优化求解过程
w ∗ = arg ⁡ min ⁡ f w ∈ F R s t r ( f w ) 。 \mathbf{w}^*=\mathop{\arg\min}_{f_{\mathbf{w}}\in\mathcal{F}}R_{str}\left(f_{\mathbf{w}}\right)。 w=argminfwFRstr(fw)

梯度下降法:通过迭代的方法来计算训练集 D D D上的风险函数最小值
w t + 1 = w t − α ∂ R D ( w ) ∂ w = w t − α 1 N ∑ i = 1 N ∂ L ( y i , f w ( x i ) ) ∂ w \begin{aligned}\mathbf{w}_{t+1}&=\mathbf{w}_t-\alpha\frac{\partial R_D\left(\mathbf{w}\right)}{\partial\mathbf{w}} \\ &=\mathbf{w}_t-\alpha\frac{1}{N}\sum_{i=1}^N\frac{\partial L\left(y_i,f_{\mathbf{w}}\left(\mathbf{x}_i\right)\right) }{\partial\mathbf{w}} \end{aligned} wt+1=wtαwRD(w)=wtαN1i=1NwL(yi,fw(xi))
其中, w t \mathbf{w}_t wt为第 t t t次迭代的参数值, α \alpha α为学习率。

1.5 性能度量与评估方法

1.5.1 训练误差

模型 Y = f ( X ) Y = f \left(X\right) Y=f(X)关于训练数据集的平均损失
R e m p ( f ) = 1 N ∑ i = 1 N L ( y i , f ( x i ) ) \begin{aligned} R_{emp} \left( f \right) = \dfrac{1}{N} \sum_{i=1}^{N} L \left(y_{i}, f \left( \mathbf{x}_{i} \right) \right) \end{aligned} Remp(f)=N1i=1NL(yi,f(xi))
其中, N N N是训练样本容量。

1.5.2 测试误差

模型 Y = f ( X ) Y = f \left(X\right) Y=f(X)关于测试数据集的平均损失
e t e s t = 1 N ′ ∑ i = 1 N ′ L ( y i , f ( x i ) ) \begin{aligned} e_{test} = \dfrac{1}{N'} \sum_{i=1}^{N'} L \left(y_{i}, f \left( \mathbf{x}_{i} \right) \right) \end{aligned} etest=N1i=1NL(yi,f(xi))
其中, N ′ N' N是测试样本容量。

当损失函数是 0 − 1 0-1 01损失,测试误差就是测试集上的误差率
e t e s t = 1 N ′ ∑ i = 1 N ’ I ( y i ≠ f ( x i ) ) \begin{aligned} e_{test} = \dfrac{1}{N'} \sum_{i=1}^{N’} I \left( y_{i} \neq f \left(\mathbf{x}_{i} \right) \right) \end{aligned} etest=N1i=1NI(yi=f(xi))
其中, I I I是指示函数,即 y ≠ f ( x ) y \neq f \left( \mathbf{x} \right) y=f(x)时为1,否则为0。

测试集上的准确率
r t e s t = 1 N ′ ∑ i = 1 N ’ I ( y i = f ( x i ) ) \begin{aligned} r_{test} = \dfrac{1}{N'} \sum_{i=1}^{N’} I \left( y_{i} = f \left(\mathbf{x}_{i} \right) \right) \end{aligned} rtest=N1i=1NI(yi=f(xi))
则, r t e s t + e t e s t = 1 r_{test} + e_{test} = 1 rtest+etest=1

分类中,模型的预测结果可分为:

  1. 真正例(True Positive,TP):将正类预测为正类;
  2. 假负类(False Negative,FN):将正类预测为负类;
  3. 假正类(False Positive,FP):将负类预测为正类;
  4. 真负类(True Negative,TN):将负类预测为负类。

1.5.3 精确率(查准率)

P = T P T P + F P \begin{aligned} P = \dfrac{TP}{TP+FP}\end{aligned} P=TP+FPTP

1.5.4 召回率(查全率)

R = T P T P + F N \begin{aligned} R = \dfrac{TP}{TP+FN}\end{aligned} R=TP+FNTP

F 1 F_{1} F1值是精确率和召回率的调和均值
2 F 1 = 1 P + 1 R F 1 = 2 T P 2 T P + F P + F N \begin{aligned} \\ & \dfrac{2}{F_{1}} = \dfrac{1}{P} + \dfrac{1}{R} \\ & F_{1} = \dfrac{2TP}{2TP+FP+FN}\end{aligned} F12=P1+R1F1=2TP+FP+FN2TP

真正率
T P R = T P T P + F N TPR=\frac{TP}{TP+FN} TPR=TP+FNTP
假正率
F P R = F P T N + F P FPR=\frac{FP}{TN+FP} FPR=TN+FPFP

1.6 欠拟合与过拟合实例

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)
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):
    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
p_lsq_0 = fitting(M=0)

在这里插入图片描述

p_lsq_1 = fitting(M=1)

在这里插入图片描述

p_lsq_3 = fitting(M=3)

在这里插入图片描述

p_lsq_9 = fitting(M=9)

在这里插入图片描述

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)))
    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()

在这里插入图片描述

print(p_lsq_regularization[0])
[ -5.13841552  -2.86712381   1.52815237   6.55153915   9.28063603
5.58868868  -6.8006399  -17.16663939   9.17580857  -0.19034247]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值