REMOVE DUPLICATE VALUES FROM NUMPY ARRAY

Removing duplicates from a Num python array, eliminate any repeated values in the array. Resulting in an array where each element appears only once. This can be achieved using the num python unique function.

EXPLANATION

1. ORIGINAL ARRAY: An array containing some duplicate values.

2.NUMPY. UNIQUE FUNCTION: This function identified and returns the unique values in the array.

3.OUTPUT: The resulting array (unique-array) contains only unique values sorted in ascending order by default.

4.  IMPORT NUMPY: First, you need to import the numpy library.

5. CREATE A NUMPY ARRAY: Create an array with duplicate values.

6. USE np. UNIQUE: Apply the np. unique function to the array. This function will return a new array with the duplicate values removed and the elements sorted.

EXAMPLE:

The np. array function creates an array containing duplicate values.

The np. unique function processes the array, remove duplicate, and returns the sorted array with unique values

Array-with-duplicate= np. array([1,2,2,3,4,4,5])

unique-array = np. unique(array-with-duplicate)

print(“original array:”, array-with-duplicates)

print(“Array with unique values:”, unique-array)

 

import numpy as np
data = np. array([1,2,2,3,4,4,5])
new-data = np. unique(data)
print(new-data)

OUTPUT:

[1 2 3 4 5]

Understanding NumPy Array Shape in Python

DETAILED

1. ORIGINAL ARRAY: The original array[1,2,2,3,4,4,5] includes several duplicate values.

.This array was created using the np. array function from numpy.

2. REMOVING DUPLICATE: The np. unique function is applied to the original array.

.np. unique scans through the array and identifier all unique elements.

. It removes any duplicate values and returns the unique values in a new array.

. The returned array is also sorted in ascending order.

3. UNIQUE ARRAY: The result is [1,2,3,4,5] which includes each value from the original array exactly once.

 

 


 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top