Calculate Mean, median and mode

Generate some random data to work with

First, we'll generate some random numeric data to work with in the form of a Numpy array

import numpy as np

data = np.random.randint(100, size=(3, 5))

Calculate the mean

We can use Numpy's np.mean() function to calculate the mean value in the range of values in the data array:

mean = np.mean(data)
print ("The mean value of the dataset is", mean)
Out: The mean value of the dataset is 61.333333333333336

Calculate the median

Numpy also has a np.median function, which is deployed like this:

median = np.median(data)
print ("The median value of the dataset is", median)
Out: The median value of the dataset is 80.0

Calculate the mode

Numpy doesn't have a built-in function to calculate the modal value within a range of values, so use the stats module from the scipy package.

from scipy import stats

mode = stats.mode(data)
print("The mode is {}".format(mode[0]))
Out: The mode is [[55  6 56 35  7]]