我刚学过Python,我想问点事情。。
例如,我有
import numpy as np
a = np.array([[11, 12, 13],
[14, 16, 13],
[17, 15, 11],
[12, 14, 15]])
我想找到该数组的标签
所以..在第一行中最大值是13,则标签结果= 3
在第二行中最大值为16,则标签结果= 2
期望的结果是这样
[3 2 1 3] or [[3]
[2]
[1]
[3]]
您可以尝试以下方法:
>>> import numpy as np
>>> a = np.array([[11, 12, 13],
[14, 16, 13],
[17, 15, 11],
[12, 14, 15]])
>>> np.argmax(a, axis=1) + 1
array([3, 2, 1, 3], dtype=int64)
np.argmax
给出指定轴上最大值的索引。因此,
np.argmax
然后您需要做的就是在其中添加>>> np.argmax(a, axis=1)
array([2, 1, 0, 2], dtype=int64)
。
您可以这样操作。
1
输出
d=[]
for lst in a:
d.append(lst.index(max(lst))+1)
print(d)
[3, 2, 1, 3]
输出
d=[]
for lst in a:
d.append([lst.index(max(lst))+1])
print(d)