《统计学习方法》(李航),《机器学习》(周志华)学习随笔

《统计学习方法》(李航)学习笔记

【1】第一章 统计学习方法概论

1.3.2的2. 经验风险最小值与结构风险最小值中提到'当模型是条件概率分布,损失函数是对数损失函数时,经验风险最小化等价于极大似然估计'。P9

个人注解:

\[假设空间 F=\{ P|P(Y|X);\theta\} \]

\[极大似然函数 L(\theta)={\Pi^n_1P(y_i|x_i)} \]

\[对数极大似然函数 log(L(\theta))={\Sigma^n_1logP(y_i|x_i)} \]

考虑到N是常数,则

\[经验风险 -\frac{1}{N}\Sigma_1^n{log(P(y_i|x_i))} \]

由此可知,经验风险最小,实际上等价于对数极大似然函数取最大。

【2】1.4.2的例1.1对wj求偏导,P12

\[L(w)=\frac{1}{2}\Sigma_{i=1}^N\Sigma_{j=0}^M{[w_jx_i^j-y_i]^2} \]

对上式求导:

\[\frac{1}{2}\Sigma_{i=1}^N2(w_jx_i^j-y_j)x_i^j=0 \\ 即: \Sigma_{i=1}^Nw_jx_i^{2j}=\Sigma_{i=1}^Nx_i^jy_j \\ 即: w_j=\frac{\Sigma_{i=1}^Nx_iy_i}{\Sigma_{i=1}^Nx_i^{j+1}} \]

《机器学习》(西瓜书)

【1】第四章 决策树ID3算法递归结束的三个条件
(1)子结点中的样本属于同一类。
(2)子结点的特征用完了。
(3)子结点没有样本了。
https://www.jianshu.com/p/d153130b813f
【2】第四章 决策树连续值处理

西瓜书 决策树连续值属性处理 (P83~85)

import numpy as np
import pandas as pd

西瓜的密度及对应的标签如下:

density=[(0.697,'y'),(0.774,'y'),(0.643,'y'),(0.608,'y'),(0.556,'y'),
         (0.403,'y'),(0.481,'y'),(0.437,'y'),(0.666,'n'),(0.243,'n'),(0.245,'n'),
         (0.343,'n'),(0.639,'n'),(0.657,'n'),(0.36,'n'),(0.593,'n'),(0.719,'n')]
df=pd.DataFrame(density,columns=['density','good_or_bad'])
df
density good_or_bad
0 0.697 y
1 0.774 y
2 0.643 y
3 0.608 y
4 0.556 y
5 0.403 y
6 0.481 y
7 0.437 y
8 0.666 n
9 0.243 n
10 0.245 n
11 0.343 n
12 0.639 n
13 0.657 n
14 0.360 n
15 0.593 n
16 0.719 n

数据集D的信息熵:

def calEnt(df):
    """
    计算信息熵
    """
    good=(df.iloc[:,1]=='y').sum()
    bad=(df.iloc[:,1]=='n').sum()
    good_ratio=good/(good+bad)
    bad_ratio=1-good_ratio
    if 1 in [good_ratio,bad_ratio]:
        return 0 
    ent=-(np.log2(good_ratio)*good_ratio+np.log2(bad_ratio)*bad_ratio)
    return ent
ent_D=calEnt(df);ent_D
0.9975025463691153

根据二分法取候选值

x1=df.iloc[:,0].values.copy();x1 #注意这里要用copy!否则就是view,而后续的sort将改变df的index!
array([0.697, 0.774, 0.643, 0.608, 0.556, 0.403, 0.481, 0.437, 0.666,
       0.243, 0.245, 0.343, 0.639, 0.657, 0.36 , 0.593, 0.719])
x1.sort()
t_ready=(x1[:-1]+x1[1:])/2;t_ready
array([0.244 , 0.294 , 0.3515, 0.3815, 0.42  , 0.459 , 0.5185, 0.5745,
       0.6005, 0.6235, 0.641 , 0.65  , 0.6615, 0.6815, 0.708 , 0.7465])
a,b=np.array([1,2]),np.array([3,4])

求解Gain(D,a)

def get_gain(df,t_ready):
    """
    求解连续属性的信息增益
    df为连续属性数据集
    t_ready是根据二分法求解的连续属性待定值
    """
    result=[]
    ent_D=calEnt(df)
    df_count=len(df)
    for i in t_ready:
        small_part=df.where(df.iloc[:,0]<=i).dropna()
        large_part=df.where(df.iloc[:,0]>i).dropna()
        small_part_ent=calEnt(small_part)
        large_part_ent=calEnt(large_part)
        small_count=len(small_part)
        large_count=len(large_part)

        ratio_group=np.array([small_count/df_count,large_count/df_count])
        ent_group=np.array([small_part_ent,large_part_ent])

        gain=ent_D-(ratio_group*ent_group).sum()
        result.append((i,gain))
    return result
        
