Skip to content

[MRG] astype sparse matrix #4585

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions sklearn/utils/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,24 @@ def divide(x1, x2, out=None, dtype=None):

try:
np.array(5).astype(float, copy=False)

except TypeError:
# Compat where astype accepted no copy argument
def astype(array, dtype, copy=True):
if not copy and array.dtype == dtype:
return array
return array.astype(dtype)
np_astype_copy_supported = False

else:
astype = np.ndarray.astype
np_astype_copy_supported = True


def astype(array, dtype, copy=True):
if not copy and array.dtype == dtype:
return array
elif sp.issparse(array):
return array.astype(dtype)
elif np_astype_copy_supported:
return array.astype(dtype, copy=copy)
else:
return array.astype(dtype)

try:
with warnings.catch_warnings(record=True):
# Don't raise the numpy deprecation warnings that appear in
Expand Down
26 changes: 25 additions & 1 deletion sklearn/utils/tests/test_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
from sklearn.utils.fixes import divide, expit
from sklearn.utils.fixes import astype

from scipy.sparse import bsr_matrix

def test_expit():
# Check numerical stability of expit (logistic function).

# Simulate our previous Cython implementation, based on
#http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression
# http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression
assert_almost_equal(expit(1000.), 1. / (1. + np.exp(-1000.)), decimal=16)
assert_almost_equal(expit(-1000.), np.exp(-1000.) / (1. + np.exp(-1000.)),
decimal=16)
Expand Down Expand Up @@ -53,3 +54,26 @@ def test_astype_copy_memory():

e_int32 = astype(a_int32, dtype=np.int32)
assert_false(np.may_share_memory(e_int32, a_int32))


def test_astype_sparse_matrix():
# Checking that the copy boolean is ignored for SciPy sparse matrices
a_spmatrix = bsr_matrix(np.ones((3, 4), dtype=np.int32))

# Check that dtype conversion works
b_spmatrix = astype(a_spmatrix, dtype=np.float32, copy=False)
assert_equal(b_spmatrix.dtype, np.float32)

# Changing dtype forces a copy even if copy=False
assert_false(np.may_share_memory(b_spmatrix, a_spmatrix))

# Check that copy can be skipped if requested dtype match
c_spmatrix = astype(a_spmatrix, dtype=np.int32, copy=False)
assert_true(c_spmatrix is a_spmatrix)

# Check that copy can be forced, and is the case by default:
d_spmatrix = astype(a_spmatrix, dtype=np.int32, copy=True)
assert_false(np.may_share_memory(d_spmatrix, a_spmatrix))

e_spmatrix = astype(a_spmatrix, dtype=np.int32)
assert_false(np.may_share_memory(e_spmatrix, a_spmatrix))