Mean
- Machine Learning course
- Beginner
- Lesson 25/112
- 5 min
The mean is found by dividing the sum of data by the number of elements.
Mean
The arithmetic mean is the sum of the values divided by their count: (x₁ + ... + xₙ) / n. It describes the "centre" of the data.
x = np.array([4, 8, 6, 2])
print(x.mean()) # 5.0
print(np.mean(x)) # the sameThe mean is sensitive to outliers: the mean of [4, 8, 6, 2, 100] is 24 and represents no typical value.
Mean along an axis
In a two-dimensional array the axis parameter picks the direction: axis=0 computes per column (along the rows), axis=1 per row.
X = np.array([[1, 10],
[3, 20],
[5, 30]])
print(X.mean(axis=0)) # [ 3. 20.] mean of each feature
print(X.mean(axis=1)) # [ 5.5 11.5 17.5] mean of each exampleIn ML, feature means (axis=0) are used in scaling and missing-value filling.
Practice Feature means
Compute the column means of matrix X with axis=0 and print them.
Starter code
import numpy as np
X = np.array([[170, 60], [180, 80], [160, 70]])
# buraya yazSample solution
import numpy as np
X = np.array([[170, 60], [180, 80], [160, 70]])
print(X.mean(axis=0))Expected output
[170. 70.]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the mean of <code>[1, 2, 3, 10]</code>?
- 10.0
- 4.0 Correct answer
- 2.5
- 3.0
Explanation: (1 + 2 + 3 + 10) / 4 = 16 / 4 = 4.
2 Compute the mean of each column.
m = X.mean(axis=____)
m = X.mean(axis=0)
Explanation: axis=0 reduces along the rows and gives one value per column.
3 Match the expressions.
| Term | Meaning |
|---|---|
x.mean() |
Mean of all values |
X.mean(axis=0) |
Column means |
X.mean(axis=1) |
Row means |
x.sum() / len(x) |
Mean by hand |