Converting an array from one shape to another? - python

I have a video i.e image sequences saved in an array. The output is:
Output:
(13,9,9)
Where the 13 represents the 13 image sequences and the two 9's represent the pixels. I wish to convert the array into an output like:
Output:
(81,13)
Where the 81 represents the 81 pixel instances and the 13 is capturing the time domain i.e. the video frames in time. I will then be feeding this into my CNN.
Does anyone have any suggestions? As using array.reshape(81,13) of course doesn't work.

Assuming x is the original video 3D array, you need this to convert it to the desired 2D array:
import numpy as np
x2d = x.transpose(1, 2, 0).reshape(-1, x.shape[0])
This also works:
x2d = x.reshape(x.shape[0], -1).T
Essentially the concept is to reshape or transpose the array in such a way that the elements you want in a row should end up in contiguous memory locations.

Related

Changing the value of values after a particular index along one axis in a 3D numpy array

I have a 3d array of format given below.
The below is the one sample of the 3D array, like it , it contain more than 1000.
sample
shape of the 3D array is (1000 x 10 x 5)
The image contain one element (10 x 5)
I want to change the value to 0 after the 3rd one on the last value
check the figure below
desired
I want to change like it for all the 1000 elements in my array.
Is there a better way to do it other than using "for loop" ?
import numpy as np
# Your array here:
arr = np.arange(50000).reshape(1000, 10, 5)
# Solution:
arr[:, 3:, -1] = 0

How to convert a python list of 3D arrays to a single 3D array of specific shape?

I have a list itemlist that has 25 3D Arrays with shape (128x128x3)
I want it to convert/merge all these values into a single common array, basically create a image out of it. I'm expecting the new shape to be (640, 640, 3) meaning 5 rows and 5 columns of (128, 128)
I tried the following, but it is giving weird results, mostly repeating some arrays:
out = np.concatenate(itemlist).ravel()
out.shape ##(1228800,)
img = np.reshape(out, (640,640,3))
img.shape ## (640, 640, 3)
The final shape I get is correct but visually it looks like set of repeated images, is something wrong with logic?
With 25 (128,128,3) arrays
out = np.concatenate(itemlist)
should produce a (25*128, 128,3) array
out = out.ravel()
should produce 25128128*3
out.shape ##(1228800,)
(640,640,3) matches in total number of elements, but it will not produce meaningful images.
Working backwards:
(5*128, 5*128,3) => (5,128,5,128,3) => (5,5,128,128,3) => (25,128,128,3)
That requires a couple of reshapes, and one tranpose.

How can I save multiple images with labels in a numpy array?

Previously this type of questions was asked. But my one is a little different. For example, I have 20 files ( 20 matrix ) in one folder, each of the matrix is 40*40 in dimension. Also, these 20 files represent 20 different categories.
Now I want to create one single Numpy array, where the length will be 20*2. In the first column of each row, I want to store a 40*40 matrix. Thus all the 20 files I want to cover. Then I want to keep the labels as number e.g. 1,2,3 .... 20. later on, when I will call the createdNumpyArray[0,0], it should show the first matrix. How to do this in Python?
I would use a dictionary to store the mapping from keys to images.
ind_to_image = {
0: numpy array with 40 x 40 shape,
...,
19: numpy array with 40 x 40 shape,
}
and save the indices(keys) to the first column of the 20 x 2 array
because I think you can't directly save a numpy array into an element of another array. The following code will get a ValueError
arr1 = np.zeros((20, 2))
arr1[0, 0] = np.zeros((40, 40))

How would you reshape a collection of images from numpy arrays into one big image?

I'm having some trouble reshaping a 4D numpy array to a 2D numpy array. Currently the numpy array is follows, (35280L, 1L, 32L, 32L). The format is number of images, channel, width, height. Basically, I have 35280 image blocks that are 32x32 and I want to combine the image blocks (keeping the indices) to create one big image.
Reshaping is not sufficient, you must carefully rearrange your data with swapaxes.
Sample data :
dims=nbim,_,h,w=np.array([6,1,7,6])
data=arange(dims.prod()).reshape(dims)%256
The images :
figure()
for i in range(nbim):
subplot(1,nbim,i+1)
imshow(data[i,0],vmin=0,vmax=255)
and the big image :
#number of images in each dim :
nh = 2 # a choice
nw=nbim // nh
bigim=data.reshape(nh,nw,h,w).swapaxes(1,2).reshape(nh*h,nw*w)
figure()
imshow(bigim)
You have an array like this:
images = np.random.randint(0,256,(35280, 1, 32, 32))
The first thing you need is to figure out (somehow) what the width of the final image is supposed to be. Let's say for this example that it's (441 * 32, 80 * 32).
Then you can do:
image = images.swapaxes(0,2).reshape((441 * 32, -1))
This gives you almost what you need, except the rows are interleaved, so you have:
AAABBBCCC
DDDEEEFFF
GGGHHHIII
AAABBBCCC
DDDEEEFFF
GGGHHHIII
You can then use "fancy indexing" to rearrange the rows:
image[np.array([0,3,1,4,2,5])]
Now you have:
AAABBBCCC
AAABBBCCC
DDDEEEFFF
DDDEEEFFF
GGGHHHIII
GGGHHHIII
I will leave as an exercise the part where you generate the fancy indexing sequence.

How to create numpy ndarray from numpy ndarrays?

I used the MNIST dataset for training a neural network, where the training data is returned as a tuple with two entries. The first entry contains the actual training images. This is a numpy ndarray with 50,000 entries. Each entry is, in turn, a numpy ndarray with 784 values, representing the 28 * 28 = 784 pixels in a single MNIST image.
I would like to create a new training set, however I do not know how to create an ndarray from other ndarrays. For instance, if I have the following two ndarrays:
a = np.ndarray((3,1), buffer=np.array([0.9,1.0,1.0]), dtype=float)
b = np.ndarray((3,1), buffer=np.array([0.8,1.0,1.0]), dtype=float)
how to make a third one containing these two?
I tried the following but it creates only one entry.
c = np.ndarray((1,6,1), buffer=np.array(([a],[b])), dtype=float)
I would need it to be two entries.
Thanks, in the meanwhile I figured out it is simply:
c = np.array((a, b))

Categories

Resources