Write a NumPy program to calculate averages without NaNs along a given array.
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]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer