14 of the 300 customers have no age: pandas shows each blank as NaN. Most models, like LogisticRegression, stop with an error when X holds a NaN, so until now you filled blanks in with fillna or left the column out.
HistGradientBoostingClassifier is gradient boosting built for big tables, and the kind of model used most on tables at work. Before it grows any tree, it sorts each column's values into at most 255 bins, so a tree only tries splits at the edges between bins, which is much faster than trying every value. Blank values get a bin of their own: at each split, the model learns from the training rows which side the blanks should go to. So you give it NaN as it is. Its max_iter is the number of trees, like n_estimators in GradientBoostingClassifier.
Here it is on another value, the money a customer spends, with the blank ages left in:
from sklearn.ensemble import HistGradientBoostingRegressor
features = df[["visits", "orders", "age"]]
reg = HistGradientBoostingRegressor(max_iter=30, random_state=0)
reg.fit(features, df["spent"])
print(reg.predict(features.head(3)).round(1))
model.predict_proba(rows) gives back one row per customer with two chances: column 0 for label 0, column 1 for label 1, buying again. cross_val_score with cv=5 gives back 5 accuracies, one per part of the customers. Your program must print (the last line comes from the lesson's check):
Missing ages: 14 | test customers without an age: 4
visits orders spent days_since_last returned_item age
248 15 4 210 130 1 NaN
103 8 5 242 16 0 NaN
299 4 2 210 24 0 NaN
7 4 2 364 4 1 NaN
Chance of buying again: [0.73 0.93 0.65 0.95]
Cross-validated, all 300 customers: [0.75 0.767 0.767 0.783 0.75 ] mean 0.763
Columns it learned from: ['visits', 'orders', 'spent', 'days_since_last', 'returned_item', 'age']
Your task: the starting code loads the customers with age as a sixth feature, splits them and prints the 4 test customers without an age, kept in no_age. Below the blank line, make a HistGradientBoostingClassifier with max_iter=50 and random_state=0 in model and fit it on the training rows, blanks and all. Print its chance of buying again for no_age, rounded to 2 digits, then cross-validate model on all of X and y with cv=5 and print the rounded scores and their mean.
Assignment
Follow the Coding Tutorial to fit a HistGradientBoostingClassifier on customers with missing ages, with no fillna, and cross-validate it.
Hint
Don't fill the blanks: fit straight on X_train. Keep column 1 of predict_proba with [:, 1] and round it with .round(2); round the scores with .round(3) and their mean with round(..., 3).
Free plan: 30 tutor messages. PRO is unlimited.
