机器学习vs统计方法
监督学习vs无监督学习
深度学习vs强化学习
泛化差距/泛化鸿沟(generalization gap):
测试风险(test risk)
使用两个常见的指导原则和两种方法来减少过度拟合:
K折交叉验证(K-fold cross-validation)
留一交叉验证(Leave-one-out cross-validation):
|
((150, 4), (150,))
((90, 4), (90,))
((60, 4), (60,))
|
|
array([0.96..., 1. , 0.96..., 0.96..., 1. ])
0.98 accuracy with a standard deviation of 0.02
array([0.96..., 1. ..., 0.96..., 0.96..., 1. ]) |
|
TimeSeriesSplit(gap=0, max_train_size=None, n_splits=3, test_size=None)
[0 1 2] [3] |
All models are wrong, but some models are useful.
--- George Box
性质 | 统计推断 | 监督机器学习 |
---|---|---|
目标 | 具有解释力的因果模型 | 预测表现,往往解释力有限 |
数据 | 数据由模型生成 | 数据生成过程未知 |
框架 | 概率 | 算法和概率 |
表达能力 | 通常是线性的 | 非线性 |
模型选择 | 基于信息准则 | 数值优化 |
可扩展性 | 仅限于低维数据 | 缩放至高维输入数据 |
稳健性 | 容易出现过度拟合 | 专为样本外性能而设计 |
诊断 | 广泛 | 有限 |
|
|
|
![]() |
均方误差(MSE)或均方根误差(RMSE)、残差分析、信息准则(AIC, BIC)、卡方检验(Chi-square test)、似然比检验等也常被用来分析回归算法的拟合优度。
其中
|
![]()
|
|
![]()
|
![]() |
|
|
|
在
例子:在点
自由度
使用更多的节点会导致更灵活的分段多项式
|
![]() |
|
![]() |
|
|
仅使用附近的训练观测值来计算目标点
|
![]() |
|
局部线性回归 ![]() |
|
|
|
|
|
|
PLS与PCA比较
|
与回归方法比较:
|
经济领域应用:
管理领域应用:
|
金融领域应用:
|
优点:
缺点:
|
实施建议:
|
|
|
|
|
探索性数据分析
|
![]() |
学习分类器
|
![]() |
似然函数(likelihood function):
对数似然函数(log likelihood function)
|
![]() source: ISLP |
|
|
|
|
|
![]() |
对具有两个以上类别的响应变量进行分类
模型
|
|
|
![]() |
![]() |
|
|
![]() |
|
![]() |
将对数几率比建模为广义加性模型: |
|
|
![]() |
对于分离超平面
和
等价地,分离超平面具有以下性质
对所有
|
![]() |
![]() |
|
![]() |
|
![]() |
|
![]() |
|
|
![]() |
支持向量机无法处理非线性。
我们能做什么?
|
![]() |
核函数的一些例子
|
假设
|
|
SVC
|
SVM
|
内积/核
|
|
|
函数形式
|
|
|
|
|
一对一(OVO)分类
一对多(OVA)分类
支持向量分类器优化的铰链损失 + 惩罚形式:
SVM vs. 逻辑回归
基于数据特征选择
|
基于应用需求选择
|
|
|
|
![]() |
|
|
|
![]() |
优点
|
缺点
|
|
|
Stacking (stacked generalization): 通过以下方式组合基础模型
举例:我们邀请不同专业背景的专家:
数学老师:根据数学练习表现预测:76分
心理咨询师:根据学习态度和压力管理预测:72分
历史老师:根据学生逻辑推理能力预测:74分
最后,校长(元学习器)不直接评估学生,而是根据这三位专家的预测结果做出最终判断:
校长:分析三位专家预测,考虑每位专家在不同类型学生上的准确率
最终预测:75分
老师A:基于自己看到的80%学生数据预测:平均75分
老师B:基于不同的80%学生数据预测:平均73分
老师C:基于不同的80%学生数据预测:平均78分
...
最终预测:所有老师预测分数的平均值 = 75分
Bootstrap的缺点
Bootstrap的主要优点是它防止集成过度依赖任何单个训练样本,从而增强鲁棒性和泛化能力。
Bagging并不总是能提高性能。特别是,它依赖于基础模型是不稳定的估计器(决策树),这样忽略一些数据就会显著改变得到的模型拟合结果。
|
![]() |
|
|
提升法是一种顺序拟合加法模型的算法,其中每个
只要每个
提升法、装袋法和随机森林
|
|
基于数据规模
基于问题复杂度
基于可解释性需求
|
基于数据质量
基于计算资源
特征特性考虑
|
|
|
![]() |
|
|
![]() |
|
![]() |
数据集中存在的总方差定义为
第
第
数据的方差可以分解为前
我们可以将PVE解释为由前
|
![]() |
![]() |
![]() |
步骤一:标准化
步骤二:PCA转换
步骤三:主成分回归
|
|
|
|
方法 | 方法特点 | 是否考虑Y进行转换? | 最佳应用场景 |
---|---|---|---|
PCR | 使用PCA后回归 | 否 | 多个相关预测变量 |
PLS | 创建与Y相关的成分 | 是 | 当Y应引导降维时 |
岭回归 | 收缩所有系数 | 否 | 当所有预测变量都应保留 |
Lasso | 将部分系数收缩至零 | 否 | 需要特征选择时 |
|
![]() |
- **The probabilistic approach** - treat all unknown quantities as random variables - it is the optimal approach to decision making under uncertainty <br> >Almost all of machine learning can be viewed in probabilistic terms, making probabilistic thinking fundamental. It is, of course, not the only view. But it is through this view that <font color="red">we can connect what we do in machine learning to every other computational science, whether that be in stochastic optimisation, control theory, operations research, econometrics, information theory, statistical physics or bio-statistics.</font> For this reason alone, mastery of probabilistic thinking is essential. <p align="right">---Shakir Mohamed, DeepMind<p>
<img src="https://scikit-learn.org/stable/_images/grid_search_workflow.png" width=150%>
--- <div align="center"> <table rules="none"> <tr> <td> <div style="width: 300"> ### 聚类(Clustering) - 在数据中查找**聚类**:将输入划分为包含“相似”点的区域。 </div> </td> <td> <div style="width: 500pt"> <img align="center" style="padding-right:10px;" width=100% src="../myfig/L01/L01-clustering.png"> </div> </td> </tr> </table> </div>
--- ## 7. Python实现案例:基础实现 ```python # PCR基础实现 from sklearn.decomposition import PCA from sklearn.linear_model import LinearRegression from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler import numpy as np import matplotlib.pyplot as plt # 创建管道 pcr_model = make_pipeline( StandardScaler(), PCA(n_components=5), # 选择主成分数量 LinearRegression() ) # 拟合模型 pcr_model.fit(X_train, y_train) # 预测 predictions = pcr_model.predict(X_test) ``` --- ## 7. Python实现案例:组件选择 ```python # 使用交叉验证选择最佳主成分数量 from sklearn.model_selection import cross_val_score from sklearn.metrics import mean_squared_error # 测试不同的主成分数量 n_components = range(1, X.shape[1] + 1) cv_scores = [] for n in n_components: pcr_model = make_pipeline( StandardScaler(), PCA(n_components=n), LinearRegression() ) # 使用交叉验证评估性能 scores = -cross_val_score(pcr_model, X, y, cv=5, scoring='neg_mean_squared_error') cv_scores.append(scores.mean()) # 绘制结果 plt.figure(figsize=(10, 6)) plt.plot(n_components, cv_scores, marker='o') plt.xlabel('主成分数量') plt.ylabel('MSE') plt.title('PCR: 主成分数量与MSE关系') ``` --- ## 7. Python实现案例:完整示例 ```python # 完整PCR实现示例 import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split, cross_val_score from sklearn.metrics import mean_squared_error, r2_score # 1. 加载数据并划分 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 2. 标准化 scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # 3. PCA pca = PCA() X_train_pca = pca.fit_transform(X_train_scaled) # 4. 检查累积方差 explained_variance = pca.explained_variance_ratio_ cumulative_variance = np.cumsum(explained_variance) optimal_n = np.argmax(cumulative_variance >= 0.95) + 1 # 解释95%方差所需的成分数 # 5. 使用选择的组件数构建模型 pca = PCA(n_components=optimal_n) X_train_pca = pca.fit_transform(X_train_scaled) X_test_pca = pca.transform(X_test_scaled) # 6. 拟合回归模型 regressor = LinearRegression() regressor.fit(X_train_pca, y_train) # 7. 预测 y_train_pred = regressor.predict(X_train_pca) y_test_pred = regressor.predict(X_test_pca) # 8. 评估模型 train_mse = mean_squared_error(y_train, y_train_pred) test_mse = mean_squared_error(y_test, y_test_pred) test_r2 = r2_score(y_test, y_test_pred) print(f"使用的主成分数: {optimal_n}") print(f"训练集MSE: {train_mse:.4f}") print(f"测试集MSE: {test_mse:.4f}") print(f"测试集R²: {test_r2:.4f}") ```