From d4663c80c4d1287c4fffd028d6cc25b5a14a8dab Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Wed, 12 Oct 2022 19:06:09 +0200 Subject: [PATCH 1/3] OPTIM use pairwise_distances_argmin in NearestCentroid.predict --- sklearn/neighbors/_nearest_centroid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/neighbors/_nearest_centroid.py b/sklearn/neighbors/_nearest_centroid.py index 653662350b38f..4e5ce354cc257 100644 --- a/sklearn/neighbors/_nearest_centroid.py +++ b/sklearn/neighbors/_nearest_centroid.py @@ -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 @@ -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) ] From 9b2a5482f64803cf274f0a774dca366e68a7e9b6 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Wed, 12 Oct 2022 19:19:36 +0200 Subject: [PATCH 2/3] Changlog --- doc/whats_new/v1.2.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 6c0e7f559429e..0700561b1f4d0 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -456,6 +456,10 @@ Changelog :pr:`10468` by :user:`Ruben ` and :pr:`22993` by :user:`Jovan Stojanovic `. +- |Efficiency| :class:`neighbors.NearestCentroid` requires less memory and better + leverages the CPU cache to compute predictions. + :pr:`24645` by :user:`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 From 21ce8eecd5de59a8f203d2450f522439424689df Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Thu, 13 Oct 2022 17:58:04 +0200 Subject: [PATCH 3/3] Update doc/whats_new/v1.2.rst Co-authored-by: Julien Jerphanion --- doc/whats_new/v1.2.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 0700561b1f4d0..cb9246f11c4fc 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -456,9 +456,9 @@ Changelog :pr:`10468` by :user:`Ruben ` and :pr:`22993` by :user:`Jovan Stojanovic `. -- |Efficiency| :class:`neighbors.NearestCentroid` requires less memory and better - leverages the CPU cache to compute predictions. - :pr:`24645` by :user:`ogrisel`. +- |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 `. - |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.