1.Stratified split
BeginnerLoad the wine dataset with load_wine(return_X_y=True). Split it into 75% training and 25% test data with random_state=0, stratified by class. Print the number of rows in each part and the class counts in the test set.
Show hint
train_test_split(X, y, test_size=0.25, random_state=0, stratify=y), then np.bincount(y_test).
Show solution
import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
print(len(X_train), len(X_test))
print(np.bincount(y_test))133 45
[15 18 12]