【发布时间】:2015-01-31 14:12:52
【问题描述】:
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)
【问题讨论】:
标签: python numpy pandas feature-selection