The estimator API: fit, predict, transform

scikit-learn · Essentials · 10 min

Once you know the one API, you can use any of the library's hundreds of models and transformers.

scikit-learn is built around one small interface. An estimator is any object with a fit method. You configure it with keyword arguments in the constructor (the hyperparameters), then learn from data with fit(X, y).

Estimators come in a few flavours that add one more method:

  • Predictors (classifiers, regressors) add predict(X), and usually score(X, y). Classifiers often add predict_proba(X).
  • Transformers (scalers, encoders, PCA) add transform(X), and fit_transform(X) as a shortcut for fitting then transforming the same data.
  • Clusterers add fit_predict or a labels_ attribute.

Two conventions make the API predictable. Constructor arguments are stored unchanged, so model.C is what you passed. Everything learned from data is stored in attributes ending in an underscore, such as coef_, classes_ or mean_, and only exists after fit. Calling predict before fit raises NotFittedError.

fit returns the estimator itself, so model = LogisticRegression().fit(X, y) is a common one-liner. Calling fit again throws away what was learned and starts over; it does not continue training.

Because the interface is the same everywhere, swapping a logistic regression for a random forest is usually a one-line change, and generic tools such as Pipeline, cross_val_score and GridSearchCV work with any estimator.

Example

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler

X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([3.0, 5.0, 7.0, 9.0])

model = LinearRegression()          # hyperparameters go in the constructor
model.fit(X, y)                     # learns from data, returns self
print(model.coef_, model.intercept_)  # [2.] 1.0 (learned attributes end in _)
print(model.predict([[10.0]]))      # [21.]

scaler = StandardScaler()
print(scaler.fit_transform(X).ravel())  # [-1.34164079 -0.4472136   0.4472136   1.34164079]
print(scaler.mean_)                     # [2.5]

Remember

  • Configure in the constructor, learn with fit, then predict or transform
  • Learned state lives in attributes with a trailing underscore
  • Calling fit again retrains from scratch

Watch out for

  • Passing training data to the constructor instead of to fit.

Check yourself

  1. What does the trailing underscore in `model.coef_` tell you?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: scikit-learn.org/stable/developers/develop.html