我真的很喜欢热图,但我需要的是热图背后的数字(AKA 相关矩阵)。
有没有简单的方法来提取数字?
有点难以追踪,但从文档开始;特别是从报告结构中挖掘到下面的函数get_correlation_items(summary)然后进入源代码并查看它的用法,我们得到这个调用,它基本上遍历摘要中的每个相关类型,以获得摘要对象我们可以找到以下内容,如果我们查找调用者,我们会发现它是get_report_structure(summary),如果我们尝试找到如何获取summary
arg,我们会发现它只是这里description_set
所示的属性。
鉴于上述情况,我们现在可以使用 2.9.0 版本执行以下操作:
import numpy as np
import pandas as pd
from pandas_profiling import ProfileReport
df = pd.DataFrame(
np.random.rand(100, 5),
columns=["a", "b", "c", "d", "e"]
)
profile = ProfileReport(df, title="StackOverflow", explorative=True)
correlations = profile.description_set["correlations"]
print(correlations.keys())
dict_keys(['pearson', 'spearman', 'kendall', 'phi_k'])
要查看特定的相关性,请执行以下操作:
correlations["phi_k"]["e"]
a 0.000000
b 0.112446
c 0.289983
d 0.000000
e 1.000000
Name: e, dtype: float64