Python 如何在相关矩阵中找到高值?

Python 如何在相关矩阵中找到高值?,python,dataframe,correlation,cross-correlation,pearson-correlation,Python,Dataframe,Correlation,Cross Correlation,Pearson Correlation,如何返回高相似性或最高相关值,或相关矩阵中高于阈值的值?例如,在下面的示例中,A1和A3具有高度相关性 import pandas as pd from io import StringIO df = pd.read_csv(StringIO('''Sentence, A1, A2, A3 text, 0.23, 0.54, 39 text, 0.33, 0.7, 36

如何返回高相似性或最高相关值,或相关矩阵中高于阈值的值?例如,在下面的示例中,A1和A3具有高度相关性

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO('''Sentence, A1, A2, A3
                        text, 0.23, 0.54, 39
                        text, 0.33, 0.7, 36
                        text, 0.8, 0.41, 29'''), sep=',')
print(df.corr())
结果:

           A1        A2        A3
 A1  1.000000 -0.732859 -0.991352
 A2 -0.732859  1.000000  0.637235
 A3 -0.991352  0.637235  1.000000

继续该示例并使用numpy:

c = df.corr()
import numpy as np

threshold = .99
np.abs(c.values) > threshold
这使得:

array([[ True, False,  True],
       [False,  True, False],
       [ True, False,  True]])
[(0, 2), (2, 0)]
您可以通过以下方式获得偏离对角线的有效值的指数:

[(i, j) for i,j in zip(*np.where(np.abs(c.values) > threshold)) if i!=j]
这使得:

array([[ True, False,  True],
       [False,  True, False],
       [ True, False,  True]])
[(0, 2), (2, 0)]
更新:

利用相关矩阵的对称性,可以得到具有列名的可读字符串列表:

[f"{c.columns[i]} and {c.columns[j]}" for i, j in zip(*np.where(np.abs(c.values) > threshold)) if i < j]

继续该示例并使用numpy:

c = df.corr()
import numpy as np

threshold = .99
np.abs(c.values) > threshold
这使得:

array([[ True, False,  True],
       [False,  True, False],
       [ True, False,  True]])
[(0, 2), (2, 0)]
您可以通过以下方式获得偏离对角线的有效值的指数:

[(i, j) for i,j in zip(*np.where(np.abs(c.values) > threshold)) if i!=j]
这使得:

array([[ True, False,  True],
       [False,  True, False],
       [ True, False,  True]])
[(0, 2), (2, 0)]
更新:

利用相关矩阵的对称性,可以得到具有列名的可读字符串列表:

[f"{c.columns[i]} and {c.columns[j]}" for i, j in zip(*np.where(np.abs(c.values) > threshold)) if i < j]

如果你需要相关性最高的对,那么你需要叠加,然后逐层找到相关性最高的对,这就是方法

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO('''Sentence, A1, A2, A3
                        text, 0.23, 0.54, 39
                        text, 0.33, 0.7, 36
                        text, 0.8, 0.41, 29'''), sep=',')

df.drop(['Sentence'],1, inplace=True)
print(df.corr())


def get_red_pair(df):
    pairs_to_drop = set()
    cols = df.columns
    for i in range(0, df.shape[1]):
        for j in range(0, i+1):
            pairs_to_drop.add((cols[i], cols[j]))
    return pairs_to_drop

def get_largest_correlations(df, n=5):
    au_corr = df.corr().abs().unstack()
    labels_to_drop = get_red_pair(df)
    au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False)
    return au_corr[0:n]

corr = get_largest_correlations(df)
print(corr)
要仅获取第一个,请确保将n作为1传递给函数,因为默认情况下它预期为5

如果这不是你想要的,那么把你的问题设计好可能会有帮助

这给

           A1        A2        A3
 A1  1.000000 -0.732859 -0.991352
 A2 -0.732859  1.000000  0.637235
 A3 -0.991352  0.637235  1.000000

 A1   A3    0.991352
      A2    0.732859
 A2   A3    0.637235

如果你需要相关性最高的对,那么你需要叠加,然后逐层找到相关性最高的对,这就是方法

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO('''Sentence, A1, A2, A3
                        text, 0.23, 0.54, 39
                        text, 0.33, 0.7, 36
                        text, 0.8, 0.41, 29'''), sep=',')

df.drop(['Sentence'],1, inplace=True)
print(df.corr())


def get_red_pair(df):
    pairs_to_drop = set()
    cols = df.columns
    for i in range(0, df.shape[1]):
        for j in range(0, i+1):
            pairs_to_drop.add((cols[i], cols[j]))
    return pairs_to_drop

def get_largest_correlations(df, n=5):
    au_corr = df.corr().abs().unstack()
    labels_to_drop = get_red_pair(df)
    au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False)
    return au_corr[0:n]

corr = get_largest_correlations(df)
print(corr)
要仅获取第一个,请确保将n作为1传递给函数,因为默认情况下它预期为5

如果这不是你想要的,那么把你的问题设计好可能会有帮助

这给

           A1        A2        A3
 A1  1.000000 -0.732859 -0.991352
 A2 -0.732859  1.000000  0.637235
 A3 -0.991352  0.637235  1.000000

 A1   A3    0.991352
      A2    0.732859
 A2   A3    0.637235

谢谢,但是如果功能空间很大,就有点难读了。有可能得到列名吗?谢谢,但是如果功能空间很大,就有点难读了。可以获取列名吗?