MCPcopy
hub / github.com/scikit-learn/scikit-learn / _clone_parametrized

Function _clone_parametrized

sklearn/base.py:100–162  ·  view source on GitHub ↗

Default implementation of clone. See :func:`sklearn.base.clone` for details.

(estimator, *, safe=True)

Source from the content-addressed store, hash-verified

98
99
100def _clone_parametrized(estimator, *, safe=True):
101 """Default implementation of clone. See :func:`sklearn.base.clone` for details."""
102
103 estimator_type = type(estimator)
104 if estimator_type is dict:
105 return {k: clone(v, safe=safe) for k, v in estimator.items()}
106 elif estimator_type in (list, tuple, set, frozenset):
107 return estimator_type([clone(e, safe=safe) for e in estimator])
108 elif not hasattr(estimator, "get_params") or isinstance(estimator, type):
109 if not safe:
110 return copy.deepcopy(estimator)
111 else:
112 if isinstance(estimator, type):
113 raise TypeError(
114 "Cannot clone object. "
115 "You should provide an instance of "
116 "scikit-learn estimator instead of a class."
117 )
118 else:
119 raise TypeError(
120 "Cannot clone object '%s' (type %s): "
121 "it does not seem to be a scikit-learn "
122 "estimator as it does not implement a "
123 "'get_params' method." % (repr(estimator), type(estimator))
124 )
125
126 klass = estimator.__class__
127 new_object_params = estimator.get_params(deep=False)
128 for name, param in new_object_params.items():
129 new_object_params[name] = clone(param, safe=False)
130
131 new_object = klass(**new_object_params)
132 try:
133 new_object._metadata_request = clone(estimator._metadata_request)
134 except AttributeError:
135 pass
136
137 params_set = new_object.get_params(deep=False)
138
139 if hasattr(estimator, "_skl_callbacks"):
140 # Callback classes are expected to be designed in a way that a single instance
141 # can be used by multiple clones of the same estimator as is typically the case
142 # in ensembles or during cross-validation. Therefore it is safe to pass the
143 # callback instances by reference.
144 new_object._skl_callbacks = estimator._skl_callbacks
145
146 # quick sanity check of the parameters of the clone
147 for name in new_object_params:
148 param1 = new_object_params[name]
149 param2 = params_set[name]
150 if param1 is not param2:
151 raise RuntimeError(
152 "Cannot clone object %s, as the constructor "
153 "either does not set or modifies parameter %s" % (estimator, name)
154 )
155
156 # _sklearn_output_config is used by `set_output` to configure the output
157 # container of an estimator.

Callers 2

cloneFunction · 0.85
__sklearn_clone__Method · 0.85

Calls 2

cloneFunction · 0.85
get_paramsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…