模型泛化:自助抽樣與交叉驗證

用 Python 練習機器學習面試題

Lisa Stuart

Data Scientist

第 4 章概要

  • 自助抽樣/交叉驗證 → 模型泛化
  • 類別不平衡
  • 特徵相關
  • 集成模型選擇
用 Python 練習機器學習面試題

模型泛化

  • ML 模型在未知資料上的表現能力
    • 測試資料集
    • 未來資料
  • 訓練指標 $\approx$ 測試指標
  • 過度擬合的模型無法泛化
用 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 練習機器學習面試題

一起來練習吧!

用 Python 練習機器學習面試題

Preparing Video For Download...