How do you find the maximum value in a NumPy array?
a) By using the max() function
b) By using the sum() function
c) By using the max() method of the array
d) By using the sort() function
Answer:
a) By using the max() function
Explanation:
You can find the maximum value in a NumPy array by using the max()
function. This function returns the largest value present in the array.
import numpy as np
# Creating a NumPy array
arr = np.array([1, 3, 5, 7, 9])
# Finding the maximum value
max_value = np.max(arr)
print(max_value) # Output: 9
In this example, the max()
function finds and returns the maximum value in the array arr
, which is 9.
Finding the maximum value is a common operation in data analysis, statistics, and optimization tasks, where identifying the largest element in a dataset is often necessary.