Finding Unique Rows in a NumPy Array: An Efficient Solution
In data analysis and processing, it is often necessary to extract unique values from a given dataset. In this context, let's consider the problem of finding unique rows in a NumPy array.
Objective:
Given a NumPy array, the goal is to identify and obtain an array containing only the unique rows in the original array.
Efficient Solution:
As of NumPy version 1.13, an efficient solution for finding unique rows has been introduced. By leveraging the np.unique function and specifying the axis parameter, we can achieve this with ease:
unique_rows = np.unique(original_array, axis=0)
By setting the axis parameter to 0, we instruct NumPy to analyze each row of the original array independently. This operation compares the rows element-wise and returns a new array that contains only the unique rows.
Example:
Consider the following NumPy array a:
a = np.array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0]])
To obtain the unique rows from a, we can use the following code:
unique_rows = np.unique(a, axis=0)
This will produce a new array unique_rows that contains the following rows:
unique_rows = np.array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 0]])
As we can see, the unique rows in the original array have been successfully extracted.
The above is the detailed content of How Can I Efficiently Find Unique Rows in a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!