Supervised and Unsupervised Inputs in scikit-learn
Learned values are computed later, during fit, from the data.
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.
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.
n_clustersis 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.
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.
| Aspect | Supervised algorithm | Unsupervised algorithm |
|---|---|---|
| Arguments to fit | X and y | X only |
| What y holds | Known labels for classification or known responses for regression | Nothing; y is not used |
| Example task named in the source | Classification or regression | Clustering |
| Data flow into fit | Data rows plus their known answers | Data 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?
Reveal 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.
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
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.
- 1Construct the scaler
Create the scaler object first. Its settings are fixed here, before any data is seen.
- 2Fit 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.
- 3Transform 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.
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.
- 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 failsThe 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 insteadPass 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 failsThe y argument holds target labels or responses, and the source describes unsupervised methods such as clustering as taking X without y.
Do this insteadCall 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 failspredict 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 insteadUse 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 failsBoth methods apply a learned model. Fit must come first, because it is what builds the model.
Do this insteadCall fit first, then choose predict or transform depending on the output you need.
Common mistake Passing integer-valued data without converting it
Why it failsThe source states that the entries in X are assumed to be continuous floating-point numbers.
Do this insteadConvert integer-valued data to floating-point before calling fit.
Checking Your Understanding
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.