results=get_gain(df,t_ready)
results.sort(key=lambda x:x[1])
results
[(0.708, 0.00033345932649475607),
 (0.6615, 0.0007697888924075302),
 (0.641, 0.0013653507075551685),
 (0.5745, 0.002226985278291793),
 (0.6005, 0.002226985278291793),
 (0.5185, 0.003585078590305879),
 (0.6234999999999999, 0.003585078590305879),
 (0.65, 0.006046489176565584),
 (0.6815, 0.024085993037174735),
 (0.45899999999999996, 0.03020211515891169),
 (0.244, 0.05632607578088),
 (0.7464999999999999, 0.06696192680347068),
 (0.42000000000000004, 0.0934986902367243),
 (0.29400000000000004, 0.1179805181500242),
 (0.35150000000000003, 0.18613819904679052),
 (0.3815, 0.2624392604045632)]

因此,可得属性“密度”的信息增益是0.262,对应的划分点为0.381,与西瓜书的结果一致!

再验证含糖率:

suger=[(0.46,'y'),(0.376,'y'),(0.264,'y'),(0.318,'y'),(0.215,'y'),(0.237,'y'),(0.149,'y'),(0.211,'y'),
      (0.091,'n'),(0.267,'n'),(0.057,'n'),(0.099,'n'),(0.161,'n'),(0.198,'n'),(0.37,'n'),(0.042,'n'),(0.103,'n')]
df_suger=pd.DataFrame(suger,columns=['suger','good_or_bad'])
xx=df_suger.iloc[:,0].values.copy()
xx.sort()
suger_ready=(xx[1:]+xx[:-1])/2
results=get_gain(df_suger,suger_ready)
results.sort(key=lambda x:x[1]);results
[(0.2655, 0.02025677859928121),
 (0.344, 0.024085993037174735),
 (0.0495, 0.05632607578088),
 (0.2505, 0.06150029019652836),
 (0.41800000000000004, 0.06696192680347068),
 (0.2925, 0.0715502899435908),
 (0.074, 0.1179805181500242),
 (0.22599999999999998, 0.12369354800009502),
 (0.373, 0.14078143361499595),
 (0.155, 0.15618502398692902),
 (0.095, 0.18613819904679052),
 (0.213, 0.21114574906025385),
 (0.1795, 0.2354661674053965),
 (0.101, 0.2624392604045632),
 (0.20450000000000002, 0.33712865788827096),
 (0.126, 0.34929372233065203)]

因此,可得属性“含糖率”的信息增益是0.349,对应的划分点为0.126,与西瓜书的结果一致!

【3】第三章 线性回归最小二乘“参数估计”的求解 P54~P55

\[E(w,b)=argmin\Sigma_{i=1}^{m}(y_i-wx_i-b)^2\\ \partial E(w,b)/\partial w=2(\Sigma (wx_i+b-y_i)x_i)=2(w\Sigma x_i^2+\Sigma (b-y_i)x_i) ....(1)\\ \partial E(w,b)/\partial b=2(\Sigma_{i=1}^{m}(wx_i+b-y_i))=2(mb+\Sigma(wx_i-y_i)) .......(2) \]

令 (1)式,(2)式均为0,

由(2)式为0得:

\[b=\frac 1m \Sigma(y_i-wx_i)=\frac 1m\Sigma y_i - \frac 1m \Sigma wm\overline x ....(4) \]

其中,

\[\overline x 表示 \frac 1m \Sigma x_i \]

将(4)带入,(1)=0中:

\[w\Sigma x_i^2=\Sigma x_iy_i -b\Sigma x_i=\Sigma x_iy_i-(\frac 1m \Sigma y_i-w\overline x)m\overline x\\ =\Sigma x_iy_i- \overline x\Sigma y_i+wm\overline x^2\\ 由此得: w=\frac {\Sigma y_i(x_i-\overline x)}{\Sigma(x_i^2-m\overline x^2)}\\ b=\frac 1m \Sigma(y_i-wx_i)=\frac 1m\Sigma y_i - \frac 1m \Sigma wm\overline x \]

【4】P55 线性回归 矩阵求导

在解决该问题之前,先进行一定的知识回顾:

对于一元微积分的导数(标量对标量)与微分有关系:

df=f'(x)dx;

对于多元微积分中的梯度(标量对向量的导数)与微分有关系:

\[df=\Sigma_{i=1}^{n}(\partial f/ \partial x_i)dx_i = (\partial f/ \partial x_i)^TdX \]

那么对于标量对矩阵导数与微分的关系:

\[df=\Sigma_{i=1}^{m}\Sigma_{j=1}^{n}(\partial f/ \partial x_{ij})dX_{ij}=tr((\partial f/ \partial X)^TdX) \]

