I'm newbie with Python and also with Numpy.
I have this code:
one_array.shape
When I run it, I get this output:
(20, 48, 240, 240)
one_array is a Numpy Array that has 20 images.
What do mean the other three numbers in shape output (48, 240, 240)?
Your array consist of 20 images, each of them is the size 48X240X240. Which is odd, I would expect that it will be something like 240X240X3 but for some reason you have way more channels (referring to RGB). ]
So the shape function return the size of dimension along each axis (the current shape of the entire array), so in your case there is (20, 48, 240, 240)
Edit:
As the user said, each image consist of 48 NITFY images of 1 channel which explain the output of shape
Imagine your Numpy Array as a Vector that can be in one dimension, but in your case it looks like it is in dimension 4.
(20, 4, 240, 240) means a big matrix composed of 20 x 4 x 240 x 240 elements.
one_array.shape == (20, 48, 240, 240) means that one_array is a 4-dimensional array with 20*48*240*240 or 55296000 elements.
You are right, you can think of one_array as an array with 20 elements, in which is element in another array with shape (48, 240, 240). However, usually is it better to think that one_array is a 4 dimensional array, that has a total of 20x48x240x240 = 55296000 elements.
Related
I have an array A of shape (30,) where each row has a list with 2000 elements. I want to convert this into a 2d array of shape (30, 2000). This is what I tried
A = np.reshape(A, (30, -1))
But, running this gives me an array of shape (30, 1) rather than (30, 2000). What should I do to get the correct shape?
where each row has a list with 2000 elements
As Ahmed Mohamed AEK points out in the comments this won't work as the numpy object is of shape (30,). One easy fix is to stack them into a 30 by 2000 np.array.
For example:
A = np.vstack(A)
or equvalently:
A = np.stack(A, axis=0)
I initially had a 3d numpy array of arr_3d of (85, 150, 150) dimension (float values) referred to as original_npy_3d (85 layers, and 150 grids in x and 150 grids in y direction).
I converted it to 2d arr(0, 150, 150) (the first layer) to perform some operation and modify the data in 2D. Now, I would like to repeat the same operation on every layer (0 or rather 1 to 85) and then convert everything back to the original size (85, 150, 150). Is there a way in Python to do that?
arr_3d = np.empty((85, 150, 150), float)
for item in range(85):
arr= original_npy_3d[item,:,:]
#some operation on arr of the size (150, 150)
return arr_3d #with all of the generated arr for every layer appended to it
I have a numpy array of shape, (320, 320, 3). I want to repeat/duplicate this data 10 times, and want to get new array of shape (10, 320, 320, 3).
How to do it?
array = np.ones((320, 320, 3))
print (array.shape)
(320, 320, 3)
I tried as:
res = np.tile(array, 10)
print (res.shape)
(320, 320, 30).
But I want shape of,
(10, 320, 320, 3)
We can use np.broadcast_to -
np.broadcast_to(a,(10,)+a.shape).copy() # a is input array
If we are okay with a view instead, skip .copy() for a virtually free runtime and zero memory overhead.
We can also use np.repeat -
np.repeat(a[None],10,axis=0)
You can use np.resize, which will tile if the new size is larger than the old one:
array = np.ones((320, 320, 3))
new_array = np.resize(array, (10, *array.shape))
print(new_array.shape)
# (10, 320, 320, 3)
From the docs:
numpy.resize(a, new_shape): If the new array is larger than the original array, then the new array is filled with repeated copies of a.
res = np.tile(array, (10,1,1,1))
print (res.shape)
I have 4554 images in my numpy array X_train with the shape of the array as follows.
print(np.shape(X_train))
(4554,) # TOtal numbe of images
X_train[0].shape
(120, 120, 4) # Each image is 120x120 with 4 channels.
Now I want to reshape the array into (4554, 120, 120, 4), so that when I print
print(np.shape(X_train)
It gives me the shape (4554, 120, 120, 4) instead of (4554,).
I tried the following reshape method but it gives me error.
X_train=X_train.reshape((X_train.shape[0],X_train[0].shape))
Error: TypeError: 'tuple' object cannot be interpreted as an integer
You're looking for the numpy.stack() method.
If you have a list of 3d matrices, you can make a 4d matrix like so:
numpy.stack(your_list_of_training_data, axis=0)
See the documentation here for an explanation: https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.stack.html
To convert 2D array to 4D, try to flat 2D array first and then reshape as below , hope it works.
num_images = 4554
X_train_flat = [img.flatten() for img in X_train]
X_train_flat = np.array(X_train_flat)
X_train = X_train_flat.reshape(num_images, 120, 120, 4)
I have a numpy array U with shape (20, 50): 20 spatial points, in a space of 50 dimensions.
How can I transform it into a (20, 1, 50) array, i.e. 20 rows, 1 column, and each element is a 50 dimension point? Kind of encapsulating each row as a numpy array.
Context
The point is that I want to expand the array along the columns (actually, replicating the same array along the columns X times) using numpy.concatenate. But if I would do it straight away I would not get the result I want.
E.g., if I would expand it once along the columns, I would get an array with shape (20, 100). But what I would like is to access each element as a 50-dimensional point, so when I expand it I would expect to have a new U' with shape (20, 2, 50).
You can do U[:, None, :] to add a new dimension to the array.
You can also use reshape:
import numpy as np
a = np.zeros((20, 50))
print a.shape # (20, 50)
b = a.reshape((20, 1, 50))
print b.shape # (20, 1, 50)