Skip to content

Instantly share code, notes, and snippets.



Last active Mar 1, 2021
What would you like to do?
Dice coefficient between two boolean NumPy arrays or array-like data. This is commonly used as a set similarity measurement (though note it is not a true metric; it does not satisfy the triangle inequality). The dimensionality of the input is completely arbitrary, but `im1.shape` and `im2.shape` much be equal. This Gist is licensed under the mod…
""" : Dice coefficient for comparing set similarity.
import numpy as np
def dice(im1, im2):
Computes the Dice coefficient, a measure of set similarity.
im1 : array-like, bool
Any array of arbitrary size. If not boolean, will be converted.
im2 : array-like, bool
Any other array of identical size. If not boolean, will be converted.
dice : float
Dice coefficient as a float on range [0,1].
Maximum similarity = 1
No similarity = 0
The order of inputs for `dice` is irrelevant. The result will be
identical if `im1` and `im2` are switched.
im1 = np.asarray(im1).astype(np.bool)
im2 = np.asarray(im2).astype(np.bool)
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
return 2. * intersection.sum() / (im1.sum() + im2.sum())

This comment has been minimized.

Copy link

@brunodoamaral brunodoamaral commented Jul 25, 2016

The value of this function is undefined if im1.sum() + im2.sum() is zero.

I forked a simple solution to that, based on a previous definition of what this function should return in case of both inputs being zero:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment