小编kus*_*ush的帖子

获取 pandas 数据框中最大条目的行和列名称(argmax)

df.idxmax() 返回沿轴(行或列)的最大值,但我希望 arg_max(df) 在整个数据帧上,它返回一个元组(行,列)。

我想到的用例是特征选择,其中我有一个相关矩阵,并且想要“递归”删除具有最高相关性的特征。我对相关矩阵进行预处理以考虑其绝对值并将对角线元素设置为 -1。然后我建议使用rec_drop,它递归地删除具有最高相关性的特征对中的一个(受到截止值:max_allowed_correlation),并返回最终的特征列表。例如:

S = S.abs()
np.fill_diagonal(S.values,-1) # so that max can't be on the diagonal now
S = rec_drop(S,max_allowed_correlation=0.95)

def rec_drop(S, max_allowed_correlation=0.99):
    max_corr = S.max().max()
    if max_corr<max_allowed_correlation: # base case for recursion
         return S.columns.tolist() 
    row,col = arg_max(S)  # row and col are distinct features - max can't be on the diagonal
    S = S.drop(row).drop(row,axis=1) # removing one of the features from S
    return rec_drop(S, max_allowed_correlation)
Run Code Online (Sandbox Code Playgroud)

python numpy feature-selection pandas

5
推荐指数
1
解决办法
4078
查看次数

标签 统计

feature-selection ×1

numpy ×1

pandas ×1

python ×1