Skip to content

Instantly share code, notes, and snippets.

@JDWarner
Last active January 25, 2023 22:28
Show Gist options
  • Save JDWarner/6730747 to your computer and use it in GitHub Desktop.
Save JDWarner/6730747 to your computer and use it in GitHub Desktop.
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.py : Dice coefficient for comparing set similarity.
"""
import numpy as np
def dice(im1, im2):
"""
Computes the Dice coefficient, a measure of set similarity.
Parameters
----------
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.
Returns
-------
dice : float
Dice coefficient as a float on range [0,1].
Maximum similarity = 1
No similarity = 0
Notes
-----
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())
@brunodoamaral
Copy link

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: https://gist.github.com/brunodoamaral/e130b4e97aa4ebc468225b7ce39b3137

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