首页 > 解决方案 > Python 相关矩阵 - 只需要绝对值大于 0.5 的列

问题描述

我有 41 个变量,其中大多数根本不相关。我只想包括几个列来说明更高相关或更高度负相关的列。尽我所能尝试,即使我查看了许多文章和问题,我似乎也无法让它发挥作用。谢谢你。

df.columns

Index(['ResponseId', 'Consent', 'AgeQualifier', 'Team', 'TeamOther', 'FanStrength', 'WinImportance', 'Emotion', 'Happiness', 'Satisfaction', 'Passion', 'ViewershipHomeGame' , 'ViewershipRoadGame', 'ViewershipTVCable', 'ViewershipStreaming', 'ViewershipRestaurantBar', 'NameChangeViewershipHomeGame', 'NameChangeViewershipRoadGame', 'NameChangeViewershipTVCable', 'NameChangeViewershipStreaming', 'NameChangeViewershipRestaurantBar', 'Purchased', 'Purchased_JerseyPuring_1', Purchased_Memorabilia_3'、'Purchased_Office_4'、'Purchased_Equipment_5'、'PurchaseIntentionNameChangeJersey'、'PurchaseIntentionNameChangeClothing'、'PurchaseIntentionNameChangeMemorabilia'、'PurchaseIntentionNameChangeHomeOffice'、'PurchaseIntentionNameChangeEquipment'、'Support_SeasonTickets'、'Support_Donations'、'Support_Volunteer'、'SupportNameChangeSeasonTickets'、'SupportNameChangeDonateMoney'、'SupportNameChangeVolunteer'、'AgeState'、' , '种族', 'EthnicityOther', '收入', '绘图', '电子邮件'], dtype='object')SupportNameChangeSeasonTickets'、'SupportNameChangeDonateMoney'、'SupportNameChangeVolunteer'、'State'、'Gender'、'Age'、'Ethnicity'、'EthnicityOther'、'Income'、'Drawing'、'Email']、dtype='object')SupportNameChangeSeasonTickets'、'SupportNameChangeDonateMoney'、'SupportNameChangeVolunteer'、'State'、'Gender'、'Age'、'Ethnicity'、'EthnicityOther'、'Income'、'Drawing'、'Email']、dtype='object')

相关矩阵 = df.corr().round(2)

无花果,ax = plt.subplots(figsize=(50,50)) sns.heatmap(data=correlation_matrix,cmap = 'rainbow' , annot=True, ax=ax)

想法?

标签: pythoncorrelation

解决方案


清洁后的矩阵将是

同意 年龄限定符 团队 粉丝力量 赢重要
同意
年龄限定符 1.0
团队 1.00 0.02 0.02
粉丝力量 0.02 1.00 0.69
赢重要 0.02 0.69 1.00

要解决这个问题,您需要选择任何非对角矩阵值且绝对值 >0.5

temp = df[(df>0.5)&(df!=1)].abs().max()
print(temp[~temp.isna()])

这将产生在相关矩阵>0.5 中具有至少一个相关性的列名

这产生

FanStrength      0.69
WinImportance    0.69
dtype: float64

推荐阅读