-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
Closed
Labels
EasyWell-defined and straightforward way to resolveWell-defined and straightforward way to resolve
Description
Describe the bug
AffinityPropagation.predict should work with a sparse X according to the documentation but the code throws an exception if a sparse X is passed. Apologies if this is a non-issue.
Steps/Code to Reproduce
AffinityPropagation.predict with a sparse X throws an exception:
Python 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],[4, 2], [4, 4], [4, 0]])
>>> from sklearn.cluster import AffinityPropagation
>>> clustering = AffinityPropagation(random_state=5).fit(X)
>>> import scipy.sparse
>>> clustering.predict(scipy.sparse.csr_matrix([[0, 0], [4, 4]]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/cluster/_affinity_propagation.py", line 451, in predict
X = self._validate_data(X, reset=False)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/base.py", line 421, in _validate_data
X = check_array(X, **check_params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/utils/validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/utils/validation.py", line 593, in check_array
array = _ensure_sparse_format(array, accept_sparse=accept_sparse,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/utils/validation.py", line 360, in _ensure_sparse_format
raise TypeError('A sparse matrix was passed, but dense '
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
Expected Results
If I change line 451 in sklearn/cluster/_affinity_propagation.py from
X = self._validate_data(X, reset=False)
to
X = self._validate_data(X, reset=False,accept_sparse='csr')
then it works:
Python 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],[4, 2], [4, 4], [4, 0]])
>>> from sklearn.cluster import AffinityPropagation
>>> clustering = AffinityPropagation(random_state=5).fit(X)
>>> import scipy.sparse
>>> clustering.predict(scipy.sparse.csr_matrix([[0, 0], [4, 4]]))
array([0, 1])
Actual Results
The exception above
Versions
System:
python: 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10) [Clang 6.0 (clang-600.0.57)]
executable: /usr/local/bin/python3.9
machine: macOS-10.11.6-x86_64-i386-64bit
Python dependencies:
pip: 20.2.3
setuptools: 49.2.1
sklearn: 0.24.1
numpy: 1.20.1
scipy: 1.6.1
Cython: None
pandas: None
matplotlib: None
joblib: 1.0.1
threadpoolctl: 2.1.0
Built with OpenMP: False
Metadata
Metadata
Assignees
Labels
EasyWell-defined and straightforward way to resolveWell-defined and straightforward way to resolve