Finding Minimum and Maximum Values with np.min() and np.max()
📖 Scenario: You work at a small fruit store. You have a list of daily sales numbers for apples over a week. You want to find the smallest and largest sales numbers to understand the best and worst days.
🎯 Goal: Learn how to use np.min() and np.max() to find the minimum and maximum values in a list of numbers.
📋 What You'll Learn
Create a NumPy array with exact daily sales numbers
Create a variable to hold the sales data
Use np.min() to find the smallest sales number
Use np.max() to find the largest sales number
Print both minimum and maximum sales values
💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to find their best and worst performing days. This helps them plan stock and promotions.
💼 Career
Data analysts and scientists use functions like <code>np.min()</code> and <code>np.max()</code> to quickly summarize data and find important insights.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a NumPy array called sales with these exact values: 23, 45, 12, 67, 34, 89, 10.
NumPy
Hint
Use np.array() to create the array with the exact sales numbers.
2
Create variables for minimum and maximum sales
Create two variables: min_sales and max_sales. Use np.min() on sales to get the smallest value and assign it to min_sales. Use np.max() on sales to get the largest value and assign it to max_sales.
NumPy
Hint
Use np.min(sales) and np.max(sales) to find the smallest and largest values.
3
Print the minimum sales value
Write a print statement to display the text "Minimum sales:" followed by the value of min_sales.
NumPy
Hint
Use print("Minimum sales:", min_sales) to show the smallest sales number.
4
Print the maximum sales value
Write a print statement to display the text "Maximum sales:" followed by the value of max_sales.
NumPy
Hint
Use print("Maximum sales:", max_sales) to show the largest sales number.
Practice
(1/5)
1. What does the function np.min() do when applied to a NumPy array?
easy
A. Calculates the average of the array
B. Finds the largest value in the array
C. Finds the smallest value in the array
D. Counts the number of elements in the array
Solution
Step 1: Understand the purpose of np.min()
The function np.min() is designed to find the minimum value in a NumPy array.
Step 2: Compare with other options
Options B, C, and D describe different functions: max, mean, and size respectively, which are not what np.min() does.
Final Answer:
Finds the smallest value in the array -> Option C
Quick Check:
np.min() = smallest value [OK]
Hint: np.min() always returns the smallest number [OK]
Common Mistakes:
Confusing np.min() with np.max()
Thinking np.min() calculates average
Assuming np.min() counts elements
2. Which of the following is the correct NumPy function syntax to find the maximum value in a NumPy array named arr?
easy
A. max(arr)
B. np.max(arr)
C. np.maximum(arr)
D. arr.max()
Solution
Step 1: Identify correct function usage
The function np.max(arr) correctly finds the maximum value in the array arr.
Step 2: Analyze other options
arr.max() is valid but uses method syntax, not function syntax. np.maximum(arr) is incorrect because np.maximum() compares two arrays element-wise. max(arr) is Python's built-in max, which works on lists but not efficiently on NumPy arrays.
Final Answer:
np.max(arr) -> Option B
Quick Check:
np.max(arr) = max value [OK]
Hint: Use np.max(array) to get max value quickly [OK]
Axis=1 means operate along columns of a 2D or higher array. For 1D arrays, axis=1 is invalid and causes an error.
Final Answer:
The array is 1D, axis=1 is invalid -> Option A
Quick Check:
1D array + axis=1 = error [OK]
Hint: Use axis only within array dimension range [OK]
Common Mistakes:
Using axis=1 on 1D arrays
Thinking np.min() can't take axis
Assuming array has non-numeric data
5. Given a 2D NumPy array data representing daily temperatures for a week (rows = days, columns = cities), how would you find the highest temperature recorded in each city?
hard
A. np.max(data, axis=1)
B. np.min(data, axis=1)
C. np.min(data, axis=0)
D. np.max(data, axis=0)
Solution
Step 1: Understand data layout
Rows represent days, columns represent cities. To find max per city, we want max down each column.
Step 2: Use axis=0 for column-wise operation
Using np.max(data, axis=0) finds the maximum temperature for each city across all days.
Final Answer:
np.max(data, axis=0) -> Option D
Quick Check:
Max per city = np.max(data, axis=0) [OK]
Hint: axis=0 aggregates down columns (cities) [OK]
Common Mistakes:
Using axis=1 to find max per city
Using np.min() instead of np.max()
Confusing rows and columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.