Skip to content

Instantly share code, notes, and snippets.

@satishgunjal
Created October 17, 2020 07:50
Show Gist options
  • Save satishgunjal/2df13407784fb831e60bfbcda738abb9 to your computer and use it in GitHub Desktop.
Save satishgunjal/2df13407784fb831e60bfbcda738abb9 to your computer and use it in GitHub Desktop.
plot_image
def plot_image(i, predictions_array, true_label, img):
"""
This method will plot the true image and also compare the prediction with true values if matcing write the caption in green color else in red color.
Format is : predicted class %confidence score (true class)
Input:
i: Index of the prediction to test
predictions_array: Every prediction contain array of 10 number
true_label: Correct image labels. In case of test data they are test_labels
img: Test images. In case of test data they are test_images.
"""
true_label, img = true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary) # For grayscale colormap
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
plot_image(0, predictions[0], test_labels, test_images)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment