In this Mean, Var, and Std problem You are given a 2-D array of size N X M. Your task is to find:

  • The mean along axis 1
  • The var along axis 0
  • The std along axis None


HackerRank Mean, Var, and Std solution in python


Problem solution in Python 2 programming.

import numpy
l=map(int,raw_input().split())
m=[]
for i in range(l[0]):
    m.append(map(int,raw_input().split()))
m=numpy.array(m)
print numpy.mean(m,axis=1)
print numpy.var(m,axis=0)
print numpy.std(m,axis=None)


Problem solution in Python 3 programming.

import numpy as np
n, m = map(int, input().split())
k = np.array([input().split() for _ in range(n)],dtype = int)
np.set_printoptions(legacy='1.13')
print(np.mean(k,axis=1), np.var(k,axis=0), np.std(k), sep='\n')


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy
n, m = map(int, raw_input().strip().split())
arr = numpy.array([raw_input().split() for _ in range(n)], dtype=int).reshape(n, m)

print numpy.mean(arr, axis=1)
print numpy.var(arr, axis=0)
print numpy.std(arr)


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy as np
n,m=map(int,input().split())
a = np.array([input().split() for _ in range(n)],dtype=np.float)
print(np.mean(a,axis=1))
print(np.var(a,axis=0))
print(np.std(a,axis=None))