|
56 | 56 | survey = fetch_openml(data_id=534, as_frame=True) |
57 | 57 |
|
58 | 58 | # %% |
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). |
61 | 61 |
|
62 | 62 | X = survey.data[survey.feature_names] |
63 | 63 | X.describe(include="all") |
|
89 | 89 | X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) |
90 | 90 |
|
91 | 91 | # %% |
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 |
93 | 93 | # at the pairwise relationships between them. Only numerical |
94 | 94 | # variables will be used. In the following plot, each dot represents a sample. |
95 | 95 | # |
|
107 | 107 | # |
108 | 108 | # The WAGE is increasing when EDUCATION is increasing. |
109 | 109 | # 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 |
111 | 111 | # of a specific variable without keeping the others fixed. |
112 | 112 | # |
113 | 113 | # Also, the EXPERIENCE and AGE are strongly linearly correlated. |
|
128 | 128 | # In particular categorical variables cannot be included in linear model if not |
129 | 129 | # coded as integers first. In addition, to avoid categorical features to be |
130 | 130 | # treated as ordered values, we need to one-hot-encode them. |
131 | | -# Our pre-processor will |
| 131 | +# Our pre-processor will: |
132 | 132 | # |
133 | 133 | # - one-hot encode (i.e., generate a column by category) the categorical |
134 | 134 | # columns, only for non-binary categorical variables; |
|
148 | 148 | ) |
149 | 149 |
|
150 | 150 | # %% |
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. |
153 | 153 |
|
154 | 154 | from sklearn.compose import TransformedTargetRegressor |
155 | 155 | from sklearn.linear_model import Ridge |
|
171 | 171 | model.fit(X_train, y_train) |
172 | 172 |
|
173 | 173 | # %% |
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. |
177 | 177 |
|
178 | 178 | from sklearn.metrics import PredictionErrorDisplay, median_absolute_error |
179 | 179 |
|
|
289 | 289 | # %% |
290 | 290 | # Now that the coefficients have been scaled, we can safely compare them. |
291 | 291 | # |
292 | | -# .. warning:: |
| 292 | +# .. note:: |
293 | 293 | # |
294 | 294 | # 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. |
297 | 298 | # |
298 | 299 | # The plot above tells us about dependencies between a specific feature and |
299 | 300 | # the target when all other features remain constant, i.e., **conditional |
|
399 | 400 | # Two regions are populated: when the EXPERIENCE coefficient is |
400 | 401 | # positive the AGE one is negative and vice-versa. |
401 | 402 | # |
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 |
403 | 404 | # on the model stability. |
404 | 405 |
|
405 | 406 | column_to_drop = ["AGE"] |
|
469 | 470 |
|
470 | 471 | # %% |
471 | 472 | # 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. |
474 | 474 |
|
475 | 475 | mae_train = median_absolute_error(y_train, model.predict(X_train)) |
476 | 476 | y_pred = model.predict(X_test) |
|
506 | 506 | plt.subplots_adjust(left=0.3) |
507 | 507 |
|
508 | 508 | # %% |
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. |
513 | 510 |
|
514 | 511 | cv_model = cross_validate( |
515 | 512 | model, |
|
768 | 765 | # * Coefficients must be scaled to the same unit of measure to retrieve |
769 | 766 | # feature importance. Scaling them with the standard-deviation of the |
770 | 767 | # 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. |
774 | 768 | # * Coefficients in multivariate linear models represent the dependency |
775 | 769 | # between a given feature and the target, **conditional** on the other |
776 | 770 | # features. |
|
780 | 774 | # coefficients could significantly vary from one another. |
781 | 775 | # * Inspecting coefficients across the folds of a cross-validation loop |
782 | 776 | # 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