Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Find unique rows in a NumPy array

Finding unique rows in a matrix or 2D array is a common operation, especially when dealing with datasets that might have duplicate entries. Let's go through a tutorial on how to find unique rows in a NumPy 2D array.

Finding Unique Rows in a NumPy Array

1. Setup:

Ensure you have NumPy installed:

pip install numpy

Then, import it:

import numpy as np

2. Creating a Sample 2D Array:

Let's consider the following 2D array (matrix) with some duplicate rows:

matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [1, 2, 3],
                   [7, 8, 9],
                   [4, 5, 6]])
print(matrix)

Notice that the first and third rows are the same, as well as the second and fifth rows.

3. Finding Unique Rows:

To find the unique rows, we can use the numpy.unique function with the axis argument:

unique_rows = np.unique(matrix, axis=0)
print(unique_rows)

Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

The duplicate rows have been removed, and only unique rows remain in the resulting array.

4. Returning Indexes of Unique Rows:

Sometimes, you might want to know the indices of the original rows that are unique. You can do this by setting the return_index argument to True:

unique_rows, indices = np.unique(matrix, axis=0, return_index=True)
print("Unique rows:")
print(unique_rows)
print("\nIndices of unique rows in the original matrix:")
print(indices)

Output:

Unique rows:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Indices of unique rows in the original matrix:
[0 1 3]

The output tells you that the unique rows in the original matrix are at indices 0, 1, and 3.

5. Additional Considerations:

  • Make sure the input to np.unique is a 2D array (or matrix). If you have higher-dimensional data, you might need to reshape or process it differently.

  • The order of the output rows might differ based on the version of NumPy and the specific data. If the order is essential, consider additional sorting operations.

6. Conclusion:

Finding unique rows in a 2D NumPy array is a simple and efficient operation using the numpy.unique function with the axis argument. This can be particularly useful for data cleaning and preprocessing tasks when working with datasets in Python.

1. NumPy unique rows in 2D array:

Finding unique rows in a 2D NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding unique rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

2. Find distinct rows in NumPy array:

Finding distinct rows in a NumPy array using the unique function.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding distinct rows
distinct_rows = np.unique(array_2d, axis=0)

print("Distinct Rows:")
print(distinct_rows)

3. Remove duplicate rows in NumPy array:

Removing duplicate rows from a NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Removing duplicate rows
unique_rows, indices = np.unique(array_2d, axis=0, return_index=True)
unique_indices = np.sort(indices)

print("Unique Rows:")
print(unique_rows)
print("Indices of Unique Rows:")
print(unique_indices)

4. Python NumPy unique rows in a matrix:

Finding unique rows in a matrix using NumPy.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding unique rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

5. Finding unique rows in a 2D NumPy array:

Identifying unique rows in a 2D NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding unique rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

6. NumPy unique rows along axis:

Finding unique rows along a specific axis in a 2D NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding unique rows along axis 0 (rows)
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

7. Eliminate duplicate rows in NumPy array:

Eliminating duplicate rows from a NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Eliminating duplicate rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

8. Unique row identification in NumPy:

Identifying unique rows in a 2D NumPy array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Identifying unique rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)

9. Distinct rows in a matrix with NumPy:

Finding distinct rows in a matrix using NumPy.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Finding distinct rows
distinct_rows = np.unique(array_2d, axis=0)

print("Distinct Rows:")
print(distinct_rows)

10. NumPy unique function for rows:

Using the NumPy unique function to find unique rows in a 2D array.

import numpy as np

# Creating a 2D NumPy array with duplicate rows
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [1, 2, 3],
                     [7, 8, 9]])

# Using the unique function to find unique rows
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows:")
print(unique_rows)