Using the numpy library you can get various statistical values in Python. NumPy (Numerical Python) is a module consisting of multidimensional array objects and a collection of routines for processing those arrays.

On this page you learn how to apply statistical functions to a Python list. You don’t need advanced statistics knowledge to follow along.

Related Course:
Complete Python Programming Course & Exercises

Five number summary

The five number summary is a set of functions in statistics that tell something about a data set. This includes the minimum, the maximum, the standard deviation, the mean and the median.

Import the numpy module.

import numpy

You can optionally import the math module. Then create a list of numbers. In this example the list is defined manually, but of course you can load a list of numbers from excel, from a text file or another data source.

import numpy
import math

x = [1,2,15,3,6,17,8,16,8,3,10,12,16,12,9]

We can get the five number summary using the math library.

The five number summary contains: minimum, maximum, median, mean and the standard deviation.

All of these functions are implemented in the numpy module, you can either output them to the screen or store them in a variable.


# output minimum
print(numpy.min(x))

# store miminimum in variable
minimum = numpy.min(x)

The example program outputs the five number summary for the given list.

import math
import numpy

x = [1,2,15,3,6,17,8,16,8,3,10,12,16,12,9]

print(numpy.min(x))
print(numpy.max(x))
print(numpy.std(x))
print(numpy.mean(x))
print(numpy.median(x))

Boxplot

In statistics, a box plot or boxplot is a method to graphically show groups of numerical data through their quartiles.

boxplot

Matplotlib has all kinds of plots. One of them being a function to create a boxplot. This code will create a boxplot:

import matplotlib.pyplot as plt
import numpy as np

x = [1,2,15,3,6,17,8,16,8,3,10,12,16,12,9]

plt.boxplot(x)
plt.show()

Download exercises