A score means nothing on its own. Is a model that is right 0.62 of the time good? Not if always guessing "came back" is already right about 0.6 of the time. So you first score a baseline: DummyClassifier(strategy="most_frequent") always guesses the most common answer of the rows it learned from. Your model has to beat it.
The model: LogisticRegression only reads numbers. make_column_transformer gives back a preparer that turns columns into numbers: you give it pairs of (a way, the columns it is for), and remainder says what happens to the columns you didn't name (by default, they are dropped). OneHotEncoder gives each city its own 0/1 column, and StandardScaler puts every number column on the same scale. make_pipeline(a, b) gives back one model that runs a, then hands its numbers to b, both when it learns and when it predicts.
Scoring on the training rows: cross_val_score(model, X, y, cv=5) cuts the rows into 5 parts, learns 5 times on 4 of them, scores each time on the part held back, and gives back the 5 accuracies. The test rows stay untouched for the final check. For example, a smaller pipeline that scales two columns and drops the rest:
prepare = make_column_transformer(
(StandardScaler(), ["spent", "visits"]),
)
small = make_pipeline(prepare, LogisticRegression(max_iter=2000))
print(cross_val_score(small, X_train, y_train, cv=5).round(3))
When you're done, the program prints:
Baseline: 0.596 Model: [0.733 0.822 0.778 0.711 0.778] mean 0.764
Your task: below the split, build prepare (one-hot "city" and "newsletter" with handle_unknown="ignore", scale every other column), the pipeline model with LogisticRegression(max_iter=2000), and the baseline. Print the baseline's mean 5-fold score, then the model's 5 scores and their mean, all rounded to 3 digits.
Assignment
Follow the Coding Tutorial to score a baseline and a logistic regression pipeline with 5-fold cross-validation on the training rows.
Hint
Each pair inside make_column_transformer( is a tuple followed by a comma: (way, [columns]),. Give cross_val_score only X_train and y_train.
Free plan: 30 tutor messages. PRO is unlimited.
