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:
- choose feature and target columns;
- split into train and test;
- fit the model on training rows;
- predict on test rows;
- evaluate predictions.
Check and explain
Complete:
In scikit-learn,
fitlearns from ______ andpredictproduces outputs for ______.
Takeaway
The core scikit-learn rhythm is choose, split, fit, predict, evaluate.
