Machine Learning Fundamentals with scikit-learn

Lesson 2 of 8

Data and Train/Test Splits

The previous lesson split data into training and test sets without explaining why, this lesson makes that explicit: it's arguably the single most important habit in applied machine learning.

Why you can't evaluate on training data

If you show a model its own training data and ask "how'd you do?", a sufficiently flexible model can simply memorize every example, scoring perfectly, while having learned nothing that generalizes to new data. The only honest way to measure "did this model actually learn something useful?" is to test it on examples it never saw during training.

Looking at the data with pandas

Pythonshares state with other Python blocks

load_iris(as_frame=True) returns the dataset as a pandas DataFrame, a table with named columns, instead of plain numpy arrays, much easier to read and explore. df.head() prints the first 5 rows, df.shape gives (rows, columns), both standard first steps when looking at any new dataset.

Splitting properly

Pythonshares state with other Python blocks
  • test_size=0.2 holds back 20% of the data for testing, 80% for training, a common default, though the right split depends on how much data you have overall.
  • random_state=42 seeds the random shuffle used to pick which rows go where. Without it, you'd get a different split every run, making results hard to compare or reproduce, setting a fixed seed is standard practice any time you want a repeatable experiment.

WARNING

Never look at your test set while choosing features, tuning models, or making any decision that shapes your final model, if test-set performance influences your choices, it stops being an honest measure of generalization. It should be touched exactly once, at the very end, to report a final number.

📝 Data and Train/Test Splits Quiz

Passing score: 70%
  1. 1.Why is it misleading to evaluate a model on the same data it was trained on?

  2. 2.Setting random_state to a fixed number makes a train/test split reproducible across runs.

  3. 3.test_size=0.____ holds back 20% of the data for the test set.