1

I have a dataset with dimensions: (32, 32, 73257) where 32x32 are pixels of a single image.

How do I reshape it to (73257, 1024) so that every image is unrolled in a row?

So far, I did:

self.train_data = self.train_data.reshape(n_training_examples, number_of_pixels*number_of_pixels)

and it looks like I got garbage instead of normal pictures. I am assuming that reshaping was performed across wrong dimension...??

2
  • You need to swap axes, moving the 73257 dimension to the front, then reshape. Commented Nov 10, 2015 at 4:59
  • @hpaulj and how do I do that? I am not so proficient with numpy. Commented Nov 10, 2015 at 14:56

1 Answer 1

1

As suggested in the comments, first get every image in a column, then transpose:

self.train_data = self.train_data.reshape(-1, n_training_examples).T

The memory layout of your array will not be changed by any of these operations, so two contiguous pixels of any image will lay 73257 bytes apart (assuming a uint8 image), which may not be the best of options if you want to process your data one image at a time. You will need to time and validate this, but creating a copy of the array may prove advantageous performance-wise:

self.train_data = self.train_data.reshape(-1, n_training_examples).T.copy()
Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking of something more like data.transpose(2,0,1).reshape(N,32*32), but don't know if the order of the flattened 32x32 part is right or not.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.