Topics / 🤖 AI & Machine Learning / 🤖 Level 21: First ML Models

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.

100 rows, shuffled: train 80%, test 20%. the model never sees the test rows while learning100 rows, shuffledtrain 80%test 20%the model never sees the test rows while learning
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.
HintX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 21: First ML Models

Share on LinkedIn