Fit a decision tree
Now, you will take a stab at building a decision tree model. The decision tree is a list of machine-learned if-else rules that decide in the telecom churn case, whether customers will churn or not. Here's an example decision tree graph built on the famous Titanic survival dataset.
The train_X
, test_X
, train_Y
, test_Y
from the previous exercise have been loaded for you. Also, the tree
module and the accuracy_score
function have been loaded from the sklearn
library. You will now build your model and check its performance on unseen data.
This exercise is part of the course
Machine Learning for Marketing in Python
Exercise instructions
- Initialize the decision tree model with
max_depth
set at 5. - Fit the model on the training data, first
train_X
, thentrain_Y
. - Predict values of the testing data, or in this case
test_X
. - Measure your model's performance on the testing data by comparing between your actual test labels and predicted ones.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Initialize the model with max_depth set at 5
mytree = tree.___(max_depth = ___)
# Fit the model on the training data
treemodel = mytree.___(___, ___)
# Predict values on the testing data
pred_Y = treemodel.___(___)
# Measure model performance on testing data
accuracy_score(___, ___)