Searching elements of ndarray
- We can search elements of ndarray by using where() function.
where(condition, [x, y])
- If we specify only the condition, then it will return the indices of the elements which satisfies the condition.
- If we provide condition,x,y then The elements which satisfies the condition will be replaced with x and remaining elements will be replaced with y
- where() function does not return the elements. It returns only the indices
- it will act as replacement operator also.
- The functionality is just similar to ternary operator when it acts as replacement
operator
Example
Python
In [340]:
import numpy as np
help(np.where)
Output
PowerShell
Help on function where in module numpy:
where(...)
where(condition, [x, y])
Return elements chosen from `x` or `y` depending on `condition`.
where() function
Example
Python
In [341]:
# Find indexes where the value is 7 from 1-D array
a = np.array([3,5,7,6,7,9,4,6,10,15])
b = np.where(a==7)
b # element 7 is available at 2 and 4 indices
Output
PowerShell
Out[341]:
(array([2, 4], dtype=int64),)
Example
Python
In [342]:
# Find indices where odd numbers present in the given 1-D array?
a = np.array([3,5,7,6,7,9,4,6,10,15])
b = np.where(a%2!=0)
b
Output
PowerShell
Out[342]:
(array([0, 1, 2, 4, 5, 9], dtype=int64),)
Finding the elements directly
We can get the elements directly in 2 ways
- using where() function
- using condition based selection
where() function
Example
Python
In [343]:
# to get the odd numbers
a = np.array([3,5,7,6,7,9,4,6,10,15])
indices = np.where(a%2!=0)
a[indices]
Output
PowerShell
Out[343]:
array([ 3, 5, 7, 7, 9, 15])
conditional based selection
Example
Python
In [344]:
# to get the odd numbers
a = np.array([3,5,7,6,7,9,4,6,10,15])
a[a%2!=0]
Output
PowerShell
Out[344]:
array([ 3, 5, 7, 7, 9, 15])
Example
Python
In [345]:
# where(condition,[x,y])
# if condition satisfied that element will be replaced from x and
# if the condition fails that element will be replaced from y.
# Replace every even number with 8888 and every odd number with 7777?
a = np.array([3,5,7,6,7,9,4,6,10,15])
b = np.where( a%2 == 0, 8888, 7777)
b
Output
PowerShell
Out[345]:
array([7777, 7777, 7777, 8888, 7777, 7777, 8888, 8888, 8888, 7777])
Example
Python
In [346]:
# Find indexes where odd numbers present in the given 1-D array and replace with
element 9999.
a = np.array([3,5,7,6,7,9,4,6,10,15])
b = np.where( a%2 != 0, 9999, a)
b
Output
PowerShell
Out[346]:
array([9999, 9999, 9999, 6, 9999, 9999, 4, 6, 10, 9999])
We can use where() function for any n-dimensional array
2-D arrays
- It will return the 2 arrays.
- First array is row indices
- Second array is column indices
Example
Python
In [347]:
# to find the indices of the elements where elements are divisible by 5
a = np.arange(12).reshape(4,3)
np.where(a%5==0)
Output
PowerShell
Out[347]:
(array([0, 1, 3], dtype=int64), array([0, 2, 1], dtype=int64))
- The first array array([0, 1, 3] represents the row indices
- The second array array([0, 2, 1] represents the column indices
- The required elements present at (0,0),(1,2) and (3,1) index places.
Example
Python
In [348]:
# we can perform replacement on 2-D arrays
a = np.arange(12).reshape(4,3)
np.where(a%5==0,9999,a)
Output
PowerShell
Out[348]:
array([[9999, 1, 2],
[ 3, 4, 9999],
[ 6, 7, 8],
[ 9, 9999, 11]])
searchsorted() function
- Internally this function will use Binary Search algorithm. Hence we can call this
- function only for sorted arrays.
- If the array is not sorted then we will get abnormal results.
- Complexicity of the Binary search algorithm is O(log n)
- It will return insertion point(i.e., index) of the given element
Example
Python
In [349]:
import numpy as np
help(np.searchsorted)
Output
PowerShell
Help on function searchsorted in module numpy:
searchsorted(a, v, side='left', sorter=None)
Find indices where elements should be inserted to maintain order.
Example
Python
In [350]:
# to find the insertion point of 6 from left
a = np.arange(0,31,5)
np.searchsorted(a,6)
Output
PowerShell
Out[350]:
2
Note
- Bydefault it will always search from left hand side to identify insertion point.
- If we want to search from right hand side we should use side=’right’
Example
Python
In [351]:
# to find the insertion point of 6 from right
a = np.arange(0,31,5)
print(f"Array a :\n {a}")
np.searchsorted(a,6,side='right')
Output
PowerShell
Array a :
[ 0 5 10 15 20 25 30]
Out[351]:
2
Example
Python
In [352]:
# to find the insetion point from left and right
a = np.array([3,5,7,6,7,9,4,10,15,6])
# first sort the elements
a = np.sort(a)
# insertion point from left(default)
left = np.searchsorted(a,6)
# insertion point from right
right = np.searchsorted(a,6,side='right')
print(f"The original array : {a}")
print(f"Insertion point for 6 from left : {left}")
print(f"Insertion point for 6 from right : {right}")
Output
PowerShell
The original array : [ 3 4 5 6 6 7 7 9 10 15]
Insertion point for 6 from left : 3
Insertion point for 6 from right : 5
Summary:
- sort() ==> To sort given array
- where() ==> To perform search and replace operation
- searchsorted() ==> To identify insertion point in the given sorted array