【问题标题】:Get index of numpy array获取numpy数组的索引
【发布时间】:2019-06-07 12:37:07
【问题描述】:
x=pred_vec.argmax(axis=-1) 
x
o/p-array([51], dtype=int64)

我需要提取 51 以便它可以用来显示它所标记的类。

【问题讨论】:

  • 你能提供你想在这里问什么的例子吗?

标签: python numpy


【解决方案1】:

您似乎正在尝试使用 python 和 numpy 查找最大值的索引(基于您的标签??)...如果是这样,那么试试这个:

In [1]: import numpy as np                                                                                                                                                                                                                                        

In [3]: x = np.random.randint(1, 200, (50))                                                                                                                                                                                                                       

In [4]: x                                                                                                                                                                                                                                                         
Out[4]: 
array([186, 117, 150,  68, 118, 197, 169,  48,  90,  67, 117,  90, 133,
       141, 175, 194,  21, 107, 103,  61, 154,  87,  42,  59,  67,  66,
       198,  76, 190,   7, 188, 146, 158,  42, 106, 157,   1, 114, 185,
        58, 169,  89, 120,  81,  28,  49, 197,  73, 169, 142])

In [5]: index_max = np.argmax(x)                                                                                                                                                                                                                                  

In [6]: x[index_max]                                                                                                                                                                                                                                              
Out[6]: 198

查看sorting, searching and counting page 了解更多选项。

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    相关资源
    最近更新 更多