We use np.mean() to find the average value of numbers. It helps us understand the typical value in a group of numbers.
np.mean() for average in NumPy
np.mean(array, axis=None, dtype=None, out=None, keepdims=False)
array is the list or array of numbers you want to average.
axis decides if you want the average across rows, columns, or the whole array.
np.mean([1, 2, 3, 4, 5])
np.mean([[1, 2], [3, 4]], axis=0)
np.mean([[1, 2], [3, 4]], axis=1)
This program shows how to use np.mean() to find averages of a simple list and a 2D array. It calculates the overall average, averages by columns, and averages by rows.
import numpy as np # List of numbers numbers = [10, 20, 30, 40, 50] # Calculate average average = np.mean(numbers) print(f"Average of numbers: {average}") # 2D array example data = np.array([[1, 2, 3], [4, 5, 6]]) # Average of all elements avg_all = np.mean(data) print(f"Average of all elements: {avg_all}") # Average by columns avg_columns = np.mean(data, axis=0) print(f"Average by columns: {avg_columns}") # Average by rows avg_rows = np.mean(data, axis=1) print(f"Average by rows: {avg_rows}")
If your data has missing values (like np.nan), use np.nanmean() to ignore them.
Setting axis=None (default) calculates the average of all elements.
np.mean() finds the average value of numbers in arrays or lists.
You can calculate the average across the whole data or along rows or columns using the axis parameter.
It helps summarize data to understand typical values quickly.
Practice
np.mean() do when applied to a list or array?Solution
Step 1: Understand the purpose of
The functionnp.mean()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 whatnp.mean()does.Final Answer:
Calculates the average (mean) value of the numbers -> Option DQuick Check:
np.mean() = average [OK]
- Confusing mean with max or count
- Thinking it sorts the data
- Using sum instead of mean
np.mean() to calculate the mean of a NumPy array named arr?Solution
Step 1: Identify the standard NumPy function for mean
The correct function to calculate mean in NumPy isnp.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 AQuick Check:
Use np.mean(array) for average [OK]
- Calling mean() without np prefix
- Using arr.mean() instead of np.mean(arr)
- Confusing np.mean() with np.average()
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.mean(arr, axis=0) print(result)
Solution
Step 1: Understand the array and axis parameter
The array is 2 rows by 3 columns: [[1,2,3],[4,5,6]]. Usingaxis=0means 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.Final Answer:
[2.5 3.5 4.5] -> Option CQuick Check:
Mean along axis=0 averages columns [OK]
- Mixing axis=0 and axis=1
- Calculating mean of entire array instead of axis
- Misreading array shape
import numpy as np arr = [1, 2, 3, 4] result = np.mean(arr, axis=1) print(result)
Solution
Step 1: Check the array shape
The variablearris a 1D list with shape (4,). Axis 1 does not exist in 1D data.Step 2: Understand axis parameter
Axis=1 means operate along columns in 2D or higher arrays. Using axis=1 on 1D data causes an error.Final Answer:
The list is 1D, so axis=1 is invalid -> Option BQuick Check:
Axis must exist in array shape [OK]
- Using axis parameter on 1D arrays
- Thinking np.mean can't handle lists
- Confusing axis indexing
temps = np.array([ [[20, 22, 21, 23], [19, 20, 22, 21]], [[21, 23, 22, 24], [20, 21, 23, 22]], [[22, 24, 23, 25], [21, 22, 24, 23]] ])
Which code correctly calculates the average temperature for each city over all days and measurements?
Solution
Step 1: Understand the array shape and axes
The array shape is (3, 2, 4): 3 days, 2 cities, 4 measurements each day.Step 2: Identify axes to average over
To get average per city over all days and measurements, we average over days (axis 0) and measurements (axis 2), keeping axis 1 (cities).Step 3: Use axis=(0,2) in np.mean
Usingnp.mean(temps, axis=(0,2))averages over days and measurements, resulting in average per city.Final Answer:
np.mean(temps, axis=(0,2)) -> Option AQuick Check:
Average over days and measurements = axis 0 and 2 [OK]
- Averaging over wrong axes
- Using single axis instead of tuple
- Confusing city axis with day or measurement axis
Ready to prove it?
Pass the test and this pattern moves unknown
