Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. """
  2. _dice.py : Dice coefficient for comparing set similarity.
  3.  
  4. """
  5.  
  6. import numpy as np
  7.  
  8.  
  9. def dice(im1, im2):
  10. """
  11. Computes the Dice coefficient, a measure of set similarity.
  12.  
  13. Parameters
  14. ----------
  15. im1 : array-like, bool
  16. Any array of arbitrary size. If not boolean, will be converted.
  17. im2 : array-like, bool
  18. Any other array of identical size. If not boolean, will be converted.
  19.  
  20. Returns
  21. -------
  22. dice : float
  23. Dice coefficient as a float on range [0,1].
  24. Maximum similarity = 1
  25. No similarity = 0
  26.  
  27. Notes
  28. -----
  29. The order of inputs for `dice` is irrelevant. The result will be
  30. identical if `im1` and `im2` are switched.
  31.  
  32. """
  33. im1 = np.asarray(im1).astype(np.bool)
  34. im2 = np.asarray(im2).astype(np.bool)
  35.  
  36. if im1.shape != im2.shape:
  37. raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
  38.  
  39. # Compute Dice coefficient
  40. intersection = np.logical_and(im1, im2)
  41.  
  42. return 2. * intersection.sum() / (im1.sum() + im2.sum())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement