Skip to content

ElasticNet does not support sparse matrices #26114

Closed
@Darel13712

Description

@Darel13712

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions