Home > Net >  Adding a dimension to a numpy arrray
Adding a dimension to a numpy arrray

Time:02-10

I'm working on the MNIST dataset and have my train_x.shape = (60000, 28, 28, ),
but my model requires a shape (60000, 28, 28, 1).
So my questions are:

  1. Why does it have a 'null' dimension instead of just (60000, 28, 28)?
  2. How can I convert the same? Is there some way I can loop through the first dimension in the array? (Or a better solution?) Thanks in advance!

CodePudding user response:

Good question! I too pondered why it had to be like this when I first started.

Looping through the first dimension can be easily done with

for image in train_x:
    image

as looping through NumPy, PyTorch and TensorFlows arrays/tensors will by default just iterate through the "leftmost" axis.

There are multiple ways to add said null dimension to get the shape (60000, 28, 28, 1):

train_x[..., None]
train_x[..., np.newaxis]
train_x.reshape((*train_x.shape, 1))
np.expand_dims(train_x, 3)

Explanation

Why does it have a 'null' dimension instead of just (60000, 28, 28)?

Generally speaking, when doing image processing you want to be able to handle an arbitrary number of image channels (e.g. RGB channels). Generally, an image dataset is of shape (number of images, height, width, number of channels) (in PyTorch the channels comes before the width and height actually), The MNIST dataset is 1 channel, thus your dataset should have the shape (60000, 28, 28, 1). The frameworks usually implement their code to handle the general case, thus you have to do said reshaping.

CodePudding user response:

Answer to question 1: it has a null dimension because it is in some sense a row vector rather than a column vector. I say "in some sense" because row and column only make sense for 1 dimensional arrays, so let me illustrate with 1 dimensional arrays.

x = np.array([1,1,1])
print(x)       # array([1, 1, 1])
print(x.shape) # (3,) <-- note the the null dimension.

x.reshape((3,1)) # This answers your second question, btw.
print(x)         # array([[1],
                 #        [1],
                 #        [1]]) <-- a column vector.

To reiterate: the reshape command is what you use to do the conversion you require.

  • Related