模型泛化:自助法与交叉验证

用 Python 练习机器学习面试题

Lisa Stuart

Data Scientist

第 4 章概览

  • 自助法/交叉验证 --> 模型泛化
  • 类别不平衡
  • 特征相关性
  • 集成模型选择
用 Python 练习机器学习面试题

模型泛化

  • 机器学习模型在未见数据上的表现能力
    • 测试集
    • 未来数据
  • 训练指标 ≈ 测试指标
  • 过拟合模型无法泛化
用 Python 练习机器学习面试题

决策树

决策树图

1 https://medium.com/@rnbrown/creating-and-visualizing-decision-trees-with-python-f8e8fa394176
用 Python 练习机器学习面试题

决策树节点

决策树根节点

用 Python 练习机器学习面试题

优缺点对比

决策树图

  • 优点:
    • 易于理解
    • 易于可视化
  • 缺点:
    • 易过拟合
    • 属于贪心算法
    • 类别不平衡时有偏
用 Python 练习机器学习面试题

随机森林

随机森林

1 https://www.researchgate.net/figure/Random-Forest-visualization_fig11_326560291
用 Python 练习机器学习面试题

K 折交叉验证

K 折交叉验证

1 https://scikit-learn.org/stable/modules/cross_validation.html
用 Python 练习机器学习面试题

函数

# decision tree
`sklearn.tree.DecisionTreeClassifier` 

# random forest 
`sklearn.ensemble.RandomForestClassifier`

# cross-validated grid search
`sklearn.model_selection.GridSearchCV` 

# model accuracy
`sklearn.metrics.accuracy_score` 

# train/test split function
`sklearn.model_selection.train_test_split`

# Parameters that gave best results
`cross-val_model.best_params_`

# Mean cross-validated score of 
# estimator with best params 
`cross-val_model.best_score_`
用 Python 练习机器学习面试题

GridSearchCV vs RandomSearchCV

网格搜索

用 Python 练习机器学习面试题

Passons à la pratique !

用 Python 练习机器学习面试题

Preparing Video For Download...