Sklearn DecisionTreeClassifier - Training a Decision Tree Classification Model

Python

In this code snippet a Decision Tree classification model is trained using Sklearn. The model is then evaluated by exploring the feature importance of each feature in the training data and then testing against new data using a range of evaluation metrics.

 1|  from sklearn.tree import DecisionTreeClassifier
 2|  from sklearn.metrics import classification_report, log_loss, roc_auc_score
 3|  
 4|  # initialise & fit Decision Tree classifier
 5|  model = DecisionTreeClassifier(criterion='gini',
 6|                                max_depth=None,
 7|                                min_samples_split=5,
 8|                                min_samples_leaf=5,
 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 predictions for test data & evaluate performance
17|  y_pred = model.predict(X_test)
18|  print('Classification Report:',classification_report(y_test, y_pred))
19|  print('Log Loss:',log_loss(y_test, y_pred))
20|  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