8000 ENH Introduce `PairwiseDistancesReduction` and `PairwiseDistancesArgKmin` (feature branch) by jjerphan · Pull Request #22134 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH Introduce PairwiseDistancesReduction and PairwiseDistancesArgKmin (feature branch) #22134

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 12 commits into from
Feb 17, 2022
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
31 changes: 30 additions & 1 deletion doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ Changelog
:pr:`123456` by :user:`Joe Bloggs <joeongithub>`.
where 123456 is the *pull request* number, not the issue number.

- |Efficiency| Low-level routines for reductions on pairwise distances
for dense float64 datasets have been refactored. The following functions
and estimators now benefit from improved performances, in particular on
multi-cores machines:
- :func:`sklearn.metrics.pairwise_distances_argmin`
- :func:`sklearn.metrics.pairwise_distances_argmin_min`
- :class:`sklearn.cluster.AffinityPropagation`
- :class:`sklearn.cluster.Birch`
- :class:`sklearn.cluster.MeanShift`
- :class:`sklearn.cluster.OPTICS`
- :class:`sklearn.cluster.SpectralClustering`
- :func:`sklearn.feature_selection.mutual_info_regression`
- :class:`sklearn.neighbors.KNeighborsClassifier`
- :class:`sklearn.neighbors.KNeighborsRegressor`
- :class:`sklearn.neighbors.LocalOutlierFactor`
- :class:`sklearn.neighbors.NearestNeighbors`
- :class:`sklearn.manifold.Isomap`
- :class:`sklearn.manifold.LocallyLinearEmbedding`
- :class:`sklearn.manifold.TSNE`
- :func:`sklearn.manifold.trustworthiness`
- :class:`sklearn.semi_supervised.LabelPropagation`
- :class:`sklearn.semi_supervised.LabelSpreading`

For instance :class:`sklearn.neighbors.NearestNeighbors.kneighbors`
can be up to ×20 faster than in the previous versions'.

:pr:`21987`, :pr:`22064`, :pr:`22065` and :pr:`22288`
by :user:`Julien Jerphanion <jjerphan>`

- |Enhancement| All scikit-learn models now generate a more informative
error message when some input contains unexpected `NaN` or infinite values.
In particular the message contains the input name ("X", "y" or
Expand Down Expand Up @@ -459,7 +488,7 @@ Changelog
now raise consistent error messages when passed invalid values for `l1_ratio`,
`alpha`, `max_iter` and `tol`.
:pr:`22240` by :user:`Arturo Amor <ArturoAmorQ>`.

- |Fix| :class:`linear_model.LogisticRegression` and
:class:`linear_model.LogisticRegressionCV` now set the `n_iter_` attribute
with a shape that respects the docstring and that is consistent with the shape
Expand Down
65 changes: 63 additions & 2 deletions sklearn/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"working_memory": int(os.environ.get("SKLEARN_WORKING_MEMORY", 1024)),
"print_changed_only": True,
"display": "text",
"pairwise_dist_chunk_size": int(
os.environ.get("SKLEARN_PAIRWISE_DIST_CHUNK_SIZE", 256)
),
"enable_cython_pairwise_dist": True,
}
_threadlocal = threading.local()

Expand Down Expand Up @@ -40,7 +44,12 @@ def get_config():


def set_config(
assume_finite=None, working_memory=None, print_changed_only=None, display=None
assume_finite=None,
working_memory=None,
print_changed_only=None,
display=None,
pairwise_dist_chunk_size=None,
enable_cython_pairwise_dist=None,
):
"""Set global scikit-learn configuration

Expand Down Expand Up @@ -80,6 +89,26 @@ def set_config(

.. versionadded:: 0.23

pairwise_dist_chunk_size : int, default=None
The number of row vectors per chunk for PairwiseDistancesReduction.
Default is 256 (suitable for most of modern laptops' caches and architectures).

Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.

.. versionadded:: 1.1

enable_cython_pairwise_dist : bool, default=None
Use PairwiseDistancesReduction when possible.
Default is True.

Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.

.. versionadded:: 1.1

See Also
--------
config_context : Context manager for global scikit-learn configuration.
Expand All @@ -95,11 +124,21 @@ def set_config(
local_config["print_changed_only"] = print_changed_only
if display is not None:
local_config["display"] = display
if pairwise_dist_chunk_size is not None:
local_config["pairwise_dist_chunk_size"] = pairwise_dist_chunk_size
if enable_cython_pairwise_dist is not None:
local_config["enable_cython_pairwise_dist"] = enable_cython_pairwise_dist


@contextmanager
def config_context(
*, assume_finite=None, working_memory=None, print_changed_only=None, display=None
*,
assume_finite=None,
working_memory=None,
print_changed_only=None,
display=None,
pairwise_dist_chunk_size=None,
enable_cython_pairwise_dist=None,
):
"""Context manager for global scikit-learn configuration.

Expand Down Expand Up @@ -138,6 +177,26 @@ def config_context(

.. versionadded:: 0.23

pairwise_dist_chunk_size : int, default=None
The number of vectors per chunk for PairwiseDistancesReduction.
Default is 256 (suitable for most of modern laptops' caches and architectures).

Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.

.. versionadded:: 1.1

enable_cython_pairwise_dist : bool, default=None
Use PairwiseDistancesReduction when possible.
Default is True.

Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.

.. versionadded:: 1.1

Yields
------
None.
Expand Down Expand Up @@ -171,6 +230,8 @@ def config_context(
working_memory=working_memory,
print_changed_only=print_changed_only,
display=display,
pairwise_dist_chunk_size=pairwise_dist_chunk_size,
enable_cython_pairwise_dist=enable_cython_pairwise_dist,
)

try:
Expand Down
21 changes: 21 additions & 0 deletions sklearn/metrics/_dist_metrics.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ cdef class DistanceMetric:
cdef DTYPE_t _rdist_to_dist(self, DTYPE_t rdist) nogil except -1

cdef DTYPE_t _dist_to_rdist(self, DTYPE_t dist) nogil except -1


######################################################################
# DatasetsPair base class
cdef class DatasetsPair:
cdef DistanceMetric distance_metric

cdef ITYPE_t n_samples_X(self) nogil

cdef ITYPE_t n_samples_Y(self) nogil

cdef DTYPE_t dist(self, ITYPE_t i, IT 23D3 YPE_t j) nogil

cdef DTYPE_t surrogate_dist(self, ITYPE_t i, ITYPE_t j) nogil


cdef class DenseDenseDatasetsPair(DatasetsPair):
cdef:
const DTYPE_t[:, ::1] X
const DTYPE_t[:, ::1] Y
ITYPE_t d
Loading
0