01
How do you set up a basic train-test split with scikit-learn?
Show answer
Use train_test_split with a fixed random_state for reproducibility and stratify for classification so class ratios match.
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, random_state=42, stratify=y
)
For time series, avoid random splits and use the most recent period as the test set.