2014-12-03 67 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)