NumPy: Find the maximum and minimum value of a given flattened array
NumPy Statistics: Exercise-1 with Solution
Write a Python program to find the maximum and minimum value of a given flattened array.
Sample Solution:
Python Code:
# Importing the NumPy library
import numpy as np
# Creating a 2x2 array 'a' using arange and reshape
a = np.arange(4).reshape((2, 2))
# Displaying the original flattened array 'a'
print("Original flattened array:")
print(a)
# Finding and displaying the maximum value of the flattened array 'a' using np.amax()
print("Maximum value of the above flattened array:")
print(np.amax(a))
# Finding and displaying the minimum value of the flattened array 'a' using np.amin()
print("Minimum value of the above flattened array:")
print(np.amin(a))
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.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: NumPy Statistics Exercises Home.
Next: Write a NumPy program to get the minimum and maximum value of a given array along the second axis.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/numpy/python-numpy-stat-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics