Table of Contents
Removing specific elements from a NumPy array can be achieved using the `np.delete()` function. This function takes in the array, the index or indices of the elements to be removed, and the axis along which the elements should be removed. It then returns a new array with the specified elements removed. Additionally, the `np.squeeze()` function can be used to remove single-dimensional entries from the array. Other methods such as boolean indexing or masking can also be used to remove elements based on certain conditions. By using these techniques, specific elements can be easily removed from a NumPy array, allowing for efficient data manipulation and analysis.
Remove Specific Elements from NumPy Array
You can use the following methods to remove specific elements from a NumPy array:
Method 1: Remove Elements Equal to Specific Value
#remove elements whose value is equal to 12
new_array = np.delete(original_array, np.where(original_array == 12))
Method 2: Remove Elements Equal to Some Value in List
#remove elements whose value is equal to 2, 5, or 12
new_array = np.setdiff1d(original_array, [2, 5, 12])Method 3: Remove Elements Based on Index Position
#remove elements in index positions 0 and 6
new_array = np.delete(original_array, [0, 6])The following examples show how to use each method in practice.
Example 1: Remove Elements Equal to Specific Value
The following code shows how to remove all elements from a NumPy array whose value is equal to 12:
import numpy as np
#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])
#remove elements whose value is equal to 12
new_array = np.delete(original_array, np.where(original_array == 12))
#view new array
print(new_array)
[1 2 2 4 5 7 9]
Notice that both elements in the array that were equal to 12 have been removed.
Example 2: Remove Elements Equal to Some Value in List
The following code shows how to remove all elements from a NumPy array whose values is equal to 2, 5, or 12:
import numpy as np
#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])
#remove elements whose value is equal to 2, 5, or 12
new_array = np.setdiff1d(original_array, [2, 5, 12])
#view new array
print(new_array)
[1 4 7 9]
Notice that all elements whose value was 2, 5, or 12 have been removed.
Example 3: Remove Elements Based on Index Position
import numpy as np
#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])
#remove elements in index positions 0 and 6
new_array = np.delete(original_array, [0, 6])
#view new array
print(new_array)
[ 2 2 4 5 7 12 12]
Notice that the elements in index position 0 (with value of 1) and index position 6 (with value of 9) have both been removed from the NumPy array.
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). How can specific elements be removed from a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-numpy-array/
stats writer. "How can specific elements be removed from a NumPy array?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-numpy-array/.
stats writer. "How can specific elements be removed from a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-numpy-array/.
stats writer (2024) 'How can specific elements be removed from a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-specific-elements-be-removed-from-a-numpy-array/.
[1] stats writer, "How can specific elements be removed from a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can specific elements be removed from a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
