-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG] Fix sklearn.base.clone when estimator has any kind of sparse matrix as attribute #6910
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# Author: Gael Varoquaux | ||
# License: BSD 3 clause | ||
|
||
import sys | ||
|
||
import numpy as np | ||
import scipy.sparse as sp | ||
|
||
|
@@ -143,6 +145,24 @@ def test_clone_nan(): | |
assert_true(clf.empty is clf2.empty) | ||
|
||
|
||
def test_clone_sparse_matrices(): | ||
sparse_matrix_classes = [ | ||
getattr(sp, name) | ||
for name in dir(sp) if name.endswith('_matrix')] | ||
|
||
PY26 = sys.version_info[:2] == (2, 6) | ||
if PY26: | ||
# sp.dok_matrix can not be deepcopied in Python 2.6 | ||
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. For the record: from copy import deepcopy
import numpy as np
from scipy import sparse as sp
m = sp.dok_matrix(np.eye(5))
deepcopy(m) fails on Python 2.6 with the error:
For some reason it looks like the reconstructed matrix is missing some attributes. I think it's fine not testing dok_matrix for Python 2.6. 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. Yes, I think it's because Py2.6 has a specialised reconstruction routine for dicts and fails to handle the subclassing correctly... I'm happy with this solution. |
||
sparse_matrix_classes.remove(sp.dok_matrix) | ||
|
||
for cls in sparse_matrix_classes: | ||
sparse_matrix = cls(np.eye(5)) | ||
clf = MyEstimator(empty=sparse_matrix) | ||
clf_cloned = clone(clf) | ||
assert_true(clf.empty.__class__ is clf_cloned.empty.__class__) | ||
assert_array_equal(clf.empty.toarray(), clf_cloned.empty.toarray()) | ||
|
||
|
||
def test_repr(): | ||
# Smoke test the repr of the base estimator. | ||
my_estimator = MyEstimator() | ||
|
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.
Not a strong test, but okay.