So far model.predict gave back a plain 1 or 0 for each customer. Behind that answer the model has a probability: how sure it is that the customer will buy again. A decision tree takes it from the leaf the customer lands in: if 9 of the 10 training customers in that leaf came back, the chance is 0.9.
model.predict_proba(X_test) gives back a numpy array with one row per customer and two columns: the chance of a 0 and the chance of a 1, which add up to 1. [:, 1] takes every row of column 1. Here is the idea on a small array of made-up chances:
probs = np.array([[0.2, 0.8],
[0.6, 0.4],
[0.1, 0.9]])
print(probs[:, 1]) # [0.8 0.4 0.9]
print(probs[:, 1] > 0.5) # [ True False True]
model.predict is the same as a cut at 0.5: yes when the chance is above 0.5, so a chance of exactly 0.5 is a no. You can move the cut. A higher cut says yes to fewer customers, the ones the model is surer about, so precision goes up and recall goes down. The given show(name, said_yes) prints how many yeses a cut gives (.sum() counts the Trues), then their precision and recall.
Your finished script prints this (the last line comes from the lesson's check):
First 5 chances: [1. 1. 0.5 0. 0. ] Cut 0.5 - says yes to 42 precision: 0.833 recall: 0.714 Cut 0.7 - says yes to 37 precision: 0.865 recall: 0.653 Cut 0.5 says what model.predict says: True
Your task: below model.fit, keep each test customer's chance of a 1 in chances, print the first 5 rounded to 3 digits, then call show for a cut at 0.5 and for a cut at 0.7.
Assignment
Follow the Coding Tutorial to turn the model's chances into yes-or-no answers at two cuts and compare their precision and recall.
Hintchances[:5] takes the first 5 numbers and .round(3) rounds each one. chances > 0.5 gives back True for every customer whose chance is above 0.5: that is the said_yes that show needs.
Free plan: 30 tutor messages. PRO is unlimited.