tr为矩阵的迹。

上式的理解:

\[tr(A^TB)实际是矩阵A,与矩阵B的内积 \]

比如

\[A=\begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ \end{pmatrix} B=\begin{pmatrix} b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33} \\ \end{pmatrix} \]

那么A与B的内积为

\[a_{11}b_{11}+a_{12}b_{12}+...a_{33}b_{33}\\ =\Sigma_{i=1}^{i=3}\Sigma_{j=1}^{j=3}a_{ij}b_{ij} \]

\[A^TB=\begin {pmatrix} a_{11}b_{11}+a_{21}b_{21}+a_{31}b_{31} & \cdots & \cdots \\ \cdots & a_{12}b_{12}+a_{22}b_{22}+a_{32}b_{32} & \cdots \\ \cdots & \cdots & a_{13}b_{13}+a_{23}b_{23}+a_{33}b_{33} \end {pmatrix} \]

\[可以发现A^TB的迹正好是A,B的内积!因此矩阵导数可以用迹来表示。 \]

下面简单介绍矩阵微分的运算法则:

\[(1)加减法:d(X\pm Y)=dX \pm dY; \\ 乘法:d(XY)=(dX)Y+X(dY)\\ 转置:d(X^T)=(dX)^T 迹:dtr(X)=tr(dX)\\ (2) 逆:dX^{-1}=-X^{-1}dX(X^{-1})\\ (3)行列式:d|X|=tr(X^{*}dX),其中X^{*}表示X的伴随矩阵,在X可逆时,又可以写作d|X|=|X|tr(X^{-1}dX)\\ (4)逐元素乘法: d(X\bigodot Y)=dX\bigodot Y +X\bigodot dY,\bigodot表示尺寸相同的矩阵X,Y逐元素相乘。\\ (5)逐元素函数:d\sigma(X)=\sigma'(X)\bigodot dX,\sigma(X)=[\sigma(X_{ij})]是主元素函数运算,\sigma'(X)=[\sigma'(X_{ij})]\\ 例如:\\ X=\begin {bmatrix} X_{11} & X_{12} \\ X_{21} & X_{22} \end {bmatrix}\\ dsin(X)=\begin {bmatrix} cosX_{11}dX_{11} & cosX_{12}dX_{12}\\ cosX_{21}dX_{21} & cosX_{22}dX_{22}\\ \end {bmatrix}=cos(X)\bigodot d(X) \]

迹的变形:

\[1.标量套上迹:a=tr(a)\\ 2.转置:tr(A^T)=tr(A)\\ 3.线性:tr(A\pm B)=tr(A)\pm tr(B)\\ 4.矩阵乘法交换: tr(AB)=tr(BA),其中A与B^T尺寸相同,两侧均为\Sigma_{i,j}A_{ij}B_{ji}\\ 5.矩阵乘法/主元素乘法交换:tr(A^T(B\bigodot C))=tr((A\bigodot B)^TC) \]

对于复合函数,链式法则失效!不能随意沿用标量的链式法则。要从微分入手建立复合法则:

\[先写出df=tr((\partial f/\partial Y) dY),再将dY用dX表示出来代入,并使用迹变换将其他交换至dX左侧,即可得到\partial f/\partial X. \]

下面直接解决《机器学习》(西瓜书)线性回归部分矩阵求导问题 (P55)

\[E_{\hat{w}}=(Y-X\hat{W})^{T}(Y-X\hat{W})\\ 令P=(Y-X\hat{W})\\ 则原式为P^TP\\ dE=d(P^TP)=(dP^T)P+P^TdP=(dP)^TP+P^TdP\\ dE=tr(dE)=tr((dP)^TP+P^TdP)=tr((dP)^TP)+tr(P^TdP) \\ tr((dP)^TP)=tr(P^T(dP)) \\ tr(P^TdP)=tr((dP)^TP)=tr(P^T(dP)) \\ 故dE=tr(P^TdP)+tr(P^TdP)=2tr(P^TdP)=tr(2P^TdP)....(1)\\ 而dP=d(Y)-d(X\hat{W})\\X,Y均为常数向量/矩阵,因此有dX=0,dY=0\\ dP=d(Y)-((dX)\hat{W}+Xd\hat{W})=-Xd\hat{W}\\ 带入(1)式:\\ dE=tr(2P^T(-Xd\hat{W}))=tr(-2X^TP)^Td\hat{W},\\ 而dE=tr((\partial E/\partial \hat{W})^Td\hat{W})\\ 因此可得:(\partial E/\partial \hat{W})=-2X^TP=2X^T(X\hat{W}-Y) \]

附:md数学符号写法:https://www.cnblogs.com/ywsun/p/14271547.html#autoid-0-7-0

posted @ 2021-02-18 17:30  JohnYang819  阅读(305)  评论(0编辑  收藏  举报