Python的 - 如何找到两个向量之间的相关性? [英] Python - How to find a correlation between two vectors?

查看:2069
本文介绍了Python的 - 如何找到两个向量之间的相关性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于两个向量的 X 的,我必须要找到他们的相关性,即它们的线性相关性/独立性。这两个向量具有相同的尺寸。结果应该是从一个浮点数[-1.0 ... 1.0]。

Given two vectors X and Y, I have to find their correlation, i.e. their linear dependence/independence. Both vectors have equal dimension. The result should be a floating point number from [-1.0 .. 1.0].

例如:

X=[-1, 2,    0]
Y=[ 4, 2, -0.3]

找到的 Y = COR(X,Y)的这样的的属于的 [ - 1.0 ... 1.0]

Find y = cor(X,Y) such that y belongs to [-1.0 .. 1.0].

这应该是一个简单的结构,涉及的列表-COM prehension。无需外部库是允许的。

It should be a simple construction involving a list-comprehension. No external library is allowed.

更新:好吧,如果点积是足够的,那么这里是我的解决方案:

UPDATE: ok, if the 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) ])

吧?

推荐答案

听起来像一个给我。

解方程两个向量夹角的余弦值,它总是在区间[1,1],你就会有你想要的。

Solve the equation for the cosine of the angle between the two vectors, which is always in the range [-1, 1], and you'll have what you want.

它等于该点积由两个矢量的量值除以

It's equal to the dot product divided by the magnitudes of two vectors.

这篇关于Python的 - 如何找到两个向量之间的相关性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