Sub topic 14
Inspecting and copying estimators with get_params, set_params, and clone
This chapter assumes you know the fit/predict workflow and the difference between constructor settings (hyperparameters) and attributes learned during fit, which end in an underscore. Here you will see how scikit-learn lets you read, change, and copy those settings in a uniform way.
Reading settings with get_params
Every estimator inherits get_params from BaseEstimator. It returns a dictionary of the constructor arguments. Tools such as grid search rely on this method to see what can be tuned.
clone builds a new, unfitted estimator from those settings. set_params changes settings on an existing object and returns that same object.
from sklearn.base import clone
from sklearn.linear_model import LogisticRegression
a = LogisticRegression(C=0.5)
print(a.get_params()["C"]) # 0.5
b = clone(a).set_params(C=2.0)
print(a.C, b.C) # 0.5 2.0clone(a) returns a new object, so changing b leaves a untouched. Because set_params returns self, you can chain it directly after clone.
What clone keeps and what it drops
clone copies only the constructor parameters. It does not copy anything learned in fit. A clone of a fitted model is a fresh model with the same settings.
from sklearn.base import clone
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
a = LogisticRegression(max_iter=200).fit(X, y)
b = clone(a)
print(hasattr(a, "coef_"), hasattr(b, "coef_")) # True False
print(b.max_iter) # 200This is why cross-validation can reuse one estimator definition. For each fold it clones the estimator, fits the clone, and discards it. Your original object is never fitted by cross_val_score.
Nested parameters in a pipeline
A Pipeline is also an estimator, so it has get_params and set_params. Its parameter names combine the step name and the parameter name, joined by two underscores. make_pipeline names each step after its lowercased class name.
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=200))
print(pipe.get_params()["logisticregression__C"]) # 1.0
pipe.set_params(logisticregression__C=10.0)
print(pipe.named_steps["logisticregression"].C) # 10.0The same step__parameter convention is what GridSearchCV uses in its parameter grids. A ColumnTransformer inside a pipeline extends it further, for example preprocess__num__with_mean.
Common errors
- Setting a learned attribute. Assigning
model.coef_ = ...does not configure anything. Only constructor parameters are settings. - Misspelled parameter names.
set_paramsraises aValueErrorfor names it does not recognize, which catches typos in pipeline names early. - Expecting clone to keep the fit. If you need a fitted copy, use
copy.deepcopyinstead. - Modifying the shared object.
b = amakes two names for one estimator. Useclonewhen you want an independent copy.
Exercise
You have pipe = make_pipeline(StandardScaler(), LogisticRegression()) and you have already called pipe.fit(X, y). Write code that gives you an unfitted copy of the pipeline with the logistic regression's C set to 5.0, without changing pipe. What happens to the fitted scaler statistics in the copy?
Show answer
pipe2 = clone(pipe).set_params(logisticregression__C=5.0). The copy is unfitted, so the learned values such as mean_ on its scaler do not exist until you call pipe2.fit(X, y). The original pipe stays fitted with C=1.0.