簡體   English   中英

索引一個numpy的二維矩陣

[英]Indexing a numpy 2D matrix

假設我有這個:

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