Skip to content

ElasticNet does not support sparse matrices #26114

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
Darel13712 opened this issue Apr 6, 2023 · 2 comments · Fixed by #26127
Closed

ElasticNet does not support sparse matrices #26114

Darel13712 opened this issue Apr 6, 2023 · 2 comments · Fixed by #26127

Comments

@Darel13712
Copy link

Darel13712 commented Apr 6, 2023

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

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.

Versions

System:
    python: 3.9.12 (main, Apr  5 2022, 06:56:58)  [GCC 7.5.0]
executable: /gpfs/space/home/yanmart/.e/bin/python
   machine: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17

Python dependencies:
      sklearn: 1.2.2
          pip: 23.0.1
   setuptools: 65.3.0
        numpy: 1.24.2
        scipy: 1.10.1
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /gpfs/space/home/yanmart/.e/lib/python3.9/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so
        version: 0.3.21
threading_layer: pthreads
   architecture: Zen
    num_threads: 2

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /gpfs/space/home/yanmart/.e/lib/python3.9/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Zen
    num_threads: 2

       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /gpfs/space/home/yanmart/.e/lib/python3.9/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 2
@Darel13712 Darel13712 added Bug Needs Triage Issue requires triage labels Apr 6, 2023
@glemaitre
Copy link
Member

This is a mistake in the documentation. Here the problem is that y is sparse. We accept X to be sparse:

In [3]: 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.A)
Out[3]: ElasticNet()

We should update the documentation and remove the sparse matrix option for y.

@glemaitre glemaitre added Documentation and removed Bug Needs Triage Issue requires triage labels Apr 7, 2023
@rprkh
Copy link
Contributor

rprkh commented Apr 7, 2023

Working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants