Parameter introspection and cloning
Quick answer
Scikit-learn estimators expose configuration through get_params and set_params. Clone constructs a fresh estimator with matching configuration in the usual case, without learned training state. Nested estimator parameters use a double underscore between step and parameter names. All library examples are standalone local Python examples tested with scikit-learn 1.8.0; ByteMind package execution is not verified.
Inspect nested parameters and clone
from sklearn.base import clonefrom sklearn.pipeline import Pipelinefrom sklearn.tree import DecisionTreeClassifierp = Pipeline([('model', DecisionTreeClassifier(max_depth=1, random_state=0))])p.fit([[0], [1]], [0, 1])q = clone(p).set_params(model__max_depth=2)print(p.get_params()['model__max_depth'], q.get_params()['model__max_depth'])print(hasattr(p.named_steps['model'], 'tree_'), hasattr(q.named_steps['model'], 'tree_'))Expected output:
1 2True FalseChanging the fresh tree configuration leaves the fitted original untouched.
How it works
get_params(deep=False) returns top-level constructor parameters; deep=True also exposes nested keys such as model__max_depth. set_params returns the estimator and updates configuration, but does not promise a valid refit or clear every learned attribute. Fit again before using a changed model. Standard clone reconstructs ordinary estimators from constructor parameters, not fitted attributes. Custom sklearn_clone hooks can intentionally alter this behavior, so do not treat it as a universal reset for arbitrary objects. Cloning is different from copying an already trained model for inference. Cross-validation and hyperparameter search rely on fresh estimators. Metadata means auxiliary information such as sample_weight or groups, separate from X and y. With metadata routing disabled, Pipeline.fit can forward fit parameters using step__parameter. With routing enabled in scikit-learn 1.8, configure each relevant consumer to request or decline supported metadata, and pass it at the router boundary. This feature has version- and consumer-specific support; never assume weights are passed to every step. A weighted mean uses sum(w*y)/sum(w) for positive total weight.
Route weights to the requesting model
from sklearn import config_contextfrom sklearn.pipeline import Pipelinefrom sklearn.dummy import DummyRegressorwith config_context(enable_metadata_routing=True): model = DummyRegressor().set_fit_request(sample_weight=True) p = Pipeline([('model', model)]) p.fit([[0], [1]], [0, 10], sample_weight=[1, 3]) print(p.predict([[5]]).tolist())Expected output:
[7.5]The model receives weights explicitly; 30 divided by 4 is 7.5.
Common mistakes
Do not modify a dictionary returned by get_params and assume all changes are automatically applied. Use set_params and refit. Do not call clone to obtain a second ready-to-predict copy. Do not turn on global routing for unrelated work; config_context limits the example to its block. Routing is not authorization or a way to bypass estimator validation.
Practice
Clone a fitted decision tree, change max_depth on the clone and verify that the original parameter and fitted state are unchanged. Fit the clone separately. Then compare unweighted and weighted DummyRegressor predictions for targets 0 and 10 with weights 1 and 3.
Reference candidate: concept-parameter-introspection-and-cloning. 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
Parameter introspection and cloning — knowledge check
Apply estimator contracts and leakage-resistant pipelines to small fixtures.
How do you address max_depth inside a step named model?
What normally remains after cloning a fitted ordinary tree?
After changing max_depth with set_params, what should you do before evaluating?
What is a weighted mean of 0 and 10 with weights 1 and 3?
With routing enabled, how should a consumer receive sample_weight?