8000 sparsity test for _as_row_vector · scikit-learn/scikit-learn@a189567 · GitHub
[go: up one dir, main page]

Skip to content

Commit a189567

Browse files
author
giorgiop
committed
sparsity test for _as_row_vector
1 parent 79f77f7 commit a189567

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sklearn/preprocessing/data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
]
5050

5151

52-
def _as_row_vector(X, copy=False):
52+
def _as_row_vector(X):
5353
"""In case X is an array with shape[1]=1, interpret it as a row vector.
5454
It assumed that X is already 2d.
5555
"""
5656
if X.shape[1] == 1:
57-
return np.transpose(np.array(X, copy=copy))
57+
return X.transpose()
5858
else:
5959
return X
6060

@@ -498,7 +498,7 @@ def partial_fit(self, X, y=None):
498498
# Treat 1D arrays as row of a matrix (instead of 1 column)
499499
X = check_array(X, accept_sparse='csr', copy=self.copy,
500500
ensure_2d=True)
501-
X = _as_row_vector(X, copy=self.copy)
501+
X = _as_row_vector(X)
502502

503503
# First partial_fit
504504
if not hasattr(self, 'n_samples_seen_'):

sklearn/preprocessing/tests/test_data.py

Lines changed: 14 additions & 0 deletions
< 3E2B /tr>
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from sklearn.preprocessing.data import _transform_selected
2929
from sklearn.preprocessing.data import _mean_and_std
3030
from sklearn.preprocessing.data import _incremental_mean_and_var
31+
from sklearn.preprocessing.data import _as_row_vector
3132
from sklearn.preprocessing.data import Binarizer
3233
from sklearn.preprocessing.data import KernelCenterer
3334
from sklearn.preprocessing.data import Normalizer
@@ -382,6 +383,19 @@ def test_standard_scaler_partial_fit_numerical_stability():
382383
# assert) while mean is OK.
383384

384385

386+
def test_partial_fit_sparse_input():
387+
388+
# Check that sparsity is not destroyed
389+
X = np.array([[1], [1], [2], [5]])
390+
X_csr = sparse.csr_matrix(X)
391+
X_csr_T = _as_row_vector(X_csr)
392+
393+
assert_array_almost_equal(X_csr.todense().T,
394+
X_csr_T.todense()) # same result
395+
assert_true(sparse.issparse(X_csr_T)) # still sparse
396+
assert_true(sparse.isspmatrix_csc(X_csr_T)) # it changes the sparsity type
397+
398+
385399
def test_trasform_with_partial_fit():
386400
# Check some postconditions after applying partial_fit and transform
387401
rng = np.random.RandomState(0)

0 commit comments

Comments
 (0)
0