You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation says that I can use ElasticNet with ndarray, sparse matrix, but I can't make it work with sparse.
Steps/Code to Reproduce
from scipy.sparse import csr_matrix
from sklearn.linear_model import ElasticNet
import numpy as np
A = csr_matrix(np.array([[1,1,0,0,1],[0,0,1,0,1],[1,0,1,0,1],[0,1,1,1,0]]))
y = A[:, 0]
regr = ElasticNet()
regr.fit(A, y)
Expected Results
Works fine
Actual Results
Falls check in check_array with TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array. because accept_sparse is False by default for check_array.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 4
2 y = A[:, 0]
3 regr = ElasticNet()
----> 4 regr.fit(A, y)
File ~/.e/lib/python3.9/site-packages/sklearn/linear_model/_coordinate_descent.py:918, in ElasticNet.fit(self, X, y, sample_weight, check_input)
907 X_copied = self.copy_X and self.fit_intercept
908 X, y = self._validate_data(
909 X,
910 y,
(...)
916 y_numeric=True,
917 )
--> 918 y = check_array(
919 y, order="F", copy=False, dtype=X.dtype.type, ensure_2d=False
920 )
922 n_samples, n_features = X.shape
923 alpha = self.alpha
File ~/.e/lib/python3.9/site-packages/sklearn/utils/validation.py:845, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
843 if sp.issparse(array):
844 _ensure_no_complex_data(array)
--> 845 array = _ensure_sparse_format(
846 array,
847 accept_sparse=accept_sparse,
848 dtype=dtype,
849 copy=copy,
850 force_all_finite=force_all_finite,
851 accept_large_sparse=accept_large_sparse,
852 estimator_name=estimator_name,
853 input_name=input_name,
854 )
855 else:
856 # If np.array(..) gives ComplexWarning, then we convert the warning
857 # to an error. This is needed because specifying a non complex
858 # dtype to the function converts complex to real dtype,
859 # thereby passing the test made in the lines following the scope
860 # of warnings context manager.
861 with warnings.catch_warnings():
File ~/.e/lib/python3.9/site-packages/sklearn/utils/validation.py:522, in _ensure_sparse_format(spmatrix, accept_sparse, dtype, copy, force_all_finite, accept_large_sparse, estimator_name, input_name)
519 _check_large_sparse(spmatrix, accept_large_sparse)
521 if accept_sparse is False:
--> 522 raise TypeError(
523 "A sparse matrix was passed, but dense "
524 "data is required. Use X.toarray() to "
525 "convert to a dense numpy array."
526 )
527 elif isinstance(accept_sparse, (list, tuple)):
528 if len(accept_sparse) == 0:
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
Describe the bug
Documentation says that I can use ElasticNet with
ndarray, sparse matrix
, but I can't make it work with sparse.Steps/Code to Reproduce
Expected Results
Works fine
Actual Results
Falls check in
check_array
withTypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
becauseaccept_sparse
isFalse
by default forcheck_array
.Versions
The text was updated successfully, but these errors were encountered: