The Essential Python Cheat Sheet for Statistical Analysis

python-statistical-analysis-cheat-sheet

Statistics can often be difficult. Which is why a quick reference cheat sheet—with essential statistics concepts—is helpful.

Python is a good programming language for statistical analysis. So it’s also useful to have Python code snippets you can look up.

This is a Python cheat sheet for statistical analysis. We’ll cover the following topics.
stats-topics
Note on code snippets: We use Python’s built-in statistics module and Python libraries like NumPy and SciPy to work with probability distributions and hypothesis testing. We also use matplotlib and seaborn for data visualization and scikit-learn to build regression models.

1. Descriptive Statistics

1.1 Measures of Central Tendency

Measures of central tendency help summarize datasets and give a rough idea of the data.

Mean: The average of the data points—the sum of all values/number of values

Significance: Best used for symmetric data distributions without extreme outliers

Formula:
mean
Code:

import statistics

data = [10, 20, 30, 40, 50]
mean = statistics.mean(data)
print("Mean:", mean)

Median: The middle value in sorted data

Significance: Ideal for skewed data or data with outliers; not affected by extreme values

Formula: When n is odd, the median is the (n+1)/2-th value. When n is even, the median is the average of the two middle values for an even-sized dataset.

Code:

import statistics

median = statistics.median(data)
print("Median:", median)

Mode: The most frequent value in the dataset

import statistics

data_with_mode = [10, 20, 20, 30, 40]
mode = statistics.mode(data_with_mode)
print("Mode:", mode)

Significance: Mostly helpful for categorical or nominal data to identify the most common category

Measure Use Case Function
Mean Symmetric data, no outliers statistics.mean()
Median Skewed data, data with outliers statistics.median()
Mode Categorical or nominal data statistics.mode()

1.2 Measures of Dispersion

Measures of dispersion help measure the spread and variability of the data. Understanding dispersion helps assess the spread and variability of numeric features in the dataset (often useful in identifying if the numeric features take on expected values).

Variance: The average squared deviation from the mean

Significance: Helps understand how spread out the data is

Formula:
variance
Code:

import statistics

variance = statistics.variance(data)
print("Variance:", variance)

Standard Deviation: The square root of the variance

Significance: Also a measure of spread, but in the same unit as the data

Formula: Standard Deviation = sqrt{Variance}

Code:

import statistics

std_dev = statistics.stdev(data)
print("Standard Deviation:", std_dev)

Range: maximum(data) – minimum(data)

Significance: Gives a simple enough measure of the overall spread of the data

Code:

data_range = max(data) - min(data)
print("Range:", data_range)
Measure Use Case Function
Variance Spread of data, sensitivity to outliers statistics.variance()
Standard Deviation Spread of data in the original unit statistics.stdev()
Range Quick estimate of data spread max() – min()

1.3 Correlation and Covariance

These metrics measure the relationship between two variables. Identifying relationships between variables helps with predictions, modeling, and understanding dependencies.

Pearson Correlation Coefficient: Used to measure the linear relationship between two variables. The value lies between -1 and 1.
correlation-coefficient
Code:

import numpy as np

x = [10, 20, 30, 40, 50]
y = [5, 10, 15, 20, 25]
correlation = np.corrcoef(x, y)[0, 1]
print("Pearson Correlation Coefficient:", correlation)

Covariance: Measures how two variables change together.

Significance: Positive covariance means both variables increase together, while negative covariance means one increases as the other decreases.

Code:

import numpy as np

x = [10, 20, 30, 40, 50]
y = [5, 10, 15, 20, 25]

covariance = np.cov(x, y)[0, 1]
print("Covariance:", covariance)
Metric Use Case Function
Pearson Correlation Linear relationship between variables numpy.corrcoef()
Covariance Relationship and joint variability numpy.cov()

1.4 Data Distribution

Helps understand how the data is spread out, the frequency of certain values, and patterns or anomalies within the dataset
Percentiles

  • Definition: Percentiles divide the data into 100 equal parts, helping to understand the position of a value relative to the entire dataset. If p% of the data falls below a particular value, it is the p-th percentile.
  • Use: Percentiles are useful for understanding the spread of data, detecting outliers, and comparing values within the dataset.

Quantiles

  • Definition: Quantiles divide the data into equal parts. Common quantiles are quartiles (4 equal parts), deciles (10 equal parts), and percentiles (100 equal parts).
  • Use: Quantiles help identify the spread and central tendency of the data.

Code:

import numpy as np

# Generating random data
data = np.random.normal(0, 1, 1000)

# Calculating percentiles
p_25 = np.percentile(data, 25)  # 25th percentile (Q1)
p_50 = np.percentile(data, 50)  # 50th percentile (median)
p_75 = np.percentile(data, 75)  # 75th percentile (Q3)

# Calculating quantiles (quartiles in this case)
q1, q2, q3 = np.quantile(data, [0.25, 0.5, 0.75])

print(f"25th Percentile: {p_25}")
print(f"50th Percentile (Median): {p_50}")
print(f"75th Percentile: {p_75}")
print(f"1st Quartile: {q1}, 2nd Quartile (Median): {q2}, 3rd Quartile: {q3}")

2. Probability Distributions

Getting the hang of probability distributions—describing how the values are distributed—is essential for statistical modeling, hypothesis testing, and predicting future events.

Understanding common distributions helps in fitting models to data and performing accurate analyses.

2.1 Normal Distribution

The normal distribution is commonly used in statistics because it is the simplest and best understood. It’s applicable when the data is symmetrically distributed around a mean.

Significance: The central limit theorem states that the sum of many independent random variables will be approximately normally distributed.

Code:

import numpy as np
import matplotlib.pyplot as plt

# Sampling from a normal distribution
data = np.random.normal(loc=0, scale=1, size=1000)
plt.hist(data, bins=30, edgecolor='black')
plt.title("Normal Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

2.2 Binomial Distribution

The binomial distribution is useful when there are only two possible outcomes (success or failure) and you perform a fixed number of trials.

Significance: Commonly used in situations like flipping a coin, passing a test, or success/failure experiments.

Code:

import numpy as np
import matplotlib.pyplot as plt

# Sampling from a binomial distribution
n, p = 10, 0.5
binomial_data =np.random.binomial(n, p, 1000)

plt.hist(binomial_data, bins=10, edgecolor='black')
plt.title("Binomial Distribution")
plt.xlabel("Number of Successes")
plt.ylabel("Frequency")
plt.show()

2.3 Poisson Distribution

The Poisson distribution models the number of events that happen in a fixed interval, given a known average rate of occurrence.

Significance: Used for rare events, like the number of accidents at a crossroad per day

Code:

import numpy as np
import matplotlib.pyplot as plt

poisson_data = np.random.poisson(lam=3, size=1000)  # λ=3, rate of occurrences

plt.hist(poisson_data, bins=10, edgecolor='black')
plt.title("Poisson Distribution")
plt.xlabel("Number of Events")
plt.ylabel("Frequency")
plt.show()

2.4 Exponential Distribution

The exponential distribution is usually used to model waiting times—the time between events in a Poisson process.

Significance: It is “memoryless,” meaning the probability of an event occurring in the future is independent of the past.

Code:

import numpy as np
import matplotlib.pyplot as plt

exponential_data = np.random.exponential(scale=1.0, size=1000)  # λ=1

plt.hist(exponential_data, bins=30, edgecolor='black')
plt.title("Exponential Distribution")
plt.xlabel("Time between events")
plt.ylabel("Frequency")
plt.show()

2.5 Uniform Distribution

Used when all outcomes within a specified range are equally likely to happen.

Significance: Useful in simulations or when you want to generate random data where all possibilities are equally probable.

Code:

import numpy as np
import matplotlib.pyplot as plt

uniform_data = np.random.uniform(low=0, high=10, size=1000)

plt.hist(uniform_data, bins=30, edgecolor='black')
plt.title("Uniform Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

2.6 Chi-Square Distribution

The chi-square distribution is used in hypothesis testing, particularly for tests of independence and goodness-of-fit tests.

Significance: Important in statistical tests like the chi-square test for independence, used to examine the relationship between two categorical variables.

Code:

import matplotlib.pyplot as plt
from scipy.stats import chi2

# Generate chi-square distribution with df=2 (degrees of freedom)
chi_square_data = chi2.rvs(df=2, size=1000)

plt.hist(chi_square_data, bins=30, edgecolor='black')
plt.title("Chi-Square Distribution (df=2)")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

2.7 Gamma Distribution

The gamma distribution is a two-parameter family of continuous probability distributions used in queuing models, insurance, and survival analysis.

Significance: Commonly used to model waiting times in processes where events occur at varying rates.

Code:

import matplotlib.pyplot as plt
from scipy.stats import gamma

# Shape parameter (k) = 2, scale (θ) = 1
gamma_data = gamma.rvs(a=2, scale=1, size=1000)

plt.hist(gamma_data, bins=30, edgecolor='black')
plt.title("Gamma Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

Common Distributions

Distribution Description Use Case Python Function
Normal Distribution Symmetrical, bell-shaped curve, defined by mean and standard deviation Most real-world data follows the normal distribution numpy.random.normal()
Binomial Distribution Used to model the number of successes in a fixed number of independent Bernoulli trials Used in problems with two outcomes (success/failure) scipy.stats.binom.rvs()
Poisson Distribution Used to model the number of events occurring in a fixed interval; the events are independent Used for rare events over time numpy.random.poisson()
Exponential Distribution Used to model the time between events in a Poisson process; Memoryless  Modeling waiting times numpy.random.exponential()
Uniform Distribution All outcomes in a given range are equally likely Used for random sampling where every outcome is equally probable numpy.random.uniform()
Chi-Square Distribution A special case of the gamma distribution, used in hypothesis testing Often used for tests of independence and goodness of fit  scipy.stats.chi2.rvs()
Gamma Distribution Used to model waiting times or process duration Applied to queuing models or survival analysis scipy.stats.gamma.rvs()

3. Inferential Statistics

With inferential statistics, you can make “inferences” about a population from samples. 

3.1 Hypothesis Testing

Hypothesis testing is used to determine whether there is enough evidence in a sample to infer that a condition holds true for the population.
Key Terms:

  • Null Hypothesis (H₀): Assumes no effect (or no difference)
  • Alternative Hypothesis (H₁): Assumes an effect or difference exists
  • p-value: The likelihood of observing the data if H₀ is true. If p-value < 0.05, reject H₀ (default significance level)
  • Test Statistic: Quantifies how far the sample statistic is from the null hypothesis value

3.2 t-test

Tests whether the means of groups differ significantly.

One-sample t-test: To compare sample mean to a known reference value

Two-sample t-test: To compares means of two independent groups

Paired t-test: To compares means of two dependent groups

Code:

import numpy as np
from scipy.stats import ttest_1samp, ttest_ind, ttest_rel

# Generating random data
group1 = np.random.normal(50, 5, 30)
group2 = np.random.normal(52, 5, 30)

# One-sample t-test
t_stat, p_value = ttest_1samp(group1, 50)

# Two-sample t-test
t_stat, p_value = ttest_ind(group1, group2)

# Paired t-test
t_stat, p_value = ttest_rel(group1, group2[:30])

3.3 ANOVA (Analysis of Variance)

Tests if the means of three or more groups differ significantly
Types:

  1. One-way ANOVA: To compares means of independent groups based on one factor
  2. Two-way ANOVA: To compares means based on two factors

Code:

import numpy as np
from scipy.stats import f_oneway

# Generating random data
group1 = np.random.normal(50, 5, 30)
group2 = np.random.normal(52, 5, 30)
group3 = np.random.normal(49, 5, 30)

# One-way ANOVA
f_stat, p_value = f_oneway(group1, group2, group3)

3.4 Chi-squared Test

Tests for independence between categorical variables
Code:

import numpy as np
from scipy.stats import chi2_contingency

# Contingency table
table = [[50, 30], [20, 40]]

# Chi-square test
chi2, p_value, dof, expected = chi2_contingency(table)

3.5 Mann-Whitney U Test

A non-parametric test for comparing two independent groups when data is not normally distributed.
Code:

import numpy as np
from scipy.stats import mannwhitneyu

# Generating random data
group1 = np.random.exponential(1, 30)
group2 = np.random.exponential(1.5, 30)

# Mann-Whitney U test
u_stat, p_value = mannwhitneyu(group1, group2)

3.6 Confidence Intervals

Confidence intervals give a range of values within which the population parameter is likely to lie—with a specified level of confidence.
Code:

import numpy as np
import scipy.stats as stats

# Generating random data
data = np.random.normal(50, 5, 30)

# Confidence interval for the mean
mean = np.mean(data)
sem = stats.sem(data)  
conf_interval = stats.t.interval(0.95, len(data)-1, loc=mean, scale=sem)

print(f"95% Confidence Interval: {conf_interval}")

Summary Table for Hypothesis Tests

Test Use Case Key Assumptions
t-test Compare means between groups Normality, equal variances (for two-sample)
ANOVA Compare means across 3+ groups Normality, equal variances
Chi-square Test Test independence of categorical variables Expected frequencies > 5 in contingency table
Mann-Whitney U Compare medians of two groups (non-parametric) Independent samples, ordinal/continuous data

4. Regression Analysis

Regression analysis is a tool that lets you model relationships between a dependent variable and one or more independent variables.

4.1 Linear Regression

Linear regression models the relationship between variables using a linear equation:
linear-regression
Where:

  • Y is the dependent variable
  • x_i are the independent variables
  • β_i are the coefficients 

Simple Linear Regression

Models the relationship between a dependent and an independent variable

Multiple Linear Regression

Models the relationship between a dependent variable and multiple independent variables

Code:

4.2 Logistic Regression

Logistic regression is an algorithm to predict the probability of data points belonging to a specific class. It uses the sigmoid (or logistic) function:
logistic-regression
Where P(y=1∣x) is the probability of the input x belonging to class 1.

Code:

from sklearn.linear_model import LogisticRegression
import numpy as np

# Data
X = np.array([[1], [2], [3], [4], [5]])  # Independent variable
y = np.array([0, 0, 0, 1, 1])  # Dependent variable (binary)

# Fit the model
model = LogisticRegression()
model.fit(X, y)

# Coefficients
print(f"Intercept: {model.intercept_}")
print(f"Coefficient: {model.coef_}")

# Predict probabilities
y_prob = model.predict_proba(X)[:, 1]
print(f"Predicted probabilities: {y_prob}")

# Predict class labels
y_pred = model.predict(X)
print(f"Predicted labels: {y_pred}")

Summary Table for Regression Analysis

Method Use Case Output
Simple Linear Regression Relationship between one predictor and response Intercept, slope, predictions
Multiple Linear Regression Relationship between multiple predictors and response Coefficients, predictions
Logistic Regression Probability of binary/categorical outcomes Probabilities, class labels

5. Data Visualization for Statistics

Visualizing data is an essential step in statistical analysis. It helps identify patterns, distributions, and outliers, making the data more interpretable. Let’s go over common visualizations used for statistical analysis:

5.1 Histograms

What: A histogram shows the frequency distribution of a dataset by dividing the data into bins.

Why: Useful for understanding the shape of the data distribution (e.g., normal, skewed, bimodal) and detecting outliers

Code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Generating random data
data = np.random.normal(0, 1, 1000)

# Plotting a histogram
plt.hist(data, bins=20, edgecolor='black', color='skyblue')
plt.title('Histogram')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

5.2 Box Plots

What: A box plot (or whisker plot) displays the distribution of data based on a five-number summary: minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum

Why: Great for detecting outliers, comparing distributions across groups, and visualizing the spread of data

Code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Generating random data
data = np.random.normal(0, 1, 100)

# Plotting a box plot
sns.boxplot(data=data, color='skyblue')
plt.title('Box Plot')
plt.xlabel('Data')
plt.show()

5.3 Scatter Plots

What: A scatter plot shows the relationship between two continuous variables by plotting points.

Why: Helps visualize correlations or patterns between two variables (e.g., linear or non-linear relationships).

Code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Generating random data
x = np.random.normal(0, 1, 100)
y = 2 * x + np.random.normal(0, 0.5, 100)

# Plotting a scatter plot
plt.scatter(x, y, color='skyblue', edgecolor='black')
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

5.4 Pair Plots

What: A pair plot visualizes pairwise relationships between numerical variables in a dataset

Why: Useful for exploring relationships and potential correlations between multiple variables

Code:

import seaborn as sns
import pandas as pd

# Generating random data
data = pd.DataFrame({
    'A': np.random.normal(0, 1, 100),
    'B': np.random.normal(1, 2, 100),
    'C': np.random.normal(2, 1, 100),
})

# Plotting a pair plot
sns.pairplot(data)
plt.show()

5.5 Line Plots

What: A line plot connects data points in a series with lines

Why: Best suited for time-series data or when analyzing trends over a continuous range

Code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Generating random data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plotting a line plot
plt.plot(x, y, color='skyblue', linewidth=2)
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

Summary Table for Visualization Choices

Visualization When to Use Key Insights
Histogram To view data distribution and frequency counts Shape, spread, and outliers
Box Plot To summarize and compare distributions Spread, quartiles, and outliers.
Scatter Plot To visualize relationships between two variables Correlation and trends
Pair Plot To explore relationships in a multi-variable dataset Correlation patterns among multiple variables
Line Plot To observe changes over time or a continuous range Trends and patterns in sequential data

That’s a wrap! To know the different Python libraries you can use for statistical analysis, read Essential Python Libraries for Statistics: A Practical Guide. And if you’re looking to learn statistics with Python, check out Statology’s library of Python guides.

 
Image by Author | Segmind SSD-1B Model

Leave a Reply

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