Sklearn Gradient Boosting Regressor - Training a Regression Model

Python

Training a regression model using Sklearn's GradientBoostingRegressor.

This example shows how to train, evaluate and output the feature importances for the model.

 1|  from sklearn.ensemble import GradientBoostingRegressor
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
 3|  
 4|  # initialise & fit Gradient Boosting Regressor
 5|  model = GradientBoostingRegressor(loss='squared_error',
 6|                                    n_estimators=100,
 7|                                    max_depth=None,
 8|                                    subsample=0.8,
 9|                                    random_state=101)
10|  model.fit(X_train, y_train)
11|  
12|  # 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|  # make prediction for test data & evaluate performance
17|  y_pred = model.predict(X_test)
18|  print('RMSE:',mean_squared_error(y_test, y_pred, squared = False))
19|  print('MAE:',mean_absolute_error(y_test, y_pred))
20|  print('MAPE:',mean_absolute_percentage_error(y_test, y_pred))
21|  print('Max Error:',max_error(y_test, y_pred))
22|  print('Explained Variance Score:',explained_variance_score(y_test, y_pred))
Did you find this snippet useful?

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