10000 remove convert_sparse_to as gael suggested. · scikit-learn/scikit-learn@1be9e46 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1be9e46

Browse files
committed
remove convert_sparse_to as gael suggested.
1 parent f3336fa commit 1be9e46

File tree

2 files changed

+30
-52
lines changed

2 files changed

+30
-52
lines changed

sklearn/utils/tests/test_validation.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
check_array)
1212

1313
from sklearn.utils.validation import _sparse_matrix_constructor
14+
from sklearn.utils.estimator_checks import NotAnArray
1415

1516
from sklearn.random_projection import sparse_random_matrix
1617

@@ -303,35 +304,24 @@ def test_check_array():
303304
for X in [X_csc, X_coo, X_dok, X_int, X_float]:
304305
for dtype in [np.int32, np.int, np.float, np.float32]:
305306
for allowed_sparse in [['csr', 'coo'], ['coo', 'dok']]:
306-
for convert_sparse_to in ['csr', 'csc', 'coo', None]:
307-
for copy in [True, False]:
308-
if (convert_sparse_to is not None and convert_sparse_to
309-
not in allowed_sparse):
310-
assert_raises(ValueError, check_array, X,
311-
allowed_sparse=allowed_sparse,
312-
convert_sparse_to=convert_sparse_to)
313-
continue
314-
X_checked = check_array(X, dtype=dtype,
315-
allowed_sparse=allowed_sparse,
316-
convert_sparse_to=convert_sparse_to,
317-
copy=copy)
318-
assert_equal(X_checked.dtype, dtype)
319-
if X.format in allowed_sparse:
320-
# no change if allowed
321-
assert_equal(X.format, X_checked.format)
322-
else:
323-
# got converted
324-
if convert_sparse_to is None:
325-
assert_equal(X_checked.format, allowed_sparse[0])
326-
else:
327-
assert_equal(X_checked.format, convert_sparse_to)
328-
if copy:
329-
assert_false(X is X_checked)
330-
else:
331-
# doesn't copy if it was already good
332-
if (X.dtype == X_checked.dtype and
333-
X.format == X_checked.format):
334-
assert_true(X is X_checked)
307+
for copy in [True, False]:
308+
X_checked = check_array(X, dtype=dtype,
309+
allowed_sparse=allowed_sparse,
310+
copy=copy)
311+
assert_equal(X_checked.dtype, dtype)
312+
if X.format in allowed_sparse:
313+
# no change if allowed
314+
assert_equal(X.format, X_checked.format)
315+
else:
316+
# got converted
317+
assert_equal(X_checked.format, allowed_sparse[0])
318+
if copy:
319+
assert_false(X is X_checked)
320+
else:
321+
# doesn't copy if it was already good
322+
if (X.dtype == X_checked.dtype and
323+
X.format == X_checked.format):
324+
assert_true(X is X_checked)
335325

336326
# other input formats
337327
# convert lists to arrays
@@ -340,3 +330,7 @@ def test_check_array():
340330
# raise on too deep lists
341331
assert_raises(ValueError, check_array, X_ndim.tolist())
342332
check_array(X_ndim.tolist(), allow_nd=True) # doesn't raise
333+
# convert weird stuff to arrays
334+
X_no_array = NotAnArray(X_dense)
335+
result = check_array(X_no_array)
336+
assert_true(isinstance(result, np.ndarray))

sklearn/utils/validation.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _sparse_matrix_constructor(string_format):
164164

165165

