Skip to content

Instantly share code, notes, and snippets.

@askeing
Last active July 26, 2016 08:25
Show Gist options
  • Save askeing/51c1fa42ccdfb332f40aaee3b303c0bb to your computer and use it in GitHub Desktop.
Save askeing/51c1fa42ccdfb332f40aaee3b303c0bb to your computer and use it in GitHub Desktop.
correlation coefficient and cosine similarity
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)
@askeing
Copy link
Author

askeing commented Jul 25, 2016

Added cosine_similarity()

@MikeLien
Copy link

MikeLien commented Jul 26, 2016

maybe change return value of corr_percentage()

return numpy.abs(numpy.corrcoef(a,b)[0][1])

@askeing
Copy link
Author

askeing commented Jul 26, 2016

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