8000 [MRG] Adapts cdist jaccard to scipy 1.2.0 by thomasjpfan · Pull Request #12692 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] Adapts cdist jaccard to scipy 1.2.0 #12692

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 8 commits into from
Nov 30, 2018
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
16 changes: 16 additions & 0 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ Version 0.20.2
This is a bug-fix release with some minor documentation improvements and
enhancements to features released in 0.20.0.

Changed models
--------------

The following estimators and functions, when fit with the same data and
parameters, may produce different models from the previous version. This often
occurs due to changes in the modelling logic (bug fixes or enhancements), or in
random sampling procedures.

- :mod:`sklearn.neighbors` when ``metric=='jaccard'`` (bug fix)

Changelog
---------

Expand All @@ -22,6 +32,12 @@ Changelog
parameter may not have been updated correctly when a step is set to ``None``
or ``'passthrough'``. :user:`Thomas Fan <thomasjpfan>`.

:mod:`sklearn.neighbors`
........................

- |Fix| Fixed :class:`sklearn.neighbors.DistanceMetric` jaccard distance
function to return 0 when two all-zero vectors are compared.
:issue:`12685` by :user:`Thomas Fan <thomasjpfan>`.

.. _changes_0_20_1:

Expand Down
5 changes: 5 additions & 0 deletions sklearn/neighbors/dist_metrics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,11 @@ cdef class JaccardDistance(DistanceMetric):
tf2 = x2[j] != 0
nnz += (tf1 or tf2)
n_eq += (tf1 and tf2)
# Based on https://github.com/scipy/scipy/pull/7373
# When comparing two all-zero vectors, scipy>=1.2.0 jaccard metric
# was changed to return 0, instead of nan.
if nnz == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some explanations and point to the scipy issue

return 0
return (nnz - n_eq) * 1.0 / nnz


Expand Down
7 changes: 7 additions & 0 deletions sklearn/neighbors/tests/test_dist_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import pytest

from distutils.version import LooseVersion
from scipy import __version__ as scipy_version
from scipy.spatial.distance import cdist
from sklearn.neighbors.dist_metrics import DistanceMetric
from sklearn.neighbors import BallTree
Expand Down Expand Up @@ -101,6 +103,11 @@ def check_pdist(metric, kwargs, D_true):
def check_pdist_bool(metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(X1_bool)
# Based on https://github.com/scipy/scipy/pull/7373
# When comparing two all-zero vectors, scipy>=1.2.0 jaccard metric
# was changed to return 0, instead of nan.
if metric == 'jaccard' and LooseVersion(scipy_version) < '1.2.0':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some explanations and point to the scipy issue

D_true[np.isnan(D_true)] = 0
assert_array_almost_equal(D12, D_true)


Expand Down
0