Search⌘ K
AI Features

Solution: Find Min and Max from a 2-D NumPy Array

Understand how to extract minimum and maximum values from each row in a 2D NumPy array. This lesson guides you through using Python and NumPy functions to process arrays effectively, enhancing your foundational skills in data handling.

We'll cover the following...

Solution

...
Python 3.5
def getMinMax(arr):
res = []
for i in range(arr.shape[0]): # Traverse over each row
res.append(arr[i].min()) # Store minimum element in list
res.append(arr[i].max()) # Store maximum element in list
return res
# Test Code
arr = np.random.randint(1,100, size=(5,5))
print("The Original Array:")
print(arr)
res_arr = getMinMax(arr)
print("\nThe Resultnt list with min & max values:")
print(res_arr)

Explanation

A function ...