Train-Test Splitting with Stratification using Scikit-Learn

Python

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.

 1|  from sklearn.model_selection import train_test_split
 2|  X_train, X_test, y_train, y_test = train_test_split(X, 
 3|                                                      y, 
 4|                                                      test_size=0.4, 
 5|                                                      random_state=101,
 6|                                                      stratify=y)
Did you find this snippet useful?

Sign up for free to to add this to your code library