@@ -200,40 +200,55 @@ def _ensure_sparse_format(spmatrix, accept_sparse, dtype, copy,
200
200
spmatrix : scipy sparse matrix
201
201
Input to validate and convert.
202
202
203
- accept_sparse : string, list of string or None (default=None)
203
+ accept_sparse : string, boolean or list/tuple of strings
204
204
String[s] representing allowed sparse matrix formats ('csc',
205
- 'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). None means that sparse
206
- matrix input will raise an error. If the input is sparse but not in
207
- the allowed format, it will be converted to the first listed format.
205
+ 'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). If the input is sparse but
206
+ not in the allowed format, it will be converted to the first listed
207
+ format. True allows the input to be any format. False means
208
+ that a sparse matrix input will raise an error.
208
209
209
- dtype : string, type or None (default=none)
210
+ dtype : string, type or None
210
211
Data type of result. If None, the dtype of the input is preserved.
211
212
212
- copy : boolean (default=False)
213
+ copy : boolean
213
214
Whether a forced copy will be triggered. If copy=False, a copy might
214
215
be triggered by a conversion.
215
216
216
- force_all_finite : boolean (default=True)
217
+ force_all_finite : boolean
217
218
Whether to raise an error on np.inf and np.nan in X.
218
219
219
220
Returns
220
221
-------
221
222
spmatrix_converted : scipy sparse matrix.
222
223
Matrix that is ensured to have an allowed type.
223
224
"""
224
- if accept_sparse in [None , False ]:
225
- raise TypeError ('A sparse matrix was passed, but dense '
226
- 'data is required. Use X.toarray() to '
227
- 'convert to a dense numpy array.' )
228
225
if dtype is None :
229
226
dtype = spmatrix .dtype
230
227
231
228
changed_format = False
232
- if (isinstance (accept_sparse , (list , tuple ))
233
- and spmatrix .format not in accept_sparse ):
234
- # create new with correct sparse
235
- spmatrix = spmatrix .asformat (accept_sparse [0 ])
236
- changed_format = True
229
+
230
+ if isinstance (accept_sparse , six .string_types ):
231
+ accept_sparse = [accept_sparse ]
232
+
233
+ if accept_sparse is False :
234
+ raise TypeError ('A sparse matrix was passed, but dense '
235
+ 'data is required. Use X.toarray() to '
236
+ 'convert to a dense numpy array.' )
237
+ elif isinstance (accept_sparse , (list , tuple )):
238
+ if len (accept_sparse ) == 0 :
239
+ raise ValueError ("When providing 'accept_sparse' "
240
+ "as a tuple or list, it must contain at "
241
+ "least one string value." )
242
+ # ensure correct sparse format
243
+ if spmatrix .format not in accept_sparse :
244
+ # create new with correct sparse
245
+ spmatrix = spmatrix .asformat (accept_sparse [0 ])
246
+ changed_format = True
247
+ elif accept_sparse is not True :
248
+ # any other type
249
+ raise ValueError ("Parameter 'accept_sparse' should be a string, "
250
+ "boolean or list of strings. You provided "
251
+ "'accept_sparse={}'." .format (accept_sparse ))
237
252
238
253
if dtype != spmatrix .dtype :
239
254
# convert dtype
@@ -251,7 +266,7 @@ def _ensure_sparse_format(spmatrix, accept_sparse, dtype, copy,
251
266
return spmatrix
252
267
253
268
254
- def check_array (array , accept_sparse = None , dtype = "numeric" , order = None ,
269
+ def check_array (array , accept_sparse = False , dtype = "numeric" , order = None ,
255
270
copy = False , force_all_finite = True , ensure_2d = True ,
256
271
allow_nd = False , ensure_min_samples = 1 , ensure_min_features = 1 ,
257
272
warn_on_dtype = False , estimator = None ):
@@ -266,11 +281,12 @@ def check_array(array, accept_sparse=None, dtype="numeric", order=None,
266
281
array : object
267
282
Input object to check / convert.
268
283
269
- accept_sparse : string, list of string or None (default=None )
284
+ accept_sparse : string, boolean or list/tuple of strings (default=False )
270
285
String[s] representing allowed sparse matrix formats, such as 'csc',
271
- 'csr', etc. None means that sparse matrix input will raise an error.
272
- If the input is sparse but not in the allowed format, it will be
273
- converted to the first listed format.
286
+ 'csr', etc. If the input is sparse but not in the allowed format,
287
+ it will be converted to the first listed format. True allows the input
288
+ to be any format. False means that a sparse matrix input will
289
+ raise an error.
274
290
275
291
dtype : string, type, list of types or None (default="numeric")
276
292
Data type of result. If None, the dtype of the input is preserved.
@@ -321,8 +337,14 @@ def check_array(array, accept_sparse=None, dtype="numeric", order=None,
321
337
X_converted : object
322
338
The converted and validated X.
323
339
"""
324
- if isinstance (accept_sparse , str ):
325
- accept_sparse = [accept_sparse ]
340
+ # accept_sparse 'None' deprecation check
341
+ if accept_sparse is None :
342
+ warnings .warn (
343
+ "Passing 'None' to parameter 'accept_sparse' in methods "
344
+ "check_array and check_X_y is deprecated in version 0.19 "
345
+ "and will be removed in 0.21. Use 'accept_sparse=False' "
346
+ " instead." , DeprecationWarning )
347
+ accept_sparse = False
326
348
327
349
# store whether originally we wanted numeric dtype
328
350
dtype_numeric = dtype == "numeric"
@@ -406,7 +428,7 @@ def check_array(array, accept_sparse=None, dtype="numeric", order=None,
406
428
return array
407
429
408
430
409
- def check_X_y (X , y , accept_sparse = None , dtype = "numeric" , order = None ,
431
+ def check_X_y (X , y , accept_sparse = False , dtype = "numeric" , order = None ,
410
432
copy = False , force_all_finite = True , ensure_2d = True ,
411
433
allow_nd = False , multi_output = False , ensure_min_samples = 1 ,
412
434
ensure_min_features = 1 , y_numeric = False ,
@@ -427,11 +449,12 @@ def check_X_y(X, y, accept_sparse=None, dtype="numeric", order=None,
427
449
y : nd-array, list or sparse matrix
428
450
Labels.
429
451
430
- accept_sparse : string, list of string or None (default=None )
452
+ accept_sparse : string, boolean or list of string (default=False )
431
453
String[s] representing allowed sparse matrix formats, such as 'csc',
432
- 'csr', etc. None means that sparse matrix input will raise an error.
433
- If the input is sparse but not in the allowed format, it will be
434
- converted to the first listed format.
454
+ 'csr', etc. If the input is sparse but not in the allowed format,
455
+ it will be converted to the first listed format. True allows the input
456
+ to be any format. False means that a sparse matrix input will
457
+ raise an error.
435
458
436
459
dtype : string, type, list of types or None (default="numeric")
437
460
Data type of result. If None, the dtype of the input is preserved.
0 commit comments