166166
def _ensure_sparse_format(spmatrix, allowed_sparse, dtype, order, copy,
167-
force_all_finite, convert_sparse_to):
167+
force_all_finite):
168168
"""Convert a sparse matrix to a given format.
169169
170170
Checks the sparse format of spmatrix and converts if necessary.
@@ -178,7 +178,7 @@ def _ensure_sparse_format(spmatrix, allowed_sparse, dtype, order, copy,
178178
String[s] representing allowed sparse matrix formats ('csc',
179179
'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). None means that sparse
180180
matrix input will raise an error. If the input is sparse but not in
181-
the allowed format, it will be converted to convert_sparse_to.
181+
the allowed format, it will be converted to the first listed format.
182182
183183
order : 'F', 'C' or None (default)
184184
Whether an array will be forced to be fortran or c-style.
@@ -190,22 +190,15 @@ def _ensure_sparse_format(spmatrix, allowed_sparse, dtype, order, copy,
190190
force_all_finite : boolean, default=True
191191
Whether to raise an error on np.inf and np.nan in X.
192192
193-
convert_sparse_to : string or None (default).
194-
Sparse format to convert sparse matrices to if allowed_sparse is not
195-
None. By default, the first entry of allowed_sparse will be used.
196-
197193
Returns
198194
-------
199195
spmatrix_convertd : scipy sparse matrix.
200-
Matrix that is ensured to have an allowed type (or convert_sparse_to).
196+
Matrix that is ensured to have an allowed type.
201197
"""
202198
if allowed_sparse is None:
203199
raise TypeError('A sparse matrix was passed, but dense '
204200
'data is required. Use X.toarray() to '
205201
'convert to a dense numpy array.')
206-
if convert_sparse_to not in allowed_sparse:
207-
raise ValueError("Conversion targed %s not in allowed_sparse: %s"
208-
% (convert_sparse_to, allowed_sparse))
209202
sparse_type = spmatrix.format
210203
if sparse_type in allowed_sparse:
211204
# correct type
@@ -218,7 +211,7 @@ def _ensure_sparse_format(spmatrix, allowed_sparse, dtype, order, copy,
218211
spmatrix = spmatrix.astype(dtype)
219212
else:
220213
# create new
221-
spmatrix = _sparse_matrix_constructor(convert_sparse_to)(
214+
spmatrix = _sparse_matrix_constructor(allowed_sparse[0])(
222215
spmatrix, copy=copy, dtype=dtype)
223216
if force_all_finite:
224217
if not hasattr(spmatrix, "data"):
@@ -232,8 +225,7 @@ def _ensure_sparse_format(spmatrix, allowed_sparse, dtype, order, copy,
232225

233226

234227
def check_array(array, allowed_sparse=None, dtype=None, order=None, copy=False,
235-
force_all_finite=True, convert_sparse_to=None, ensure_2d=True,
236-
allow_nd=False):
228+
force_all_finite=True, ensure_2d=True, allow_nd=False):
237229
"""Input validation on an array, list, sparse matrix or similar.
238230
239231
By default, the input is converted to an at least 2nd numpy array.
@@ -247,7 +239,7 @@ def check_array(array, allowed_sparse=None, dtype=None, order=None, copy=False,
247239
String[s] representing allowed sparse matrix formats, such as 'csc',
248240
'csr', etc. None means that sparse matrix input will raise an error.
249241
If the input is sparse but not in the allowed format, it will be
250-
converted to convert_sparse_to.
242+
converted to the first listed format.
251243
252244
order : 'F', 'C' or None (default)
253245
Whether an array will be forced to be fortran or c-style.
@@ -259,10 +251,6 @@ def check_array(array, allowed_sparse=None, dtype=None, order=None, copy=False,
259251
force_all_finite : boolean, default=True
260252
Whether to raise an error on np.inf and np.nan in X.
261253
262-
convert_sparse_to : string or None (default).
263-
Sparse format to convert sparse matrices to if allowed_sparse is not
264-
None. By default, the first entry of allowed_sparse will be used.
265-
266254
ensure_2d : boolean, default=True
267255
Whether to make X at least 2d.
268256
@@ -276,14 +264,10 @@ def check_array(array, allowed_sparse=None, dtype=None, order=None, copy=False,
276264
"""
277265
if isinstance(allowed_sparse, str):
278266
allowed_sparse = [allowed_sparse]
279-
if allowed_sparse is not None and convert_sparse_to is None:
280-
# sensible default converter ;)
281-
convert_sparse_to = allowed_sparse[0]
282267

283268
if sp.issparse(array):
284269
array = _ensure_sparse_format(array, allowed_sparse, dtype, order,
285-
copy, force_all_finite,
286-
convert_sparse_to)
270+
copy, force_all_finite)
287271
else:
288272
if ensure_2d:
289273
array = np.atleast_2d(array)

0 commit comments

Comments
 (0)
0