Python - How to find a correlation between two vectors ?
Posted
by psihodelia
on Stack Overflow
See other posts from Stack Overflow
or by psihodelia
Published on 2010-06-15T12:25:20Z
Indexed on
2010/06/15
12:42 UTC
Read the original article
Hit count: 286
Given two vectors X and Y I have to find their correlation, i.e. their linear dependence/independence. Both vectors have equal dimension. A resulted answer should be a floating point number from [-1.0 .. 1.0].
Example:
X=[-1, 2, 0]
Y=[ 4, 2, -0.3]
Find y=cor(X,Y) such that y belongs to [-1.0 .. 1.0].
It should be a simple construction involving a list-comprehension. No external library is allowed.
UPDATE: ok, if dot product is enough, then here is my solution:
nX = 1/(sum([x*x for x in X]) ** 0.5)
nY = 1/(sum([y*y for y in Y]) ** 0.5)
cor = sum([(x*nX)*(y*nY) for x,y in zip(X,Y) ])
right?
© Stack Overflow or respective owner