Thursday, October 9, 2025
HomeLanguagesnumpy.ma.choose() function – Python

numpy.ma.choose() function – Python

numpy.ma.choose() function use an index array to construct a new array from a set of choices. Given an array of integers and a set of n choice arrays, this method will create a new array that merges each of the choice arrays. Where arr value in arr is i, the new array will have the value that choices[i] contains in the same place.

Syntax : numpy.ma.choose(arr, choices, out = None, mode = ‘raise’)

Parameters :
arr : [ndarray of ints] This array must contain integers in [0, n-1], where n is the number of choices.
choices : [sequence of arrays] Choice arrays. The index array and all of the choices should be broadcastable to the same shape.
out : [array, optional] If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
mode : [{‘raise’, ‘wrap’, ‘clip’}, optional] Specifies how out-of-bounds indices will behave. ‘raise’: raise an error. ‘wrap’: wrap around. ‘clip’: clip to the range.

Return : A new array that merges each of the choice arrays.

Code #1 :




# Python program explaining
# numpy.ma.choose() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
choice = geek.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
arr = geek.array([2, 1, 0])
  
gfg = geek.ma.choose(arr, choice)
  
print (gfg)


Output :

[3 2 1]

 
Code #2 :




# Python program explaining
# numpy.ma.choose() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
choice = geek.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
arr = geek.array([0, 1, 2])
  
gfg = geek.ma.choose(arr, choice)
  
print (gfg)


Output :

[1 2 3]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6790 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS