Regression with Histogram Gradient Boosting in Scikit-Learn

Python

A step-by-step example of how to train a Histogram Gradient Boosted model using HistGradientBoostingRegressor from Sklearn.

 1|  from sklearn.ensemble import HistGradientBoostingRegressor
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
 3|  
 4|  # Step 1: Create list containing the indices of categorical features
 5|  categorical_features = [1]
 6|  
 7|  # Step 2: Initialise histogram gradient boosting regression model and fit
 8|  model = HistGradientBoostingRegressor( loss='squared_error',
 9|                                         learning_rate=0.1,
10|                                         max_depth=None,
11|                                         max_bins=255,
12|                                         categorical_features=categorical_features,
13|                                         random_state=101)
14|  model.fit(X_train, y_train)
15|  
16|  # Step 3: 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