NumPy: Find the maximum and minimum value of a given flattened array
1. Maximum and Minimum of Flattened Array
Write a Python program to find the maximum and minimum value of a given flattened array.
Sample Solution:
Python Code:
Sample Output:
Original flattened array: [[0 1] [2 3]] Maximum value of the above flattened array: 3 Minimum value of the above flattened array: 0
Explanation:
In the above exercise –
a = np.arange(4).reshape((2,2)): This line creates a 2D array of shape (2, 2) using the np.arange function and then reshape it to the desired shape using the reshape method.
np.amax(a): This code returns the maximum value of the entire array, which is 3.
np.amin(a): This code returns the minimum value of the entire array, which is 0.
For more Practice: Solve these Related Problems:
- Create a function that flattens any multidimensional array and returns both the maximum and minimum values using vectorized operations.
- Implement an algorithm that computes the maximum and minimum of a flattened array by first sorting it and then selecting the first and last elements.
- Design a solution that finds the max and min values without using np.max or np.min by leveraging np.reduce or np.accumulate.
- Develop a program that flattens an array and returns the difference between its maximum and minimum values.
Go to:
PREV : NumPy Statistics Exercises Home.
NEXT : Minimum and Maximum Along Second Axis
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.