我正在使用MLP模型进行分类。
当我对新数据进行预测时,我只希望保留那些预测概率大于0.5的预测,并将所有其他预测转换为0类。
我怎么才能在角角做这件事?
我正在使用最后一层,如下所示:model.add(layers.Dense(7 , activation='softmax'))
使用softmax得到概率大于0.5的预测是否有意义?
newdata = (nsamples, nfeatures)
predictions = model.predict (newdata)
print (predictions.shape)
(500, 7)发布于 2021-01-25 14:21:25
你可以这样做:
preds=model.predict etc
index=np.argmax(preds)
probability= preds(index)
if probability >=.75:
print (' class is ', index,' with high confidence')
elif probability >=.5:
print (' class is ', index,' with medium confidence')
else:
print (' class is ', index,' with low confidence')发布于 2021-01-25 10:47:17
软件最大输出probabilities.所以在你的例子中,你会有7个类,它们的概率之和等于1。
现在考虑一个例子[0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.3],它是softmax的输出。在这种情况下应用阈值是没有意义的,正如您所看到的。
阈值0.5与n类预测无关.这对于二进制分类来说是很特别的。
为了得到类,您应该使用argmax。
编辑:如果您想在某个阈值以下放弃您的预测,您可以使用,但这并不是处理多类预测的正确方法:
labels = []
threshold = 0.5
for probs_thresholded in out:
labels.append([])
for i in range(len(probs_thresholded)):
if probs_thresholded[i] >= threshold:
labels[-1].append(1)
else:
labels[-1].append(0)https://stackoverflow.com/questions/65882997
复制相似问题