Last active
July 26, 2016 08:25
-
-
Save askeing/51c1fa42ccdfb332f40aaee3b303c0bb to your computer and use it in GitHub Desktop.
correlation coefficient and cosine similarity
This file contains 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
import numpy | |
def corr_percentage(a, b): | |
# Ref: | |
# - https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient | |
# - http://docs.scipy.org/doc/numpy/reference/generated/numpy.corrcoef.html | |
#return 0.5 * numpy.corrcoef(a,b)[0][1] + 0.5 | |
return numpy.abs(numpy.corrcoef(a,b)[0][1]) | |
def cosine_similarity(a, b): | |
# Ref: | |
# - https://en.wikipedia.org/wiki/Cosine_similarity | |
ma = numpy.matrix(a, copy=False) | |
mb = numpy.matrix(b, copy=False) | |
#return 0.5 * (float(ma*mb.T)/(numpy.linalg.norm(ma)*numpy.linalg.norm(mb))) + 0.5 | |
return numpy.abs(float(ma*mb.T)/(numpy.linalg.norm(ma)*numpy.linalg.norm(mb))) | |
a = [1,2,3,4,5] | |
b = [1,3,4,4,5] | |
corr_percentage(a, b) | |
cosine_similarity(a, b) |
maybe change return value of corr_percentage()
return numpy.abs(numpy.corrcoef(a,b)[0][1])
abs the negative correlation?
maybe the cosine_similarity() also have to do the same thing...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added
cosine_similarity()