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 values in an array is a common operation, especially in data preprocessing. NumPy provides a convenient function for this: numpy.unique()
. Here's a tutorial on how to get the unique values from an array using NumPy:
Initialization
Start by importing NumPy and creating a sample array:
import numpy as np array = np.array([1, 2, 3, 2, 4, 5, 3, 6, 7, 5]) print("Original Array:", array)
This will display:
Original Array: [1 2 3 2 4 5 3 6 7 5]
Using numpy.unique()
Now, to get the unique values:
unique_values = np.unique(array) print("Unique Values:", unique_values)
This will produce:
Unique Values: [1 2 3 4 5 6 7]
Getting Unique Values with Counts
If you also want to know the counts of each unique value, you can use the return_counts
argument:
unique_values, counts = np.unique(array, return_counts=True) print("Unique Values:", unique_values) print("Counts:", counts)
This will display:
Unique Values: [1 2 3 4 5 6 7] Counts: [1 2 2 1 2 1 1]
As you can see, the value 2
appears twice, 3
appears twice, 5
appears twice, and the other values appear only once.
Working with 2D Arrays
The numpy.unique()
function can also work with 2-dimensional arrays:
matrix = np.array([[1, 2, 3], [4, 2, 5], [6, 7, 3]]) unique_values_matrix = np.unique(matrix) print("Unique Values from 2D Array:", unique_values_matrix)
This will output:
Unique Values from 2D Array: [1 2 3 4 5 6 7]
Conclusion
Finding unique values in arrays is particularly useful in data analysis for understanding the distinct elements in your dataset. Using numpy.unique()
, you can easily obtain and work with these unique values.
Remember, the unique values are always returned in sorted order. If you need the unsorted unique values, you can use the return_index
argument to get the indices of the unique values in the original array, and then use these indices to fetch the values in their original order.
Finding unique values involves identifying elements that appear only once in an array.
import numpy as np # Create a NumPy array with duplicate values array = np.array([1, 2, 3, 1, 2, 4, 5, 6, 5]) # Find unique values using np.unique() unique_values = np.unique(array) print("Original Array:") print(array) print("\nUnique Values:") print(unique_values)
Utilize np.unique()
to obtain unique values from an array in NumPy.
# Assuming 'array' is already defined # Find unique values using np.unique() unique_values = np.unique(array) print("Array:") print(array) print("\nUnique Values:") print(unique_values)
Example code demonstrating the extraction of unique values using np.unique()
in NumPy.
# Assuming 'array' is already defined # Find unique values using np.unique() unique_values = np.unique(array) print("Array:") print(array) print("\nUnique Values:") print(unique_values)
Extract unique values from both 1D and 2D arrays using np.unique()
in NumPy.
import numpy as np # Create a 1D array with duplicate values array_1d = np.array([1, 2, 3, 1, 2, 4, 5, 6, 5]) # Create a 2D array with duplicate values array_2d = np.array([[1, 2, 3], [4, 5, 6], [1, 2, 3]]) # Find unique values in 1D array unique_values_1d = np.unique(array_1d) # Find unique values in 2D array unique_values_2d = np.unique(array_2d) print("1D Array:") print(array_1d) print("\nUnique Values in 1D Array:") print(unique_values_1d) print("\n2D Array:") print(array_2d) print("\nUnique Values in 2D Array:") print(unique_values_2d)
Sample code illustrating the process of extracting unique values using np.unique()
in NumPy.
# Assuming 'array' is already defined # Find unique values using np.unique() unique_values = np.unique(array) print("Array:") print(array) print("\nUnique Values:") print(unique_values)
Use np.unique()
to remove duplicate values from an array in NumPy.
# Assuming 'array' is already defined # Find unique values using np.unique() to remove duplicates unique_values = np.unique(array) print("Array:") print(array) print("\nUnique Values (No Duplicates):") print(unique_values)
Understand the difference between unique values and distinct values in NumPy arrays.
# Assuming 'array' is already defined # Find unique values using np.unique() unique_values = np.unique(array) # Find distinct values by converting the array to a set distinct_values = set(array) print("Array:") print(array) print("\nUnique Values:") print(unique_values) print("\nDistinct Values:") print(distinct_values)
Apply np.unique()
in NumPy for array analysis and obtaining unique values.
# Assuming 'array' is already defined # Find unique values using np.unique() unique_values = np.unique(array) print("Array:") print(array) print("\nUnique Values:") print(unique_values)