Construct a new unfitted estimator with the same parameters. Clone does a deep copy of the model in an estimator without actually copying attached data. It returns a new estimator with the same parameters that has not been fitted on any data. .. versionchanged:: 1.3 Delegat
(estimator, *, safe=True)
| 44 | |
| 45 | |
| 46 | def clone(estimator, *, safe=True): |
| 47 | """Construct a new unfitted estimator with the same parameters. |
| 48 | |
| 49 | Clone does a deep copy of the model in an estimator |
| 50 | without actually copying attached data. It returns a new estimator |
| 51 | with the same parameters that has not been fitted on any data. |
| 52 | |
| 53 | .. versionchanged:: 1.3 |
| 54 | Delegates to `estimator.__sklearn_clone__` if the method exists. |
| 55 | |
| 56 | Parameters |
| 57 | ---------- |
| 58 | estimator : {list, tuple, set} of estimator instance or a single \ |
| 59 | estimator instance |
| 60 | The estimator or group of estimators to be cloned. |
| 61 | safe : bool, default=True |
| 62 | If safe is False, clone will fall back to a deep copy on objects |
| 63 | that are not estimators. Ignored if `estimator.__sklearn_clone__` |
| 64 | exists. |
| 65 | |
| 66 | Returns |
| 67 | ------- |
| 68 | estimator : object |
| 69 | The deep copy of the input, an estimator if input is an estimator. |
| 70 | |
| 71 | Notes |
| 72 | ----- |
| 73 | If the estimator's `random_state` parameter is an integer (or if the |
| 74 | estimator doesn't have a `random_state` parameter), an *exact clone* is |
| 75 | returned: the clone and the original estimator will give the exact same |
| 76 | results. Otherwise, *statistical clone* is returned: the clone might |
| 77 | return different results from the original estimator. More details can be |
| 78 | found in :ref:`randomness`. |
| 79 | |
| 80 | Examples |
| 81 | -------- |
| 82 | >>> from sklearn.base import clone |
| 83 | >>> from sklearn.linear_model import LogisticRegression |
| 84 | >>> X = [[-1, 0], [0, 1], [0, -1], [1, 0]] |
| 85 | >>> y = [0, 0, 1, 1] |
| 86 | >>> classifier = LogisticRegression().fit(X, y) |
| 87 | >>> cloned_classifier = clone(classifier) |
| 88 | >>> hasattr(classifier, "classes_") |
| 89 | True |
| 90 | >>> hasattr(cloned_classifier, "classes_") |
| 91 | False |
| 92 | >>> classifier is cloned_classifier |
| 93 | False |
| 94 | """ |
| 95 | if hasattr(estimator, "__sklearn_clone__") and not inspect.isclass(estimator): |
| 96 | return estimator.__sklearn_clone__() |
| 97 | return _clone_parametrized(estimator, safe=safe) |
| 98 | |
| 99 | |
| 100 | def _clone_parametrized(estimator, *, safe=True): |
searching dependent graphs…