Python 为numpy 2D矩阵编制索引
假设我有:Python 为numpy 2D矩阵编制索引,python,numpy,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.
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])
现在,我想手动
显示分数超过4.0的学生的ID
和分数
我怎样才能完成以下日常工作
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
>(正在执行文件“arrays.py”)
回溯(最近一次呼叫最后一次):
文件“D:\Python\arrays.py”,第25行,在
打印(学生资料([False,True,False,False,False]))
TypeError:“numpy.ndarray”对象不可调用
你用括号而不是圆括号索引数组:学生[False,True,False,False,False]
我认为你还需要将其转换为numpy数组。布尔索引似乎不适用于列表。@user2357112,程序编译。但是,给出了不正确的输出。您使用括号而不是括号对数组进行索引:students\u mat[[False,True,False,False,False]]
我认为您还需要将其转换为numpy数组。布尔索引似乎不适用于列表。@user2357112,程序编译。但是,给出了不正确的输出。@anonymous:1)因为索引是[]
,而不是像在matlab中那样()
,2)因为arr[[True,False,True]]
被解释为arr[True,False,True]
,以便向后兼容reasons@anonymous:1)因为索引是[]
,而不是()
就像在matlab中一样,2)因为出于向后兼容性的原因,arr[[True,False,True]]
被解释为arr[True,False,True]
#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]]