Machine Learning cu modele bazate pe arbori în Python
Elie Kawerk
Data Scientist
Estimator de bază: Arbore de Decizie, Regresie Logistică, Rețea Neuronală, ...
Fiecare estimator este antrenat pe un eșantion bootstrap distinct din setul de antrenament
Estimatorii folosesc toate caracteristicile pentru antrenament și predicție
Estimator de bază: Arbore de Decizie
Fiecare estimator este antrenat pe un eșantion bootstrap diferit, de aceeași dimensiune cu setul de antrenament
RF introduce diversitate suplimentară în antrenarea arborilor individuali
$d$ caracteristici sunt selectate aleatoriu la fiecare nod fără înlocuire
( $d < \text{total number of features}$ )


Clasificare:
RandomForestClassifier în scikit-learn Regresie:
RandomForestRegressor în scikit-learn# 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)
# 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
Metodele bazate pe arbori permit măsurarea importanței fiecărei caracteristici în predicție.
În sklearn:
feature_importance_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 cu modele bazate pe arbori în Python