Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 40 ยท Getting Started

The Estimator API

Understand the scikit-learn estimator, predictor and transformer interfaces, hyperparameters vs learned attributes, get_params, set_params, clone and repr.

Every object in scikit-learn that learns from data is an estimator. Understanding the three roles an estimator can play, and the conventions around its settings, makes the whole library predictable. This is the most important lesson in the chapter.

Three kinds of object

Three roles, one pattern

Estimator Learns with fit(X, y).
Predictor Adds predict(X) and score.
Transformer Adds transform(X).

Some objects, like a pipeline, play more than one role.

1 / 2
  • Estimator: anything with fit(X, y). After fitting it stores what it learned.
  • Predictor: an estimator that also has predict(X) (and often predict_proba, score). Classifiers, regressors and clusterers are predictors.
  • Transformer: an estimator with transform(X) that returns a changed version of X, such as scaled or encoded features. fit_transform(X) does both steps at once.

A transformer in action

A StandardScaler learns the mean and standard deviation of each column during fit, then transform uses them.

Python fit then transform

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the new data is transformed with the numbers learned from the training data. You never call fit on test data; that would leak information. See Train, Validation and Test Sets.

Hyperparameters versus learned attributes

Two kinds of values live on an estimator, and scikit-learn keeps them apart by naming:

  • Hyperparameters are settings you choose before training, passed to the constructor: Ridge(alpha=2.0).
  • Learned attributes are results of training, and they end with an underscore: coef_, intercept_, classes_, feature_importances_.
Python Settings in, results out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Trailing underscores make it easy to know what exists only after fit. Using them before fitting raises a NotFittedError.

Reading and changing settings

get_params() lists every hyperparameter, and set_params() changes them. Tools like grid search rely on this.

Python get_params, set_params, clone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

clone builds a fresh, unfitted copy with identical settings. Cross-validation uses it to train a new model on every fold.

The NotFittedError

Calling predict before fit fails with a clear message, which protects you from silent mistakes.

Python Using an unfitted model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fitting twice

Calling fit again replaces what was learned; it does not add to it. If you want a model to learn from additional batches, look for partial_fit on algorithms that support it (such as SGDClassifier).

Conventions worth memorising

  • fit returns self, so you can chain: Model().fit(X, y).predict(X_new).
  • Hyperparameters go in the constructor; data goes in fit.
  • Always set random_state when an algorithm uses randomness, so results repeat.
  • Use n_jobs=-1 on algorithms that support it to use all CPU cores (it has little effect in the browser, where there is one thread).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which attribute is a learned result of fit?

    Learned attributes end with an underscore. The others are hyperparameters you set.
  2. 2. What should you call on the test set when using a fitted scaler?

    Fit only on training data; use transform on test data with the values learned from training.
  3. 3. What does clone(model) return?

    clone copies the settings but not the learned state.
  4. 4. What happens if you call fit a second time on new data?

    fit starts over. Use partial_fit, where available, for incremental learning.

Summary

  • Estimators learn with fit; predictors add predict; transformers add transform.
  • Hyperparameters go in the constructor; learned values end with _.
  • get_params, set_params and clone let tools tune and reuse models.
  • Fit transformers on training data only.
  • fit returns self, enabling chaining.

Next: Train, Validation and Test Sets