简体   繁体   English

索引一个numpy的二维矩阵

[英]Indexing a numpy 2D matrix

Suppose, I have this: 假设我有这个:

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])

Now, I want to manually show the ids and scores of those students whose score is more than 4.0. 现在,我想manually显示那些分数大于4.0的学生的idsscores

How can I make the following routine work? 如何使以下常规工作正常进行?

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

Error 错误

>>> (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
#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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM