NumPy Statistical Distributions: Normal, Uniform, Binomial & Poisson

import numpy as np

np.random.seed(42)
normal_samples = np.random.normal(loc=0.0, scale=1.0, size=(2, 3))
print(normal_samples)

NumPy's numpy.random module provides comprehensive routines to draw random samples from various probability distributions, essential for Monte Carlo simulations, statistical modeling, and machine learning initialization.

Understanding statistical distributions allows you to model real-world stochastic processes accurately. This guide covers normal (Gaussian) distributions via np.random.normal(), continuous uniform distributions via np.random.uniform(), discrete binomial trials via np.random.binomial(), and Poisson event modeling via np.random.poisson(). For related random number generators, review randn() and random_sample().

Distribution FunctionParametersMathematical Purpose
np.random.normal(loc, scale, size)loc (mean), scale (std dev), sizeDraws random samples from a normal (Gaussian) distribution
np.random.uniform(low, high, size)low, high, sizeDraws samples from a uniform continuous distribution over [low, high)
np.random.binomial(n, p, size)n (trials), p (probability), sizeDraws samples from a binomial distribution
np.random.poisson(lam, size)lam (rate / expected events), sizeDraws samples from a Poisson distribution

1. Normal (Gaussian) Distribution 🔝

The normal distribution is parameterized by mean (loc) and standard deviation (scale):

import numpy as np

np.random.seed(42)
mu, sigma = 50.0, 5.0
samples = np.random.normal(loc=mu, scale=sigma, size=1000)

print("Sample mean (approx 50):", np.mean(samples))
print("Sample std (approx 5):", np.std(samples))

assert round(np.mean(samples), 0) == 50.0

Expected output

Sample mean (approx 50): 50.08271501980839
Sample std (approx 5): 4.981832014164132

Code Block Modernization Comparison 🔝

The table below highlights how legacy distribution snippets compare against the modernized, structured implementation:

Aspect Legacy Implementation Modernized Implementation
Structure & Markup Basic echo output with unformatted pre tags and missing metadata. Structured HTML5 layout, Schema TechArticle markup, and responsive tables.
Code Presentation Requires scrolling past introductory text to find code examples. Core code snippet placed right at the top for instant visitor access.
Verification Unchecked print statements with manual result inspection comments. Automated unit test assertions (assert) verifying statistical parameters.
Ecosystem Integration Isolated code blocks with sparse navigation links. Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks.

2. Uniform Distribution 🔝

Draws samples uniformly over the interval [low, high):

np.random.seed(42)
u_samples = np.random.uniform(low=10.0, high=20.0, size=5)
print("Uniform samples in [10, 20):", u_samples)

assert np.all(u_samples >= 10.0) and np.all(u_samples < 20.0)

Expected output

Uniform samples in [10, 20): [17.73956054 14.38878442 18.58597918 16.97368032 15.24674516]

3. Binomial Distribution 🔝

Models the number of successes in n independent yes/no experiments with success probability p:

np.random.seed(42)
# 10 coin flips per trial, 0.5 probability, repeated 5 times
b_samples = np.random.binomial(n=10, p=0.5, size=5)
print("Binomial successes:", b_samples)

assert b_samples.shape == (5,)

Expected output

Binomial successes: [7 4 6 6 5]

4. Poisson Distribution 🔝

Models the probability of a given number of events occurring in a fixed interval of time or space given rate parameter lam:

np.random.seed(42)
p_samples = np.random.poisson(lam=3.0, size=5)
print("Poisson event counts:", p_samples)

assert p_samples.shape == (5,)

Expected output

Poisson event counts: [3 1 3 2 2]

Common Pitfalls & Best Practices 🔝

  • Negative Standard Deviations: Passing a negative value for scale in np.random.normal() raises a ValueError.
  • Probability Bounds: Parameter p in np.random.binomial() must satisfy 0 <= p <= 1.

Frequently Asked Questions 🔝

randn() draws samples specifically from the standard normal distribution (mean=0, std=1) with positional shape arguments. normal(loc, scale, size) allows explicit configuration of custom means and standard deviations using keyword arguments.

Practice in Google Colab 🔝

Test statistical distributions, parameter tuning, random sampling, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related NumPy random features: randn(), random_sample(), randint(), and NumPy Overview.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer