python - 在 Pandas 数据中删除标签时出现 KeyError
问题描述
我将 CSV 中的数据集加载到数据框中。我想显示列之间的最高相关性(前 10 个负值和前 10 个正值)
我在这个网站上发现了一个我认为对我有帮助的代码——
def get_redundant_pairs(df):
'''Get diagonal and lower triangular pairs of correlation matrix'''
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_top_abs_correlations(df, n=5):
au_corr = df.corr().abs().unstack()
labels_to_drop = get_redundant_pairs(df)
au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False)
return au_corr[0:n]
我从我的 DataFrame 调用这个函数 -
train = pd.read_csv('/content/drive/My Drive/DSF_HW3_Datasets/train.csv')
get_top_abs_correlations(train.loc[:, train.columns != 'Id'],10)
我得到一个 KeyError 值 -
KeyError: 'Foundation'
During handling of the above exception, another exception occurred:
....
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/multi.py in get_loc(self, key, method)
2404
2405 if keylen == self.nlevels and self.is_unique:
-> 2406 return self._engine.get_loc(key)
2407
2408 # -- partial selection or non-unique index
pandas/_libs/index.pyx in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc()
KeyError: ('Foundation', 'OverallQual')
我该如何解决这个错误?Train.csv 文件 - https://pastebin.com/vTh6md5W
解决方案
你想要掩码/最大:
# get the correlation matrix
corr = df.corr()
# mask away the lower triangle and diagonal
mask = np.triu(np.ones_like(corr),1) == 1
# get the upper triangle (excluding diagonal) by masking and stack:
corr = corr.where(mask).stack()
# 10 largest by absolute values
max10 = corr.abs().nlargest(10)
输出(最大10):
GarageCars GarageArea 0.882475
YearBuilt GarageYrBlt 0.825667
GrLivArea TotRmsAbvGrd 0.825489
TotalBsmtSF 1stFlrSF 0.819530
OverallQual SalePrice 0.790982
GrLivArea SalePrice 0.708624
2ndFlrSF GrLivArea 0.687501
BedroomAbvGr TotRmsAbvGrd 0.676620
BsmtFinSF1 BsmtFullBath 0.649212
YearRemodAdd GarageYrBlt 0.642277
dtype: float64
要获得原始(有符号)相关性:
corr.loc[max10.index]
巧合的是,这与绝对最大值相同。
推荐阅读
- node.js - 为什么我在尝试获取我过去的贝宝交易时会收到此错误?
- github - Git - 关闭不正确的拉取请求后再次推送相同的提交
- php - PHP表单不向MySQL数据库添加值
- c++ - 当我有风扇时,为什么没有 Win32_Fan 的 WMI 实例?
- python - 如何阻止正则表达式匹配不需要的空字符串?
- c++ - c++ 输入的排列
- ios - ios 7 的 QRScanner 代码经常崩溃
- html - 响应式网页设计的缩放与媒体查询的好处
- sml - 我正在尝试从标准输入读取字符串并将其刷新到标准输出,但我找不到标准 ML 方式
- snowflake-cloud-data-platform - 无法创建雪花阅读器帐户 - 试用帐户