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.
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started