2016-05-30 30 views
0

のインデックスを作成する際に、私は次のコード行列の行をソートして、別のベクトルから対応する要素を選択するために実行している:エラーnumpyの配列

import pandas as pd 
import numpy as np 
# compute ids 
coeff = np.dot(matrix1, np.transpose(matrix2)) 
print coeff.shape, ids.shape 
indices = coeff.argsort()[:, ::-1] 
print indices.shape 
coeff_idx = ids[indices] 

をしかし、プログラムがになったとき、私はエラーを取得します最後の行:

(11396, 45582) (11396,) 
(11396, 45582) 

... 
File "pandas/hashtable.pyx", line 359, in pandas.hashtable.Int64HashTable.lookup (pandas/hashtable.c:7427) 
ValueError: Buffer has wrong number of dimensions (expected 1, got 2) 

答えて

1

numpyのアレイはinteger array indexingを許可しますが、パンダシリーズにはない:

In [168]: arr = np.arange(5) 

In [169]: ser = pd.Series(arr) 

In [170]: indices = np.array([[0,1],[4,3],[2,2]]) 

In [171]: arr[indices] 
Out[171]: 
array([[0, 1], 
     [4, 3], 
     [2, 2]]) 

In [172]: ser[indices] 
ValueError: Buffer has wrong number of dimensions (expected 1, got 2) 
したがって

indicesとインデックスにそれをしようとする前に、numpyのアレイにパンダシリーズからidsを変更:

ids = ids.values 
coeff_idx = ids[indices]