Machine Learning - Mean Median Mode

Mean, median, and mode

What can we learn from a set of numbers?

In machine learning (and mathematics), there are usually three values we are interested in:

  • Mean - Average value
  • Median - Midpoint value, also known as the median
  • Mode - Most common value

For example: we have recorded the speeds of 13 cars:

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

What is the average, median, or most common speed value?

Mean

The mean is the average value.

To calculate the average, find the sum of all values and then divide the sum by the number of values:

(99+86+87+88+111+86+103+87+94+78+77+85+86) / 13 = 89.77

The NumPy module has methods for this purpose:

Example

Please use NumPy mean() The method determines the average speed:

import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.mean(speed)
print(x)

Run Example

Median

The median is the middle value after sorting all the values:

77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111

It is important to sort the numbers before finding the median.

The NumPy module has methods for this purpose:

Example

Please use NumPy median() Method to find the middle value:

import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)

Run Example

If there are two numbers in the middle, divide the sum of these numbers by 2.

, 77, 78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103
(86 + 87) / 2 = 86.5

Example

Use the NumPy module:

import numpy
speed = [99,86,87,88,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)

Run Example

Mode

The mode is the value that occurs most frequently:

99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86 = 86

The SciPy module has methods for this purpose:

Example

Please use SciPy mode() Method to find the most frequently occurring number:

from scipy import stats
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = stats.mode(speed)
print(x)

Run Example

Chapter Summary

Mean, median, and mode are frequently used techniques in machine learning, so it is important to understand the concepts behind them.