索引numpy 2D矩阵

时间:2017-05-02 20:57:00

标签: python numpy

假设,我有这个:

import numpy as np

N = 5

ids = [ 1.,          2.,          3.,          4.,          5.,        ]
scores = [ 3.75320381,  4.32400937,  2.43537978,  3.73691774,  2.5163266, ]

ids_col = ids.copy()
scores_col = scores.copy()

students_mat = np.column_stack([ids_col, scores_col])

现在,我想 manually 显示分数超过4.0的学生的idsscores

如何使以下常规工作?

print(students_mat([False, True, False, False, False]))

错误

>>> (executing file "arrays.py")
Traceback (most recent call last):
  File "D:\Python\arrays.py", line 25, in <module>
    print(students_mat([False, True, False, False, False]))
TypeError: 'numpy.ndarray' object is not callable

1 个答案:

答案 0 :(得分:2)

#you need to convert Boolean list to an array to be used when selecting elements.
print(students_mat[np.asarray([False, True, False, False, False])])
[[ 2.          4.32400937]]