Exercise

Balancing classes

It can significantly affect prediction results, as shown by the difference between the recall and accuracy scores. To solve the imbalance, equal weights are usually given to each class. Using the class_weight argument in sklearn's DecisionTreeClassifier, one can make the classes become "balanced".

Let’s correct our model by solving its imbalance problem:

  • first, you’re going to set up a model with balanced classes
  • then, you will fit it to the training data
  • finally, you will check its accuracy on the test set

The variables features_train, target_train, features_test and target_test are already available in your workspace.

Instructions

100 XP
  • Initialize the Decision Tree Classifier, prune your tree by limiting its maximum depth to 5, and balance the class weights.
  • Fit the new model.
  • Print the accuracy score of the prediction (in percentage points) for the test set.