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

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?

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.

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

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)

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 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]

Numpy two matrices, pairwise dot product of rows [duplicate]

This question already has answers here:
Vectorized way of calculating row-wise dot product two matrices with Scipy
(5 answers)
Closed 6 years ago.
We are currently working on a python project and have to vectorize a lot due to performance constraints. We end up with the following calculation: We have two numpy arrays of shape (20,6) and want to calculate the pairwise dot product of the rows, i.e. we should obtain a (20,1) matrix in the end, where each row is the scalar obtained by the respective vector dot multiplication.
You can multiply the two arrays element wise and then do sum by rows, and then you have an array where each element is a dot product from rows of the two original arrays:
a = np.array([[1,2], [3,4]])
b = np.array([[3,4], [2,1]])
(a * b).sum(axis=1)
# array([11, 10])

Categories