Python exercise · about 5 minutes · free
Train/Test Split
Grading a model on the data it studied is like giving students the exam answers. Hold some data back.
train_test_split(X, y, test_size=0.2, random_state=42) shuffles and keeps 20% aside for testing. random_state makes the split repeatable. (For time-ordered data, split by date instead.)
First run downloads scikit-learn (~30 MB), once.
Your quest: Split
X, y 80/20 with random_state=42 into X_train, X_test, y_train, y_test.Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)Output appears here.