2016-05-30 99 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前,从熊猫系列改变ids到NumPy的数组:

ids = ids.values 
coeff_idx = ids[indices]