Skip to content Skip to sidebar Skip to footer

How To Find The Mean Of The Value At An Index In Numpy

Suppose I have a numpy array as show below and I want to calculate the mean of values at index 0 of each array (1,1,1) or index 3 (4,5,6). Is there a numpy function that can solve

Solution 1:

a = array([[1, 2, 3, 4],
       [1, 2, 3, 5],
       [1, 2, 3, 6]])

np.mean(a, axis=0)

-> array([ 1.,  2.,  3.,  5.])

The parameter axis lets you select the direction across which you want to calculate the mean.

Solution 2:

Take the mean along the first axis - axis 0:

>>>a = np.array([[1,2,3,4],...              [1,2,3,5],  ...              [1,2,3,6]])>>>a.mean(axis=0)
array([ 1.,  2.,  3.,  5.])

Post a Comment for "How To Find The Mean Of The Value At An Index In Numpy"