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

Skip to content

MAINT Make ArgKminClassMode accept sparse datasets #27018

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
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.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Changelog
:mod:`sklearn.neighbors`
........................

- |Efficiency| :meth:`sklearn.neighbors.KNeighborsRegressor.predict` and
:meth:`sklearn.neighbors.KNeighborsRegressor.predict_proba` now efficiently support
pairs of dense and sparse datasets.
:pr:`27018` by :user:`Julien Jerphanion <jjerphan>`.

- |Fix| Neighbors based estimators now correctly work when `metric="minkowski"` and the
metric parameter `p` is in the range `0 < p < 1`, regardless of the `dtype` of `X`.
:pr:`26760` by :user:`Shreesha Kumar Bhat <Shreesha3112>`.
Expand Down
38 changes: 9 additions & 29 deletions sklearn/metrics/_pairwise_distances_reduction/_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,35 +451,15 @@ class ArgKminClassMode(BaseDistancesReductionDispatcher):
"""

@classmethod
def is_usable_for(cls, X, Y, metric) -> bool:
"""Return True if the dispatcher can be used for the given parameters.

Parameters
----------
X : ndarray of shape (n_samples_X, n_features)
The input array to be labelled.

Y : ndarray of shape (n_samples_Y, n_features)
The input array whose labels are provided through the `Y_labels`
parameter.

metric : str, default='euclidean'
The distance metric to use. For a list of available metrics, see
the documentation of :class:`~sklearn.metrics.DistanceMetric`.
Currently does not support `'precomputed'`.

Returns
-------
True if the PairwiseDistancesReduction can be used, else False.
"""
return (
ArgKmin.is_usable_for(X, Y, metric)
# TODO: Support CSR matrices.
and not issparse(X)
and not issparse(Y)
# TODO: implement Euclidean specialization with GEMM.
and metric not in ("euclidean", "sqeuclidean")
)
def valid_metrics(cls) -> List[str]:
excluded = {
# Euclidean is technically usable for ArgKminClassMode
# but its current implementation would not be competitive.
# TODO: implement Euclidean specialization using GEMM.
"euclidean",
"sqeuclidean",
}
return list(set(BaseDistancesReductionDispatcher.valid_metrics()) - excluded)

@classmethod
def compute(
Expand Down