Random Forests

Python में Tree-Based Models के साथ Machine Learning

Elie Kawerk

Data Scientist

Bagging

  • बेस एस्टिमेटर: Decision Tree, Logistic Regression, Neural Net, ...

  • हर एस्टिमेटर को training set के अलग bootstrap sample पर train किया जाता है

  • एस्टिमेटर training और prediction के लिए सभी features उपयोग करते हैं

Python में Tree-Based Models के साथ Machine Learning

Random Forests से और विविधता

  • बेस एस्टिमेटर: Decision Tree

  • हर एस्टिमेटर को training set के समान आकार वाले अलग bootstrap sample पर train किया जाता है

  • RF व्यक्तिगत trees के training में अतिरिक्त यादृच्छिकता जोड़ता है

  • हर node पर बिना replacement के $d$ features sample किए जाते हैं
    ( $d < \text{total number of features}$ )

Python में Tree-Based Models के साथ Machine Learning

Random Forests: Training

RFtrain

Python में Tree-Based Models के साथ Machine Learning

Random Forests: Prediction

RFpredict

Python में Tree-Based Models के साथ Machine Learning

Random Forests: Classification & Regression

Classification:

  • majority voting से predictions aggregate करता है
  • scikit-learn में RandomForestClassifier

Regression:

  • averaging से predictions aggregate करता है
  • scikit-learn में RandomForestRegressor
Python में Tree-Based Models के साथ Machine Learning

sklearn में Random Forests Regressor (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)
Python में Tree-Based Models के साथ Machine Learning
# 400 estimators वाला random forests regressor 'rf' instantiate करें
rf = RandomForestRegressor(n_estimators=400, 
                                   min_samples_leaf=0.12,  
                                   random_state=SEED)

# 'rf' को training set पर fit करें rf.fit(X_train, y_train) # test set के labels 'y_pred' predict करें y_pred = rf.predict(X_test)
# test set RMSE निकालें
rmse_test = MSE(y_test, y_pred)**(1/2)

# test set RMSE प्रिंट करें
print('Test set RMSE of rf: {:.2f}'.format(rmse_test))
Test set RMSE of rf: 3.98
Python में Tree-Based Models के साथ Machine Learning

Feature Importance

Tree-based methods: prediction में हर feature की importance मापने देते हैं.

sklearn में:

  • कोई feature impurity घटाने को tree nodes में कितना उपयोग होता है (weighted average)
  • attribute feature_importance_ से एक्सेस करें
Python में Tree-Based Models के साथ Machine Learning

sklearn में Feature Importance

import pandas as pd
import matplotlib.pyplot as plt

# features importances की pd.Series बनाएँ
importances_rf = pd.Series(rf.feature_importances_, index = X.columns)

# importances_rf sort करें                                   
sorted_importances_rf = importances_rf.sort_values()   

# एक horizontal bar plot बनाएँ
sorted_importances_rf.plot(kind='barh', color='lightgreen'); plt.show()
Python में Tree-Based Models के साथ Machine Learning

sklearn में Feature Importance

fimp_rf

Python में Tree-Based Models के साथ Machine Learning

अभ्यास करते हैं!

Python में Tree-Based Models के साथ Machine Learning

Preparing Video For Download...