Search code examples
pythonarraysstdmeanvar

Mean, Var, and Std


The Task is You are given a 2-D array of size X. Your task is to find:

  1. The mean along axis
  2. The var along axis
  3. The std along axis

Input Format The first line contains the space separated values of and . The next lines contains space separated integers.

Output Format First, print the mean. Second, print the var. Third, print the std.

Sample Input

2 2
1 2
3 4

Sample Output

[ 1.5  3.5]
[ 1.  1.]
1.11803398875

My Code:

import numpy
N,M = map(int, input().split(" "))
A = numpy.array([input().split() for _ in range(N)],int)
print(numpy.mean(A, axis = 1))
print(numpy.var(A, axis = 0))
print(round(numpy.std(A, axis = None),11))

The output: enter image description here

I seem to have some indentation issue or my printed result than the expected, there is a space in front of the first vertical elements of the array. Am I doing anything wrong?


Solution

  • Anywhere between importing numpy and printing numpy data. It tells the numpy print formatter to use the default settings from numpy version 1.13 instead of numpy version 1.14 (which is the current version). The problem-set results are canned, and were apparently done with the old numpy, so if you don't do this you get various format mismatches which causes failures in the tests even when you got the actual answers correct.

    So use np.set_printoptions(legacy='1.13')

    import numpy as np 
    
    n,m = map(int, input().split())
    b = []
    for i in range(n):
        a = list(map(int, input().split()))
        b.append(a)
    
    b = np.array(b)
    
    np.set_printoptions(legacy='1.13')
    print(np.mean(b, axis = 1))
    print(np.var(b, axis = 0))
    print(np.std(b))