Home >Backend Development >Python Tutorial >How Can I Efficiently Find Unique Rows in a NumPy Array?

How Can I Efficiently Find Unique Rows in a NumPy Array?

Linda Hamilton
Linda HamiltonOriginal
2024-12-30 06:37:08381browse

How Can I Efficiently Find Unique Rows in a NumPy Array?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn