Concepts / Supervised and Unsupervised Inputs in scikit-learn

Supervised and Unsupervised Inputs in scikit-learn

Learned values are computed later, during fit, from the data.

  • Programming

A Model That Has Not Seen Data Yet

Imagine you have just created a scikit-learn model object and printed it. Nothing inside the object has learned anything yet. It is closer to a configured tool sitting on a workbench: its settings have been chosen, but it has not seen a single example. Every scikit-learn estimator follows the same general life cycle. You construct it, you fit it to data, and then you use the fitted model in one of two ways. This article follows that order and shows how data moves through each step.

Three methods govern what happens next. The fit method builds the model from data. The predict method turns a fitted model into a new output, such as a classification, a regression result, or a cluster assignment. The transform method turns a fitted model into a new representation of the input data, which is useful for preprocessing, dimensionality reduction, feature extraction, and feature selection. Before looking at these methods in detail, we need to be clear about the one decision that happens before any data arrives: the choice of parameters.

Tracing a Model Through Its Lifecycle

Follow one model object from creation to use. At the start, the object exists and its settings are fixed, but it holds no learned knowledge. Calling fit with data changes the object: it now holds a model built from that data. Only at that point can you ask it for predictions or transformed data. Calling predict or transform before fit is not a valid step in this sequence, because the model has not yet been built.

Constructed modelsettings chosen, no dataseenfit(X, y)builds the model from dataFitted modelholds what it learnedpredict(X)new outputtransform(X)new representationdata arrivesmodel is builtchoose if you need an outputchoose if you need a new representation
What happens to the model object over time: it is constructed with its settings, fit changes it into a fitted model, and that fitted model can then be used by predict or by transform.

Read the diagram from top to bottom. Predict and transform are alternatives that both start from the fitted model. You pick one based on what you need, and neither one replaces fit. The diagram shows the order of the calls, not a choice you make at every step: once a model is fitted, you can use it many times, but the fitting itself happens first.

Settings Chosen at Construction

The source guidance is clear: set any parameters of a model when you construct the model object, not later. These parameters control things such as regularization, complexity control, and the number of clusters the algorithm should find. In other words, parameters are decisions made before any data is supplied. When you later call fit, the algorithm uses those decisions while it builds the model.

It helps to separate two kinds of values that a model object ends up holding. Constructor settings are fixed when the object is created. Learned values are computed later, during fit, from the data. The constructor settings describe how the algorithm should behave, while the learned values describe what the algorithm found in the data. Keeping this distinction clear will prevent a common class of errors discussed later in this article.

python
Notes on the code
  • n_clusters is a constructor parameter: it is decided when the object is created
  • fit receives the data X and uses the constructor setting while it builds the model

What fit Receives: X and y

Every estimator has a fit method, and its job is to build the model. The first argument is always the data X. In scikit-learn, X is expected to be a NumPy array or a SciPy sparse matrix in which each row represents a single data point. The entries are assumed to be continuous, meaning floating-point numbers. If your data holds integer values, convert it to floating-point before fitting.

fitmethod that builds themodelXdata matrix, one row perpoint, always requiredytarget labels or responses,supervised only
Which arguments fit receives: X is always present and holds one row per data point, while y is supplied only when the algorithm is supervised.

Supervised algorithms need one more input. The y argument is a one-dimensional NumPy array holding the target values, which are the known output labels for classification or the known responses for regression. Unsupervised methods such as clustering are described in the source as taking X without a y argument. So y is required only when the algorithm is supervised. Supplying y to an unsupervised method is a mismatch: the data it needs is just X.

AspectSupervised algorithmUnsupervised algorithm
Arguments to fitX and yX only
What y holdsKnown labels for classification or known responses for regressionNothing; y is not used
Example task named in the sourceClassification or regressionClustering
Data flow into fitData rows plus their known answersData rows alone

How the two kinds of algorithm differ at the fit call

Predicting the Output of a Fitted Model

What do you think happens?

You have fitted a preprocessing scaler on a table of 100 rows. If you call transform on that same table, what kind of result should you expect: a single class label, or a table with 100 rows?

  • A single class label

  • A table with 100 rows holding a reworked version of each row

Reveal answer
Answer:

A table with 100 rows holding a reworked version of each row

Transform creates a new representation of the input. The transformed result has the same rows as the input, with each row describing the same data point in a new form. A class label is a predict-style output, not a transform output.

