Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ Changelog
- |Efficiency| :class:`cluster.MiniBatchKMeans` is now faster in multicore
settings. :pr:`17622` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

- |Enhancement| The `predict` and `fit_predict` methods of
:class:`cluster.AffinityPropagation` now accept sparse data type for input
data.
:pr:`20117` by :user:`Venkatachalam Natchiappan <venkyyuvy>`

- |Fix| Fixed a bug in :class:`cluster.MiniBatchKMeans` where the sample
weights were partially ignored when the input is sparse. :pr:`17622` by
:user:`Jérémie du Boisberranger <jeremiedbb>`.
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def predict(self, X):
Cluster labels.
"""
check_is_fitted(self)
X = self._validate_data(X, reset=False)
X = self._validate_data(X, reset=False, accept_sparse='csr')
if not hasattr(self, "cluster_centers_"):
raise ValueError("Predict method is not supported when "
"affinity='precomputed'.")
Expand Down
19 changes: 19 additions & 0 deletions sklearn/cluster/tests/test_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ def test_affinity_propagation_float32():
assert_array_equal(afp.labels_, expected)


def test_sparse_input_for_predict():
# Test to make sure sparse inputs are accepted for predict
# (non-regression test for issue #20049)
af = AffinityPropagation(affinity="euclidean", random_state=42)
af.fit(X)
labels = af.predict(csr_matrix((2, 2)))
assert_array_equal(labels, (2, 2))


def test_sparse_input_for_fit_predict():
# Test to make sure sparse inputs are accepted for fit_predict
# (non-regression test for issue #20049)
af = AffinityPropagation(affinity="euclidean", random_state=42)
rng = np.random.RandomState(42)
X = csr_matrix(rng.randint(0, 2, size=(5, 5)))
labels = af.fit_predict(X)
assert_array_equal(labels, (0, 1, 1, 2, 3))


# TODO: Remove in 1.1
def test_affinity_propagation_pairwise_is_deprecated():
afp = AffinityPropagation(affinity='precomputed')
Expand Down