9

I have built a convolutional neural network which is needed to classify the test data into either 0 or 1. I am training the CNN with labels either 0 or 1 but while running the below code I am getting the following result.

predictions = classifier.predict(x_test)

print(predictions)

[0.0128037 ]
 [0.01182843]
 [0.01042355]
 [0.00906552]
 [0.00820154]
 [0.00726516]......

logloss_score = log_loss(y_test, predictions)
print(logloss_score)
0.047878393431377855

How do I get the results between 0 and 1? What do I need to modify in the above code?

Ethan
  • 1,657
  • 9
  • 25
  • 39
LIsa
  • 93
  • 1
  • 1
  • 3

4 Answers4

14

What you have are predicted class probabilities. Since you are doing binary classification, each output is the probability of the first class for that test example.

To convert these to class labels you can take a threshold:

import numpy as np

probas = np.array([[0.4],[0.7],[0.2]])
labels = (probas < 0.5).astype(np.int)
print(labels)
[[1]
 [0]
 [1]]

For multiclass classification where you want to assign one class from multiple possibilities you can use argmax:

probas = np.array([[0.4, 0.1, 0.5],[0.7, 0.2, 0.1],[0.3, 0.4, 0.3]])
labels = np.argmax(probas, axis=-1)    
print(labels)
[2 0 1]

And to get these as one-hot encoded arrays you can use LabelBinarizer:

from sklearn import preprocessing

lb = preprocessing.LabelBinarizer()
lb.fit_transform(labels)
array([[0, 0, 1],
       [1, 0, 0],
       [0, 1, 0]])

And for multilabel classification where you can have multiple output classes per example you can use thresholding again:

probas = np.array([[0.6, 0.1, 0.7],[0.7, 0.2, 0.1],[0.8, 0.9, 0.6]])
labels = (probas > 0.5).astype(np.int)
print(labels)
[[1 0 1]
 [1 0 0]
 [1 1 1]]

Some packages provide separate methods for getting probabilities and labels, so there is no need to do this manually, but it looks like you are using Keras which only gives you probabilities.

As a sidenote, this is not called "normalization" for neural networks. Normalization typically describes scaling your input data to fit in a nice range like [-1,1].

Imran
  • 2,381
  • 13
  • 22
2

predictions = classifier.predict(x_test)

You have not provided the shape of your x_test but based on the documentation of the predict function that you should provide an array-like item, you are inputting an array-like input. Each output already shows the probability of each corresponding input. It seems that because the low values of predictions, they are smaller than 0.5, the predicted labels for your test data are all zero.

Green Falcon
  • 14,308
  • 10
  • 59
  • 98
1

The predictions you are getting are logits, meaning the sum across all categories is 1. So the largest-number should be the category you are looking for. To get the category, you can use argmax to find the index of the maximum number. The cross entropy loss is a measure of discrepancy between predicted value and labels. In this case you are 95% accurate.

Ricky Han
  • 111
  • 1
0

To convert your class probabilities to class labels just let it through argmax that will encode the highest probability as 1

prob_ = np.array([[0.12, 0.18, 0.2, 0.6],[0.7, 0.08,0.12, 0.1],[0.15, 0.4, 
0.3, 0.15]])
labels = np.argmax(prob_, axis=-1)    
print(labels)
[3 0 1]