Machine Learning Foundations·Lesson 35 of 67·advanced
The estimator API and your first model
fit, predict, score — the three methods every scikit-learn model shares, and why a linear classifier over embeddings is the baseline you have to beat.
In this lesson · 18 min
- →Shape data as X and y the way scikit-learn expects
- →Fit a model, predict with it and score it
- →Read predicted probabilities, and why they are not confidence
- →Swap estimators without changing the surrounding code
- →Explain what a transformer is and how it differs from an estimator
scikit-learn has one interface, and every model in it follows the same three steps. Learn that interface once and you can use the entire library — and, more usefully, you can swap models in and out of a pipeline without rewriting anything around them.
X and y
Features go in a 2-D array of shape (n_samples, n_features). Targets go in a 1-D array of
length n_samples. That is the whole data contract:
fit, predict, score
fit learns, predict applies, score is a convenience that predicts and compares in one call.
Anything the model learned is stored on it with a trailing underscore:
Probabilities
predict_proba gives the probability of each class, in the order of model.classes_:
One interface, many models
The point of the API is that the surrounding code never changes:
Regression
The same three methods, with a continuous target:
Transformers
A transformer learns something from data and then changes data, with fit and transform
instead of fit and predict:
Scaling matters more than it looks: the first column runs to 4,300 and the second to 1, so anything distance-based treats the second column as irrelevant.
Note that the scaler was fitted on the training set and then applied to the test set. Fitting it
on everything would leak the test set’s statistics into training — the next lesson is about
exactly that, and the lesson after it about the Pipeline that makes the mistake impossible.
Try it yourself
Fit a LogisticRegression on the training data, put its held-out accuracy in accuracy, and print it to four decimal places.
Fit every candidate and build scores: a dict of name → held-out accuracy, rounded to four places.
Check yourself
Your abuse classifier reports 95% accuracy on a dataset where 95% of requests are benign. What should you check?
Check yourself
What is the difference between an estimator and a transformer in scikit-learn?
Recap
Xis(n_samples, n_features)andyis(n_samples,)— a single feature still needsreshape(-1, 1).fitlearns,predictapplies,scoredoes both. Learned attributes end in an underscore.predict_probagives per-class probabilities inclasses_order, andpredictis their argmax — but an uncalibrated probability is not a confidence.- Every estimator shares the interface, so swapping models changes one line.
- Always fit a
DummyClassifierfirst; on an imbalanced problem it tells you what accuracy means. - Transformers use
fit/transform, learn on the training data only, and are applied to both splits.