[英]How to get correlation for a rolling pandas series and a fixed series?
I want to calculate corr between two series.我想计算两个系列之间的 corr。 I defined them as:
我将它们定义为:
s = pd.Series([1,2,3,4,5,6,7])
s2 = pd.Series([2,3,4])
what i want is a correlation series, that the values is the corr between s.rolling(3) with s2我想要的是一个相关系列,该值是 s.rolling(3) 与 s2 之间的 corr
For example: the first element of result should be [1,2,3].corr(s2), the second should be [2,3,4].corr(s2), .....例如:结果的第一个元素应该是 [1,2,3].corr(s2),第二个应该是 [2,3,4].corr(s2), .....
I read the usage example of rolling, but i think it inner method rolling(3).corr cant solve this, is there any good methods to do this?我阅读了rolling的用法示例,但我认为内部方法rolling(3).corr无法解决这个问题,有没有什么好的方法可以做到这一点?
Why not just use a for loop?为什么不直接使用 for 循环?
import pandas as pd
s = pd.Series([1,2,3,4,5,6,7])
s2 = pd.Series([2,3,4])
window_size = len(s2)
output = []
for i in range(len(s)-window_size):
output.append(s[i:window_size+i].corr(s2))
Can you do a rolling apply:你能做一个滚动申请吗:
from scipy.stats import pearsonr
s.rolling(3).apply(lambda x: pearsonr(x,s2)[0])
Output:输出:
0 NaN
1 NaN
2 1.0
3 1.0
4 1.0
5 1.0
6 1.0
dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.