0
$\begingroup$

I have trained a normal CNN to recognize patients with a disease or not. I print the predicted values for the test set, and get the probability of the various images of belonging to a class rather than the other one... But there are two main problems. How do I know which class is '0' and which is '1'? And is there a function to quickly display if the results predicted within a certain probability are correct or wrong? like an array with 1s if the result predicted for that image is correct and 0 if not. The code is this:

pred = model.predict(test, steps=len(test))
predSeq = [] # if probability is higher than 0.5 assign 1, otherwise 0
for i in range(0,624):
    if pred[i] >= 0.5:
        predSeq.append(1)
    else:
        predSeq.append(0)
print(predSeq)
$\endgroup$

1 Answer 1

0
$\begingroup$

To answer your first question, you should be able to tell which class is '0' and which is '1' from how you've set up the training label data. If you're still unsure, then use your model to predict the training labels from the training data and see which way round of assigning the classes gives the best results.

To answer your second question, sklearn.metrics provides lots of functions for calculating classification metrics. But if you want to see which predictions are correct, convert the lists of labels and predictions to numpy arrays and compare them, as in this example:

import numpy as np

# Set up test labels
y_test = ['a', 'a', 'b', 'b', 'a', 'b']          # Classes are 'a' and 'b'
y_test = (np.array(y_test) == 'b').astype(int)   # Convert to numpy array; class 'b' is 1
print(f'Transformed labels: {y_test}')

# Set up predictions
y_preds = [0, 1, 1, 0, 0, 1]                     # predicted labels
y_preds = np.array(y_preds)                      # Convert to numpy array

# Compare labels and predictions
correct = y_preds == y_temp
print(f'Correct prediction: {correct}')

output:

Transformed labels: [0 0 1 1 0 1]
Correct prediction: [ True False  True False  True  True]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.