CodeBlanks Logo Icon CodeBlanks

Estimator API

All scikit-learn models share fit() and predict() methods.

The Estimator API

In scikit-learn everything is an estimator: an object that learns something from data. There are three roles:

  • Predictor: fit + predict (models).
  • Transformer: fit + transform (scalers, encoders, PCA).
  • Evaluator: score.
sc = StandardScaler()            # transformer
sc.fit(X_train)
Z = sc.transform(X_train)
m = LogisticRegression()          # predictor
m.fit(Z, y_train)
m.predict(sc.transform(X_test))

Hyperparameters are given in the constructor; learned values are stored afterwards in attributes ending with _.

clone and get_params

An estimator's hyperparameters are read with get_params() and changed with set_params(). clone produces a new, untrained copy with the same settings; cross-validation does this in every fold:

from sklearn.base import clone
m = LogisticRegression(C=0.5).fit(X_train, y_train)
yeni = clone(m)
print(yeni.get_params()["C"])   # 0.5
print(hasattr(yeni, "coef_"))   # False: untrained

Thanks to this design, classes you write yourself work with all the tools once they derive from BaseEstimator.

Practice Copy with clone

Train a LogisticRegression(C=0.1, max_iter=1000). Copy it with clone; print the copy's C value and whether it has a coef_ attribute on one line. Then change the copy with set_params(C=10), train it and print the test accuracy with round(..., 3).

Starter code

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
from sklearn.base import clone
from sklearn.linear_model import LogisticRegression

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
from sklearn.base import clone
from sklearn.linear_model import LogisticRegression

m = LogisticRegression(C=0.1, max_iter=1000).fit(X_train, y_train)
yeni = clone(m)
print(yeni.get_params()["C"], hasattr(yeni, "coef_"))
yeni.set_params(C=10).fit(X_train, y_train)
print(round(yeni.score(X_test, y_test), 3))

Expected output

0.1 False
1.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which estimator role does PCA have?

  • Evaluator
  • None of them
  • Transformer Correct answer
  • Predictor

Explanation: PCA learns the components with fit and transforms the data with transform; it doesn't predict.

2 Create an untrained copy with the same settings.

Question
yeni = ____(m)
Answer
yeni = clone(m)

Explanation: clone copies only the hyperparameters, not what was learned.

3 Match the roles.

Term Meaning
StandardScaler Transformer
LogisticRegression Predictor
clone Untrained copy
set_params Changing settings

All lessons in Intermediate