Numpy remove a dimension from np array
When the shape of your array is (106, 106, 3)
, you can visualize it as a table with 106 rows and 106 columns filled with data points where each point is array of 3 numbers
which we can represent as [x, y ,z]
. Therefore, if you want to get the dimensions (106, 106)
, you must make the data points in your table of to not be arrays but single numbers. You can achieve this by extracting either the x-component, y-component or z-component of each data point or by applying a function that somehow aggregates the three component like the mean, sum, max etc. You can extract any component just like @matt Messersmith suggested above.
You could use numpy's fancy indexing (an extension to Python's built-in slice notation):
x = np.zeros( (106, 106, 3) )
result = x[:, :, 0]
print(result.shape)
prints
(106, 106)
A shape of (106, 106, 3)
means you have 3 sets of things that have shape (106, 106)
. So in order to "strip" the last dimension, you just have to pick one of these (that's what the fancy indexing does).
You can keep any slice you want. I arbitrarily choose to keep the 0th, since you didn't specify what you wanted. So, result = x[:, :, 1]
and result = x[:, :, 2]
would give the desired shape as well: it all just depends on which slice you need to keep.
Just take the mean value over the colors dimension (axis=2
):
Xtrain_monochrome = Xtrain.mean(axis=2)