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

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. Now we want to obtain a tuple of 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 find (4,2), so that array[4,2] will give the maximum.

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

Please write two functions array_argmin(arr) and array_argmax(arr) where the input arr is an n-dim array, and the functions will return a tuple of n indices for the minimum and maximum respectively.

Write the code in Python and show output.

np.random.seed(10) array2 np.random.randn (5, 4) print(array2) array2.argmax()

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!