How to find mean, median and mode in python using numpy


Mean, Median, and Mode

What can we learn from looking at a group of numbers?

In Machine Learning (and in mathematics) there are often three values that interests us:

  • Mean - The average value
  • Median - The mid point value
  • Mode - The most common value

Example: We have registered the speed of 13 cars:

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

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


Mean

The mean value is the average value.

To calculate the mean, find the sum of all values, and 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 a method for this. Learn about the NumPy module in our NumPy Tutorial.

Example

Use the NumPy mean() method to find 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 value is the value in the middle, after you have sorted all the values:

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

It is important that the numbers are sorted before you can find the median.

The NumPy module has a method for this:

Example

Use the 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)

Try it Yourself »

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

77, 78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103

(86 + 87) / 2 =

86.5

Example

Using the NumPy module:

import numpy

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

x = numpy.median(speed)

print(x)

Try it Yourself »


Mode

The Mode value is the value that appears the most number of times:

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

The SciPy module has a method for this. Learn about the SciPy module in our SciPy Tutorial.

Example

Use the SciPy mode() method to find the number that appears the most:

from scipy import stats

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

x = stats.mode(speed)

print(x)

Try it Yourself »


Chapter Summary

The Mean, Median, and Mode are techniques that are often used in Machine Learning, so it is important to understand the concept behind them.



In NumPy, we use special inbuilt functions to compute mean, standard deviation, and variance.

Mean

The mean gives the arithmetic mean of the input values. It is the sum of elements divided by the total number of elements. It is given by the syntax numpy.mean() or np.mean().

import numpy as np

array = np.arange(20)
print(array)
r1 = np.mean(array)
print("\nMean: ", r1)

Median

The median gives the middle values in the given array. Its syntax is given by np.median().

import numpy as np
array = np.arange(20)
print(array)
r1 = np.median(array)
print("\nstd: ", r1)

Standard deviation

The standard deviation gives us the spread of distribution of array values. Standard deviation is given by the syntax np.std() or numpy.std().

import numpy as np
array = np.arange(20)
print(array)
r1 = np.std(array)
print("\nstd: ", r1)

RELATED TAGS

communitycreator

python

numpy

How to find mean, median and mode in python using numpy

In this article, we show how to compute the mean, median, and mode in Python.

To compute the mean and median, we can use the numpy module.

To compute the mode, we can use the scipy module.

The mean is the average of a set of numbers.

The median is the middle number of a set of numbers.

The mode is the number that occurs with the greatest frequency within a data set.

So below, we have code that computes the mean, median, and mode of a given data set.

So let's break down this code.

We import the numpy module as np. This means that we reference the numpy module with the keyword, np.

We also have to import stats from the scipy module, since we need this in order to get the mode (numpy doesn't supply the mode).

So we create a variable, dataset, and set it equal to, [1,1,2,3,4,6,18]

We then create a variable, mean, and set it equal to, np.mean(dataset)

This puts the mean of the dataset into the mean variable.

We then create a variable, median, and set it equal to, np.median(dataset)

This puts the median of the dataset into the mean variable.

We then create a variable, mode, and set it equal to, np.mode(dataset)

This puts the mode of the dataset into the mode variable.

Doing the math with the mean, (1+1+2+3+4+6+18)= 35/7= 5. Thus, numpy is correct.

The median, the middle value, is 3.

And the number 1 occurs with the greatest frequency (the mode) out of all numbers.

With scipy, an array, ModeResult, is returned that has 2 attributes. The first attribute, mode, is the number that is the mode of the data set. The second attribute, count, is the number of times it occurs in the data set.

And this is how to compute the mean, median, and mode of a data set in Python with numpy and scipy.

Related Resources

How do you find the mean median and mode in NumPy?

To compute the mean and median, we can use the numpy module. To compute the mode, we can use the scipy module. The mean is the average of a set of numbers. The median is the middle number of a set of numbers.

How do you find the mean median and mode in Python?

Program to find Mean, Median, and Mode without using Libraries:.
Mean: numb = [2, 3, 5, 7, 8] no = len(numb) summ = sum(numb) mean = summ / no print("The mean or average of all these numbers (", numb, ") is", str(mean)) ... .
Median: ... .
Mode: ... .
Program to find Mean, Median, and Mode using pre-defined library:.

How do you find the mode using NumPy in Python?

If we want to use the NumPy package only to find the mode, we can use the numpy. unique() function. The numpy. unique() function takes an array as an input argument and returns an array of all the unique elements inside the input array.

Can we calculate mode using NumPy?

stats package. Note : To apply mode we need to create an array. In python, we can create an array using numpy package. So first we need to create an array using numpy package and apply mode() function on that array.