Let's think about it this way for a second
If you train a model on all your data and then evaluate it on that same data, the model can just 'memorize' the training data, and you have no way of knowing how well it'll actually do on unseen data (overfitting risk). Train/Test Split solves this by dividing the data by a ratio (e.g. 80/20) and showing the model only the training set — the test set is kept completely 'unseen' by the model and used only at evaluation time, like handing out an exam paper the students have never seen before.
Let's connect it to a real scenario
Writing `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)` splits the data into 80% training (`X_train`, `y_train`) and 20% test (`X_test`, `y_test`) — fixing `random_state=42` makes the split reproducible, so you get the same split every run.
Let's look at it together
from sklearn.model_selection import train_test_split
# X = features, y = target (what we want to predict)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print(f"Training samples: {len(X_train)}")
print(f"Test samples: {len(X_test)}")Training samples: 800
Test samples: 2005-minute try-it
Split the sample dataset (from Basic lesson 4) using `train_test_split()` with an 80/20 ratio — print the training/test sample counts to confirm.
A quick word of caution
Random splits aren't suitable for time-series data (stock prices, weather) — if 'future' data ends up in the training set (breaking the time order), you get data leakage; use a chronological split instead.