机器学习之父汤姆·米切尔,1997 年出版《机器学习》
A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.
DeepSeek 的翻译: 若计算机程序在某类任务 T 中的表现 (以评估指标 P 衡量) 随经验 E 的积累而提升,则称该程序具备从经验 E 中学习的能力。
汤姆·米切尔,Tom M. Mitchell,美国卡内基梅隆大学计算机学院院长,美国国家工程院、美国艺术与科学院院士,国际人工智能促进会 (AAAI) 前任主席,国际机器学习大会 (ICML) 创始人之一
若计算机程序在某类任务 T 中的表现 (以评估指标 P 衡量) 随经验 E 的积累而提升,则称该程序具备从经验 E 中学习的能力。
四个关键元素
结构型数据:二维表格
| 花萼长度 | 花萼宽度 | 花瓣长度 | 花瓣宽度 | 类别标记 |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | 山鸢尾 |
| 4.9 | 3.0 | 1.4 | 0.2 | 山鸢尾 |
| 7.0 | 3.2 | 4.7 | 1.4 | 杂色鸢尾 |
| 6.4 | 3.2 | 4.5 | 1.5 | 杂色鸢尾 |
| 6.2 | 3.4 | 5.4 | 2.3 | 维吉尼亚鸢尾 |
| 5.9 | 3.0 | 5.1 | 1.8 | 维吉尼亚鸢尾 |
该表格取自鸢尾花数据集,由美国植物学家 Edgar Shannon Anderson 收集,英国统计学家 Ronald Aylmer Fisher 引入到统计分析中,共有 150 个样本、3 个类别,每类 50 个样本
sklearn 集成了该数据集
import numpy as np import pandas as pd from sklearn.datasets import load_iris iris = load_iris() print(iris.DESCR) # -------------------- # Iris plants dataset # # **Data Set Characteristics:** # # :Number of Instances: 150 (50 in each of three classes) # :Number of Attributes: 4 numeric, predictive attributes and the class # :Attribute Information: # - sepal length in cm 花萼长度 # - sepal width in cm 花萼宽度 # - petal length in cm 花瓣长度 # - petal width in cm 花瓣宽度 # - class: # - Iris-Setosa 山鸢尾 # - Iris-Versicolour 杂色鸢尾 # - Iris-Virginica 维吉尼亚鸢尾 # # :Summary Statistics: # # ============== ==== ==== ======= ===== ==================== # Min Max Mean SD Class Correlation # ============== ==== ==== ======= ===== ==================== # sepal length: 4.3 7.9 5.84 0.83 0.7826 # sepal width: 2.0 4.4 3.05 0.43 -0.4194 # petal length: 1.0 6.9 3.76 1.76 0.9490 (high!) # petal width: 0.1 2.5 1.20 0.76 0.9565 (high!) # ============== ==== ==== ======= ===== ==================== # # :Missing Attribute Values: None # :Class Distribution: 33.3% for each of 3 classes. # :Creator: R.A. Fisher # :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov) # :Date: July, 1988 # # The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken # from Fisher's paper. Note that it's the same as in R, but not as in the UCI # Machine Learning Repository, which has two wrong data points. # # This is perhaps the best known database to be found in the # pattern recognition literature. Fisher's paper is a classic in the field and # is referenced frequently to this day. (See Duda & Hart, for example.) The # data set contains 3 classes of 50 instances each, where each class refers to a # type of iris plant. One class is linearly separable from the other 2; the # latter are NOT linearly separable from each other. # # .. dropdown:: References # # - Fisher, R.A. "The use of multiple measurements in taxonomic problems" # Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to # Mathematical Statistics" (John Wiley, NY, 1950). # - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis. # (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218. # - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System # Structure and Classification Rule for Recognition in Partially Exposed # Environments". IEEE Transactions on Pattern Analysis and Machine # Intelligence, Vol. PAMI-2, No. 1, 67-71. # - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions # on Information Theory, May 1972, 431-433. # - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II # conceptual clustering system finds 3 classes in the data. # - Many, many more ... X, y = iris.data, iris.target irisdf = pd.DataFrame( # numpy格式 -> pandas格式 np.hstack((X, y[:, np.newaxis])), columns=['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class'] ) kind_dict = {0: "setosa", 1: "versicolor", 2: "virginica"} irisdf["class"] = irisdf["class"].map(kind_dict) print(irisdf.head()) # 前5个样本 # -------------------- # sepal-length sepal-width petal-length petal-width class # 0 5.1 3.5 1.4 0.2 setosa # 1 4.9 3.0 1.4 0.2 setosa # 2 4.7 3.2 1.3 0.2 setosa # 3 4.6 3.1 1.5 0.2 setosa # 4 5.0 3.6 1.4 0.2 setosa print(irisdf.tail()) # 最后5个样本 # -------------------- # sepal-length sepal-width petal-length petal-width class # 145 6.7 3.0 5.2 2.3 virginica # 146 6.3 2.5 5.0 1.9 virginica # 147 6.5 3.0 5.2 2.0 virginica # 148 6.2 3.4 5.4 2.3 virginica # 149 5.9 3.0 5.1 1.8 virginica print(irisdf.describe()) # 特征统计信息 # -------------------- # sepal-length sepal-width petal-length petal-width # count 150.000000 150.000000 150.000000 150.000000 # mean 5.843333 3.057333 3.758000 1.199333 # std 0.828066 0.435866 1.765298 0.762238 # min 4.300000 2.000000 1.000000 0.100000 # 25% 5.100000 2.800000 1.600000 0.300000 # 50% 5.800000 3.000000 4.350000 1.300000 # 75% 6.400000 3.300000 5.100000 1.800000 # max 7.900000 4.400000 6.900000 2.500000
将样本想象为欧几里得空间中的点
非结构型数据,不限于下面四种
图片
文本
语音
棋盘
所有样本都有标记
| 原始数据 | 样本/示例 | 属性/特征 | 类别标记 |
|---|---|---|---|
| $o_1$ | $(\xv_1, y_1)$ | $\xv_1$ | $y_1$ |
| $o_2$ | $(\xv_2, y_2)$ | $\xv_2$ | $y_2$ |
| $\vdots$ | $\vdots$ | $\vdots$ | $\vdots$ |
| $o_m$ | $(\xv_m, y_m)$ | $\xv_m$ | $y_m$ |
任务类型:
乳腺癌预测:美国威斯康星大学医院 William H. Wolberg 博士及其合作者于 1995 年收集
一共 569 个样本,2 个类别,其中恶性 (212)、良性 (357)
特征从乳腺肿块细针穿刺的数字化图像计算而来,对每张图像中的细胞核计算 radius (半径)、texture (纹理)、perimeter (周长)、area (面积)、smoothness (平滑度)、compactness (紧凑度)、concavity (凹度)、concave points (凹点)、symmetry (对称性)、fractal dimension (分形维数) 等 10 个特征,然后取平均值、标准差、极值,共得到 30 个特征
sklearn 集成了该数据集
from sklearn.datasets import load_breast_cancer breast_cancer = load_breast_cancer() print(breast_cancer.DESCR) # -------------------- # **Data Set Characteristics:** # # :Number of Instances: 569 # # :Number of Attributes: 30 numeric, predictive attributes and the class # # :Attribute Information: # - radius (mean of distances from center to points on the perimeter) # - texture (standard deviation of gray-scale values) # - perimeter # - area # - smoothness (local variation in radius lengths) # - compactness (perimeter^2 / area - 1.0) # - concavity (severity of concave portions of the contour) # - concave points (number of concave portions of the contour) # - symmetry # - fractal dimension ("coastline approximation" - 1) # # The mean, standard error, and "worst" or largest (mean of the three # worst/largest values) of these features were computed for each image, # resulting in 30 features. For instance, field 0 is Mean Radius, field # 10 is Radius SE, field 20 is Worst Radius. # # - class: # - WDBC-Malignant 恶性 # - WDBC-Benign 良性 # # :Summary Statistics: # # ===================================== ====== ====== # Min Max # ===================================== ====== ====== # radius (mean): 6.981 28.11 半径 # texture (mean): 9.71 39.28 纹理 # perimeter (mean): 43.79 188.5 周长 # area (mean): 143.5 2501.0 面积 # smoothness (mean): 0.053 0.163 平滑度 # compactness (mean): 0.019 0.345 紧凑度 # concavity (mean): 0.0 0.427 凹度 # concave points (mean): 0.0 0.201 凹点 # symmetry (mean): 0.106 0.304 对称性 # fractal dimension (mean): 0.05 0.097 分形维数 # radius (standard error): 0.112 2.873 # texture (standard error): 0.36 4.885 # perimeter (standard error): 0.757 21.98 # area (standard error): 6.802 542.2 # smoothness (standard error): 0.002 0.031 # compactness (standard error): 0.002 0.135 # concavity (standard error): 0.0 0.396 # concave points (standard error): 0.0 0.053 # symmetry (standard error): 0.008 0.079 # fractal dimension (standard error): 0.001 0.03 # radius (worst): 7.93 36.04 # texture (worst): 12.02 49.54 # perimeter (worst): 50.41 251.2 # area (worst): 185.2 4254.0 # smoothness (worst): 0.071 0.223 # compactness (worst): 0.027 1.058 # concavity (worst): 0.0 1.252 # concave points (worst): 0.0 0.291 # symmetry (worst): 0.156 0.664 # fractal dimension (worst): 0.055 0.208 # ===================================== ====== ====== # # :Missing Attribute Values: None # # :Class Distribution: 212 - Malignant, 357 - Benign # # :Creator: Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian # # :Donor: Nick Street # # :Date: November, 1995 # # This is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets. # https://goo.gl/U2Uwz2 # # # # Separating plane described above was obtained using # Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree # Construction Via Linear Programming." Proceedings of the 4th # Midwest Artificial Intelligence and Cognitive Science Society, # pp. 97-101, 1992], a classification method which uses linear # programming to construct a decision tree. Relevant features # were selected using an exhaustive search in the space of 1-4 # features and 1-3 separating planes. # # The actual linear program used to obtain the separating plane # in the 3-dimensional space is that described in: # [K. P. Bennett and O. L. Mangasarian: "Robust Linear # Programming Discrimination of Two Linearly Inseparable Sets", # Optimization Methods and Software 1, 1992, 23-34]. # # This database is also available through the UW CS ftp server: # # ftp ftp.cs.wisc.edu # cd math-prog/cpo-dataset/machine-learn/WDBC/ # # .. dropdown:: References # # - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction # for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on # Electronic Imaging: Science and Technology, volume 1905, pages 861-870, # San Jose, CA, 1993. # - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and # prognosis via linear programming. Operations Research, 43(4), pages 570-577, # July-August 1995. # - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques # to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) # 163-171.
手写数字识别:美国国家标准与技术研究所 (National Institute of Standards and Technology, NIST) 收集,共 10 个类别
sklearn 中的 digits 数据集就是 UCI 数据集中的测试集
from sklearn.datasets import load_digits digits = load_digits() # print(digits.DESCR) # -------------------- # **Data Set Characteristics:** # # :Number of Instances: 1797 # :Number of Attributes: 64 # :Attribute Information: 8x8 image of integer pixels in the range 0..16. # :Missing Attribute Values: None # :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr) # :Date: July; 1998 # # This is a copy of the test set of the UCI ML hand-written digits datasets # https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten# +Digits # # The data set contains images of hand-written digits: 10 classes where # each class refers to a digit. # # Preprocessing programs made available by NIST were used to extract # normalized bitmaps of handwritten digits from a preprinted form. From a # total of 43 people, 30 contributed to the training set and different 13 # to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of # 4x4 and the number of on pixels are counted in each block. This generates # an input matrix of 8x8 where each element is an integer in the range # 0..16. This reduces dimensionality and gives invariance to small # distortions. # # For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G. # T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C. # L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469, # 1994. # # .. dropdown:: References # # - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their # Applications to Handwritten Digit Recognition, MSc Thesis, Institute of # Graduate Studies in Science and Engineering, Bogazici University. # - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika. # - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin. # Linear dimensionalityreduction using relevance weighted LDA. School of # Electrical and Electronic Engineering Nanyang Technological University. # 2005. # - Claudio Gentile. A New Approximate Maximal Margin Classification # Algorithm. NIPS. 2000.
糖尿病检测:美国国立糖尿病与消化与肾脏疾病研究所收集,一共 442 个样本,$y$是糖尿病人一年后病情进展的定量测量,每个样本包含 10 个特征:
sklearn 集成了该数据集
from sklearn.datasets import load_diabetes diabetes = load_diabetes() print(diabetes.DESCR) # -------------------- # Ten baseline variables, age, sex, body mass index, average blood # pressure, and six blood serum measurements were obtained for each of n = # 442 diabetes patients, as well as the response of interest, a # quantitative measure of disease progression one year after baseline. # # **Data Set Characteristics:** # # :Number of Instances: 442 # # :Number of Attributes: First 10 columns are numeric predictive values # # :Target: Column 11 is a quantitative measure of disease progression one year # after baseline # # :Attribute Information: # - age 年龄 # - sex 性别 # - bmi 身体质量指数 # - bp 平均血压 # - s1 血清总胆固醇 # - s2 低密度脂蛋白 # - s3 高密度脂蛋白 # - s4 总胆固醇 / 高密度脂蛋白 # - s5 血清甘油三酯水平的对数 # - s6 血糖水平 # # Note: Each of these 10 feature variables have been mean centered and scaled by # the standard deviation times the square root of `n_samples` (i.e. the sum of # squares of each column totals 1). # # Source URL: # https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html # # For more information see: # Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) # "Least Angle Regression," Annals of Statistics (with discussion), 407-499. # (https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)
$y$是向量
$y$是有序列表,用于信息检索任务
$y$是序列,用于机器翻译、问答系统等任务
$y$是句法树,用于对自然语言的句法分析
只有部分样本有标记,常见于标记获许代价很高的场景,关键问题如何利用未标记样本的分布信息辅助对有标记样本的学习?
| 原始数据 | 样本/示例 | 属性/特征 | 类别标记 |
|---|---|---|---|
| $o_1$ | $(\xv_1, y_1)$ | $\xv_1$ | $y_1$ |
| $\vdots$ | $\vdots$ | $\vdots$ | $\vdots$ |
| $o_l$ | $(\xv_l, y_l)$ | $\xv_m$ | $y_l$ |
| $o_{l+1}$ | $(\xv_{l+1}, -)$ | $\xv_{l+1}$ | $-$ |
| $\vdots$ | $\vdots$ | $\vdots$ | $\vdots$ |
| $o_{l+u}$ | $(\xv_{l+u}, -)$ | $\xv_{l+u}$ | $-$ |
任务类型:
所有样本都没有标记
| 原始数据 | 样本/示例 | 属性/特征 | 类别标记 |
|---|---|---|---|
| $o_1$ | $(\xv_1, -)$ | $\xv_1$ | $-$ |
| $o_2$ | $(\xv_2, -)$ | $\xv_2$ | $-$ |
| $\vdots$ | $\vdots$ | $\vdots$ | $\vdots$ |
| $o_m$ | $(\xv_m, -)$ | $\xv_m$ | $-$ |
任务类型:
最具代表性的是$k$-均值 ($k$-means) 算法,其中$k$是目标簇数,需事先指定 (算法的输入)
基本想法:设数据聚成$k$个簇$\Ccal_1, \ldots, \Ccal_k$,每个样本都恰属于某一个簇,每个样本到所在簇簇中心的距离小于与其它簇中心的距离
优化问题:$\min_{\muv_i} \sum_{i \in [k]} \sum_{\xv \in \Ccal_i} \| \xv - \muv_i \|_2^2, ~ \st ~ \muv_i = \sum_{\xv \in \Ccal_i} \xv / |\Ccal_i|.$
求解算法:随机初始化$k$个簇中心,重复以下两步直至收敛:1) 将每个样本分配到距其最近的簇中心;2) 更新每个簇的中心
$k$-均值这一叫法由 James MacQueen 于 1967 年首次使用,但其思想可追溯到 1957 年的 Hugo Steinhaus。上述求解算法最初在 1957 年由贝尔实验室的 Stuart Lloyd 作为一种脉冲码调制技术提出,但直到 1982 年才公开发表,最终结果是优化问题的局部最优解 (不是全局最优) 且受簇中心的初始化影响。1965 年,Edward W. Forgy 发表了本质上相同的方法,故该算法有时也称为 Lloyd–Forgy 方法。
从 6 个中心随机生成、各向同性、标准差为 1 的正态分布里各采样 2000 个样本,并指定聚成 4 个簇
$$ \begin{align*} \quad \Xv \in \Rbb^{m \times D} \xrightarrow[\text{降维}]{\Wv \in \Rbb^{D \times d}} \Xv \Wv \in \Rbb^{m \times d} \xrightarrow[\text{重构}]{\Wv^\top \in \Rbb^{d \times D}} \Xv \Wv \Wv^\top \in \Rbb^{m \times D} \end{align*} $$
主成分分析 (principal components analysis, PCA) 的目标为最小化重构误差
$$ \begin{align*} \quad \min_{\Wv} \| \Xv - \Xv \Wv \Wv^\top \|_F^2, \quad \st ~ \Wv^\top \Wv = \Iv. \end{align*} $$
PCA 由 Karl Pearson 于 1901 年提出,之后在 1930 年左右由 Harold Hotelling 独立发展并命名。在力学中叫主轴定理 (principal axis theorem),在信号处理中叫做离散 K-L 转换 (discrete Karhunen–Loève transform, KLT)。
输入为$\Rbb^2$中 500 个点,服从正态分布 (等高线为一族椭圆),$\Rbb^2 \mapsto \Rbb$重构误差最小的投影方向 (主成分) 是椭圆的长轴
选定 bin 的起始点和宽度,统计样本落在每个 bin 中的个数,再归一化就是概率密度
20 个样本,其中 6 个采样自$\Ncal(0,1)$,14 个采样自$\Ncal(0,5)$
由上图可见直方图估计对 bin 的选取很敏感
假设某个 bin 为区间$\Ical = [x-h, x+h]$,则点$x$处的概率估计为
$$ \begin{align*} \quad p(x) = \frac{1}{mh} \sum_{i \in [m]} \frac{1}{2} \Ibb \left( \left| \frac{x - x_i}{h} \right| \le 1 \right) = \frac{1}{mh} \sum_{i \in [m]} \kappa \left( \frac{x - x_i}{h} \right) \end{align*} $$
其中$\Ibb(\cdot)$是指示函数 (indicator function)、$\kappa(u) = \frac{1}{2} \Ibb(|u| \le 1)$
直方图估计有两个问题
$$ \begin{align*} \quad \kappa(u) = \underbrace{(1 - |u|)}_{\text{重要性权重}} \Ibb(|u| \le 1) = \left( 1 - \left| \frac{x - x_i}{h} \right| \right) \Ibb \left( \left| \frac{x - x_i}{h} \right| \le 1 \right) \end{align*} $$
类似$\kappa(u) = (1 - |u|) \Ibb(|u| \le 1)$之类关于$0$中心对称的非负函数称为核函数 (kernel function),对应的估计为核密度估计 (kernel density estimation, KDE)
| 核 | $\kappa(u)$ |
|---|---|
| tophat | $\frac{1}{2} \Ibb( \shu u \shu \le 1)$ |
| linear | $(1 - \shu u \shu) \Ibb(\shu u \shu \le 1)$ |
| epanechnikov | $\frac{3}{4} (1 - u^2) \Ibb(\shu u \shu \le 1)$ |
| cosine | $\frac{\pi}{4} \cos (\frac{\pi}{2} u) \Ibb(\shu u \shu \le 1)$ |
| gaussian | $\frac{1}{\sqrt{2 \pi}} \exp (-\frac{1}{2} u^2)$ |
| exponential | $\frac{1}{2} \exp (- \shu u \shu)$ |
sklearn 提供了上面 6 种核函数,此外$\frac{15}{16} (1 - u^2)^2 \Ibb(|u| \le 1)$、$\frac{35}{32} (1 - u^2)^3 \Ibb(|u| \le 1)$也是常用核函数。
100 个样本,其中 30 个采样自$\Ncal(0,1)$,70 个采样自$\Ncal(0,5)$
强化学习 (reinforcement learning):研究智能体 (agent) 在环境 (environment)中如何根据状态 (state) 采取行动 (action) 以最大化期望收益 (reward)
迁移学习 (transfer learning):假设在旧任务上已经学得一个模型,如何利用该模型辅助新任务的学习,任务间的相似度很关键
受课时所限,本课程不会涉及这些机器学习任务,前者可单独作为一门课程,后者也至少需要 2-3 周的研讨班,此外还有主动学习 (active learning)、元学习 (meta learning) 等,不一而足。
米哈尔斯基 等《机器学习:一种人工智能途径》
Machine Learning: An Artificial Intelligence Approach, 1983
费根鲍姆 等《人工智能手册》
The Handbook of Artificial Intelligence, 1983
佩德罗·多明戈斯于 2015 年在《终极算法》中提出
佩德罗·多明戈斯,Pedro Domingos,华盛顿大学计算机科学与工程学院终身名誉教授,以研究能进行不确定推断的马尔可夫逻辑网而在机器学习领域闻名,2015 年出版 The Master Algorithm 一书,中文译作《终极算法》。
| 次序 | 时间 | 方式 | 天气 | 课业 | 疫情 | 电视 | 约会 |
|---|---|---|---|---|---|---|---|
| 1 | 周六 | 吃饭 | 晴天 | 轻松 | 清零 | 精彩 | 是 |
| 6 | 周六 | 逛街 | 晴天 | 轻松 | 平缓 | 无聊 | 是 |
| 10 | 周六 | 学习 | 雨天 | 轻松 | 严峻 | 无聊 | 否 |
| 13 | 周六 | 逛街 | 晴天 | 适中 | 清零 | 精彩 | 否 |
| 14 | 周间 | 逛街 | 阴天 | 适中 | 清零 | 精彩 | ? |
| 次序 | 时间 | 方式 | 天气 | 课业 | 疫情 | 电视 | 约会 |
|---|---|---|---|---|---|---|---|
| 1 | 周六 | 吃饭 | 晴天 | 轻松 | 清零 | 精彩 | 是 |
| 6 | 周六 | 逛街 | 晴天 | 轻松 | 平缓 | 无聊 | 是 |
| 10 | 周六 | 学习 | 雨天 | 轻松 | 严峻 | 无聊 | 否 |
| 13 | 周六 | 逛街 | 晴天 | 适中 | 清零 | 精彩 | 否 |
| 14 | 周间 | 逛街 | 阴天 | 适中 | 清零 | 精彩 | ? |
用 if-then 形式的合取规则尽可能地概括正样本
$\text{是} \longleftarrow (\text{天气} = \text{晴天}) \wedge (\text{课业} = \text{轻松})$
| 次序 | 时间 | 方式 | 天气 | 课业 | 疫情 | 电视 | 约会 |
|---|---|---|---|---|---|---|---|
| 1 | 周六 | 吃饭 | 晴天 | 轻松 | 清零 | 精彩 | 是 |
| 6 | 周六 | 逛街 | 晴天 | 轻松 | 平缓 | 无聊 | 是 |
| 10 | 周六 | 学习 | 雨天 | 轻松 | 严峻 | 无聊 | 否 |
| 13 | 周六 | 逛街 | 晴天 | 适中 | 清零 | 精彩 | 否 |
| 14 | 周间 | 逛街 | 阴天 | 适中 | 清零 | 精彩 | ? |
用感知机 (perceptron) 拟合数据
$\{1, -1\} \longleftarrow \sign(w_0 + w_1 \cdot \text{次序} + \cdots + w_7 \cdot \text{电视})$
| 次序 | 时间 | 方式 | 天气 | 课业 | 疫情 | 电视 | 约会 |
|---|---|---|---|---|---|---|---|
| 1 | 周六 | 吃饭 | 晴天 | 轻松 | 清零 | 精彩 | 是 |
| 6 | 周六 | 逛街 | 晴天 | 轻松 | 平缓 | 无聊 | 是 |
| 10 | 周六 | 学习 | 雨天 | 轻松 | 严峻 | 无聊 | 否 |
| 13 | 周六 | 逛街 | 晴天 | 适中 | 清零 | 精彩 | 否 |
| 14 | 周间 | 逛街 | 阴天 | 适中 | 清零 | 精彩 | ? |
利用贝叶斯公式求后验 (posterior)
$p (\text{约会}|\text{次序},\text{时间},\ldots,\text{电视}) = \frac{p(\text{次序},\text{时间},\ldots,\text{电视}|\text{约会}) ~ p(\text{约会}) \quad \quad \quad ~~~~~}{p(\text{次序},\text{时间},\ldots,\text{电视}) \qquad \quad}$
| 次序 | 时间 | 方式 | 天气 | 课业 | 疫情 | 电视 | 约会 |
|---|---|---|---|---|---|---|---|
| 1 | 周六 | 吃饭 | 晴天 | 轻松 | 清零 | 精彩 | 是 |
| 6 | 周六 | 逛街 | 晴天 | 轻松 | 平缓 | 无聊 | 是 |
| 10 | 周六 | 学习 | 雨天 | 轻松 | 严峻 | 无聊 | 否 |
| 13 | 周六 | 逛街 | 晴天 | 适中 | 清零 | 精彩 | 否 |
| 14 | 周间 | 逛街 | 阴天 | 适中 | 清零 | 精彩 | ? |
引入相似度函数$s(\cdot, \cdot)$和样本权重$\alpha$
$\{1, -1\} \longleftarrow \sign(\alpha_1 \cdot s(\xv_1, \xv_5) \cdot y_1 + \cdots + \alpha_4 \cdot s(\xv_4, \xv_5) \cdot y_4)$
Q:哪个算法更好?
A:不存在最强的算法,否则机器学习课把最强算法一讲就可以结课了
假设$\xv \in \{ 0,1 \}^2$、$y \in \{0,1\}$
| $x_1$ | $x_2$ | $y$ | 薛吒 | 薛跋 | 薛深 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 抛硬币 | 决策树 | 感知机 |
| 1 | 1 | 1 | |||
| 1 | 0 | ? | |||
| 0 | 1 | ? |
| $x_1$ | $x_2$ | $y$ | 薛吒 | 薛跋 | 薛深 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | $\sgn(\text{rand}() - 0.5)$ | $x_1 = 1 \wedge x_2 = 1$ | $\sgn(0.7 \cdot x_1 + 0.3 \cdot x_2 - 0.5)$ |
| 1 | 1 | 1 | |||
| 1 | 0 | ? | 1 | 0 | 1 |
| 0 | 1 | ? | 1 | 0 | 0 |
| 真实模型 | $\yv$ | 薛吒 | 薛跋 | 薛深 |
|---|---|---|---|---|
| $x_1 \cup x_2$ | 1、1 | 全对,完胜 | 全错,完败 | 对一,错一 |
| $x_1 \cap x_2$ | 0、0 | 全错,完败 | 全对,完胜 | 对一,错一 |
| $x_1$ | 1、0 | 对一,错一 | 对一,错一 | 全对,完胜 |
| $x_2$ | 0、1 | 对一,错一 | 对一,错一 | 全错,完败 |
若四个真实模型出现概率相同,则所有算法期望表现相同
$\sgn(\text{正数}) = 1$、$\sgn(\text{负数}) = 0$、$\sgn(\cdot) = (\sign(\cdot) + 1)/2$
前页的结果并非偶然,在对真实模型一无所知 (只能假设等概率出现) 的情况下,所有算法的期望错误率均为$1/2$,与随便猜等同,这称为没有免费的午餐 (no free lunch, NFL) 定理
设数据集$D \subseteq (\Xcal \times \{0,1\})^m$,其中$\Xcal$是离散的,$p(f \mid A, D)$为算法$A$基于$D$产生模型$f$的概率
模型集合$\Gcal = \{ \Xcal \mapsto \{0,1\} \}$,易知$|\Gcal| = 2^{|\Xcal|}$
给定$g \in \Gcal$为真实模型,则算法$A$的期望预测错误率为
$$ \begin{align*} \quad E (A \mid D, g) = \Ebb_\xv \int_f \Ibb(f(\xv) \ne g(\xv)) \cdot p(f \mid A, D) ~ \diff f \end{align*} $$
模型集合$\Gcal$在机器学习理论中称为假设空间 (hypothesis space)。
假设$\Gcal$中模型为真实模型的概率均为$1 / |\Gcal|$,则算法$A$的期望预测错误率为
$$ \begin{align*} \quad \sum_g \frac{E (A \mid D, g)}{|\Gcal|} & = \sum_g \frac{1}{|\Gcal|} \Ebb_\xv \int_f \Ibb(f(\xv) \ne g(\xv)) \cdot p(f \mid A, D) ~ \diff f \\ & = \Ebb_\xv \int_f p(f \mid A, D) \frac{1}{|\Gcal|} \underbrace{\sum_g \Ibb(f(\xv) \ne g(\xv))}_{|\Gcal| / 2} ~ \diff f \\ & = \Ebb_\xv \int_f \frac{p(f \mid A, D)}{2} ~ \diff f = \Ebb_\xv \frac{1}{2} = \frac{1}{2} \end{align*} $$
其中第二行是因为$\Xcal$是离散的,对任意$\xv \in \Xcal$,$\Gcal$中的$2^{|\Xcal|}$个模型恰有一半预测$\xv$为正、一半预测$\xv$为负
NFL 定理的启示:脱离具体的任务空谈什么算法好没有意义!
开汽车 vs. 骑电瓶车
NFL 定理假设了$g$的等可能性,但根据已有数据其实可以确定假设空间中部分模型为真实模型的可能性较高、而另一些则较低,因此学习算法自身的偏倚 (bias) 应与任务 (数据) 相匹配
给定模型$f: \Xcal \mapsto \Rbb$、数据集$D = \{ (\xv_i, y_i) \}_{i \in [m]}$,回归任务最常用的评估指标均方损失 (mean squared error, MSE) 定义为
$$ \begin{align*} \quad E_D (f) = \frac{1}{m} \sum_{i \in [m]} (f(\xv_i) - y_i)^2 \end{align*} $$
除此之外,以下损失也常用于回归任务
给定模型$f: \Xcal \mapsto \Rbb$、数据集$D = \{ (\xv_i, y_i) \}_{i \in [m]}$,$y_i \in \{1,-1\}$,分类任务最常用的评估指标错误率 (error rate) 定义为
$$ \begin{align*} \quad E_D (f) = \frac{1}{m} \sum_{i \in [m]} \Ibb (y_i f(\xv_i) < 0) \end{align*} $$
此外$\text{Acc}_D (f) = 1 - E_D (f)$为准确率 (accuracy)
错误率也称为 0-1 损失,不连续、难优化,常用以下替代损失
只看准确率有时不够全面
二分类结果的混淆矩阵 (confusion matrix)
| 预测 正样本 | 预测 负样本 | |
|---|---|---|
| 真实 正样本 | $\TP$ (真正例) | $\FN$ (假反例) |
| 真实 负样本 | $\FP$ (假正例) | $\TN$ (真反例) |
需要更精细的评估指标
$\quad \text{准确率} = \frac{\TP + \TN}{\TP + \TN + \FP + \FN}$
| 预测 正样本 | 预测 负样本 | |
|---|---|---|
| 真实 正样本 | $\TP$ (真正例) | $\FN$ (假反例) |
| 真实 负样本 | $\FP$ (假正例) | $\TN$ (真反例) |
查准率、查全率是一对矛盾的指标,单看其中一个没意义
precision、recall 也有人译作精确率、召回率,个人觉得没有查准率、查全率好
对于可以输出预测值的模型,可以将所有样本按模型的输出排序,前面的最可能是正样本,后面的最不可能是正样本,分类的过程就是在排序中寻找“截断点”,通过调整截断点可以得到一系列查准率、查全率,连线后就是查准查全曲线
模型 A 优于模型 B
曲线下面积缩写为 AUC (area under ROC curve)
查准查全曲线下面积不太容易算,平衡点过于简单
更常用的是查准率、查全率的调和平均,称为$F_1$
$$ \begin{align*} \quad \mathrm{F_1} = 2 \bigg/ \left( \frac{1}{P} + \frac{1}{R} \right) = 2 \bigg/ \frac{2 \cdot \TP + \FP + \FN}{\TP} = \frac{2 \cdot \TP}{\text{样本数} + \TP - \TN \quad} \end{align*} $$
若对查准率、查全率的重视程度不同
$$ \begin{align*} \quad \mathrm{F_\beta} = (1+\beta^2) \bigg/ \left( \frac{1}{P} + \frac{\beta^2}{R} \right) = \frac{(1 + \beta^2) \cdot P \cdot R}{\beta^2 \cdot P + R} \end{align*} $$
根据$\text{平方平均} \ge \text{算术平均} \ge \text{几何平均} \ge \text{调和平均}$,$F_1$最重视较小值
ROC 曲线与查准查全曲线类似,只不过纵横轴不同
$$ \begin{align*} \quad \mathrm{TPR} = \frac{\TP}{\TP+\FN}, \quad \mathrm{FPR} = \frac{\FP}{\TN+\FP} \end{align*} $$
模型 A 优于模型 B
ROC (Receiver Operating Characteristic) 曲线全称是“受试者工作特征”曲线,源于二战中用于敌机检测的雷达信号分析技术,上世纪六七十年代开始被用于心理学、医学检测中,后被引入机器学习领域
设正样本数$m^+$、负样本数$m^-$,所有样本按模型的预测值排序
ROC 曲线从左下角$(0,0)$开始绘制,设当前绘制点为$(x,y)$
根据绘制过程可知
$$ \begin{align*} \quad \mathrm{AUC} = \frac{1}{m^+ m^-} \sum_{x^+} \sum_{x^-} \left( \Ibb (f(x^+) > f(x^-)) + \frac{1}{2} \Ibb (f(x^+) = f(x^-)) \right) \end{align*} $$
ROC 曲线 vs. 查准查全曲线
多分类同样有错误率、准确率
$$ \begin{align*} \quad E_D (f) = \frac{1}{m} \sum_{i \in [m]} \Ibb (y_i \ne f(\xv_i)), \quad \text{Acc}_D (f) = 1 - E_D (f) \end{align*} $$
以及混淆矩阵
| 预测第$1$类 | 预测第$2$类 | ... | 预测第$c$类 | |
|---|---|---|---|---|
| 真实第$1$类 | ||||
| 真实第$2$类 | ||||
| ... | ||||
| 真实第$c$类 |
错误率不连续、难优化,通常采用交叉熵 (cross-entropy) 损失
设$c$个类别的预测函数分别为$f_1, \ldots, f_c$,则样本$x$的预测结果为
$$ \begin{align*} \quad \pv = \left[ \frac{e^{f_1(x)}}{\sum_{j \in [c]} e^{f_i(x)}}, \frac{e^{f_2(x)}}{\sum_{j \in [c]} e^{f_i(x)}}, \ldots, \frac{e^{f_c(x)}}{\sum_{j \in [c]} e^{f_i(x)}} \right] \quad \longleftarrow \text{softmax} \end{align*} $$
这是一个$c$维向量,同时也是一个离散概率分布
类标记$y$可转化为独热编码$\ev_y$,这也是一个$c$维离散概率分布
替代损失的要求:关于$\pv$、$\ev_y$连续,且$\pv$、$\ev_y$越接近损失越小
问题:给定离散概率分布$\qv$,如何度量分布$\pv$与它的距离?
交叉熵$H_{\qv} (\pv) \triangleq - \sum_i q_i \ln p_i$
当$\pv = \qv$时交叉熵最小,此时交叉熵$H_{\qv} (\pv)$即为分布$\qv$的熵$H(\qv)$
$$ \begin{align*} \quad \min_{\pv} H_{\qv} (\pv) = - \sum_i q_i \ln p_i, \quad \st ~ \sum_i p_i = 1 \end{align*} $$
拉格朗日函数为$L(p_i, \alpha) = - \sum_i q_i \ln p_i + \alpha (\sum_i p_i - 1)$,于是
$$ \begin{align*} \quad \nabla_{p_i} L(p_i, \alpha) & = - \frac{q_i}{p_i} + \alpha = 0 \Longrightarrow q_i = \alpha p_i \\ & \Longrightarrow \sum_i q_i = \alpha \sum_i p_i \Longrightarrow \alpha = 1 \Longrightarrow \pv = \qv \end{align*} $$
对$(x,y)$,$y \in [c]$,交叉熵损失为$- \ln \frac{e^{f_y(x)}}{\sum_{j \in [c]} e^{f_i(x)}}$
对$(x,y)$,$y \in \{1, -1\}$,$\qv = [(1+y)/2; (1-y)/2]$,交叉熵损失为
$$ \begin{align*} \quad \text{CE} & = - \frac{1+y}{2} \ln \frac{e^{f_1(x)}}{e^{f_1(x)}+e^{f_2(x)}} - \frac{1-y}{2} \ln \frac{e^{f_2(x)}}{e^{f_1(x)}+e^{f_2(x)}} \\ & = - \frac{1+y}{2} \ln \frac{e^{f_1(x)-f_2(x)}}{e^{f_1(x)-f_2(x)}+1} - \frac{1-y}{2} \ln \frac{1}{e^{f_1(x)-f_2(x)}+1} \\ & = - \frac{1+y}{2} \ln \frac{e^{w(x)}}{e^{w(x)}+1} - \frac{1-y}{2} \ln \frac{1}{e^{w(x)}+1} \quad \leftarrow w(x) \triangleq f_1(x)-f_2(x) \\ & = \begin{cases} \ln (1 + e^{-w(x)}), & y = 1 \\ \ln (1 + e^{-w(x)}), & y = -1 \end{cases} \\ & = \ln (1 + e^{- y w(x)}) \end{align*} $$
由此可见,多分类的交叉熵损失就是二分类的对率损失的拓展