Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Improve wording in stratification notebook #760

Merged
merged 7 commits into from
May 17, 2024
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
54 changes: 32 additions & 22 deletions python_scripts/cross_validation_stratification.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
model = make_pipeline(StandardScaler(), LogisticRegression())

# %% [markdown]
# Once we created our model, we will use the cross-validation framework to
# evaluate it. We will use the `KFold` cross-validation strategy. We will define
# a dataset with nine samples and repeat the cross-validation three times (i.e.
# `n_splits`).
# Once the model is created, we can evaluate it using cross-validation. We start
# by using the `KFold` strategy.
#
# We can quickly remind ourselves how this strategy works. For such purpose we
# define a dataset with nine samples and repeat the cross-validation three times
# (i.e. `n_splits=3`).
ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved

# %%
import numpy as np
Expand All @@ -51,9 +53,9 @@
print("TRAIN:", train_index, "TEST:", test_index)

# %% [markdown]
# By defining three splits, we will use three samples for testing and six for
# training each time. `KFold` does not shuffle by default. It means that it will
# select the three first samples for the testing set at the first split, then
# By defining three splits, we use three samples for testing and six for
ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved
# training each time. `KFold` does not shuffle by default. It means that the
# three first samples are selected for the testing set at the first split, then
# the three next three samples for the second split, and the three next for the
# last split. In the end, all samples have been used in testing at least once
# among the different splits.
Expand All @@ -73,8 +75,8 @@

# %% [markdown]
# It is a real surprise that our model cannot correctly classify any sample in
# any cross-validation split. We will now check our target's value to understand
# the issue.
# any cross-validation split. We now check our target's value to understand the
# issue.

# %%
import matplotlib.pyplot as plt
Expand All @@ -86,18 +88,17 @@
_ = plt.title("Class value in target y")

# %% [markdown]
# We see that the target vector `target` is ordered. It will have some
# unexpected consequences when using the `KFold` cross-validation. To illustrate
# the consequences, we will show the class count in each fold of the
# cross-validation in the train and test set.
# We see that the target vector `target` is ordered. This has some unexpected
# consequences when using the `KFold` cross-validation. To illustrate the
# consequences, we show the class count in each fold of the cross-validation in
# the train and test set.
#
# Let's compute the class counts for both the training and testing sets using
# the `KFold` cross-validation, and plot these information in a bar plot.
#
# We will iterate given the number of split and check how many samples of each
# are present in the training and testing set. We will store the information
# into two distincts lists; one for the training set and one for the testing
# set.
# We iterate given the number of split and check how many samples of each are
# present in the training and testing set. We then store the information into
# two distincts lists; one for the training set and one for the testing set.
ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved

# %%
import pandas as pd
Expand All @@ -114,8 +115,8 @@
test_cv_counts.append(target_test.value_counts())

# %% [markdown]
# To plot the information on a single figure, we will concatenate the
# information regarding the fold within the same dataset.
# To plot the information on a single figure, we concatenate the information
# regarding the fold within the same dataset.

# %%
train_cv_counts = pd.concat(
Expand Down Expand Up @@ -168,7 +169,7 @@
# 90%. Now that we solved our first issue, it would be interesting to check if
# the class frequency in the training and testing set is equal to our original
# set's class frequency. It would ensure that we are training and testing our
# model with a class distribution that we will encounter in production.
# model with a class distribution that we would encounter in production.

# %%
train_cv_counts = []
Expand Down Expand Up @@ -255,5 +256,14 @@
# train set and the test set. The difference is due to the small number of
# samples in the iris dataset.
#
# In conclusion, this is a good practice to use stratification within the
# cross-validation framework when dealing with a classification problem.
# In other words, `StratifiedKFold` maintains the original distribution of
# classes in each fold, ensuring that each fold is a good representative of the
ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved
# whole dataset. This can have a particular impact when using performance
# metrics that depend on the proportion of the positive class, as we
# will see in a future notebook.
#
# In conclusion, it is a good practice to use stratification within the
# cross-validation framework when dealing with a classification problem,
# especially for datasets with imbalanced classes or when the class distribution
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure of the conclusion. To me, I'm thinking more about the following two aspects:

  • if target labels are ordered or grouped, then stratification allows to overcome this issue if you forget to shuffle as in the k-fold case;
  • if the sample size is limited or small (and data are shuffled), then taking a stratified fold ensure similar train/test distribution compare to the uniform sampling.

And thus overcoming the both above issues make that the evaluation is closer to reality.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not entirely convinced on the rewording I made, but I think I addressed your points in 42c9775

# is crucial for model evaluation. This approach provides a more realistic
# assessment of the model's ability to generalize across the entire dataset.
Loading