gpt4 book ai didi

python - 计算一定数量的列之间的大型数据帧的成对相关性

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:28 25 4
gpt4 key购买 nike

我意识到过去已经在 SO 上发布了与我在这里的问题类似的问题。然而,他们没有给我我想要的东西。

我有一个大型数据框,data,有 314 列。我想计算所有对之间的相关值仅计算数据帧的前 30 列。之后,我想报告前 5 个相关对,无论符号如何,因此就相关系数的大小。我意识到我应该使用 .corr() 因为某些数据框单元格是空的,我们不想包含它们。

这是我到目前为止所拥有的。我仍在努力。也不知道 cc 的类型,这就是为什么我没有报告前 5 个值:

W = 30 # taking the first 30 columns
cc = np.zeros((1,W)) # pre-allocation for coefficients

for c in range(1:W) in data:
tmp = data.corr(data(:,c0),data(:,c));
cc(c) = tmp(1,2);

以下是数据框的前 15 行和 5 列:

    Group  Age  Gender  Weight     Height
0 1 50 1 224 73.533514
1 1 59 0 180 62.625479
2 1 22 0 167 62.253894
3 1 48 0 113 61.476092
4 1 53 1 166 70.076665
5 1 48 1 210 71.384046
6 1 29 0 140 61.438960
7 1 44 1 181 74.992675
8 1 28 0 98 60.145635
9 1 42 1 187 71.588029
10 1 35 0 199 66.773644
11 0 54 1 228 76.971180
12 0 43 0 145 67.586941
13 1 50 0 190 67.229118
14 1 62 0 281 63.645601

最佳答案

好的,这应该可以。第一部分给出前 30 列的绝对相关矩阵,并基本上消除了自相关。下一部分通过找到绝对最大值、将其标记下来、将其从相关矩阵中删除然后移至下一个来寻找五个总体最大相关性。 max_list 中的每个元素都类似于 (0.8764779791676971, 'Gender', 'Height') 并带有abs。相关性以及给出该相关性的两列。

import pandas as pd
import numpy as np
corr = data.iloc[:,0:30].corr().replace(1, np.NaN).abs()

max_list = []
for i in range(0,5):
max_val = max(corr.max())
max_list.append((max_val, corr.columns[np.where(corr == max_val)[0][:]][0],
corr.columns[np.where(corr == max_val)[0][:]][1]))
corr.replace(max_val, np.NaN, inplace=True)

关于python - 计算一定数量的列之间的大型数据帧的成对相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49355681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com