Python: convert a 1-by-28-by-28 array to a 28-by-28 matrix -
i have grayscale image stored 1-by-28-by-28 array (uint8) wish display using matplotlib.
the relevant codes follows:
import matplotlib.pyplot plt image = get_image() print image.shape # prints (1, 28, 28) plt.imshow(image)
this code results in typeerror: invalid dimensions image data
.
to compatible imshow()
need either squeeze array 28-by-28 (removing redundant dimension) or replicate 2d matrix (28-by-28) 3 (rgb) or 4 (rgba) channels.
my preference use first approach, squeeze()
not remove redundant dimension.
how convert 1-by-28-by-28 array 28-by-28 matrix?
you can pass squeeze or reshape:
imshow(np.squeeze(image)) imshow(image.reshape((28,28))
Comments
Post a Comment