2014-12-03 64 views
3

df.idxmax()返回的max沿一个轴(行或列),但我想ARG_MAX(DF)在整个数据框,它返回一个元组(行列)。获取行和列的名称(argmax)在大熊猫数据帧最大入口

使用情况下,我已经记特征选择,其中我有一个相关矩阵,并希望“递归”与最高的相关删除功能。我预处理相关矩阵以考虑其绝对值并将对角线元素设置为-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) 

回答

2

假设你所有的大熊猫表的数值,是你可以做的是改变其numpy的解释,并从那里获取最大的位置。不过,numpy的的argmax作品的扁平数据,所以你需要解决:

# Synthetic data 
>>> table = pd.DataFrame(np.random.rand(5,3)) 
>>> table 
      0   1   2 
0 0.367720 0.235935 0.278112 
1 0.645146 0.187421 0.324257 
2 0.644926 0.861077 0.460296 
3 0.035064 0.369187 0.165278 
4 0.270208 0.782411 0.690871 

[5 rows x 3 columns 

变换表numpy的数据和计算argmax:

>>> data = table.as_matrix() 
>>> amax = data.argmax() # 7 in this case 
>>> row, col = (amax//data.shape[1], amax%data.shape[1]) 
>>> row, col 
(2, 1)