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)
Related
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 single 2D array that I want to stack identical versions of into a third dimension, specifically axis=1. The following code does the job, but it's very slow for a 2D array of size (300,300) stacked into a third-dimension of length 300.
arr_2d = arr_2d.reshape(arr_2d.shape[0],arr_2d.shape[1],1)
arr_3d = np.empty((sampling,sampling,sampling)) # allocate space
arr_3d = [arr_3d[:,:,i]==arr_2d for i in range(sampling)]
Is there a better, more efficient way of doing this?
You can use numpy.repeat after you add a new third dimension to stack on:
import numpy as np
arr = np.random.rand(300, 300)
# arr.shape => (300, 300)
dup_arr = np.repeat(arr.reshape(*arr.shape, 1), repeats=10, axis=-1)
# dup_arr.shape => (300, 300, 10)
As commented by #xdurch0 and since you're stacking your copies along the last dimension, you can also use numpy.tile:
dup_arr = np.tile(arr.reshape(*arr.shape, 1), reps=10)
# dup_arr.shape => (300, 300, 10)
I have 5 large size numpy array, I want to merge them into one numpy array.
Using np.concatenate doesn't help because MemoryError:Unable to allocate ...
so i decide to use np.memmap.
The shape of my arrays as follow :
#print(arrayA.shape) (29097, 280, 212, 3)
#print(arrayB.shape) (16058, 280, 212, 3)
#print(arrayC.shape) (15412, 280, 212, 3)
#print(arrayD.shape) (21634, 280, 212, 3)
#print(arrayF.shape) (9477 , 280, 212, 3)
my Code:
import glob
import numpy as np
npfiles= glob.glob("D:/mycode/*.npy")
npfiles.sort()
#print(npfiles)
# create a memory-mapped array
pred = np.memmap('memm4', dtype='uint8', mode='w+', shape=(91678,280,212,3))
print(pred.shape)
for i,npfile in enumerate(npfiles):
pred[i,:,:,:]=np.load(npfile)
np.save('D:/mycode/pred.npy',pred)
but it shows me this problem "cann't broadcast input array from shape (29097,280,212,3) into shape
(280,212,3)
could some one help me and thanks
Currently you are putting a 3 dimension tensor into a 4 dimension 1
the i variable contains the index of the file from 0 4. Hence pred[i,:,:,:] has only three dimension
but you need to indicate where the array is going to be stored in memory.
last_index = 0
for npfile in npfiles:
temporary_array = np.load(npfile)
pred[last_index:last_index+ len(temporary_array),:,:,:] = temporary_array
last_index += len(temporary_array)
you might also want to try something like hdf5 / that can allow you to store large arrays easily.
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.
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)