Random Forests

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

Bagging

  • Base estimator: Decision Tree, Logistic Regression, Neural Net, ...

  • แต่ละ estimator ถูกเทรนบน bootstrap sample ที่แตกต่างกันของชุดข้อมูลเทรน

  • estimator ใช้ฟีเจอร์ทั้งหมดในการเทรนและพยากรณ์

Machine Learning with Tree-Based Models in Python

ความหลากหลายเพิ่มเติมด้วย Random Forests

  • Base estimator: Decision Tree

  • แต่ละ estimator ถูกเทรนบน bootstrap sample ที่ต่างกัน โดยมีขนาดเท่ากับชุดข้อมูลเทรน

  • RF เพิ่มความหลากหลายในการเทรนต้นไม้แต่ละต้น

  • สุ่มเลือก $d$ ฟีเจอร์ที่แต่ละโหนดโดยไม่ซ้ำ
    ( $d < \text{total number of features}$ )

Machine Learning with Tree-Based Models in Python

Random Forests: การเทรน

การเทรน RF

Machine Learning with Tree-Based Models in Python

Random Forests: การพยากรณ์

การพยากรณ์ RF

Machine Learning with Tree-Based Models in Python

Random Forests: การจำแนกประเภทและการถดถอย

การจำแนกประเภท:

  • รวมผลพยากรณ์ด้วยการโหวตเสียงข้างมาก
  • RandomForestClassifier ใน scikit-learn

การถดถอย:

  • รวมผลพยากรณ์ด้วยการหาค่าเฉลี่ย
  • RandomForestRegressor ใน scikit-learn
Machine Learning with Tree-Based Models in Python

Random Forests Regressor ใน sklearn (ชุดข้อมูล auto)

# Basic imports
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
# Set seed for reproducibility
SEED = 1

# Split dataset into 70% train and 30% test
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.3,
                                                    random_state=SEED)
Machine Learning with Tree-Based Models in Python
# Instantiate a random forests regressor 'rf' 400 estimators
rf = RandomForestRegressor(n_estimators=400, 
                                   min_samples_leaf=0.12,  
                                   random_state=SEED)

# Fit 'rf' to the training set rf.fit(X_train, y_train) # Predict the test set labels 'y_pred' y_pred = rf.predict(X_test)
# Evaluate the test set RMSE
rmse_test = MSE(y_test, y_pred)**(1/2)

# Print the test set RMSE
print('Test set RMSE of rf: {:.2f}'.format(rmse_test))
Test set RMSE of rf: 3.98
Machine Learning with Tree-Based Models in Python

ความสำคัญของฟีเจอร์

วิธีที่ใช้ต้นไม้: สามารถวัดความสำคัญของแต่ละฟีเจอร์ในการพยากรณ์ได้

ใน sklearn:

  • วัดจากปริมาณที่โหนดของต้นไม้ใช้ฟีเจอร์นั้นลด impurity (ค่าเฉลี่ยแบบถ่วงน้ำหนัก)
  • เข้าถึงได้ผ่าน attribute feature_importance_
Machine Learning with Tree-Based Models in Python

ความสำคัญของฟีเจอร์ใน sklearn

import pandas as pd
import matplotlib.pyplot as plt

# Create a pd.Series of features importances
importances_rf = pd.Series(rf.feature_importances_, index = X.columns)

# Sort importances_rf                                   
sorted_importances_rf = importances_rf.sort_values()   

# Make a horizontal bar plot
sorted_importances_rf.plot(kind='barh', color='lightgreen'); plt.show()
Machine Learning with Tree-Based Models in Python

ความสำคัญของฟีเจอร์ใน sklearn

ความสำคัญของฟีเจอร์ RF

Machine Learning with Tree-Based Models in Python

มาฝึกกันเถอะ!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...