Skip to content

Instantly share code, notes, and snippets.

@raulsenaferreira
Forked from jstadler/bhattacharyya
Created February 19, 2017 14:00
Show Gist options
  • Save raulsenaferreira/6f8140f2af47f1b496c4dc7384fe091a to your computer and use it in GitHub Desktop.
Save raulsenaferreira/6f8140f2af47f1b496c4dc7384fe091a to your computer and use it in GitHub Desktop.
Implementation of the Bhattacharyya distance in Python
# bhattacharyya test
import numpy
import math
h1 = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
h2 = [ 6, 5, 4, 3, 2, 1, 0, 0 ];
h3 = [ 8, 7, 6, 5, 4, 3, 2, 1 ];
h4 = [ 1, 2, 3, 4, 4, 3, 2, 1 ];
h5 = [ 8, 8, 8, 8, 8, 8, 8, 8 ];
h = [ h1, h2, h3, h4, h5 ];
def mean( hist ):
mean = 0.0;
for i in hist:
mean += i;
mean/= len(hist);
return mean;
def bhatta ( hist1, hist2):
# calculate mean of hist1
h1_ = mean(hist1);
# calculate mean of hist2
h2_ = mean(hist2);
# calculate score
score = 0;
for i in range(8):
score += math.sqrt( hist1[i] * hist2[i] );
# print h1_,h2_,score;
score = math.sqrt( 1 - ( 1 / math.sqrt(h1_*h2_*8*8) ) * score );
return score;
# generate and output scores
scores = [];
for i in range(len(h)):
score = [];
for j in range(len(h)):
score.append( bhatta(h[i],h[j]) );
scores.append(score);
for i in scores:
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment