Statistics¶
This section showcases the utility functions found in the datachart.utils.stats module.
Let us start by importing the supporting libraries:
import random
Statistics Submodule¶
The dataset.utils.stats submodule contains functions for calculating statistics. To showcase its use, let us create a list of random numbers:
random_values = random.sample(range(1, 100), 10)
random_values
[13, 85, 35, 65, 18, 90, 7, 39, 8, 64]
Let us now showcase the functions in the stats module.
Count¶
The count function returns the number of elements in the list.
from datachart.utils.stats import count
count(random_values)
10
Sum¶
The sum_values function returns the sum of all values in the list.
from datachart.utils.stats import sum_values
sum_values(random_values)
424.0
Mean¶
The mean function returns the mean of the values.
from datachart.utils.stats import mean
mean(random_values)
42.4
Median¶
The median function returns the median of the values.
from datachart.utils.stats import median
median(random_values)
37.0
Standard Deviation¶
The stdev function returns the standard deviation of the values.
from datachart.utils.stats import stdev
stdev(random_values)
30.033980755138003
Variance¶
The variance function returns the variance of the values. Variance is the square of the standard deviation.
from datachart.utils.stats import variance
variance(random_values)
902.04
Quantile¶
The quantile function returns the quantile of the values.
from datachart.utils.stats import quantile
Show the 25th quantile:
quantile(random_values, 25)
14.25
Show the 75th quantile:
quantile(random_values, 75)
64.75
Interquartile Range (IQR)¶
The iqr function returns the interquartile range, which is the difference between the 75th percentile (Q3) and 25th percentile (Q1). It is useful for identifying outliers and understanding the spread of the middle 50% of the data.
from datachart.utils.stats import iqr
iqr(random_values)
50.5
Minimum¶
The minimum function returns the minimum of the values.
from datachart.utils.stats import minimum
minimum(random_values)
7.0
Maximum¶
The maximum function returns the maximum of the values.
from datachart.utils.stats import maximum
maximum(random_values)
90.0
Correlation¶
The correlation function calculates the Pearson correlation coefficient between two lists of values. It measures the linear relationship between the datasets, ranging from -1 (perfect negative correlation) to 1 (perfect positive correlation).
from datachart.utils.stats import correlation
Create a second list of random values to compare:
random_values_2 = random.sample(range(1, 100), 10)
random_values_2
[52, 53, 33, 14, 97, 34, 63, 46, 50, 2]
correlation(random_values, random_values_2)
-0.5346380232911461
Kernel density estimate¶
The kde1d function estimates the density of a list of values with a Gaussian kernel and returns it as {x, y} points, ready to draw as a datachart.charts.LineChart — over a density datachart.charts.Histogram of the same values, for instance. The bandwidth is a rule of datachart.constants.BANDWIDTH or a scalar factor, gridsize the number of points, and cut how many bandwidths the curve extends past the extremes (xlim fixes the range instead).
from datachart.utils.stats import kde1d
curve = kde1d(random_values, gridsize=5)
curve
[{'x': -52.925669969227435, 'y': 5.369685049671554e-05},
{'x': -2.212834984613714, 'y': 0.006844798655948651},
{'x': 48.50000000000001, 'y': 0.008398528668814874},
{'x': 99.21283498461372, 'y': 0.004262640825392958},
{'x': 149.92566996922744, 'y': 3.276375337326581e-05}]
The kde2d function does the same for (x, y) points and returns the {x, y, z} surface a datachart.charts.ContourChart draws — the density contours of a scattered dataset. The gridsize can be one number or an (x, y) pair of column and row counts, and xlim/ylim fix the grid so several surfaces share it.
from datachart.utils.stats import kde2d
surface = kde2d(random_values, random_values_2, gridsize=3)
surface
{'x': [-57.706250018619315, 48.499999999999986, 154.7062500186193],
'y': [-51.81086115238257, 49.500000000000014, 150.81086115238259],
'z': [[9.2260055480947e-23, 1.2759816599495725e-08, 7.1680617633241185e-09],
[9.96560240175761e-08, 9.364386505251947e-05, 5.830277412765298e-08],
[4.278774194643878e-08, 9.254928689045964e-10, 5.505654139376627e-23]]}
Under development
This theme is still under development. If you are interested in improving it, please let us know.