0

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 this? I tried numpy.mean, but it does not solve the issue.

[[1,2,3,4],
[1,2,3,5],  --> = [(1+1+1)/3, (2+2+2)/3, (3+3+3)/3, (4+5+6)/3] --> [1,2,3,5] 
[1,2,3,6]]
matchifang
  • 5,190
  • 12
  • 47
  • 76

2 Answers2

3
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.

Johannes
  • 3,300
  • 2
  • 20
  • 35
  • Please think twice before answering questions that are blatant duplicates. Vote to close, when you have the privilege to. Sorry the advice comes this late, but better late than never. – cs95 May 30 '18 at 05:59
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.])
cs95
  • 379,657
  • 97
  • 704
  • 746
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Sorry, about what happened here. After many months and answers as a seasoned user here I'd like to say that neither the question nor its answers deserve the upvotes it's gotten. This is a blatant duplicate. – cs95 May 30 '18 at 05:56