-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[WIP] FIX avoid making deepcopy in clone #9696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+186
−136
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from scipy import sparse | ||
from .externals import six | ||
from .utils.fixes import signature | ||
from .exceptions import ChangedBehaviorWarning | ||
from . import __version__ | ||
|
||
|
||
|
@@ -26,7 +27,7 @@ def _first_and_last_element(arr): | |
return arr[0, 0], arr[-1, -1] | ||
|
||
|
||
def clone(estimator, safe=True): | ||
def clone(estimator, safe=True, deepcopy=None): | ||
"""Constructs a new estimator with the same parameters. | ||
|
||
Clone does a deep copy of the model in an estimator | ||
|
@@ -39,17 +40,40 @@ def clone(estimator, safe=True): | |
The estimator or group of estimators to be cloned | ||
|
||
safe : boolean, optional | ||
If safe is false, clone will fall back to a deep copy on objects | ||
that are not estimators. | ||
|
||
If safe is false, clone will fall back to a copy (deep copy or simple | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this be better described as "if True, the argument to clone must be an estimator (i.e. support |
||
copy depending of the ``deepcopy`` parameter) on objects that are not | ||
estimators. | ||
|
||
.. deprecated:: 0.20 | ||
From 0.22, only a simple copy will be done instead of a deep copy. | ||
Use ``deepcopy=True`` to get the previous behavior. | ||
|
||
deepcopy : boolean, optional | ||
Whether to make a deep copy or a simple copy of the objects that are | ||
not estimators. | ||
|
||
.. versionadded:: 0.20 | ||
|
||
""" | ||
if not safe and deepcopy is None: | ||
warnings.warn("A simple copy will be performed after 0.22 instead of a" | ||
" deep copy. Set 'deepcopy=True' if you wish to make a" | ||
" deep copy of the parameters which are not estimators.", | ||
ChangedBehaviorWarning) | ||
deepcopy = True | ||
|
||
estimator_type = type(estimator) | ||
# XXX: not handling dictionaries | ||
if estimator_type in (list, tuple, set, frozenset): | ||
return estimator_type([clone(e, safe=safe) for e in estimator]) | ||
return estimator_type([clone(e, safe=safe, deepcopy=deepcopy) | ||
for e in estimator]) | ||
elif not hasattr(estimator, 'get_params'): | ||
if not safe: | ||
return copy.deepcopy(estimator) | ||
if deepcopy: | ||
return copy.deepcopy(estimator) | ||
else: | ||
return copy.copy(estimator) | ||
else: | ||
raise TypeError("Cannot clone object '%s' (type %s): " | ||
"it does not seem to be a scikit-learn estimator " | ||
|
@@ -58,7 +82,7 @@ def clone(estimator, safe=True): | |
klass = estimator.__class__ | ||
new_object_params = estimator.get_params(deep=False) | ||
for name, param in six.iteritems(new_object_params): | ||
new_object_params[name] = clone(param, safe=False) | ||
new_object_params[name] = clone(param, safe=False, deepcopy=deepcopy) | ||
new_object = klass(**new_object_params) | ||
params_set = new_object.get_params(deep=False) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been preferring
deepcopy='warn'
and similar lately over None.I wonder if we can instead use magic to check if the caller is internal to this library, and if so, just replace
deepcopy='warn'
withdeepcopy=False
.