Codecademy Logo

Statistics in NumPy

Datasets and their Histograms

When datasets are plotted as histograms, the way the data is distributed determines the distribution type of the data.

The number of peaks in the histogram determines the modality of the dataset. It can be unimodal (one peak), bimodal (two peaks), multimodal (more than two peaks) or uniform (no peaks).

Unimodal datasets can also be symmetric, skew-left or skew-right depending on where the peak is relative to the rest of the data.

Normal Distribution using Python Numpy module

Normal distribution in NumPy can be created using the below method.

np.random.normal(loc, scale, size)

Where loc is the mean for the normal distribution, scale is the standard deviation of the distribution, and size is the number of observations the distribution will have.

import numpy as np
mu = 0 #mean
sigma = 0.1 #standard deviation
np.random.normal(mu, sigma, 1000)

Standard deviation

The standard deviation of a normal distribution determines how spread out the data is from the mean.

  • 68% of samples will fall between +/- 1 standard deviation of the mean.

  • 95% of samples will fall between +/- 2 standard deviations of the mean.

  • 99.7% of samples will fall between +/- 3 standard deviations of the mean.

Histogram Visualization

A histogram is a plot that visualizes the distribution of samples in a dataset. Histogram shows the frequency on the vertical axis and the horizontal axis is another dimension. Usually horizontal axis has bins, where every bin has a minimum and maximum value. Each bin also has a frequency between x and infinite.

import matplotlib.pyplot as plt
plt.xlabel('Age bins')
plt.title('Age Distribution')

NumPy’s Mean and Axis

In a two-dimensional array, you may want the mean of just the rows or just the columns. In Python, the NumPy .mean() function can be used to find these values. To find the average of all rows, set the axis parameter to 1. To find the average of all columns, set the axis parameter to 0.

We will use the following 2-dimensional array for this example:
ring_toss = np.array([[1, 0, 0],
[0, 0, 1],
[1, 0, 1]])
The code below will calculate the average of each row.
np.mean(ring_toss, axis=1)
# Output: array([ 0.33333333, 0.33333333, 0.66666667])

Conditions in Numpy.mean()

In Python, the function numpy.mean() can be used to calculate the percent of array elements that satisfies a certain condition.

import numpy as np
a = np.array([1,2,3,4])
# Output = 2.5
# The array now becomes array([False, False, True, True])
# True = 1.0,False = 0.0
# Output = 0.5
# 50% of array elements are greater than 2

NumPy Percentile Function

In Python, the NumPy .percentile function accepts a NumPy array and percentile value between 0 and 100. The function returns the value of the array element at the percentile specified.

d = np.array([1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8])
np.percentile(d, 40)
# Output: 4.00

NumPy’s Percentile and Quartiles

In Python, the NumPy .percentile() function can calculate the first, second and third quartiles of an array. These three quartiles are simply the values at the 25th, 50th, and 75th percentiles, so those numbers would be the parameters, just as with any other percentile.

d = [1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8]
np.percentile(d, 25)
# Output: 3.5
np.percentile(d, 75)
#Output: 6.5

NumPy’s Sort Function

In Python, the NumPy .sort() function takes a NumPy array and returns a different NumPy array, this one containing the same numbers in ascending order.

heights = np.array([49.7, 46.9, 62, 47.2, 47, 48.3, 48.7])
# Output: array([ 46.9, 47. , 47.2, 48.3, 48.7, 49.7, 62])

Definition of Percentile

In statistics, a data set’s Nth percentile is the cutoff point demarcating the lower N% of samples.

An example dataset showing its 40th percentile. At the top of the example is the title ‘40th Percentile’.

Under the title we are given a variable, a white letter 'd', whose value is an array of white numbers, '[1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8]. Under the first four numbers, there is a blue horizontal bracket starting at the number 1 and ending at the first number 4. There is a vertical blue line extending down to the label '40%'. The second number 4 in the array is circled in blue. Then, similar to the first horizontal bracket, but starting with the third number 4 and ending at the last number 8, there is another horizontal bracket under the array with a vertical line extending down to the label '60%'.

Learn More on Codecademy