In the context of the train_test_split function in machine learning, the stratify parameter is used to ensure that the splitting process preserves the proportion of classes in the target variable. When you set stratify=y, where y is your target variable, the data is split in a way that maintains the distribution of classes in both the training and testing sets.
For example, if you have a classification problem with two classes, where Class A constitutes 70% of the data and Class B constitutes 30%, using stratify=y will ensure that both the training and testing sets have the same class distribution.
This is particularly useful when dealing with imbalanced datasets, where one class may be significantly more prevalent than others. Ensuring that the class distribution is maintained in both the training and testing sets can help prevent issues such as overfitting or biased model performance evaluation.
GO ONE LEVEL DEEPER
Why stratification matters
Random splitting can accidentally change class proportions, especially in smaller or imbalanced datasets. Stratification keeps the target mix approximately consistent.
Original data
Example mix: 70% class A and 30% class B.
Training set
Stratify preserves approximately the same 70/30 balance.
Test set
The held-out set also retains approximately the same balance.
Keep in mind
- Stratify classification targets, not continuous regression targets.
- For cross-validation, use a stratified splitter as well.
- Grouped or time-dependent data requires a different splitting strategy.