我使用大型数据表,在这些数据表中,我试图将所有列关联起来。
我使用以下方法实现这一目标:
df = df.rolling(5).corr(pairwise = True)这会产生这样的数据:
477
s1 -0.240339 0.932141 1.000000 0.577741 0.718307 -0.518748 0.772099
s2 0.534848 0.626280 0.577741 1.000000 0.645064 -0.455503 0.447589
s3 0.384720 0.907782 0.718307 0.645064 1.000000 -0.831378 0.406054
s4 -0.347547 -0.651557 -0.518748 -0.455503 -0.831378 1.000000 -0.569301
s5 -0.315022 0.576705 0.772099 0.447589 0.406054 -0.569301 1.000000 对于数据集中包含的每一行。在本例中,行号或索引为477,列标题为s1 - s5。
目的是找出传感器之间的高度相关性。我希望通过以下方法实现这一点:(a)使用上面的代码使用5行滚动窗口计算相关性;(b)对于生成的每一行,即对于500行excel表,i =0至I= 500,将dataframe.rolling(5).corr()为i的每个值生成的表之和,即在每单位时间产生一个值,例如在底部包含的图表中。我是新的堆叠溢出,所以请告诉我,如果有更多的信息,我可以提供。
示例代码+数据:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
d = {'col1': [-2587.944231, -1897.324231,-2510.304231,-2203.814231,-2105.734231,-2446.964231,-2963.904231,-2177.254231, 2796.354231,-2085.304231], 'col2': [-3764.468462,-3723.608462,-3750.168462,-3694.998462,-3991.268462,-3972.878462,3676.608462,-3827.808462,-3629.618462,-1841.758462,], 'col3': [-166.1357692,-35.36576923, 321.4157692,108.9257692,-123.2257692, -10.84576923, -100.7457692, 89.27423077, -211.0857692, 101.5342308]}
df = pd.DataFrame(data=d)
dfn = df.rolling(5).corr(pairwise = True)完成我想要的东西的MATLAB代码:
% move through the data and get a correlation for 5 data points
for i=1:ns-4 C(:,:,i)=corrcoef(X(i:i+4,:));
cact(i)=sum(C(:,:,i),'all')-nv; % subtracting nv removes the diagaonals that are = 1 and dont change
end对于原始数据,下面是我试图用Python生成的图形,其中x轴是时间:相关图
发布于 2021-05-31 14:21:44
在两个方向上对整个表进行求和,并减去1的对角线,即与自身相关的传感器。
使用dfn第四行是
>>> dfn.loc[4]
col1 col2 col3
col1 1.000000 -0.146977 -0.227059
col2 -0.146977 1.000000 0.435216
col3 -0.227059 0.435216 1.000000可以在基础数据上使用Numpy的ndarray.sum()对完整的表进行求和
>>> dfn.loc[4].to_numpy().sum()
3.1223603416753103然后假设相关表是平方的,您只需要减去列/传感器的数量。如果还没有变量,则可以使用基础numpy数组的形状。
>>> v = dfn.loc[4].to_numpy()
>>> v.shape
(3, 3)
>>> v.sum() - v.shape[0]
0.12236034167531029
>>>在不使用numpy数组的情况下,可以在减去之前对关联表进行两次求和。
>>> four = dfn.loc[4]
>>> four.sum().sum()
3.1223603416753103
>>> four.sum().sum() - four.shape[0]
0.12236034167531029获取整个滚动和相关的numpy数组,并对其进行整形以获得每个原始行的独立相关性。
n_sensors = 3
v = dfn.to_numpy() # v.shape = (30,3)
new_dims = df.shape[0], n_sensors, n_sensors
v = v.reshape(new_dims) # shape = (10,3,3)
print(v[4])
[[ 1. -0.14697697 -0.22705934]
[-0.14697697 1. 0.43521648]
[-0.22705934 0.43521648 1. ]]最后两个维度之和,减去传感器的数量
result = v.sum((1,2)) - n_sensors
print(result)
[nan, nan, nan, nan, 0.12236034, 0.25316027, -2.40763192, -1.9370202, -2.28023618, -2.57886457]在潘达斯可能有办法做到这一点,但我必须努力去弄清楚。也许有人会用全灵丹妙药的解决方案来回答。
滚动平均相关DataFrame有一个多指标
>>> dfn.index
MultiIndex([(0, 'col1'),
(0, 'col2'),
(0, 'col3'),
(1, 'col1'),
(1, 'col2'),
(1, 'col3'),
(2, 'col1'),
(2, 'col2'),
(2, 'col3'),
...通过快速回顾MultiIndex文档和使用pandas multi index sum on level 0 site:stackoverflow.com进行搜索,我得到了- group按级别0和和,然后沿着列再次求和。
>>> four_five = dfn.loc[[4,5]]
>>> four_five
col1 col2 col3
4 col1 1.000000 -0.146977 -0.227059
col2 -0.146977 1.000000 0.435216
col3 -0.227059 0.435216 1.000000
5 col1 1.000000 0.191238 -0.644203
col2 0.191238 1.000000 0.579545
col3 -0.644203 0.579545 1.000000
>>> four_five.groupby(level=0).sum()
col1 col2 col3
4 0.625964 1.288240 1.208157
5 0.547035 1.770783 0.935343
>>> four_five.groupby(level=0).sum().sum(1)
4 3.12236
5 3.25316
dtype: float64
>>>然后是完整的DataFrame。
>>> dfn.groupby(level=0).sum().sum(1) - n_sensors
0 -3.000000
1 -3.000000
2 -3.000000
3 -3.000000
4 0.122360
5 0.253160
6 -2.407632
7 -1.937020
8 -2.280236
9 -2.578865
dtype: float64 从搜索中读到更多的答案(我应该仔细看一下DataFrame.sum文档)
>>> dfn.sum(level=0).sum(1) - n_sensors
0 -3.000000
1 -3.000000
2 -3.000000
3 -3.000000
4 0.122360
5 0.253160
6 -2.407632
7 -1.937020
8 -2.280236
9 -2.578865
dtype: float64https://stackoverflow.com/questions/67762975
复制相似问题