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

Skip to content

[MRG+1] Add sample_weight parameter to metrics.jaccard_similarity_score #3497

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

Closed
wants to merge 6 commits into from
Closed
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.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Enhancements
descent for :class:`linear_model.Lasso`, :class:`linear_model.ElasticNet`
and related. By `Manoj Kumar`_.

- Add ``sample_weight`` parameter to `metrics.jaccard_similarity_score`.
By `Jatin Shah`.



Documentation improvements
..........................
Expand Down
14 changes: 11 additions & 3 deletions sklearn/metrics/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Lars Buitinck <[email protected]>
# Joel Nothman <[email protected]>
# Noel Dawe <[email protected]>
# Jatin Shah <[email protected]>
# License: BSD 3 clause

from __future__ import division
Expand Down Expand Up @@ -237,7 +238,8 @@ def confusion_matrix(y_true, y_pred, labels=None):
return CM


def jaccard_similarity_score(y_true, y_pred, normalize=True):
def jaccard_similarity_score(y_true, y_pred, normalize=True,
sample_weight=None):
"""Jaccard similarity coefficient score

The Jaccard index [1], or Jaccard similarity coefficient, defined as
Expand All @@ -258,6 +260,9 @@ def jaccard_similarity_score(y_true, y_pred, normalize=True):
over the sample set. Otherwise, return the average of Jaccard
similarity coefficient.

sample_weight : array-like of shape = [n_samples], optional
Sample weights.

Returns
-------
score : float
Expand Down Expand Up @@ -340,9 +345,12 @@ def jaccard_similarity_score(y_true, y_pred, normalize=True):
score = y_true == y_pred

if normalize:
return np.mean(score)
return np.average(score, weights=sample_weight)
else:
return np.sum(score)
if sample_weight is not None:
return np.dot(score, sample_weight)
else:
return np.sum(score)


def matthews_corrcoef(y_true, y_pred):
Expand Down
9 changes: 4 additions & 5 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@
"confusion_matrix",
"hamming_loss",
"hinge_loss",
"jaccard_similarity_score", "unnormalized_jaccard_similarity_score",
"log_loss",
"matthews_corrcoef_score",
]
Expand Down Expand Up @@ -904,10 +903,10 @@ def check_sample_weight_invariance(name, metric, y1, y2):

# check that unit weights gives the same score as no weight
unweighted_score = metric(y1, y2, sample_weight=None)
assert_equal(
assert_almost_equal(
unweighted_score,
metric(y1, y2, sample_weight=np.ones(shape=len(y1))),
msg="For %s sample_weight=None is not equivalent to "
err_msg="For %s sample_weight=None is not equivalent to "
"sample_weight=ones" % name)

# check that the weighted and unweighted scores are unequal
Expand All @@ -920,9 +919,9 @@ def check_sample_weight_invariance(name, metric, y1, y2):
# check that sample_weight can be a list
weighted_score_list = metric(y1, y2,
sample_weight=sample_weight.tolist())
assert_equal(
assert_almost_equal(
weighted_score, weighted_score_list,
msg="Weighted scores for array and list sample_weight input are "
err_msg="Weighted scores for array and list sample_weight input are "
"not equal (%f != %f) for %s" % (
weighted_score, weighted_score_list, name))

Expand Down