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
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.
Ensure you have NumPy installed:
pip install numpy
Then, import it:
import numpy as np
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.
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.
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.
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)