How can I index this numpy array? [duplicate] - python

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]

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.

Numpy.Append(): ValueError: could not broadcast input array from shape (4) into shape (3) [duplicate]

This question already has answers here:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
I met with a problem when doing appending in NumPy. array_1 will throw an error: ValueError: could not broadcast input array from shape (4) into shape (3) but the bottom one does not, where am I doing it wrong? I need to write a loop to append arrays to each array in array_1.
The blunt way is to convert my 2d-arrays to a 2d-list, but as a keen learner, I am really curious about how to do it properly.
array_1 = np.array([[1,2,3], [4,5,6]])
array_1[0] = np.append(array_1[0], 1)
array_2 = np.array([[1,2,3]])
array_2 = np.append(array_2, 1)
Change it to this:
array_1 = np.array([[1,2,3], [4,5,6]])
array_1 = [np.append(array_1[0], 1), array_1[1]]

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?

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