Numpy ndarray shape parameters [duplicate] - python

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.

Related

Numpy indexing and slicing not working as intended! (Bug?) [duplicate]

This question already has answers here:
Unexpected behaviour numpy array indexing [duplicate]
(1 answer)
Why using an array as an index changes the shape of a multidimensional ndarray?
(1 answer)
Closed 2 years ago.
Consider the following code
import numpy as np
z = np.zeros((3,5,10,100))
indices = np.array([8, 0, 6, 1])
print(z[:,:,indices,:].shape)
print(z[1,:,indices,:].shape)
The output is as follows:
(3, 5, 4, 100)
(4, 5, 100)
I want to assign z[1,:,indices,:] = some_array where some_array has shape (5,4,100) but that assignment throws an error as there is shape mismatch.
I am confused about the second output (shape of z[1,:,indices,:]). I thought it should be (5,4,100). Why are the first 2 axes getting switched? Is this a bug or is there any explanation why this is the correct behavior?
I got my code to go the desired thing by changing it to:
for i,index in enumerate(indices):
z[1,:,index,:] = some_array[i]

How to calculate Average of n numpy arrays [duplicate]

This question already has answers here:
How to get the element-wise mean of an ndarray
(2 answers)
Closed 3 years ago.
I have 'n' numpy arrays each with shape (128,)
How to get an average numpy array of shape (128,) for the list of numpy arrays.
I have seen the documentation of numpy's average() and mean() which describes that the average is calculated for all the elements in a single numpy array rather than multiple or list of numpy arrays.
Example
numpyArrayList = [ar1,ar2,ar3,ar4...arn]
avgNumpyArray = avg(numpyArrayList)
avgNumpyArray.shape
should give result as (128,)
and this array should contain the average of all the numpy arrays
Thanks in advance
I would use np.mean([ar1,ar2,ar3,ar4...arn], axis=0).
You can achieve this by using the following code
ar = [ar1,ar2,ar3,...,arn]
r = np.mean(ar)
for axis=0 use following
r = np.mean(ar, axis=0)
for axis=1 use following
r = np.mean(ar, axis=1)
something like?
mean=0
n=len(numpyArrayList)
for i in numpyArrayList:
mean += i.sum()/(128.*n)
Edit: misunderstood the question, sry

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]

How to normalize one dimension of a 2-dimensional array in python numpy? [duplicate]

This question already has answers here:
How to normalize a 2-dimensional numpy array in python less verbose?
(12 answers)
Closed 8 years ago.
For example, to normalize each row in a 2-dimensional vector such that the magnitude of a row is one:
import numpy as np
a = np.arange(0,27,3).reshape(3,3)
result = a / norm_of_rows( a )
Such that:
np.sum( result**2, axis=-1 )
# array([ 1., 1., 1.])
The original question, How to normalize a 2-dimensional numpy array in python less verbose?, which people feel my question is a duplicate of, the author actually asks how to make the elements of each row sum to one. This is different than normalizing each row such that its magnitude is one (the sum of the square of each element equals one).
np.max(a[0,:]) will give you the maximum of the 1st row,
np.max(a[1,:]) will give you the maximum of the 2nd row
To normalize the whole matrix just loop through your rows and divide each element by the corresponding max_row

Categories