Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis.
# 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()
Comments
Leave a comment