Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python np.在哪里计算年龄组指数_Python_Numpy - Fatal编程技术网

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