{ } Raw JSON

bundles / scipy latest / scipy / spatial / distance / jaccard

function

scipy.spatial.distance:jaccard

source: /scipy/spatial/distance.py :778

Signature

def   jaccard ( u v w = None )

Summary

Compute the Jaccard dissimilarity between two boolean vectors.

Extended Summary

Given boolean vectors and that are not both zero, their Jaccard dissimilarity is defined as ([1], p. 26)

where

for . If and are both zero, their Jaccard dissimilarity is defined to be zero. [2]

If a (non-negative) weight vector is supplied, the weighted Jaccard dissimilarity is defined similarly but with replaced by

Parameters

u : (N,) array_like of bools

Input vector.

v : (N,) array_like of bools

Input vector.

w : (N,) array_like of floats, optional

Weights for each pair of . Default is None, which gives each pair a weight of 1.0.

Returns

jaccard : float

The Jaccard dissimilarity between vectors u and v, optionally weighted by w if supplied.

Notes

The Jaccard dissimilarity satisfies the triangle inequality and is qualified as a metric. [2]

The Jaccard index, or Jaccard similarity coefficient, is equal to one minus the Jaccard dissimilarity. [3]

The dissimilarity between general (finite) sets may be computed by encoding them as boolean vectors and computing the dissimilarity between the encoded vectors. For example, subsets of may be encoded into boolean vectors by setting , for .

Examples

from scipy.spatial import distance
Non-zero vectors with no matching 1s have dissimilarity of 1.0:
distance.jaccard([1, 0, 0], [0, 1, 0])
Vectors with some matching 1s have dissimilarity less than 1.0:
distance.jaccard([1, 0, 0, 0], [1, 1, 1, 0])
Identical vectors, including zero vectors, have dissimilarity of 0.0:
distance.jaccard([1, 0, 0], [1, 0, 0])
distance.jaccard([0, 0, 0], [0, 0, 0])
The following example computes the dissimilarity from a confusion matrix directly by setting the weight vector to the frequency of True Positive, False Negative, False Positive, and True Negative:
distance.jaccard([1, 1, 0, 0], [1, 0, 1, 0], [31, 41, 59, 26])

Aliases

  • scipy.spatial.distance.jaccard

Referenced by

This package