Skip to content Skip to sidebar Skip to footer

How Do You Calc The Mean Along An Axis Of Numpy Array?

I am new to python. Here is my three dimensional array: my_data=numpy.zeros((index1,index2,index3)) For illustration, let's say the sizes are: index1 = 5 index2 = 4 index3 = 100

Solution 1:

Just use np.mean() with the axis keyword:

import numpy as np

np.random.seed(0)

data = np.random.randint(0,5,size=(3,3,3))

Yields:

[[[4 0 3]
  [3 3 1]
  [3 2 4]][[0 0 4]
  [2 1 0]
  [1 1 0]][[1 4 3]
  [0 3 0]
  [2 3 0]]]

Then apply:

np.mean(data,axis=1)

#Or data.mean(axis=1)

Returns:

[[3.33333333 1.66666667 2.66666667]
 [1.         0.66666667 1.33333333]
 [1.         3.33333333 1.        ]]

Post a Comment for "How Do You Calc The Mean Along An Axis Of Numpy Array?"