NumPy: Replace all the nan of a given array with the mean of another array
Replace NaN values with the mean of another array.
Write a NumPy program to replace all the nan (missing values) of a given array with the mean of another array.
Sample Solution:
Python Code:
# Importing the NumPy library
import numpy as np
# Creating NumPy arrays: array_nums1 from 0 to 19 reshaped into a 4x5 array and array_nums2 with NaN values
array_nums1 = np.arange(20).reshape(4, 5)
array_nums2 = np.array([[1, 2, np.nan], [4, 5, 6], [np.nan, 7, np.nan]])
# Printing the original arrays
print("Original arrays:")
print(array_nums1)
print(array_nums2)
# Replacing all the NaN values in array_nums2 with the mean of non-NaN values in array_nums1
array_nums2[np.isnan(array_nums2)] = np.nanmean(array_nums1)
print("\nAll the NaN of array_nums2 replaced by the mean of array_nums1:")
print(array_nums2)
Sample Output:
Original arrays: [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] [[ 1. 2. nan] [ 4. 5. 6.] [nan 7. nan]] All the nan of array_nums2 replaced by the mean of array_nums1: [[1. 2. 9.5] [4. 5. 6. ] [9.5 7. 9.5]]
Explanation:
In the above example -
array_nums1 = np.arange(20).reshape(4,5) creates a 1-dimensional NumPy array containing numbers from 0 to 19 and then reshapes it into a 2-dimensional array with 4 rows and 5 columns.
array_nums2 = np.array([[1,2,np.nan],[4,5,6],[np.nan, 7, np.nan]]) creates a 2-dimensional NumPy array with NaN values.
array_nums2[np.isnan(array_nums2)]= np.nanmean(array_nums1)
In the above code -
- np.nanmean(array_nums1): This part computes the mean of the ‘array_nums1’ while ignoring any NaN values that might be present. In this case, since there are no NaN values in array_nums1, it is equivalent to computing the mean of all elements in array_nums1.
- array_nums2[np.isnan(array_nums2)]: This part selects all NaN values in array_nums2.
- array_nums2[np.isnan(array_nums2)] = np.nanmean(array_nums1) replaces the selected NaN values in array_nums2 with the computed mean from array_nums1.
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics