Loading, please wait...

A to Z Full Forms and Acronyms

What is NumPy Array Search & Sort

Aug 14, 2020 search() , searchsort() , sort(), 2519 Views
This article is about the searching and sorting techniques of NumPy Array.

NumPy Array Search & Sort

1. Array Search:


Sometimes we want to search an array-like structure for a certain value,
We can also search a NumPy array for a certain value. Numpy has a method called “where()”.
where() method can be used to search an array for a certain value, it returns the indexes if get a match.

For Example:

import numpy as np
ar1 = np.array([9 ,8 ,7, 6])
y=np.where(ar1 == 6)
print(y)
#output:(array([3], dtype=int64),)
# if any value is repeated then it will print all the indexes.
ar2 = np.array([9 ,8 ,7, 6, 6, 8])
z=np.where(ar2 == 8)
print(z)
#output:(array([1, 5], dtype=int64),)


searchsorted() method:
This method performs a binary search on the provided NumPy array to find the required insertion indices. In simple terms, we can say that this function is used to determine the indices into a sorted array.

Syntax:

numpy.searchsorted(array , value, side = ‘left’ , sorter =None)

array: input array
value: a value that we want to insert into the array.
Side: this parameter is optional, if it is set to ‘left’, the index of the first suitable location detected is found.if set to ‘right’, return the last such index. Otherwise, either 0 or N will be returned.

For Example:

import numpy as np
ar1 = np.array([9 ,8 ,7, 6])
y = np.searchsorted(ar1 , 10)
print(y)
#output:4
ar2 = np.array([9 ,8 ,7, 6, 6, 8])
#search from right
z = np.searchsorted(ar1 , 5, side='right')
print(z)
#output:0
#multiple value insert
m = np.searchsorted(ar1 , [2, 12] , side='right')
print(m)
#output:[0 4]


2. Array Sort:


Sorting an array means putting elements in order like ascending, descending, numeric, or alphabetical.
The NumPy ndarray object has a function called sort(). This function sorts the array. This method returns an array with the same shape, i.e it does not change the original element.

Using this function we can sort arrays of strings or any other data type.

For Example:
a)

import numpy as np
ar1 = np.array([9 ,8 ,7, 2, 6])
y = np.sort(ar1)
print(y)
#output:[2 6 7 8 9]


b) sorting the array in alphabetical order

#sort the array alphabetically
ar2 = np.array(['a' ,'z' ,'y' , 'o'])
m = np.sort(ar2)
print(m)
#output:['a' 'o' 'y' 'z']

c) sorting a 2-D array

#sorting a 2-D array
import numpy as np
ar2 = np.array([['a' ,'z' ,'y' , 'o'],[9, 1, 10 , 18]])
m = np.sort(ar2)
print(m)
#output:[['a' 'o' 'y' 'z']
# ['1' '10' '18' '9']]

d)sorting a 3-D array

#sorting a 3-D array
import numpy as np
ar2 = np.array([[['a' ,'z' ,'y' , 'o'] , [True ,False ,True ,False ], [9, 1, 10 , 18] ] ])
m = np.sort(ar2)
print(m)
#output:[[['a' 'o' 'y' 'z']
['False' 'False' 'True' 'True']
['1' '10' '18' '9']]]

Thanks for reading this article. I just hope you like this article, please do comment and share this with your friends.

stay tuned for upcoming articles.

happy Learning 😊😊 

A to Z Full Forms and Acronyms

Related Article