Essential Python Libraries for Statistics: A Practical Guide

fimg

If you’re into statistics, you’re probably comfortable programming with Python or R or both. Python is a great and simple-to-learn programming language for statistical analysis.

For simple tasks such as descriptive statistics, you can use the built-in statistics modules or other core Python functions. But there’s a suite of Python libraries for basic to advanced statistical analysis—from descriptive statistics to hypothesis testing and more.

In this guide, we’ll go over popular Python libraries for statistics. We’ll review the key features of the libraries and spin up code examples as needed.

You don’t need to be proficient in all of these libraries to be able to perform statistical analysis with Python. But we give you enough options so you can choose what works best for you. Let’s begin!

1. Python’s Built-in Statistics Module

Before we go over the Python libraries, let’s start with the statistics module that is built into the Python standard library.

It comes with functions for calculating mathematical statistics of numeric data and classes for probability distributions. It is part of Python’s standard library, which means you can use it without needing to install any additional packages.

Read 10 Essential Statistical Functions in Python to learn more.

Key Features

  • The statistics module offers simple and intuitive functions that make it easy to perform basic statistical analysis.
  • It includes functions for calculating measures of central tendencies and measures of spread.
  • Additionally, the module has functions for correlation, covariance, and linear regression.

Code Example: Mean Difference Test

Although the statistics module doesn’t directly support advanced statistical tests, you can use it with other libraries for more complex statistical analyses. In this example, we calculate the mean difference between two datasets.

This example demonstrates calculating the mean of two different datasets and finding the difference between their means.

import statistics as stats

# Sample data
data1 = [10, 12, 14, 15, 18, 20, 22]
data2 = [16, 18, 20, 21, 22, 24, 26]

# Calculate means
mean1 = stats.mean(data1)
mean2 = stats.mean(data2)

# Mean difference
mean_diff = mean2 - mean1

print(f"Mean of data1: {mean1}")
print(f"Mean of data2: {mean2}")
print(f"Mean difference: {mean_diff}")

Output:

Mean of data1: 15.43
Mean of data2: 21.00
Mean difference: 5.57

2. NumPy

NumPy is the go-to Python library for numerical computing. In NumPy, you use n-dimensional arrays for efficient manipulation of large, multidimensional arrays and matrices. You can use NumPy for linear algebra and statistical analysis.

Key Features

  • NumPy provides n-dimensional array objects that support a wide array of mathematical functions for operations like addition, subtraction, multiplication, and division on arrays.
  • It comes with functions for linear algebra operations such as matrix multiplication, matrix decomposition (like LU, QR), and eigenvalue problems.
  • NumPy also has functions for generating random numbers, which can be important for simulations and statistical sampling.
  • Broadcasting in NumPy allows for operations on arrays of different shapes—facilitating vectorized operations and reducing the need for explicit loops.

Code Example: Linear Regression

In this example, we perform a simple linear regression using NumPy’s built-in functions.

import numpy as np

# Example data
X = np.random.rand(100)
y = 2 * X + np.random.randn(100) * 0.2

X = np.vstack([np.ones(len(X)), X]).T

# Linear regression
beta = np.linalg.inv(X.T @ X) @ X.T @ y

print(f"Intercept: {beta[0]}")
print(f"Coefficient: {beta[1]}")

This code demonstrates how to perform a linear regression using NumPy. We generate some random data, set up matrix X, add a column of ones for the intercept, and solve the normal equations to find the regression coefficients. The resulting beta array contains the intercept and the slope of the regression line.

Output:

Intercept: 0.07
Coefficient: 1.89

3. SciPy

SciPy extends NumPy’s capabilities with more advanced functions for statistical analysis and signal processing.

Key Features

  • SciPy provides a comprehensive set of statistical functions, including probability distributions (such as normal, binomial), descriptive statistics, statistical tests (t-tests, chi-square tests), and more.
  • Contains modules for optimization—scipy.optimize—allowing for solving problems like linear programming, curve fitting, and finding minima/maxima of functions.

Code Example: Hypothesis Testing

In this example, we perform a two-sample t-test using SciPy to determine if there is a significant difference between the means of two independent samples.

from scipy import stats

# Sample data
data1 = [10, 11, 14, 15, 18, 19, 21]
data2 = [16, 18, 20, 21, 22, 24, 26]

# Perform t-test
t_stat, p_val = stats.ttest_ind(data1, data2)

print(f"T-statistic: {t_stat}")
print(f"P-value: {p_val}")

The two-sample t-test is used to compare the means of two independent samples to determine if they are significantly different from each other. The stats.ttest_ind function returns the t-statistic and the p-value, which we use to decide whether to reject the null hypothesis (that the means are equal).

Output:

T-statistic: -2.76
P-value: 0.02

4. Statsmodels

Statsmodels is a popular Python library for estimating and testing statistical models, such as linear regression, GLM, and time series models.

It is a library that provides classes and functions for the estimation of statistical models, performing hypothesis tests, and conducting data exploration.

Key Features

  • Statsmodels provides quite the range of statistical models, including linear regression, generalized linear models (GLM), discrete choice models, and time series models.
  • It offers a variety of statistical tests like ANOVA, t-tests, chi-square tests, and more, with detailed results and interpretations.
  • Provides detailed result objects for models, including statistical summaries, parameter estimates, confidence intervals, and diagnostic tests.
  • It contains implementations of time series analysis algorithms, including ARIMA, SARIMA, and state space models, with methods for forecasting and testing stationarity.

Code Example: Linear Regression

Let’s take an example of a simple linear regression using statsmodels.

import statsmodels.api as sm
import numpy as np

# Example data
X = np.random.rand(100)
y = 2 * X + np.random.randn(100) * 0.3

X = sm.add_constant(X)

# Fitting the regression model
model = sm.OLS(y, X).fit()

# Model summary
print(model.summary())

This code demonstrates how to perform linear regression. We generate some random data, add a constant to our predictor variable, and fit an Ordinary Least Squares (OLS) regression model. The model.summary() function provides a comprehensive summary of the regression results, including coefficients, R-squared value, and statistical tests.

Output:
ols-op

5. Pingouin

Pingouin is a statistical package in Python that is designed for easy use and includes a variety of statistical tests and functions. It is particularly user-friendly and integrates well with pandas.

Key Features

  • Pingouin offers a user-friendly syntax that makes it easy to perform a wide range of statistical tests.
  • It includes robust functions for ANOVA, t-tests, correlation tests, and more, providing comprehensive tools for statistical analysis.

Code Example: ANOVA Test

Let’s perform an ANOVA test using Pingouin to determine if there are any statistically significant differences between the means of three or more independent groups.

import pingouin as pg
import pandas as pd

# Sample data
data = pd.DataFrame({
	'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
	'Group': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D', 'D', 'E']
})

# Perform ANOVA
anova = pg.anova(data=data, dv='Value', between='Group')

print(anova)

Here, the pg.anova function performs the test and returns a table with the ANOVA results, including the F-statistic and p-value.

  Source  ddof1  ddof2         F     p-unc       np2
0  Group      4      7  29.53125  0.000178  0.944056

Conclusion

And that’s a wrap. Except for Python’s built-in statistics module, all other libraries require installation.

If you use a notebook environment like Google Colab, some of these libraries will be pre-installed. Or you can install Python, create virtual environments, and install all the libraries using the pip package manager.

You can find the code examples in this guide in this Google Colab notebook. Happy statistics!

Leave a Reply

Your email address will not be published. Required fields are marked *