'Deep learning how to assign probability scores to the classes

I am running deep learning with keras. I need to assign probabilities to the predicted classes of a binary classification. The command model.predict() is providing an output which is an array consisting of a single column with probability scores. I need to sort both of the 2 classes according to their probability scores. In case of SVM, there is a 2d array provided that may look like this:

array([[1.73012153e-01, 8.26987847e-01],
[9.20157632e-01, 7.98423679e-02],
[1.65736191e-01, 8.34263809e-01],
[9.61620647e-01, 3.83793535e-02]]

for the following list of classes:

1
0
1
0

By comparing the list of the classes with this array one concludes that class 1 is being assigned the probability scores from the second entries of the array and the class 0 from the first entries. Both classes can thus be assigned scores greater then 0.5. It is clear that both entries sum up to one.

Can somebody provide a comment and explain how I can find the probability scores for the class 0 and 1 ? By setting a threshold of 0.5 and claiming that class 1 is the one with probability score greater than 0.5 and class 0 the one with probability score less than 0.5 leads to having class 1 being assigned high probability scores and class 0 low probability scores. Since in my task I need to chose hits from both classes with high probability scores, I thus do not know how to proceed in the case of keras. Here is how the output of model.predict() in keras looks like:

array([[6.6122383e-01],
[6.3882202e-02],
[9.7862136e-01],
[3.9379299e-03],
[4.2064726e-02],
[9.7605175e-07],
[5.5374026e-01]]

The code that I use to generate the model is:

from tensorflow.keras.layers import Dropout 
model = Sequential()  
model.add(Dense(30,activation='relu')) 
model.add(Dropout(.5)) 
model.add(Dense(15,activation='relu')) 
model.add(Dropout(.5)) #binary classifcation 
model.add(Dense(1,activation='sigmoid')) 
model.compile(loss='binary_crossentropy',optimizer='adam') 
model.fit(x=x_train,y=y_train,epochs=600,validation_data=  (x_test,y_test), verbose = 0) 

Thanks.



Solution 1:[1]

A DNN with sigmoid activation function in the last layer would give one array of probabilities, you can construct the class column by yourself:

df["class_1_prob"]=predicted_prob
df["class_0_prob"]=1-predicted_prob

df["target_class_0"]=[0 if x <0.5 else 1 for x in predicted_prob]
df["target_class_1"]=np.abs(1-df["target_class_0"])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1