Machine Learning
Beginner
4.5
Splitting Data the Right Way
Avoid the most common cause of misleading ML results: leakage.
0h 20m
1 lesson
1.2K students
What You'll Learn
Learning objectives will be added soon.
Tutorial Content
The rule
Evaluate on data the model has never seen. Split first, then fit any preprocessing on the training set only.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)Beware leakage
If you scale or impute using statistics from the whole dataset before splitting, information from the test set leaks into training and inflates your score. Use a Pipeline so transforms are fit inside cross-validation:
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = make_pipeline(StandardScaler(), LogisticRegression())Your Progress
Sign in to track your progress
Tags
Machine Learning
Python
scikit-learn