The source frames the two ways of applying a learned model around the kind of result you want. If you want a prediction in the form of a new output, such as a y-like value, you use predict. If you want a new representation of the input data X, you use transform. The two methods differ in what comes out, not in how the model was fit. Both start from a fitted model, and both are used after fit has run.

Choosing Between predict and transform

The rule for choosing is to ask what you need from the model. The source's use cases split cleanly into two groups. The predict group covers outputs such as classification, regression, and clustering results, where each row receives an answer to a question, such as which class it belongs to. The transform group covers reworked inputs for preprocessing, dimensionality reduction, feature extraction, and feature selection, where each row receives a new form of its own features that a later step can use.

Output kinda prediction about each rowExample resultclassification, regression,or clusterOutput kinda reworked form of theinputExample resultpreprocessed, reduced, orselected features
Both methods start from one fitted model. predict returns a new output that answers a question about each row, while transform returns a new representation of the input rows.

Notice that the choice is not about the order of calls. Once a model is fitted, you can call either method. The decision is about the goal of the step. If a downstream step needs the data in a different form, you transform. If the goal is an answer for each row, you predict.

Preprocessing with a Scaler

Worked example

Fitting a scaler once and transforming its input

A preprocessing step should put each feature on a comparable scale before a later model uses the data. Use a standard scaler: fit it on the training data, then transform that data.

  1. 1
    Construct the scaler

    Create the scaler object first. Its settings are fixed here, before any data is seen.

  2. 2
    Fit on the training data

    Call fit with X. The scaler learns what it needs from the data, and it does not need a y argument because scaling is not a supervised task.

  3. 3
    Transform the same data

    Call transform with X. The result has the same rows as the input, with each row describing the same data point in a scaled form.

Result

A new table with the same number of rows as X, holding a new representation of each row, ready for a later step to use.

python
Notes on the code
  • X is a NumPy array of floating-point values, one row per data point
  • fit learns the scaling from X; no y is passed because this is not a supervised step
  • transform returns a new array with the same number of rows as X

Mistakes Learners Make with the Three Methods

  • Common mistake

    Setting parameters after the model object has been created

    Why it fails

    The source says parameters should be set when the model object is constructed. Parameters such as the number of clusters are decisions that the fit step uses while it builds the model.

    Do this instead

    Pass the settings to the constructor, for example KMeans(n_clusters=3), and then call fit.

  • Common mistake

    Passing y to an unsupervised method

    Why it fails

    The y argument holds target labels or responses, and the source describes unsupervised methods such as clustering as taking X without y.

    Do this instead

    Call fit with X alone for an unsupervised algorithm, and reserve y for supervised algorithms.

  • Common mistake

    Calling predict when you need transformed features

    Why it fails

    predict returns a new output, such as a class label. If the next step needs the data in a new representation, predict is the wrong method.

    Do this instead

    Use transform when the goal is a new representation of X, such as preprocessed or reduced features.

  • Common mistake

    Calling predict or transform before fit

    Why it fails

    Both methods apply a learned model. Fit must come first, because it is what builds the model.

    Do this instead

    Call fit first, then choose predict or transform depending on the output you need.

  • Common mistake

    Passing integer-valued data without converting it

    Why it fails

    The source states that the entries in X are assumed to be continuous floating-point numbers.

    Do this instead

    Convert integer-valued data to floating-point before calling fit.

Checking Your Understanding

MEDIUM

A colleague wants to group customers into 4 segments with no known labels. They write model = KMeans(n_clusters=4), then call model.fit(X, y) where y holds customer region codes. Identify the two problems in this code and explain which method you would call next if the goal is a segment assignment for each customer.

Hints
  • Ask whether clustering is supervised, and what the source says about the y argument for unsupervised methods.
  • Think about which method returns a new output for each row, and which returns a new representation of the input.

Key Takeaways

  • Parameters such as regularization, complexity control, and the number of clusters are set when the model object is constructed, before any data is supplied.
  • fit builds the model from data: X is always required as a NumPy array or SciPy sparse matrix of floating-point values, and y is supplied only for supervised algorithms.
  • After fitting, predict creates a new output such as a classification, regression, or clustering result, while transform creates a new representation of the input.
  • Choose between predict and transform by asking whether you need a new output or a new representation of the data.

Related concepts

More concepts starting with S