Created
January 22, 2010 18:21
-
-
Save xoebus/283997 to your computer and use it in GitHub Desktop.
Diatance between Feature Vectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inf2B | |
Guardian = (6,4,1,6,6,2,4) | |
Times = (8,4,4,6,6,6,8) | |
Telegraph = (10,4,6,6,6,2,9) | |
Independent = (8,4,3,4,3,3,6) | |
def dist(vec1, vec2): | |
vectors = zip(vec1, vec2) | |
total = 0 | |
for (e1,e2) in vectors: | |
total += (e1 - e2) ** 2 | |
return total ** 0.5 | |
def m(vector): | |
return ((1/float(len(vector))) * sum(vector)) | |
def s(vector): | |
n = float(len(vector)) | |
m_value = m(vector) | |
total = 0 | |
for element in vector: | |
total += (element - m_value) ** 2 | |
return ((1/(n-1)) * total) ** 0.5 | |
def p(vec1, vec2): | |
n = float(len(vec1)) | |
m_x = m(vec1) | |
m_y = m(vec2) | |
total = 0 | |
for (ele1, ele2) in zip(vec1, vec2): | |
total += ((ele1 - m_x) / s(vec1)) * ((ele2 - m_y) / s(vec2)) | |
return (1/(n-1)) * total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment