After fiddling around for a while, I figured things out, and am posting them here hoping it will help others.
Intuitively, np.where is like asking "tell me where in this array, entries satisfy a given condition".
>>> a = np.arange(5,10)
>>> np.where(a < 8) # tell me where in a, entries are < 8
(array([0, 1, 2]),) # answer: entries indexed by 0, 1, 2
It can also be used to get entries in array that satisfy the condition:
>>> a[np.where(a < 8)]
array([5, 6, 7]) # selects from a entries 0, 1, 2
When a is a 2d array, np.where() returns an array of row idx's, and an array of col idx's:
>>> a = np.arange(4,10).reshape(2,3)
array([[4, 5, 6],
[7, 8, 9]])
>>> np.where(a > 8)
(array(1), array(2))
As in the 1d case, we can use np.where() to get entries in the 2d array that satisfy the condition:
>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2
array([9])
Note, when a is 1d, np.where() still returns an array of row idx's and an array of col idx's, but columns are of length 1, so latter is empty array.
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
03:35
NumPy np.where() Tutorial - Conditional Selection Made Easy for ...
03:48
Mastering Multiple Conditions in numpy.where function! - YouTube
05:33
Smart Data Filtering with np.where | NumPy Tutorial for Beginners ...
NumPy Crash Course - Complete Tutorial
01:09:10
NumPy Tutorial : Numpy Full Course
24:23
Ultimate Guide to NumPy Arrays - VERY DETAILED TUTORIAL for beginners!
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([10, 20, 30, 40, 50]) result = np.where(x > 3, x, y) print(result) Powered by · In this example, elements from `x` are selected where `x > 3`, and from `y` otherwise, resulting in `array([10, 20, 30, 4, 5])`.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.where.html
numpy.where — NumPy v2.1 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where() | Programiz
August 30, 2023 - The NumPy where() method finds indices that are true in an array based on a given condition. The numpy.where() method returns a new array based on a condition applied to each element of an array.
Machine Learning Plus
machinelearningplus.com › blog › how to use numpy where function?
How to Use Numpy Where Function? - machinelearningplus
January 26, 2023 - When the np.where() function is applied to a 2-D numpy array, a tuple containing two arrays is returned. These arrays correspond to respective indices of an element in both dimensions. An element position is determined by taking one value each from arrays in order. See the example below.
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.where.html
numpy.where — NumPy v1.15 Manual
>>> np.where([[0, 1], [1, 0]]) (array([0, 1]), array([1, 0]))
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.where.html
numpy.where — NumPy v2.4 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
OpenSourceOptions
opensourceoptions.com › how-to-use-numpy-where-in-python-with-examples
How to Use numpy.where() in Python with Examples – OpenSourceOptions
The general usage of numpy.where is as follows: numpy.where(condition, value if true (optional), value if false (optional) ). The condition is applied to a numpy array and must evaluate to a boolean. For example a > 5 where a is a numpy array.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.where.html
numpy.where — NumPy v2.3 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...