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

Skip to content

OPTIM use pairwise_distances_argmin in NearestCentroid.predict #24645

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

Merged
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
4 changes: 4 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ Changelog
:pr:`10468` by :user:`Ruben <icfly2>` and :pr:`22993` by
:user:`Jovan Stojanovic <jovan-stojanovic>`.

- |Efficiency| :class:`neighbors.NearestCentroid` is faster and requires
less memory as it better leverages CPUs' caches to compute predictions.
:pr:`24645` by :user:`Olivier Grisel <ogrisel>`.

- |Feature| Adds new function :func:`neighbors.sort_graph_by_row_values` to
sort a CSR sparse graph such that each row is stored with increasing values.
This is useful to improve efficiency when using precomputed sparse distance
Expand Down
4 changes: 2 additions & 2 deletions sklearn/neighbors/_nearest_centroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scipy import sparse as sp

from ..base import BaseEstimator, ClassifierMixin
from ..metrics.pairwise import pairwise_distances
from ..metrics.pairwise import pairwise_distances_argmin
from ..preprocessing import LabelEncoder
from ..utils.validation import check_is_fitted
from ..utils.sparsefuncs import csc_median_axis_0
Expand Down Expand Up @@ -234,5 +234,5 @@ def predict(self, X):

X = self._validate_data(X, accept_sparse="csr", reset=False)
return self.classes_[
pairwise_distances(X, self.centroids_, metric=self.metric).argmin(axis=1)
pairwise_distances_argmin(X, self.centroids_, metric=self.metric)
]