迴歸模型

Python 的模型驗證

Kasey Jones

Data Scientist

scikit-learn 的隨機森林

from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
rfr = RandomForestRegressor(random_state=1111)
rfc = RandomForestClassifier(random_state=1111)
Python 的模型驗證

決策樹一開始包含所有點,並從頂端依不同切分往下分支。想像每個資料點依其特徵沿著樹的不同路徑前進。

Python 的模型驗證

隨機森林建立完成後,會對每個資料點取各棵決策樹的結果平均,作為該資料點的最終預測。

Python 的模型驗證

隨機森林參數

n_estimators:森林中的樹數

max_depth:樹的最大深度

random_state:隨機種子

from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(n_estimators=50, max_depth=10)
rfr = RandomForestRegressor(random_state=1111)
rfr.n_estimators = 50
rfr.max_depth = 10
Python 的模型驗證

特徵重要性

列印各欄位對模型的重要性

for i, item in enumerate(rfr.feature_importances_):
    print("{0:s}: {1:.2f}".format(X.columns[i], item))
weight: 0.50
height: 0.39
left_handed: 0.72
union_preference: 0.05
eye_color: 0.03
Python 的模型驗證

開始吧

Python 的模型驗證

Preparing Video For Download...