# How to Compute the Mean, Median, and Mode in Python

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 to Randomly Select From or Shuffle a List in Python