Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

tensorflow - Predict if probability is higher than certain value

I am using a MLP model for classification.

When I predict for new data, I want to keep only those predictions whose probability of prediction is larger than 0.5, and change all other predictions into class 0.

How can I do it in keras ?

I'm using using last layer as follows model.add(layers.Dense(7 , activation='softmax'))

Is it meaningful to get predictions with probability larger than 0.5 using the softmax?

newdata = (nsamples, nfeatures)
predictions = model.predict (newdata)
print (predictions.shape)
(500, 7)
question from:https://stackoverflow.com/questions/65882997/predict-if-probability-is-higher-than-certain-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can do something like:

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...