Answer to Question #283351 in Python for ramk

Question #283351

Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis.


1
Expert's answer
2021-12-28T12:59:44-0500
# Python 3.9.5
import numpy as np

def array_variance(array_numbers):
    result1 = np.var(array_numbers).round(2)
    result2 = np.mean((array_numbers - np.mean(array_numbers)) ** 2).round(2)
    assert np.allclose(result1, result2)
    return result1

def array_std(array_numbers):
    result1 = np.std(array_numbers).round(2)
    result2 = np.sqrt(np.mean((array_numbers - np.mean(array_numbers)) ** 2)).\
        round(2)
    assert np.allclose(result1, result2)
    return result1

def array_mean(array_numbers):
    result1 = np.mean(array_numbers).round(2)
    result2 = np.average(array_numbers).round(2)
    assert np.allclose(result1, result2)
    return result1

def enter_array_numbers():
    enter_array = input('Enter numbers separate by space: ').split()
    enter_array = [int(i) for i in enter_array]
    return enter_array

def main():
    array_numbers = enter_array_numbers()
    print("\nMean: ", array_mean(array_numbers))
    print('\nStd: ', array_std(array_numbers))
    print('\nVariance: ', array_variance(array_numbers))

if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS