Question: python The ndarray.argmin() and ndarray.argmax() methods can only return the index from a flat array, which means they unfold the n-dim array into 1-d array

 python The ndarray.argmin() and ndarray.argmax() methods can only return the index

python

The ndarray.argmin() and ndarray.argmax() methods can only return the index from a flat array, which means they unfold the n-dim array into 1-d array and return the 1-d index. For example, the following code will return 18 from argmaxl) and 2 from argmin(). Now we want to obtain a tuple of dimensional indices which can be used to locate the minimum or maximum. Each element of the tuple is the index of minimum or maximum in the corresponding dimension. In the following example we want to return the index (4, 2) for the maximum value and return (0, 2) for the minimum value, because the entry at the 5th row and 3rd column gives the maximum, and entry at the 1st row and 3rd column gives the minimum. np.random.seed (10) arrayl = np.random.randn (5, 4) print(arrayl) print (arrayl.argmax()) print (arrayl.argmin()) [[ 1.3315865 0.71527897 -1.54540029 -0.00838385] [ 0.62133597 -0.72008556 0.26551159 0.10854853] [ 0.00429143 -0.17460021 0.43302619 1.20303737] (-0.96506567 1.02827408 0.22863013 0.44513761] [-1.13660221 0.13513688 1.484537 -1.07980489]] Please write two functions array_argmin (arr) and array_argmax (arr) where the input arr is an arbitrary n-dim array, and the functions will return a tuple of n dimensional indices for the minimum and maximum respectively

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!