I have 60000 train_images brought in as a shape (28,28,60000) matrix. It is a numpy.ndarray. I want to convert it to an array of 1 dimensional images, meaning each image is represented as a single line/array of numbers, and I want 60000 arrays. In other words, I want to go from (28, 28, 60000) to (60000, 28*28). In python, it would be:
images_features = []
for image in images:
imageLine = []
for y in range(len(image)):
for x in range(len(image[0])):
imageLine.append(image[y][x])
images_features.append(imageLine)
How can I do this? I suspect that I need to use reshape but I couldn't figure out how exactly I can do this.
This is how I'm getting the images:
data = scipy.io.loadmat('train.mat')
images = data["train_images"]
So the "images" is the array I'm talking about.
Someone suggested to me that:
"You may need to change axes or combine them do get the functionality you want. I recommend plotting them as well in case an image ends up sideways. Make sure you are diligent with your axes to avoid further problems there."
I have no idea what "axes" is being referred to here and how to take what's said above into account.
Can someone explain what I need to do and why? (What it does)
I think you just need to use reshape:
Since this is coming via
loadmat
, a shape of(28,28,60000)
makes sense - MATLAB iterates starting with the last index.reorders the axes, so the result is
(60000,28,28)
. The last two dimensions can combined with a reshapeYou many need to transpose the 28x28 images, e.g.
.T
is the same as the MATLAB'
(or.'
).images
may also beorder='F'
.I chose test dimensions to be small, and to make it easy to distinguish the different axes.
In a Ipython session:
You can reshape
train_images
and verify it by plotting the images,Reshaping:
Plotting images: