1 Upvote

Sklearn Stratified Train Test Split

Python
Data Preparation for Models

Ensures that the test and train splits have the same ratio of class ratio for training classification models. We use the stratify parameter and pass the y series.

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, 
                                                    y, 
                                                    test_size=0.4, 
                                                    random_state=101,
                                                    stratify=y)

By detro - Last Updated March 17, 2022, 7:24 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Scale Data Using Standard Scaler in Sklearn
Python
Data Preparation for Models

Sklearn

3
Create Dummy Variables with Pandas
Python
Data Preparation for Models

Pandas

1
Top Contributors
103
100