Sklearn Random Forest Regression Example

Python

An example of training a Random Forest model using RandomForestRegressor from Sklearn, evaluating the model on test data and reviewing feature importance.

 1|  from sklearn.ensemble import RandomForestRegressor
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
 3|  
 4|  # Step 1: Initialise & fit Random Forest Regressor
 5|  model = RandomForestRegressor(n_estimators=10, 
 6|                                criterion="squared_error",
 7|                                max_depth=None,
 8|                                min_samples_split=2,
 9|                                min_samples_leaf=1,
10|                                n_jobs=-1,
11|                                random_state=101)
12|  model.fit(X_train, y_train)
13|  
14|  # Step 2: Create dictionary that contains feature importance
15|  feature_importance= dict(zip(X_train.columns, model.feature_importances_))
16|  print('Feature Importance',feature_importance)
17|  
18|  # Step 3: Make prediction for test data & evaluate performance
19|  y_pred = model.predict(X_test)
20|  print('RMSE:',mean_squared_error(y_test, y_pred, squared = False))
21|  print('MAE:',mean_absolute_error(y_test, y_pred))
22|  print('MAPE:',mean_absolute_percentage_error(y_test, y_pred))
23|  print('Max Error:',max_error(y_test, y_pred))
24|  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