Estimator fit and predict
Quick answer
An estimator is an object that learns from data through fit. A predictor is an estimator that can produce predictions for new inputs through predict. Calling fit on a supervised estimator supplies features X and target y; calling predict supplies only features. The fitted object retains learned state, so its predictions can change after another fit. All library examples are standalone local Python examples tested with scikit-learn 1.8.0; ByteMind package execution is not verified.
Fit a constant predictor
from sklearn.dummy import DummyRegressormodel = DummyRegressor(strategy='mean')print(model.fit([[1], [2], [3]], [4, 7, 10]) is model)print(model.predict([[9], [12]]).tolist())Expected output:
True[7.0, 7.0]The baseline learns the training target mean, regardless of the new feature values.
How it works
X usually has one row per sample and one column per feature; y commonly has one target per row for a single-output task. A constructor configures behavior but should not train on data. Most scikit-learn fit methods return the estimator itself. Learned public attributes conventionally end in an underscore, such as classes_ or mean_. Predict requires fitted state for ordinary learned predictors. It uses the learned mapping, not the held-out labels. A new fit usually replaces previous learned state; incremental partial_fit and warm_start are separate contracts and not supported by every estimator. A fitted object is not proof of useful generalization. Keep the test labels outside fitting and evaluate only after decisions are locked.
Inspect classifier output
from sklearn.tree import DecisionTreeClassifiermodel = DecisionTreeClassifier(max_depth=1, random_state=0)model.fit([[0], [1], [8], [9]], ['small', 'small', 'large', 'large'])print(model.classes_.tolist())print(model.predict([[0], [9]]).tolist())Expected output:
['large', 'small']['small', 'large']The learned class list is ordered by label, not by appearance in the training rows.
Common mistakes
Calling fit on a test row just before predict changes the model and invalidates a holdout evaluation. Passing a one-dimensional list to a predictor expecting two-dimensional X also changes the input contract: one sample with two features is [[a, b]], not [a, b]. Check n_features_in_ and the training schema. A classifier may offer predict_proba, but not all classifiers do; class order must be checked with classes_.
Practice
Fit the mean baseline on targets 4, 7, 10. Predict for two new feature rows without using their targets. The prediction is 7 for both. Compare with a model trained on a different target mean and explain why the constructor did not change.
Reference candidate: concept-estimator-fit-and-predict. Review the associated knowledge check. Native estimator tasks require a local scientific Python environment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Estimator fit and predict — knowledge check
Apply estimator contracts and leakage-resistant pipelines to small fixtures.
Which data is needed by supervised fit?
What is the mean baseline for targets 2, 4, 9?
Which call uses fitted state without learning from a new target?
Which shape represents one row with two features?
What does a trailing underscore on classes_ typically indicate?