numpy.select() function | Python

numpy.select()() function return an array drawn from elements in choicelist, depending on conditions.

Syntax : numpy.select(condlist, choicelist, default = 0)
Parameters :
condlist : [list of bool ndarrays] It determine from which array in choicelist the output elements are taken. When multiple conditions are satisfied, the first one encountered in condlist is used.
choicelist : [list of ndarrays] The list of arrays from which the output elements are taken. It has to be of the same length as condlist.
default : [scalar, optional] The element inserted in output when all conditions evaluate to False.
Return : [ndarray] An array drawn from elements in choicelist, depending on conditions.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.select() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.arange(8)
  
condlist = [arr<3, arr>4]
choicelist = [arr, arr**3]
  
gfg = geek.select(condlist, choicelist)
  
print (gfg)

chevron_right


Output :

[  0, 1, 2, 0, 0, 125, 216, 343]

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.select() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.arange(8)
  
condlist = [arr<4, arr>6]
choicelist = [arr, arr**2]
  
gfg = geek.select(condlist, choicelist)
  
print (gfg)

chevron_right


Output :

[ 0, 1, 2, 3, 0, 0, 0, 49]
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.