Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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 将预测转换为具有阈值的标签_Python_Numpy - Fatal编程技术网

Python 将预测转换为具有阈值的标签

Python 将预测转换为具有阈值的标签,python,numpy,Python,Numpy,假设我有以下numpy数组 import numpy as np arr = np.array([[0.2, 0.8], [0.99, 0.01], [0.08, 0.92]]) arr Out[57]: array([[0.2 , 0.8 ], [0.99, 0.01], [0.08, 0.92]]) 如果我想将此输出转换为“类”(或每行中最大值的索引),我只需使用: arr.argmax(axis=1) Out[58]: array([1, 0, 1], d

假设我有以下numpy数组

import numpy as np

arr = np.array([[0.2, 0.8], [0.99, 0.01], [0.08, 0.92]])
arr
Out[57]: 
array([[0.2 , 0.8 ],
       [0.99, 0.01],
       [0.08, 0.92]])
如果我想将此输出转换为“类”(或每行中最大值的索引),我只需使用:

arr.argmax(axis=1)
Out[58]: array([1, 0, 1], dtype=int64)
问题是,我想限制一定的限制。例如,让我们使用0.9。因此,不满足阈值约束的每一行都将返回标签-1

上述示例的输出将是
[-1,0,1]
(因为0.8和0.2都不大于0.9)

做这件事最像蟒蛇的方式是什么?希望(但不是必须)使用
numpy

,您可以使用:

细节

(arr>0.9)
返回一个具有相同形状的
ndarray
,指示满足条件的位置:

array([[False, False],
       [ True, False],
       [False,  True]])
m.argmax(axis=1)
返回
m
True

array([0, 0, 1])
np.where
将为满足
m.any(axis=1)
的行返回
m.argmax(axis=1)
,因此其中至少有一个元素大于阈值。此处
m.any(轴=1)
给出:

array([False,  True,  True])

否则
np。其中
将返回
-1

漂亮!谢谢亚图。
array([False,  True,  True])