![]() |
VOOZH | about |
Removing NaN values from a NumPy array is essential for accurate numerical computations and data analysis. NumPy provides efficient methods to identify and filter out missing values, ensuring clean and reliable datasets.
Example:
Input: [[5, nan, 8],
[2, 6, nan],
[nan, 1, 3]]Output: [5. 8. 2. 6. 1. 3.]
The ~ operator reverses the boolean array returned by np.isnan(), keeping only the non-NaN elements.
2D array converted to 1D after removing NaNs -> [12. 5. 7. 2. 61. 1. 1. 5.]
Explanation:
This method removes NaN and infinite values from a NumPy array. np.isfinite() returns True for all finite numbers, allowing you to keep only valid numeric elements.
2D array converted to 1D after removing NaNs -> [12. 5. 7. 2. 61. 1. 1. 5.]
Explanation: np.isfinite(arr): Returns True for all finite numbers (i.e., not NaN or Infinity).
This method helps you filter out all NaN (Not a Number) values from a NumPy array. np.isnan() identifies the NaNs and np.logical_not() reverses the boolean result to select only the valid numbers.
2D array converted to 1D after removing NaNs -> [6. 2. 2. 6. 1. 1.]
Explanation: