Lab 3 of 8about 40 minutes
A call list the bank can trust
It uses what you learned in Precision, recall and the confusion matrix, Thresholds, ROC and PR curves, Preparing features and Data leakage.
Start here
Run itThe bank's calls from the videos, made small for the page: 2,000 of the 41,188, drawn at random with the same share of yes, so 225 said yes. It keeps the twelve columns the bank knows before it calls, the call's length, duration, and the answer, y. A sample of 2,000 gives numbers near the videos', never quite the same.
1The table as it is
Run itFrom Preparing features
Run the cell. It keeps 500 calls aside, stratified so they keep the share of yes, and hands the rest to logistic regression just as they are. It stops, as in ml-feature-prep: a model reads numbers only.
2One pipeline
Your turnFrom Preparing features
Write the column transformer of ml-feature-prep. Age and the two counts of calls go on one ruler. The days since an earlier call get 999 filled and flagged. The eight columns of words go one-hot. The cell joins it to the model and scores the pipeline on five folds, by the area under the ROC curve.
The folds spread wider than the video's: each holds 300 calls and about 34 yeses, so luck moves each one more.
Hint
The ruler takes a list of three column names. The words are already in a list, words.
One way to do it
prep = make_column_transformer(
(StandardScaler(), ["age", "campaign", "previous"]),
(SimpleImputer(missing_values=999, add_indicator=True), ["pdays"]),
(OneHotEncoder(handle_unknown="ignore"), words))
model = make_pipeline(prep, LogisticRegression())
scores = cross_val_score(model, X_train, y_train, cv=5, scoring="roc_auc")
print(scores.round(3), scores.mean())3The call's length
On your ownFrom Data leakage and Preparing features
On your own now. Make long_model: the same pipeline with duration on the ruler too. Score it on five folds of the training calls with their lengths, calls.loc[X_train.index, cols + ["duration"]], and keep the average in long_score. Then ask: when the bank chooses whom to call, would it have this?
The length is known only when the call ends, with the answer. So it leaks, and the model stays model, without it.
Hint
Copy the column transformer from step 2 and add "duration" to the ruler's list. cross_val_score(...).mean() gives one number.
One way to do it
long_prep = make_column_transformer(
(StandardScaler(), ["age", "campaign", "previous", "duration"]),
(SimpleImputer(missing_values=999, add_indicator=True), ["pdays"]),
(OneHotEncoder(handle_unknown="ignore"), words))
long_model = make_pipeline(long_prep, LogisticRegression())
X_long = calls.loc[X_train.index, cols + ["duration"]]
long_score = cross_val_score(long_model, X_long, y_train, cv=5, scoring="roc_auc").mean()
print(long_score)4The four counts
Your turnFrom Precision, recall and the confusion matrix
Fit model on all 1,500 training calls, and open the 500 kept aside. Keep its picks at one half in picks, then its precision and its recall. The cell also scores the accuracy of the model and of a dummy that says no to everyone.
The dummy is right 0.888 of the time and the model 0.906, yet the model finds only 12 of the 56 yeses. Accuracy hides a rare yes.
Hint
predict gives the picks at one half. Each score takes the truth first, then the picks.
One way to do it
model.fit(X_train, y_train)
picks = model.predict(X_test)
print(confusion_matrix(y_test, picks))
precision = precision_score(y_test, picks)
recall = recall_score(y_test, picks)
dummy = DummyClassifier().fit(X_train, y_train)
print(precision, recall, model.score(X_test, y_test), dummy.score(X_test, y_test))5A budget of 100 calls
Your turnFrom Thresholds, ROC and PR curves and Precision, recall and the confusion matrix
The bank can make only 100 calls from these 500. The cell puts the calls in a queue, the surest first. Keep the chance of the 100th in line, so the line picks exactly the first 100. Then count how many of them said yes, in found.
Hint
The queue counts from 0, so the 100th is at place 99. The yeses among the picks are y_test[picks].
One way to do it
chance = model.predict_proba(X_test)[:, 1]
queue = pd.Series(chance, index=X_test.index).sort_values(ascending=False)
line = queue.iloc[99]
picks = chance >= line
found = y_test[picks].sum()
print(picks.sum(), "picked,", found, "said yes; the line falls at", round(line, 3))6The cheapest line
On your ownFrom Thresholds, ROC and PR curves and Precision, recall and the confusion matrix
On your own now. A missed yes costs the bank as much as ten wasted calls. So a line's cost is its wasted calls plus ten for each missed yes. Try every line from 0.01 to 0.99, in steps of 0.01, on the 500 calls. Keep the cheapest in cheapest, and its cost in lowest.
It lands near 0.12, the video's at 0.09: on 500 calls the lines near it cost about the same.
Hint
A wasted call is picked and said no: picked & ~y_test. A missed yes was not picked and said yes. min(costs, key=costs.get) gives the line with the smallest cost.
One way to do it
def cost(line):
picked = chance >= line
return (picked & ~y_test).sum() + 10 * (~picked & y_test).sum()
costs = {line: cost(line) for line in np.arange(0.01, 1, 0.01)}
cheapest = min(costs, key=costs.get)
lowest = costs[cheapest]
print(round(cheapest, 2), lowest, "against", cost(0.5), "at one half")Run on Pyodide 314, Python 3.14, NumPy 2.4, pandas 3.0 and scikit-learn 1.8. Checked on .
