Q:

Write a NumPy program to calculate averages without NaNs along a given array

0

Write a NumPy program to calculate averages without NaNs along a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
arr1 = np.array([[10, 20 ,30], [40, 50, np.nan], [np.nan, 6, np.nan], [np.nan, np.nan, np.nan]])
print("Original array:")
print(arr1)
temp = np.ma.masked_array(arr1,np.isnan(arr1))
result = np.mean(temp, axis=1)
print("Averages without NaNs along the said array:")
print(result.filled(np.nan))

Sample Output:

Original array:
[[10. 20. 30.]
 [40. 50. nan]
 [nan  6. nan]
 [nan nan nan]]
Averages without NaNs along the said array:
[20. 45.  6. nan]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now