In this article, we will cover NumPy.choose(). Along with that, for an overall better understanding, we will look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But at first, let us try to get a brief understanding of the function through its definition.
Numpy Choose is a function to select options from the multiple arrays according to our need. Suppose you have multiple Numpy arrays grouped under a single array, and you want to get values from them collectively at once. In such cases the function NumPy.choose() comes handy. Up next, we will see the parameter associated with it, and followed by which we will look at the different parameters.
Syntax of Numpy.Choose()
np.choose(a,c) == np.array(][I] ])
Above, we can see the general syntax of our function. It may seem a bit complicated at first sight. But worry not. We will be discussing each of the parameters associated with it in detail.
Parameter of Numpy.Choose()
a: int array
This Numpy array must contain numbers between 0 to n-1. Here n represents the number of choices unless a mode(an optional parameter) is associated with it.
choices: a sequence of array
“a” and all of the choices must be broadcastable to the same shape. If choices are itself an array, then its outermost dimension is taken as defining the “sequence”.
out: array
It is an optional parameter. If it is declared the output will be inserted in the pre-existing array. It should be of appropriate d-type and shape.
mode:{raise(defalut), wrap,clip}
It is an optional parameter. The function of this parameter is to decide how index numbers outside[0,n-1] will be treated. It has 3 conditions associated with it, which are:
“raise”: In this case an exception is raised.
“wrap”: In this case the value becomes value |N|
“clip”: In this case value <0 are mapped to 0, values >n-1 are mapped to n-1.
Return of Numpy.Choose()
merged array: array
This function returns a merged array as output.
NOTE: If a and each choice array are not broadcastable to the same shape then in that case “VALUE ERROR” is displayed.
Numpy Choose Elements of Array Example –
We have covered the syntax and parameters of NumPy.choose() in detail. Now let us see them in action through different examples. These examples will help us in understanding the topic better. We will start with an elementary level and then look at various variations.
#input
import numpy as ppool
a=[[1,23,3,6],[3,5,6,9]]
print(ppool.choose([1,0,1,0],a))
Output:
[ 3 23 6 6]
In the above example, we have first imported the NumPy module. Then we have declared a Numpy array. After which, we have used our general syntax and a print statement to get the desired result. The output here justifies our input. We can understand it as follows: we have declared our choices as [1,0,1,0].
This means that the first component of our element will be the 1st element of sub-array number 2. Then, the second element will be the second element of array number 1. Next term will 3rd element of array number sub-array number 2, and the last element will be the 4th element of sub-array 2.
Example of Choosing Values from Numpy Array
#input
import numpy as ppool
a=[[1,23,3],[3,5,6],[45,78,90]]
print(ppool.choose([1,2,0],a))
Output:
[ 3 78 3]
Again in this example, we have followed a similar procedure as in the above example. Here we have 3 sub-array instead of 2 and 3 choices instead of 4. The reason behind this is that we have 3 elements in the array. If we declare 4 choices, then there would be no 4th term to fill its space, and all we would get is an error. That’s something everyone should take care of while dealing working with this function.
According to outputs, numpy choose() function selected values from the 2nd, 3rd, and 1st array respectively.
Numpy Choose Random from an Array Example
We all know a case where we need to choose a choice from a list of options. Luckily, in numpy, there is a method to achieve it precisely. Given that you have an array, numpy.choose() will select a random option from the Numpy array. The following example can help you to understand it –
Code –
import numpy as np
x = np.random.choice(5, 3)
print(x)
Output –
[2 3 2] (Random output)
Explanation –
First, we import the module numpy in the first line. Then numpy.random.choice returns the random number from 0 to 5 and form an array of lengths 3.
Must Read
Understanding Python Bubble Sort
Numpy Determinant | What is NumPy.linalg.det()
Numpy log in Python
Conclusion
In this article, we have covered NumPy.choose(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case.
In the end, we can conclude that Numpy. choose() helps us getting elements from the different sub-arrays at once. I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read fliplr next.