如何解决python中的加权相关
def m(x,w):
"""Weighted Mean"""
return np.sum(x * w) / np.sum(w)
def cov(x,y,w):
"""Weighted Covariance"""
return np.sum(w * (x - m(x,w)) * (y - m(y,w))) / np.sum(w)
def corr(x,w):
"""Weighted Correlation"""
return cov(x,w) / np.sqrt(cov(x,x,w) * cov(y,w))
这是我正在使用的数据
x = np.arange(78)
y = np.arange(21)
w = np.random.random((21,78)) # this is just an example matrix
# the key is to have a weight matrix that is of size (y.shape,x.shape)
现在,当我尝试计算加权相关时,我遇到了ValueError: operands Could not be broadcast together with shapes
。关于如何修改脚本以使其可用于这样的数据集的任何建议?
作为参考,我正在尝试实现标题Bayesian Replay Analysis
http://www.buzsakilab.com/content/PDFs/Grosmark2016Supp.pdf
解决方法
根据您提到的帖子中提供的Wikipedia链接(请参阅https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#Weighted_correlation_coefficient),向量x
和y
的大小应该相同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。