Table of Contents
Removing duplicate elements from a NumPy array refers to the process of eliminating any repeated values within the array. This can be achieved by using the unique() function, which returns a new array with only the unique elements present in the original array. This function is useful for data analysis and manipulation, as it helps in accurately representing data without any redundancy. In order to remove duplicate elements, the unique() function can be applied to the desired array, and the resulting array can be assigned to a new variable, effectively removing any duplicate values. This process ensures efficient and organized data handling, making it an essential step in data analysis using NumPy arrays.
Remove Duplicate Elements from NumPy Array
You can use the following methods to remove duplicate elements in NumPy:
Method 1: Remove Duplicate Elements from NumPy Array
new_data = np.unique(data)Method 2: Remove Duplicate Rows from NumPy Matrix
new_data = np.unique(data, axis=0)
Method 3: Remove Duplicate Columns from NumPy Matrix
new_data = np.unique(data, axis=1)
The following examples show how to use each method in practice.
Example 1: Remove Duplicate Elements from NumPy Array
The following code shows how to remove duplicate elements from a NumPy array:
import numpy as np
#create NumPy array
data = np.array([1, 1, 1, 2, 2, 4, 5, 5, 5, 5, 7, 8])
#create new array that removes duplicates
new_data = np.unique(data)
#view new array
print(new_data)
[1 2 4 5 7 8]
Notice that all duplicates have been removed from the NumPy array and only unique values remain.
Example 2: Remove Duplicate Rows from NumPy Matrix
The following code shows how to remove duplicate rows from a NumPy matrix:
import numpy as np
#create NumPy matrix
data = np.array([[1, 5, 5, 8],
[1, 5, 5, 8],
[6, 2, 3, 4],
[6, 2, 3, 4]])
#create new array that removes duplicate rows
new_data = np.unique(data, axis=0)
#view new matrix
print(new_data)
[[1 5 5 8]
[6 2 3 4]]
Notice that all duplicate rows have been removed from the NumPy matrix and only unique rows remain.
Example 3: Remove Duplicate Columns from NumPy Matrix
import numpy as np
#create NumPy matrix
data = np.array([[1, 1, 5, 8, 1],
[1, 1, 2, 6, 1],
[4, 4, 3, 8, 4]])
#create new matrix that removes duplicate columns
new_data = np.unique(data, axis=1)
#view new matrix
print(new_data)
[[1 5 8]
[1 2 6]
[4 3 8]]
Notice that all duplicate columns have been removed from the NumPy matrix and only unique columns remain.
The following tutorials explain how to perform other common tasks in NumPy:
Cite this article
stats writer (2024). How can I remove duplicate elements from a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-duplicate-elements-from-a-numpy-array/
stats writer. "How can I remove duplicate elements from a NumPy array?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-duplicate-elements-from-a-numpy-array/.
stats writer. "How can I remove duplicate elements from a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-duplicate-elements-from-a-numpy-array/.
stats writer (2024) 'How can I remove duplicate elements from a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-duplicate-elements-from-a-numpy-array/.
[1] stats writer, "How can I remove duplicate elements from a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I remove duplicate elements from a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
