Skip to course content
Free course

Machine Learning Foundations / Module 3

Module 3 lesson

The scikit-learn Fit, Predict, and Score Pattern

Unit ID: ML-M03-U06 Estimated active time: 20-30 minutes

Why this matters

Machine learning code becomes less scary when the pattern is clear.

Most first scikit-learn examples follow the same rhythm:

choose features -> split -> fit -> predict -> evaluate

Once you see the rhythm, you can read many examples more calmly.

The idea

The core calls are:

model.fit(X_train, y_train)
predictions = model.predict(X_test)

fit means the model learns from the training rows.

predict means the model uses what it learned to produce outputs for new rows.

Evaluation compares those predictions with y_test.

Predict

Which line should happen first?

predictions = model.predict(X_test)
model.fit(X_train, y_train)

fit must happen first. A model cannot predict from a pattern it has not learned.

Run or inspect

The same pattern appears in regression:

linear_model.fit(X_train, y_train)
quiz_predictions = linear_model.predict(X_test)

And in classification:

classifier.fit(X_train, y_train)
completion_predictions = classifier.predict(X_test)

The difference is the target and the metric.

Change one thing

What changes if X_train contains the target column?

The model may appear much better than it really is. That is leakage. Module 5 studies this deeply, but Module 3 already checks that the target is not inside X.

Practice

Put these steps in order:

  • evaluate predictions;
  • choose feature and target columns;
  • fit the model on training rows;
  • split into train and test;
  • predict on test rows.

Correct order:

  1. choose feature and target columns;
  2. split into train and test;
  3. fit the model on training rows;
  4. predict on test rows;
  5. evaluate predictions.

Check and explain

Complete:

In scikit-learn, fit learns from ______ and predict produces outputs for ______.

Takeaway

The core scikit-learn rhythm is choose, split, fit, predict, evaluate.