Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions python_scripts/linear_models_sol_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# %% [markdown]
# # 📃 Solution for Exercise M4.03
#
# Now, we tackle a more realistic classification problem instead of making a
# synthetic dataset. We start by loading the Adult Census dataset with the
# Now, we tackle a (relatively) realistic classification problem instead of making
# a synthetic dataset. We start by loading the Adult Census dataset with the
# following snippet. For the moment we retain only the **numerical features**.

# %%
Expand All @@ -24,10 +24,12 @@
# %% [markdown]
# We confirm that all the selected features are numerical.
#
# Compute the generalization performance in terms of accuracy of a linear model
# composed of a `StandardScaler` and a `LogisticRegression`. Use a 10-fold
# cross-validation with `return_estimator=True` to be able to inspect the
# trained estimators.
# Define a linear model composed of a `StandardScaler` followed by a
# `LogisticRegression` with default parameters.
#
# Then use a 10-fold cross-validation to estimate its generalization performance
# in terms of accuracy. Also set `return_estimator=True` to be able to inspect
# the trained estimators.

# %%
# solution
Expand Down Expand Up @@ -84,11 +86,11 @@
# - The numerical data must be scaled.
# - The categorical data must be one-hot encoded, set `min_frequency=0.01` to
# group categories concerning less than 1% of the total samples.
# - The predictor is a `LogisticRegression`. You may need to increase the number
# of `max_iter`, which is 100 by default.
# - The predictor is a `LogisticRegression` with default parameters, except that
# you may need to increase the number of `max_iter`, which is 100 by default.
#
# Use the same 10-fold cross-validation strategy with `return_estimator=True` as
# above to evaluate this complex pipeline.
# above to evaluate this scaled and encoded pipeline.
Comment thread
ArturoAmorQ marked this conversation as resolved.
Outdated

# %%
# solution
Expand Down Expand Up @@ -195,23 +197,29 @@

# %% [markdown]
# Now create a similar pipeline consisting of the same preprocessor as above,
# followed by a `PolynomialFeatures` and a logistic regression with `C=0.01`.
# Set `degree=2` and `interaction_only=True` to the feature engineering step.
# Remember not to include a "bias" feature to avoid introducing a redundancy
# with the intercept of the subsequent logistic regression.
# followed by a `PolynomialFeatures` and a logistic regression with `C=0.01` and
# enough `max_iter`. Set `degree=2` and `interaction_only=True` to the feature
# engineering step. Remember not to include a "bias" feature to avoid
# introducing a redundancy with the intercept of the subsequent logistic
# regression.

# %%
# solution
from sklearn.preprocessing import PolynomialFeatures

model_with_interaction = make_pipeline(
model_with_interactions = make_pipeline(
preprocessor,
PolynomialFeatures(degree=2, include_bias=False, interaction_only=True),
LogisticRegression(C=0.01, max_iter=5_000),
)
model_with_interaction
model_with_interactions

# %% [markdown]
# Use the same 10-fold cross-validation strategy as above to evaluate this
# pipeline with interactions. In this case there is no need to return the
# estimator, as the number of features is much larger to visually explore the
# learned coefficients.
Comment thread
ArturoAmorQ marked this conversation as resolved.
Outdated
#
# By comparing the cross-validation test scores of both models fold-to-fold,
# count the number of times the model using multiplicative interactions and both
# numerical and categorical features has a better test score than the model
Expand All @@ -220,11 +228,10 @@
# %%
# solution
cv_results_interactions = cross_validate(
model_with_interaction,
model_with_interactions,
data,
target,
cv=10,
return_estimator=True,
n_jobs=2,
)
test_score_interactions = cv_results_interactions["test_score"]
Expand Down