Python np.在哪里计算年龄组指数
下面的代码有一部分我不太理解 代码如下:Python np.在哪里计算年龄组指数,python,numpy,Python,Numpy,下面的代码有一部分我不太理解 代码如下: 将numpy导入为np medalNames=np.array(['none','brown','silver','gold'])) ageGroupCategories=np.array(['B','P','G','T'])) allLowerThresholds=np.数组([-1,0,5,10],[0,5,10,15],[0,11,14,17],[0,15,17,19]) ageGroupIndex=np.其中(ageGroup[0]==ageGr
将numpy导入为np
medalNames=np.array(['none','brown','silver','gold']))
ageGroupCategories=np.array(['B','P','G','T']))
allLowerThresholds=np.数组([-1,0,5,10],[0,5,10,15],[0,11,14,17],[0,15,17,19])
ageGroupIndex=np.其中(ageGroup[0]==ageGroupCategories)[0][0]
在最后一行中,[0][0]
是做什么的,为什么没有它代码就不能工作?有几件事:
ageGroup
不存在由于它是一个数组,
[0][0]
调用数组结果的第一行和第一列np.where()
您的问题是一般性的,并且与numpy.where
函数相关
让我们举一个简单的例子如下:
A=np.array([[3,2,1],[4,5,1]])
# array([[3, 2, 1],
# [4, 5, 1]])
print(np.where(A==1))
# (array([0, 1]), array([2, 2]))
您可以看到np。其中
函数返回一个元组。元组的第一个元素(它是一个numpy数组)是行/行索引,第二个元素(它也是一个numpy数组)是列索引。
上面告诉您,矩阵a
的第一(0)行和第二(1)行中有一个值=1
下一步:
返回包含值为1的第一行的索引。0这是矩阵的第一行
A
非常感谢!这正是我想要的
np.where(A==1)[0] # this is the first element of the tuple thus,
# the numpy array containing all the row/line
# indices where the value is = 1.
#array([0, 1])
np.where(A==1)[0][0]
0