## Removing NaN Values from a NumPy Array

Any data scientist or statistician who has worked with numerical data in NumPy will inevitably come across NaN (Not a Number) values. These values can pose a problem, especially when we need to perform mathematical operations on the array.

Luckily, NumPy provides several methods to remove these NaN values, and in this article, we’ll explore three different approaches to tackle this issue. Method 1: isnan()

The first method involves using the built-in function isnan() to locate all the NaN values in a NumPy array and then remove them.

The isnan() function returns a Boolean array of the same shape as the input, which is True wherever NaN appears in the input array. To illustrate this, let’s consider the following example:

“` python

## import numpy as np

arr = np.array([5, np.nan, 8, 1, np.nan, 7, 3])

# create a boolean array where True indicates a NaN value

mask = np.isnan(arr)

# remove all NaN values using the boolean mask

arr = arr[~mask]

print(arr) # Output: [5. 8.

1. 7.

3.]

“`

Here, we first create a NumPy array with six floating-point numbers, including two NaN values. We then use the isnan() function to obtain a boolean mask, where True indicates the NaN values in the array.

Finally, we use the inverted mask to extract all non-NaN values from the original array. Method 2: isfinite()

The second approach involves using the isfinite() function to filter out all the NaN values from a NumPy array.

Unlike the isnan() function, which specifically handles NaN values, isfinite() function identifies and removes all non-finite values, including NaN and infinity. Here’s an example of how we can use the isfinite() function:

“` python

## import numpy as np

arr = np.array([5, np.nan, 8, 1, np.nan, 7, 3])

# keep only the finite values

arr = arr[np.isfinite(arr)]

print(arr) # Output: [5. 8.

1. 7.

3.]

“`

Here, we use the isfinite() function to generate a boolean mask where True indicates all non-finite values. Then, we use this mask to extract only those finite values from the original array.

Method 3: logical_not()

The third and final method employs the logical_not() function, which returns the opposite of a boolean array. We can use this function in conjunction with isnan() or isfinite() to remove NaN values from a NumPy array.

Here’s an example of how we can use the logical_not() function to remove NaN values:

“` python

## import numpy as np

arr = np.array([5, np.nan, 8, 1, np.nan, 7, 3])

# filter out the NaN values

arr = arr[np.logical_not(np.isnan(arr))]

print(arr) # Output: [5. 8.

1. 7.

3.]

“`

In this example, we create a boolean mask that identifies all the NaN values, and then we use the logical_not() function to negate the mask, so True becomes False and vice versa. Finally, we use this negated mask to extract only the non-NaN values from the array.

## Conclusion

In conclusion, NaN values can sometimes cause issues when working with NumPy arrays in scientific or statistical computations. However, NumPy provides several methods to handle these situations and remove NaN values effectively.

Using the techniques outlined in this article, programmers can confidently manipulate their data with ease while still maintaining the integrity of the results. Example 2: Remove NaN Values Using isfinite()

In this example, we’ll show you how to remove NaN values using the isfinite() function.

Unlike the previous method, isfinite() function also filters out all the non-finite values, including infinity and NaN. Let’s consider the following array we want to work with:

“` python

## import numpy as np

arr = np.array([10, np.nan, 25, np.inf, np.nan, 50, 60, np.nan])

“`

As you can see, the array contains NaN values as well as infinite values. To remove them, we can use isfinite() as follows:

“` python

filtered_arr = arr[np.isfinite(arr)]

“`

Here, we pass the original array arr as the argument of the isfinite() function, which returns a Boolean mask that is True wherever the array contains valid finite values and False wherever it contains NaN or infinite values.

Then, we use the Boolean mask to extract only those finite valid values from the array, which is assigned to filtered_arr. If we print the contents of filtered_arr, we should see the resulting array with only valid finite values:

“` python

## print(filtered_arr)

“`

Output:

“`

[10. 25.

50. 60.]

“`

As you can see, the output array contains only the valid float values we wanted to keep.

Any NaN or infinite values have been filtered out automatically. Example 3: Remove NaN Values Using logical_not()

In the third example, we will show you how to use the logical_not() function to remove NaN and infinite values from a NumPy array.

As explained earlier, logical_not() returns the inverse of a Boolean array. This can be used to obtain a boolean mask where True represents the valid finite values in an array.

To demonstrate this technique, let’s consider the following array:

“` python

## import numpy as np

arr = np.array([10, np.nan, 25, np.inf, np.nan, 50, 60, np.nan])

“`

Here the array contains both NaN and infinite values. To remove these, we can use logical_not() in conjunction with isnan() and isfinite() functions to generate a boolean mask that indicates the valid finite values.

“` python

mask = np.logical_and(np.isfinite(arr), np.logical_not(np.isnan(arr)))

filtered_arr = arr[mask]

“`

Here, we first generate a Boolean mask where the logical_and() function returns True wherever the array contains finite values and False otherwise. Then, the logical_not() function returns the inverse of the Boolean mask obtained from np.isnan(), which is True for valid finite values and False for any NaN values.

Finally, we apply the resulting Boolean mask to arr, and this gives us filtered_arr with only the valid finite values:

“` python

## print(filtered_arr)

“`

Output:

“`

[10. 25.

50. 60.]

“`

As you can see, only the valid finite values are kept in the output array.

NaN values and infinite values are removed from the array automatically, making it much easier to perform computations on the remaining values.

## Conclusion

In conclusion, NumPy provides various methods to remove NaN and infinite values from NumPy arrays. Whether you use the isnan() function to remove NaN values only, the isfinite() function to remove non-finite values, or the logical_not() function to obtain a boolean mask where valid finite values are set to True, all of these methods are very effective in handling these types of data.

By applying these techniques, data scientists and statisticians can clean up their data quickly and easily, allowing for smooth and accurate analysis. In summary, NumPy provides several methods to remove NaN and infinite values from arrays effectively.

The three methods, including isnan(), isfinite(), and logical_not(), each have unique advantages depending on the specific use case. By using these methods, data scientists and statisticians can efficiently manipulate data while maintaining the integrity of their results.

Removing NaN values is an essential step in data analysis and can ensure accuracy in analytical models. Therefore, it is essential to master and understand these techniques to ensure the quality of data analysis.