Remove duplicates from numpy array
In this numpy tutorial, we will discuss how to remove duplicates from the numpy array.
Introduction
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
Method 1 : Using unique()
This is used to get the unique elements by removing duplicate elements in the given numpy array.
Syntax:
numpy.unique(array_input)
where, array_input is the input numpy array.
Example:
In this example, we will create an numpy array with integer elements( 5 duplicates ).
#import numpy module
import numpy
#create first numpy array with 10 elements
array_input1=numpy.array([1,2,3,4,5,3,2,4,5,2])
#display array
print(array_input1)
#display array by removing duplicates
print(numpy.unique(array_input1))
Output:
From the above array, there are 5 elements that contains duplicates - 3 2 4 5 2. So these are removed.
[1 2 3 4 5 3 2 4 5 2]
[1 2 3 4 5]
Method 2 : Using set()
This is used to get the unique elements by removing duplicate elements in the given numpy array. set will not allow duplicates. It will return the new set by removing duplicates from the numpy array
Syntax:
set(array_input)
where, array_input is the input numpy array.
Example:
In this example, we will create an numpy array with integer elements( 5 duplicates ) and remove duplicates using set().
#import numpy module
import numpy
#create first numpy array with 10 elements
array_input1=numpy.array([1,2,3,4,5,3,2,4,5,2])
#display array
print(array_input1)
#display array by removing duplicates
print(set(array_input1))
Output:
From the above array, there are 5 elements that contains duplicates - 3 2 4 5 2. So these are removed.
[1 2 3 4 5 3 2 4 5 2]
{1, 2, 3, 4, 5}
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024