Let’s see how to getting the row numbers of a numpy array that have at least one item is larger than a specified value X. So, for doing this task we will use numpy.where() and numpy.any() functions together.
Syntax: numpy.where(condition[, x, y])
Return: [ndarray or tuple of ndarrays] If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.
Syntax: numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c)
Return: [ndarray, optional]Output array with same dimensions as Input array, Placed with result
Example :
Arr = [[1,2,3,4,5],
[10,-3,30,4,5],
[3,2,5,-4,5],
[9,7,3,6,5]]
and X = 6 then output is [ 0, 2 ].
Here,
[[1,2,3,4,5],
no element is greater than 6 so output is [0].
[10,-3,30,4,5],
10 is greater than 6 so output is [0].
[3,2,5,-4,5],
no element is greater than 6 so output is [0, 2].
[4,7,3,6,5]]
7 is greater than 6 so output is [0, 2].
Below is the implementation:
Python3
import numpy
arr = numpy.array([[ 1 , 2 , 3 , 4 , 5 ],
[ 10 , - 3 , 30 , 4 , 5 ],
[ 3 , 2 , 5 , - 4 , 5 ],
[ 9 , 7 , 3 , 6 , 5 ]
])
X = 6
print ( "Given Array:\n" , arr)
output = numpy.where(numpy. any (arr > X,
axis = 1 ))
print ( "Result:\n" , output)
|
Output:
Given Array:
[[ 1 2 3 4 5]
[10 -3 30 4 5]
[ 3 2 5 -4 5]
[ 9 7 3 6 5]]
Result:
(array([1, 3], dtype=int64),)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Oct, 2020
Like Article
Save Article