Sklearn Decision Tree Regressor - Training a Decision Tree Regression Model

Python

In this code example a Decision Tree Regressor model is trained using Sklearn. A dictionary is also created containing a feature importance value for each feature and then the performance of the model is evaluated using test data.

 1|  from sklearn.tree import DecisionTreeRegressor
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
 3|  
 4|  # initialise & fit Decision Tree Regressor
 5|  model = DecisionTreeRegressor(criterion='squared_error',
 6|                                max_depth=None,
 7|                                min_samples_split=2,
 8|                                min_samples_leaf=1,
 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