Skip to content

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:

PythonPython not started

fit, predict, score

PythonPython not started

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:

PythonPython not started

Probabilities

predict_proba gives the probability of each class, in the order of model.classes_:

PythonPython not started

One interface, many models

The point of the API is that the surrounding code never changes:

PythonPython not started

Regression

The same three methods, with a continuous target:

PythonPython not started

Transformers

A transformer learns something from data and then changes data, with fit and transform instead of fit and predict:

PythonPython not started

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.

PythonPython not started

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

Exercise 1

Fit a LogisticRegression on the training data, put its held-out accuracy in accuracy, and print it to four decimal places.

Your turnPython not started
Exercise 2

Fit every candidate and build scores: a dict of name → held-out accuracy, rounded to four places.

Your turnPython not started

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

  • X is (n_samples, n_features) and y is (n_samples,) — a single feature still needs reshape(-1, 1).
  • fit learns, predict applies, score does both. Learned attributes end in an underscore.
  • predict_proba gives per-class probabilities in classes_ order, and predict is their argmax — but an uncalibrated probability is not a confidence.
  • Every estimator shares the interface, so swapping models changes one line.
  • Always fit a DummyClassifier first; 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.