The tree makes guesses. To know if they are any good, compare them with the real answers on the test rows, the customers the model never saw while learning. The share of guesses that match is the model's accuracy: a number from 0 (always wrong) to 1 (always right).
accuracy_score(real, guesses) from sklearn.metrics gives back that share. You can also get it by hand: guesses == real compares row by row and gives back True or False for each row. .sum() counts the Trues, and .mean() gives back their share, because True counts as 1 and False as 0.
real = pd.Series([1, 0, 1, 1])
guesses = pd.Series([1, 1, 1, 0])
print((guesses == real).sum()) # 2
print((guesses == real).mean()) # 0.5
print(accuracy_score(real, guesses)) # 0.5
A model can also score itself: model.score(X, y) predicts the rows X and gives back the accuracy against the answers y, in one call. Score it on the training rows too: it does better there, because it learned from those very rows. Only the test score tells you how it does on new customers.
The starting code loads the data, splits it and fits the tree. When you are done, your code prints:
Accuracy: 0.627 Right answers: 47 of 75 By hand: 0.627 model.score: 0.627 On the training rows: 0.813
After your lines, the check prints how many predictions you made.
Your task: predict every test row into predictions, then print the accuracy with accuracy_score, the count of right answers, the same share by hand with .mean(), and model.score on the test rows and on the training rows, rounding each share to 3 digits.
Assignment
Follow the Coding Tutorial to measure how often the tree's guesses are right, in three ways that agree.
Hint
Put the real answers first: accuracy_score(y_test, predictions). len(y_test) gives back the number of test rows, and round(x, 3) keeps 3 digits.
Free plan: 30 tutor messages. PRO is unlimited.
