Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

fit

fit trains a scikit-learn estimator from feature data and, for supervised learning, targets. It stores learned state and returns the fitted estimator.

How it works

Calling fit trains an estimator on supplied data. It validates inputs, estimates model parameters, stores the fitted state, and returns the same estimator so calls can be chained; supervised estimators use X and y, while unsupervised estimators may need only X.

Syntax

estimator.fit(X, y)

Parameters

NameTypeDescription
Xarray-likeTraining samples, typically shaped as rows of samples and columns of features.
yarray-like, optionalTarget values for supervised learning; omitted for estimators that learn without targets.
**kwargskeyword arguments, optionalAdditional training-data metadata, such as sample weights, when accepted by the estimator.

Returns

The fitted estimator itself.

Key points

  • The estimator API provides fit as the common training entry point.
  • Training computations, such as estimating linear-model coefficients, occur during fit.
  • The required data shape depends on whether the estimator is supervised or unsupervised.
  • Some estimators accept extra data-dependent metadata through keyword arguments.
  • The returned estimator can be assigned back to the same variable after fitting.

Common mistakes

  • For supervised learning, X and y must contain the same number of samples.
  • A later fit normally replaces the estimator's previous learned state; warm-start behavior is a deliberate exception.
  • Do not expect the estimator to retain the original training arrays as references.

Example: Fit a linear regression model

from sklearn.linear_model import LinearRegression

X = [[0], [1], [2]]
y = [1, 3, 5]
model = LinearRegression().fit(X, y)
print(model.predict([[3]]))

Creates a regression estimator, trains it on feature rows and targets, then predicts a new target.

When to use it

Supervised model training

Train a supervised classifier or regressor from feature rows and known target values.

Unsupervised fitting

Learn structure from feature data without target labels, such as with clustering or dimensionality reduction.

Metadata-aware training

Pass supported per-sample information while training, such as weighting observations.

Interview questions

QWhat does fit do in scikit-learn?

fit is the estimator method where training occurs: it uses one data array for unsupervised learning or two arrays, X and y, for supervised learning. It returns the fitted estimator, which enables method chaining.

Follow-up: What must a supervised implementation guarantee about X and y? Their sample counts must match, so X.shape[0] must equal y.shape[0].

QHow do supervised and unsupervised fit calls differ?

A supervised estimator generally receives feature data X and targets y, while an unsupervised estimator can receive only X. Additional training-data metadata, such as sample_weight, may be accepted as keyword arguments when supported.

Follow-up: Why should an estimator avoid retaining X and y? The documented estimator contract says the fitted object holds no reference to those input arrays.

QWhy is fit commonly written as a chained call?

fit returns the estimator after training rather than a separate model object. The returned object contains the learned state and can therefore be used immediately for subsequent estimator operations.

Follow-up: What happens on repeated fitting? A new fit should disregard the prior fit unless the estimator explicitly supports warm-start semantics.

  • fit_transform
  • fit_predict
  • predict
  • score
  • estimator

Source: Developing scikit-learn estimators — scikit-learn 1.8.dev0 documentation. Checked 2026-10-15.