Numpy

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]]

Generate some random data with Numpy

Numpy, Python's pre-eminent mathematics library, makes it easy to generate random numeric data in the form of two-dimension arrays.

Here's one way of generating some random numeric data with Numpy.

Import Numpy

When importing numpy the standard practice is to import is as np.

import numpy as np

Build some random data in the form of a numpy array

Suppose I want to generate some random numeric data that consists of integers ranging from 0 to 99 in the form of an array that has 3 rows and five columns.

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

This will yield an array that looks something like this:

array([[82,  7, 92, 35,  7],
       [81, 71, 56, 80, 82],
       [55,  6, 96, 87, 83]])