tiling a multi-dimensional numpy array into a lower dimensional array [duplicate] - python

This question already has an answer here:
3d Numpy array to 2d
(1 answer)
Closed 2 years ago.
I have a (100,128,128) shaped array and want to convert it into a (1280,1280) array. Actually (128,128) is the size of the images and there are 100 of them. I want to tile all images on a single grid like
im1 im2 im3 ... im10
im11 im12 im13 ... im20
and so on.

Assuming arr is your array:
arr=np.broadcast_to(arr,(128,128,100))
arr.reshape(1280,1280)

Related

Numpy ndarray shape parameters [duplicate]

This question already has an answer here:
Very Basic Numpy array dimension visualization
(1 answer)
Closed 1 year ago.
I've a 3 dimensional numpy array. When I try to print it's shape, I get (4, 1, 2). Now I'm struggling to figure out which value corresponds to row, column and depth.
Code:
import numpy as np
zone = np.array([[[221,529]],
[[156,850]],
[[374,858]],
[[452,537]]])
print(zone.shape)
.shape returns dimensions respectively. So it means you have 4 rows, 1 column, and depth of 2.

How do I find the most common RGBA pixel value in a image? [duplicate]

This question already has an answer here:
How would I find the mode (stats) of pixel values of an image?
(1 answer)
Closed 2 years ago.
I have an image of a dog, which has shape (432, 575, 4). The 3rd dimension in the ndarray contains the RGBA values for each pixel. I want to find out what the most common pixel is, i.e. the mode.
For a 2d array, I can use the following line: np.unique(a, axis=0, return_counts=True)
However, I can't work out how to only compare the vectors in the 3rd dimension of a 3d array. This question is similar, but it only works for a 2d array: Finding the most common subarray within a numpy array. If I simply change the axis to '=1`, it doesn't give me the result that I need.
Since you don't care for the image's shape, you can use reshape to flatten your image and use the linked answer:
rgba, counts = np.unique(a.reshape(-1,4), axis=0, return_counts=True)
# the mode here
rgba[np.argmax(counts)]

Turn numpy list into column vector [duplicate]

This question already has answers here:
Numpy reshape 1d to 2d array with 1 column
(7 answers)
Closed 4 years ago.
I can't turn a numpy array into a vector. What I want to do is transform this:
>> x.shape
(784,)
into this:
>> x.shape
(784,1)
x was created in the normal numpy.array() way.
Note: I want to change, not create a new array.
You can add a new axis to a vector in multiple ways: e.g. using np.reshape
x = np.zeros(784)
x0 = x.reshape(784, 1) # shape: (784, 1)
or using np.newaxis while slicing:
x = np.zeros(784)
x0 = x[:,None] # short-hand for x[:,np.newaxis], shape: (784, 1)
Vectors, arrays and lists are totally different data structures, especially in Python. In your question you mentioned I can't turn a numpy array into a vector. Actually what you are trying to do is exactly the opposite, means you want to turn a vector of shape (784,) to an array of shape(784, 1). Anyways #cheersmate gave you the correct answer.

How to multiply 3D array by 2D array, numpy [duplicate]

This question already has answers here:
Multiplying 3D matrix with 2D matrix
(1 answer)
filtering a 3D numpy array according to 2D numpy array
(2 answers)
Closed 4 years ago.
I am standing in front of following problem: I have a stack of images - shape is (x, y, N), and I want to multiply it fast by a 2D array - shape(x, y).
The only way I came up with is to interate through elements of the 2D array and multiply it :
3D[id_x, id_y,:]=2D[id_x, id_y]*3D[id_x, id_y,:].
But for bigger arrays (and this is my case) it would not be time efficient. So here comes by question, do you how to do it without iteration?

How can I index this numpy array? [duplicate]

This question already has answers here:
using an numpy array as indices of the 2nd dim of another array? [duplicate]
(2 answers)
Closed 5 years ago.
Given a numpy matrix a of shape (5,3), and a index vector b of shape (5,), where each entry in the index vector is between 0 to 2, how can I create a new vector c based on a and its index vector b.
Use arange for the other dimension:
c = a[np.arange(5), b]

Categories