Mean and Median
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 1 of 64.
Before a model learns from a column, you describe it with two numbers: its center and its spread. This chapter starts with the center:
The same incomes in code, the founder last:
incomes = np.array([38, 41, 35, 47, 44, 31, 390])
print(np.round(incomes.mean(), 1))
print(np.median(incomes))This prints:
89.4
41.0The founder drags the mean above six friends. The median, the middle value once sorted, barely moves: trust it for columns with extreme values, like prices.
An even count averages the two middle values. Arrays need np.median(a); a pandas column has .median().
Challenge
EasyOne mansion can make a whole street look pricey. The starter reads the street's house prices, in thousands, into prices. Print:
- The mean price, rounded to 1 decimal.
- The median price.
- How many houses cost less than the mean.
For the prices 320, 300, 2500, 310 and 335:
Mean: 753.0
Median: 320.0
4 of 5 houses cost less than the meanTry it yourself
import numpy as np
n = int(input())
values = []
for i in range(n):
values.append(int(input()))
prices = np.array(values)
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler