Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 111 ยท Getting Started

The scikit-learn API

Understand scikit-learn's estimator API: fit, predict, transform and score, hyperparameters versus learned attributes, get_params, set_params and clone.

scikit-learn is the standard Python library for classical machine learning, and its biggest strength is consistency. Whether you use a linear model, a forest or a clustering algorithm, you create an object, call fit, and then call predict or transform. Learn this small vocabulary once and you can use more than a hundred algorithms.

This lesson explains that shared interface, the difference between settings you choose and values the model learns, and the helper methods that let you inspect, change and copy models safely. Everything later in this tutorial builds on it.

Installing and importing scikit-learn

On your own computer you install the library with pip (it needs NumPy and SciPy, which pip installs automatically; conda users can run conda install scikit-learn). Use a virtual environment so that the versions of your projects do not collide:

Terminal
# read-only: run these in a terminal on your own machine
python -m venv .venv
source .venv/bin/activate        # on Windows: .venv\Scripts\activate
pip install scikit-learn pandas matplotlib
python -c "import sklearn; print(sklearn.__version__)"

In code, the package is imported as sklearn (not scikit-learn), and you import classes from its submodules rather than the whole package: from sklearn.linear_model import LogisticRegression. The in-browser examples in this course already have everything installed.

Why do so many people use it? It is integrated with the scientific Python stack (it takes NumPy arrays, pandas DataFrames and SciPy sparse matrices), it is user-friendly (the same fit / predict interface everywhere), it saves time (preprocessing, models, metrics, cross-validation and pipelines are all included), and it is flexible and scalable enough to write your own transformers and to swap one model for another with one line.

The five-step model-building recipe

Every scikit-learn project follows the same short recipe, which the next lessons expand: load the data, split it, fit a model, predict, evaluate. Here it is for a classifier, with the target labels as strings. Instead of encoding them yourself, you can usually pass strings straight to fit; LabelEncoder (categorical encoding) is only needed by tools that insist on integers.

Python Load, split, fit, predict, evaluate

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.

Swap RandomForestClassifier for any other classifier (LogisticRegression, KNeighborsClassifier, SVC) and nothing else in the recipe changes. That single fact is the main reason to learn the API well.

Estimators, predictors and transformers

Every learning object in scikit-learn is an estimator: something that learns from data through a fit method. Two special kinds exist.

  • A predictor (classifier or regressor) also has predict(X), and usually score(X, y). It turns features into predictions.
  • A transformer has transform(X). It changes the features, for example by scaling them. fit_transform(X) does both steps at once.

One small interface

fit Learn from the data.
predict Turn features into answers.
transform Change the features.
score A default quality number.

Every estimator speaks this same language.

1 / 2

Here is a transformer and a predictor side by side.

Python fit, transform, predict and score

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.

The scaler learned its means during fit, then transform applied them. The classifier learned coefficients and used them in predict. score returns accuracy for classifiers and R-squared for regressors, so it is a quick check and not a full evaluation.

Hyperparameters versus learned attributes

Two very different kinds of numbers live on an estimator.

Hyperparameters are settings you choose before training, passed to the constructor: the number of neighbors in k-nearest neighbors, the depth limit of a tree, the regularization strength. The algorithm cannot learn them from the data it is fitting, so you pick them (or search for them, as in hyperparameter tuning).

Learned attributes are results of training. By convention they end with an underscore: coef_, intercept_, classes_, feature_importances_. They do not exist until you call fit, and asking for them earlier raises an error.

Python Attributes appear only after fit

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 the extra attribute n_features_in_. scikit-learn remembers how many columns it saw, and complains if you later predict with a different number. That protects you from silently mixing up the shapes of your data.

Calling fit a second time on the same object discards the earlier learning and starts again. It does not add to what the model knew. Only a few estimators offer incremental learning through partial_fit.

Inspecting and changing settings

Every estimator can describe its hyperparameters with get_params() and change them with set_params(...). This uniform access is what lets tools such as grid search tune any model without special code.

sklearn.base.clone(estimator) builds a fresh, unfitted copy with the same hyperparameters. Cloning is how cross-validation trains a new model on every fold without contaminating one with another.

Python get_params, set_params and 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.

The printed object shows just the settings that differ from the defaults, which makes experiments easy to read. Settings with a trailing double underscore, such as step__param, address estimators nested inside a pipeline; you will meet that syntax later.

The same API everywhere

Because every classifier shares the same methods, comparing algorithms is a loop. This is one of the most practical consequences of the design. Models differ in how they learn, but the way you use them is identical.

Python Swap models without changing the code

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.

The loop never mentions what kind of model it holds. A pipeline, which chains a scaler and a classifier, even behaves like a single estimator with its own fit and score. That composability is the reason scikit-learn code stays short.

Input conventions

Two shape rules apply everywhere.

  • X is two-dimensional: (n_samples, n_features). A single column must be (n, 1), not (n,). Use X.reshape(-1, 1) if needed.
  • y is one-dimensional for ordinary problems: (n_samples,).

Passing the wrong shape is the most common beginner error, and the error message normally tells you exactly how to fix it. The next lessons on datasets show these shapes in practice.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which attribute name is a learned attribute that exists only after fit?

    By convention learned attributes end with an underscore, so coef_ appears only after fit. The others are hyperparameters set in the constructor.
  2. 2. What does sklearn.base.clone(model) return?

    clone creates a new, unfitted estimator that carries the constructor settings of the original.
  3. 3. What happens if you call fit twice on the same estimator with different data?

    fit starts from scratch each time. Incremental learning needs partial_fit on estimators that support it.
  4. 4. A single feature column must be given to fit with which shape?

    X must be two-dimensional with samples as rows. Reshape a 1D array with reshape(-1, 1).
  5. 5. What does a transformer provide that a plain predictor usually does not?

    Transformers change the features through transform. Predictors produce predictions through predict.

Summary

  • Every scikit-learn estimator learns with fit; predictors add predict and score, transformers add transform.
  • Hyperparameters are chosen by you in the constructor; learned attributes end with an underscore and exist only after fit.
  • get_params and set_params inspect and change settings; clone makes a fresh unfitted copy.
  • A shared interface lets you swap models in a loop, and pipelines act like single estimators.
  • X is (n_samples, n_features) and y is (n_samples,).

Next: Working with Datasets