Concept Flow - np.mean() for average
Start with array
Sum all elements
Count number of elements
Divide sum by count
Return average value
np.mean() calculates the average by summing all numbers and dividing by how many there are.
import numpy as np arr = np.array([2, 4, 6, 8]) avg = np.mean(arr) print(avg)
| Step | Action | Value/Calculation | Result |
|---|---|---|---|
| 1 | Input array | [2, 4, 6, 8] | [2, 4, 6, 8] |
| 2 | Sum elements | 2 + 4 + 6 + 8 | 20 |
| 3 | Count elements | 4 | 4 |
| 4 | Divide sum by count | 20 / 4 | 5.0 |
| 5 | Return average | 5.0 | 5.0 |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| arr | [2, 4, 6, 8] | [2, 4, 6, 8] | [2, 4, 6, 8] | [2, 4, 6, 8] | [2, 4, 6, 8] |
| sum | N/A | 20 | 20 | 20 | 20 |
| count | N/A | N/A | 4 | 4 | 4 |
| avg | N/A | N/A | N/A | 5.0 | 5.0 |
np.mean(array) calculates the average value. It sums all elements and divides by the count. Returns a float number. Does not change the original array. Works on numpy arrays or lists.