Sklearn Gradient Boosting Classifier - Training a Classification Model

Python

How to train a classification model using gradient boosted trees with sklearn

 1|  from sklearn.ensemble import GradientBoostingClassifier
 2|  from sklearn.metrics import classification_report, log_loss, roc_auc_score
 3|  
 4|  # Step 1: Initialise histogram gradient boosting classification model and fit
 5|  model = GradientBoostingClassifier(learning_rate=0.1,
 6|                                     n_estimators=10,
 7|                                     subsample=1,
 8|                                     max_depth=None,
 9|                                     random_state=101
10|                                        )
11|  model.fit(X_train, y_train)
12|  
13|  # Step 2: Create dictionary that contains feature importance
14|  feature_importance = dict(zip(X_train.columns, model.feature_importances_))
15|  print('Feature Importance:',feature_importance)
16|  
17|  # Step 3: Make predictions for test data & evaluate performance
18|  y_pred = model.predict(X_test)
19|  print('Classification Report:',classification_report(y_test, y_pred))
20|  print('Log Loss:',log_loss(y_test, y_pred))
21|  print('ROC AUC:',roc_auc_score(y_test, y_pred))
Did you find this snippet useful?

Sign up for free to to add this to your code library