Skip to content

Commit 57a6704

Browse files
DOC improve linear model coefficient interpretation example (scikit-learn#31760)
Co-authored-by: Stefanie Senger <91849487+StefanieSenger@users.noreply.github.com>
1 parent 919527e commit 57a6704

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

examples/inspection/plot_linear_model_coefficient_interpretation.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
survey = fetch_openml(data_id=534, as_frame=True)
5757

5858
# %%
59-
# Then, we identify features `X` and targets `y`: the column WAGE is our
60-
# target variable (i.e., the variable which we want to predict).
59+
# Then, we identify features `X` and target `y`: the column WAGE is our
60+
# target variable (i.e. the variable which we want to predict).
6161

6262
X = survey.data[survey.feature_names]
6363
X.describe(include="all")
@@ -89,7 +89,7 @@
8989
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
9090

9191
# %%
92-
# First, let's get some insights by looking at the variable distributions and
92+
# First, let's get some insights by looking at the variables' distributions and
9393
# at the pairwise relationships between them. Only numerical
9494
# variables will be used. In the following plot, each dot represents a sample.
9595
#
@@ -107,7 +107,7 @@
107107
#
108108
# The WAGE is increasing when EDUCATION is increasing.
109109
# Note that the dependence between WAGE and EDUCATION
110-
# represented here is a marginal dependence, i.e., it describes the behavior
110+
# represented here is a marginal dependence, i.e. it describes the behavior
111111
# of a specific variable without keeping the others fixed.
112112
#
113113
# Also, the EXPERIENCE and AGE are strongly linearly correlated.
@@ -128,7 +128,7 @@
128128
# In particular categorical variables cannot be included in linear model if not
129129
# coded as integers first. In addition, to avoid categorical features to be
130130
# treated as ordered values, we need to one-hot-encode them.
131-
# Our pre-processor will
131+
# Our pre-processor will:
132132
#
133133
# - one-hot encode (i.e., generate a column by category) the categorical
134134
# columns, only for non-binary categorical variables;
@@ -148,8 +148,8 @@
148148
)
149149

150150
# %%
151-
# To describe the dataset as a linear model we use a ridge regressor
152-
# with a very small regularization and to model the logarithm of the WAGE.
151+
# We use a ridge regressor
152+
# with a very small regularization to model the logarithm of the WAGE.
153153

154154
from sklearn.compose import TransformedTargetRegressor
155155
from sklearn.linear_model import Ridge
@@ -171,9 +171,9 @@
171171
model.fit(X_train, y_train)
172172

173173
# %%
174-
# Then we check the performance of the computed model plotting its predictions
175-
# on the test set and computing,
176-
# for example, the median absolute error of the model.
174+
# Then we check the performance of the computed model by plotting its predictions
175+
# against the actual values on the test set, and by computing
176+
# the median absolute error.
177177

178178
from sklearn.metrics import PredictionErrorDisplay, median_absolute_error
179179

@@ -289,11 +289,12 @@
289289
# %%
290290
# Now that the coefficients have been scaled, we can safely compare them.
291291
#
292-
# .. warning::
292+
# .. note::
293293
#
294294
# Why does the plot above suggest that an increase in age leads to a
295-
# decrease in wage? Why the :ref:`initial pairplot
296-
# <marginal_dependencies>` is telling the opposite?
295+
# decrease in wage? Why is the :ref:`initial pairplot
296+
# <marginal_dependencies>` telling the opposite?
297+
# This difference is the difference between marginal and conditional dependence.
297298
#
298299
# The plot above tells us about dependencies between a specific feature and
299300
# the target when all other features remain constant, i.e., **conditional
@@ -399,7 +400,7 @@
399400
# Two regions are populated: when the EXPERIENCE coefficient is
400401
# positive the AGE one is negative and vice-versa.
401402
#
402-
# To go further we remove one of the 2 features and check what is the impact
403+
# To go further we remove one of the two features, AGE, and check what is the impact
403404
# on the model stability.
404405

405406
column_to_drop = ["AGE"]
@@ -469,8 +470,7 @@
469470

470471
# %%
471472
# Again, we check the performance of the computed
472-
# model using, for example, the median absolute error of the model and the R
473-
# squared coefficient.
473+
# model using the median absolute error.
474474

475475
mae_train = median_absolute_error(y_train, model.predict(X_train))
476476
y_pred = model.predict(X_test)
@@ -506,10 +506,7 @@
506506
plt.subplots_adjust(left=0.3)
507507

508508
# %%
509-
# We now inspect the coefficients across several cross-validation folds. As in
510-
# the above example, we do not need to scale the coefficients by the std. dev.
511-
# of the feature values since this scaling was already
512-
# done in the preprocessing step of the pipeline.
509+
# We now inspect the coefficients across several cross-validation folds.
513510

514511
cv_model = cross_validate(
515512
model,
@@ -768,9 +765,6 @@
768765
# * Coefficients must be scaled to the same unit of measure to retrieve
769766
# feature importance. Scaling them with the standard-deviation of the
770767
# feature is a useful proxy.
771-
# * Interpreting causality is difficult when there are confounding effects. If
772-
# the relationship between two variables is also affected by something
773-
# unobserved, we should be careful when making conclusions about causality.
774768
# * Coefficients in multivariate linear models represent the dependency
775769
# between a given feature and the target, **conditional** on the other
776770
# features.
@@ -780,7 +774,6 @@
780774
# coefficients could significantly vary from one another.
781775
# * Inspecting coefficients across the folds of a cross-validation loop
782776
# gives an idea of their stability.
783-
# * Coefficients are unlikely to have any causal meaning. They tend
784-
# to be biased by unobserved confounders.
785-
# * Inspection tools may not necessarily provide insights on the true
786-
# data generating process.
777+
# * Interpreting causality is difficult when there are confounding effects. If
778+
# the relationship between two variables is also affected by something
779+
# unobserved, we should be careful when making conclusions about causality.

0 commit comments

Comments
 (0)