numpy.where() in Python

The NumPy module provides a function numpy.where() for selecting elements based on a condition. It returns elements chosen from a or b depending on the condition.

For example, if all arguments -> condition, a & b are passed in numpy.where() then it will return elements selected from a & b depending on values in bool array yielded by the condition.

If only the condition is provided, this function is a shorthand to the function np.asarray (condition).nonzero(). Although nonzero should be preferred directly, as it behaves correctly for subclasses.

Syntax:

snippet
numpy.where(condition[, x, y])

Parameters:

These are the following parameters in numpy.where() function:

condition: array_like, bool

If this parameter set to True, yield x otherwise yield y.

x, y: array_like:

This parameter defines the values from which to choose. The x, y, and condition need to be broadcastable to some shape.

Returns:

This function returns the array with elements from x where the condition is True and elements from y elsewhere.

Example 1: np.where()

snippet
import numpy as np
a=np.arange(12)
b=np.where(a<6,a,5*a)
b

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arange() function.
  • We have declared the variable 'b' and assigned the returned value of np.where() function.
  • We have passed the array 'a' in the function.
  • Lastly, we tried to print the value of b.

In the output, the values ranging from 0 to 5 remain the same as per the condition, and the other values have been multiplied with 5.

Output:

Output
array([ 0, 1, 2, 3, 4, 5, 30, 35, 40, 45, 50, 55])

Example 2: For multidimensional array

snippet
import numpy as np
a=np.arange(12)
b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]])
b

Output:

Output
array([[1, 8], [3, 4]])

Example 3: Broadcasting x, y, and condition

snippet
import numpy as np
x, y = np.ogrid[:3, :4]
a=np.where(x > y, x, 10 + y)
a

Output:

Output
array([[10, 11, 12, 13], [ 1, 11, 12, 13], [ 2, 2, 12, 13]])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arange() function.
  • We declared the variable 'b' and assigned the returned value of np.where() function.
  • We have passed a multidimensional array of boolean as a condition and x and y as an integer arrays.
  • Lastly, we tried to print the value of b.

In the output, the x value has been compared to y value if it satisfied the condition, then it will be printed x value otherwise, it will print y value, which has passed as an argument in the where() function.

Example 4: Broadcasting specific value

snippet
x=np.array([[0,1,2],[0,2,5],[0,4,8]])
y=np.where(x<4,x,-2)
y

Output:

Output
array([[ 0, 1, 2], [ 0, 2, -2], [ 0, -2, -2]])
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +