๐ Scenario: You work in a small bakery. You have daily sales numbers for a week. You want to find the average sales to understand how well the bakery is doing.
๐ฏ Goal: Calculate the average daily sales using np.mean() from the sales data.
๐ What You'll Learn
Create a numpy array called sales with the exact daily sales numbers
Create a variable called average_sales to store the average
Use np.mean() to calculate the average of sales
Print the value of average_sales
๐ก Why This Matters
๐ Real World
Calculating averages is common in business to understand typical performance, like average sales per day.
๐ผ Career
Data analysts and scientists often use averages to summarize data and find trends.
Progress0 / 4 steps
1
Create the sales data array
Create a numpy array called sales with these exact daily sales numbers: 120, 150, 130, 170, 160, 180, 140.
NumPy
Hint
Use np.array() to create the array with the exact numbers inside square brackets.
2
Create a variable for the average
Create a variable called average_sales and set it to 0 as a starting point.
NumPy
Hint
Just write average_sales = 0 to create the variable.
3
Calculate the average using np.mean()
Use np.mean() on the sales array and assign the result to average_sales.
NumPy
Hint
Use average_sales = np.mean(sales) to calculate the average.
4
Print the average sales
Print the value of average_sales to see the average daily sales.
NumPy
Hint
Use print(average_sales) to display the average.
Practice
(1/5)
1. What does the function np.mean() do when applied to a list or array?
easy
A. Counts the number of elements in the list or array
B. Finds the maximum value in the list or array
C. Sorts the list or array in ascending order
D. Calculates the average (mean) value of the numbers
Solution
Step 1: Understand the purpose of np.mean()
The function np.mean() is designed to calculate the average value of numbers in a list or array.
Step 2: Compare with other options
Other options describe different functions: maximum value, count, or sorting, which are not what np.mean() does.
Final Answer:
Calculates the average (mean) value of the numbers -> Option D
Quick Check:
np.mean() = average [OK]
Hint: Mean = sum of values รท number of values [OK]
Common Mistakes:
Confusing mean with max or count
Thinking it sorts the data
Using sum instead of mean
2. Which of the following is the correct syntax for np.mean() to calculate the mean of a NumPy array named arr?
easy
A. np.mean(arr)
B. mean(arr)
C. arr.mean()
D. np.average(arr)
Solution
Step 1: Identify the standard NumPy function for mean
The correct function to calculate mean in NumPy is np.mean(), which takes the array as an argument.
Step 2: Check other options
mean(arr) is not defined without importing; arr.mean() is valid but not the function asked; np.average(arr) is similar but not the exact function requested.
Final Answer:
np.mean(arr) -> Option A
Quick Check:
Use np.mean(array) for average [OK]
Hint: Use np.mean(array) to get average quickly [OK]
Common Mistakes:
Calling mean() without np prefix
Using arr.mean() instead of np.mean(arr)
Confusing np.mean() with np.average()
3. What is the output of the following code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.mean(arr, axis=0)
print(result)
medium
A. [1.5 2.5 3.5]
B. [3. 4. 5.]
C. [2.5 3.5 4.5]
D. [3.5 4.5 5.5]
Solution
Step 1: Understand the array and axis parameter
The array is 2 rows by 3 columns: [[1,2,3],[4,5,6]]. Using axis=0 means calculating the mean down each column.
Step 2: Calculate mean for each column
Column 1: (1+4)/2 = 2.5, Column 2: (2+5)/2 = 3.5, Column 3: (3+6)/2 = 4.5. So the result is [2.5, 3.5, 4.5].
Step 3: Check options carefully
[2.5 3.5 4.5] matches the calculated values exactly.