Python में Tree-Based Models के साथ Machine Learning
Elie Kawerk
Data Scientist
बेस एस्टिमेटर: Decision Tree, Logistic Regression, Neural Net, ...
हर एस्टिमेटर को training set के अलग bootstrap sample पर train किया जाता है
एस्टिमेटर training और prediction के लिए सभी features उपयोग करते हैं
बेस एस्टिमेटर: Decision Tree
हर एस्टिमेटर को training set के समान आकार वाले अलग bootstrap sample पर train किया जाता है
RF व्यक्तिगत trees के training में अतिरिक्त यादृच्छिकता जोड़ता है
हर node पर बिना replacement के $d$ features sample किए जाते हैं
( $d < \text{total number of features}$ )


Classification:
RandomForestClassifier Regression:
RandomForestRegressor# 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)
# 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
Tree-based methods: prediction में हर feature की importance मापने देते हैं.
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