Menu
CoddyTech

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:

Bars of seven yearly incomes in thousands: six from 31 to 47 and one founder at 390. A median line sits at 41, on the middle bar, and a mean line far above, at 89.4

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.0

The 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 icon

Challenge

Easy

One mansion can make a whole street look pricey. The starter reads the street's house prices, in thousands, into prices. Print:

  1. The mean price, rounded to 1 decimal.
  2. The median price.
  3. 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 mean

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler