Sklearn Random Forest Classifier Example - Training the Model
Python
This snippet demonstrates how to train a Random Forest classifier using Sklearn. In this code example the classifier is initialised, hyperparameters are set before fitting. The model is then evaluated on test data and feature importances are output for review.
1| from sklearn.ensemble import RandomForestClassifier 2| from sklearn.metrics import classification_report, log_loss, roc_auc_score 3| 4| # Step 1: Initialise Random Forest Classifier and fit model 5| model = RandomForestClassifier(n_estimators=10, 6| max_depth=None, 7| class_weight='balanced', 8| n_jobs=-1, 9| random_state=101) 10| model.fit(X_train, y_train) 11| 12| # Step 2: Create dictionary that contains feature importance 13| feature_importance = dict(zip(X_train.columns, model.feature_importances_)) 14| print('Feature Importance:',feature_importance) 15| 16| # Step 3: Make predictions for test data & evaluate performance 17| y_pred = model.predict(X_test) 18| print('Classification Report:',classification_report(y_test, y_pred)) 19| print('Log Loss:',log_loss(y_test, y_pred)) 20| print('ROC AUC:',roc_auc_score(y_test, y_pred))
149
132
127
119