我正在运行以下代码对矩阵的行进行排序,然后从另一个向量中选取相应的元素:
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)
NumPy 数组允许整数数组索引,但 Pandas 系列不允许:
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
从 Pandas 系列更改为 NumPy 数组:
ids = ids.values
coeff_idx = ids[indices]