Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Probability Distributions in NumPy

Learn how probability distributions describe random outcomes and how to generate samples using NumPy. Explore uniform, normal, binomial, and Poisson distributions with detailed Python examples, parameter explanations, practical applications, and simulation exercises.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand probability distributions and random variables.
  • Generate samples from a uniform distribution.
  • Generate normally distributed random values.
  • Simulate success counts using the binomial distribution.
  • Model event counts using the Poisson distribution.
  • Understand distribution parameters and their meanings.
  • Calculate sample statistics using NumPy.
  • Use histograms to inspect simulated distributions.
  • Apply probability distributions to practical problems.

2. Introduction to Probability Distributions

A probability distribution describes the possible outcomes of a random variable and how probabilities are assigned to those outcomes.

For example, when generating random marks, the values may follow a uniform distribution, a normal distribution, or another probability model. The selected distribution affects how frequently different values are generated.

NumPy provides distribution methods through the random generator returned by np.random.default_rng(). Each distribution has its own method and parameters.

import numpy as np rng = np.random.default_rng(100) values = rng.normal( loc=50, scale=10, size=5 ) print(values)

This program generates five random values from a normal distribution with mean 50 and standard deviation 10. The exact values depend on the generator and seed.

Common Probability Distributions

Distribution NumPy Method Typical Application
Uniform rng.uniform() Random values within an interval
Normal rng.normal() Measurements and variation around a mean
Binomial rng.binomial() Number of successes in fixed trials
Poisson rng.poisson() Counts of events in a fixed interval
Exponential rng.exponential() Waiting-time modelling
Choice-based sampling rng.choice() Selecting elements from a population
Remember: A distribution defines the statistical behavior of generated values. It does not guarantee that a small sample will match the theoretical distribution perfectly.

3. Uniform Distribution

In a continuous uniform distribution, values are generated across an interval with constant probability density. No part of the interval is favored over another part of the same length.

NumPy provides the uniform() method for generating continuous random values.

rng.uniform(low=0.0, high=1.0, size=None)

Important Parameters

Parameter Meaning
low Lower boundary of the interval.
high Upper boundary of the interval.
size Number or shape of generated values.

Example 1: Generate Uniform Random Values

import numpy as np rng = np.random.default_rng(10) values = rng.uniform( low=0, high=1, size=5 ) print(values)

Five floating-point values are generated over the interval from 0 up to 1. The upper boundary is normally excluded, although a floating-point result may occasionally round to a boundary value in numerical computation.

Example 2: Generate Values Between 10 and 50

import numpy as np rng = np.random.default_rng(10) values = rng.uniform( low=10, high=50, size=8 ) print(values) print("Minimum:", values.min()) print("Maximum:", values.max())

The values are generated across the specified interval. The sample minimum and maximum will generally lie inside the interval rather than equal its boundaries.

Example 3: Create a Two-Dimensional Uniform Array

import numpy as np rng = np.random.default_rng(10) matrix = rng.uniform( low=0, high=100, size=(3, 4) ) print(matrix) print("Shape:", matrix.shape)

The result is a three-row, four-column array of continuous random values.

Constant probability density Low High Values across the interval

Conceptual illustration of a continuous uniform probability density.

4. Normal Distribution

The normal distribution is a continuous probability distribution that is symmetric around its mean. It is often represented by a bell-shaped curve.

NumPy provides the normal() method to generate normally distributed values.

rng.normal(loc=0.0, scale=1.0, size=None)

Important Parameters

  • loc: The mean of the distribution.
  • scale: The standard deviation. It must be non-negative.
  • size: The number or shape of generated values.

Example 1: Standard Normal Distribution

import numpy as np rng = np.random.default_rng(20) values = rng.normal( loc=0, scale=1, size=10 ) print(values)

The mean is 0 and the standard deviation is 1. This is the standard normal distribution.

Example 2: Generate Simulated Marks

import numpy as np rng = np.random.default_rng(20) marks = rng.normal( loc=70, scale=10, size=1000 ) print("Sample mean:", marks.mean()) print("Sample standard deviation:", marks.std())

The distribution has a theoretical mean of 70 and standard deviation of 10. The statistics calculated from 1,000 samples should generally be near these values, but they will not necessarily equal them exactly.

Important: A normal distribution can generate values outside a desired range. For example, normally distributed marks may fall below 0 or above 100. If marks must remain within those limits, define a suitable bounded model or apply an explicitly justified transformation.

Example 3: Compare Different Standard Deviations

import numpy as np rng = np.random.default_rng(20) small_spread = rng.normal( loc=50, scale=3, size=1000 ) large_spread = rng.normal( loc=50, scale=12, size=1000 ) print("Small spread standard deviation:", small_spread.std()) print("Large spread standard deviation:", large_spread.std())

Both distributions have the same theoretical mean, but the second distribution has a larger standard deviation. Its values are generally more spread out.

5. Understanding Mean and Standard Deviation

The mean describes the center of a distribution, while the standard deviation describes how much values typically vary around the mean.

import numpy as np rng = np.random.default_rng(25) values = rng.normal( loc=100, scale=15, size=5000 ) print("Mean:", values.mean()) print("Standard deviation:", values.std()) print("Minimum:", values.min()) print("Maximum:", values.max())

The calculated mean and standard deviation describe the generated sample. The minimum and maximum depend on the actual random values.

Mean and Standard Deviation Comparison

Parameter Effect
Increase mean Moves the center of the distribution.
Decrease mean Moves the center in the opposite direction.
Increase standard deviation Increases the spread of values.
Decrease standard deviation Concentrates values more closely around the mean.
Remember: A distribution parameter describes the theoretical model. Statistics calculated from a finite sample can differ from the theoretical parameters.

6. Binomial Distribution

The binomial distribution models the number of successes in a fixed number of independent trials when each trial has the same probability of success.

Each trial has two possible outcomes for the model: success or failure. Examples include counting the number of successful responses in a fixed number of attempts.

rng.binomial(n, p, size=None)

Important Parameters

  • n: Number of trials in each experiment.
  • p: Probability of success in each trial, between 0 and 1 inclusive.
  • size: Number or shape of experiments to simulate.

Example 1: Simulate Coin Tosses

import numpy as np rng = np.random.default_rng(30) successes = rng.binomial( n=10, p=0.5, size=5 ) print(successes)

Each generated value represents the number of successes in ten independent trials, with success probability 0.5. If success represents heads, each value counts heads in ten coin tosses.

Example 2: Simulate a Quality Check

import numpy as np rng = np.random.default_rng(30) successful_items = rng.binomial( n=20, p=0.90, size=1000 ) print("Average successful items:", successful_items.mean()) print("Minimum successes:", successful_items.min()) print("Maximum successes:", successful_items.max())

Each experiment represents checking 20 items when the probability that an individual item passes is 0.90. The generated value counts how many items pass in each experiment.

Example 3: Calculate Theoretical Mean

For a binomial random variable, the theoretical mean is the number of trials multiplied by the probability of success.

import numpy as np n = 20 p = 0.90 theoretical_mean = n * p print("Theoretical mean:", theoretical_mean)

Output:

Theoretical mean: 18.0

The theoretical expected number of successful items is 18. Individual experiments can have fewer or more successes.

7. Poisson Distribution

The Poisson distribution models the number of events occurring within a fixed interval when events occur at a constant average rate under the model's assumptions.

Examples include counting incoming requests in a time interval or modelling the number of events observed over a fixed period.

rng.poisson(lam=1.0, size=None)

Important Parameter

  • lam: The expected event count in the specified interval. It must be non-negative.
  • size: The number or shape of generated samples.

Example 1: Generate Event Counts

import numpy as np rng = np.random.default_rng(40) events = rng.poisson( lam=4, size=10 ) print(events)

Each generated value is a non-negative integer representing an event count. The theoretical mean event count is 4.

Example 2: Simulate Incoming Requests

import numpy as np rng = np.random.default_rng(40) requests = rng.poisson( lam=12, size=1000 ) print("Average requests:", requests.mean()) print("Minimum requests:", requests.min()) print("Maximum requests:", requests.max())

This example simulates request counts for 1,000 equal intervals when the model's expected count is 12 per interval.

Example 3: Compare Two Event Rates

import numpy as np rng = np.random.default_rng(40) low_rate = rng.poisson( lam=3, size=1000 ) high_rate = rng.poisson( lam=15, size=1000 ) print("Low-rate sample mean:", low_rate.mean()) print("High-rate sample mean:", high_rate.mean())

The second sample has a higher theoretical event rate. Its average generated count will generally be higher.

8. Exponential Distribution

The exponential distribution is commonly used to model waiting times between events in certain random processes. It produces non-negative continuous values.

rng.exponential(scale=1.0, size=None)

Understanding the Scale Parameter

The scale parameter is the theoretical mean waiting time. For the exponential distribution, it is the reciprocal of the rate parameter.

Example 1: Generate Waiting Times

import numpy as np rng = np.random.default_rng(50) waiting_times = rng.exponential( scale=5, size=10 ) print(waiting_times)

Ten non-negative waiting-time samples are generated. The theoretical mean waiting time is 5 in the chosen time units.

Example 2: Simulate Waiting Times for Requests

import numpy as np rng = np.random.default_rng(50) waiting_times = rng.exponential( scale=2, size=1000 ) print("Average waiting time:", waiting_times.mean()) print("Shortest waiting time:", waiting_times.min()) print("Longest waiting time:", waiting_times.max())

The sample mean should generally be near 2 for a large sample, but the observed value varies with the generated data.

Important: A distribution is appropriate only when its assumptions reasonably describe the problem. Do not assume real-world waiting times follow an exponential distribution without examining the data and context.

9. Creating Histograms to Inspect Distributions

A histogram groups numerical observations into intervals called bins and displays the number of observations in each interval. It helps visualize the shape of generated data.

NumPy can calculate histogram counts using np.histogram(). This example does not require Matplotlib.

Example 1: Histogram of Normally Distributed Values

import numpy as np rng = np.random.default_rng(60) values = rng.normal( loc=50, scale=10, size=1000 ) counts, bin_edges = np.histogram( values, bins=10 ) print("Counts:") print(counts) print("Bin edges:") print(bin_edges)

The counts array records how many observations fall within each bin. The bin edges define the interval boundaries.

Example 2: Compare Uniform and Normal Samples

import numpy as np rng = np.random.default_rng(60) uniform_values = rng.uniform( low=0, high=100, size=5000 ) normal_values = rng.normal( loc=50, scale=15, size=5000 ) uniform_counts, edges = np.histogram( uniform_values, bins=10, range=(0, 100) ) normal_counts, _ = np.histogram( normal_values, bins=edges ) print("Uniform histogram:") print(uniform_counts) print("Normal histogram:") print(normal_counts)

The uniform sample tends to spread observations more evenly across equal-width bins in its interval. The normal sample tends to have more observations near its mean and fewer observations far into its tails.

Tip: A histogram from a finite random sample will vary between runs and may not look exactly like the theoretical probability curve.

10. Comparing Probability Distributions

Distribution Type of Output Key Parameters Typical Use
Uniform Continuous values Low, high Random values across an interval
Normal Continuous values Mean, standard deviation Variation around a central value
Binomial Integer counts Trials, success probability Successes in a fixed number of trials
Poisson Non-negative integer counts Expected event count Events per interval
Exponential Non-negative continuous values Scale Waiting-time modelling

Example: Generate Samples from Different Distributions

import numpy as np rng = np.random.default_rng(70) uniform_sample = rng.uniform(0, 1, size=5) normal_sample = rng.normal(0, 1, size=5) binomial_sample = rng.binomial( n=10, p=0.5, size=5 ) poisson_sample = rng.poisson( lam=4, size=5 ) print("Uniform:", uniform_sample) print("Normal:", normal_sample) print("Binomial:", binomial_sample) print("Poisson:", poisson_sample)

Each method generates data according to a different statistical model. The methods should be chosen according to the type of outcome and assumptions of the application.

11. Practical Project: Student Marks Simulation

Project Description

CIIT TRANING INSTITUTE wants to create a simulated marks dataset for classroom exercises. The dataset will contain 100 students' marks generated around a mean of 70 with a standard deviation of 10.

The program will calculate the average, standard deviation, minimum, and maximum values. It will also count how many generated values fall within the interval from 0 through 100.

Complete Program

import numpy as np rng = np.random.default_rng(123) marks = rng.normal( loc=70, scale=10, size=100 ) valid_marks = marks[(marks >= 0) & (marks <= 100)] print("Institute: CIIT TRANING INSTITUTE") print("Total simulated values:", marks.size) print("Average marks:", marks.mean()) print("Standard deviation:", marks.std()) print("Minimum simulated value:", marks.min()) print("Maximum simulated value:", marks.max()) print("Values within 0-100:", valid_marks.size)

The program generates normally distributed values and reports descriptive statistics. Because the normal distribution is unbounded, some values may fall outside the usual marks range.

Practical Interpretation

  • The mean describes the average of the generated values.
  • The standard deviation describes the spread of values.
  • The minimum and maximum describe the observed sample extremes.
  • The valid count reports how many values fall within 0 to 100.
Important: Filtering out-of-range values does not make the remaining values a newly generated normal distribution with exactly the same parameters. For realistic marks modelling, consider a suitable bounded distribution or an explicitly justified method.

12. Common Errors and Best Practices

Error 1: Using an Invalid Standard Deviation

import numpy as np rng = np.random.default_rng(1) # Invalid: scale cannot be negative # values = rng.normal(loc=50, scale=-5, size=10) # Correct values = rng.normal( loc=50, scale=5, size=10 ) print(values)

Error 2: Confusing the Mean with the Maximum

In rng.normal(loc=50, scale=10), 50 is the distribution mean, not the maximum possible value. A normal distribution has no finite upper or lower bound.

Error 3: Assuming Every Sample Matches the Mean

Random samples vary. The mean of a finite sample may differ from the theoretical mean, particularly when the sample is small.

Error 4: Using the Wrong Distribution

  • Use uniform sampling when a constant probability density across an interval is appropriate.
  • Use normal sampling when a bell-shaped continuous model is justified.
  • Use binomial sampling for successes in a fixed number of independent trials with a constant success probability.
  • Use Poisson sampling for event counts when its assumptions are reasonable.
  • Use exponential sampling for suitable non-negative waiting-time models.

Best Practices

  • Use np.random.default_rng() for new code.
  • Set a seed when reproducibility is important.
  • Understand the parameters before generating data.
  • Use sample statistics to inspect generated values.
  • Check whether the distribution can produce invalid values for your task.
  • Do not assume a simulation automatically represents real-world data.

13. Practice Exercises

Complete these exercises to practise NumPy probability distributions.

  1. Generate 20 uniform random values between 0 and 1.
  2. Generate 1,000 normally distributed values with mean 100 and standard deviation 15.
  3. Calculate the mean and standard deviation of a normal sample.
  4. Simulate ten coin-toss experiments, each containing 20 trials, using a binomial distribution.
  5. Generate 500 Poisson samples with an expected count of 8.
  6. Generate exponential waiting times with a mean of 3 units.
  7. Calculate histogram counts for a normal sample using np.histogram().
  8. Compare the sample means of uniform and normal distributions.
  9. Create a simulated marks dataset and report its descriptive statistics.
  10. Repeat a simulation with the same seed and check whether the results match.

14. Lesson Summary

Key Concepts to Remember

Probability distributions describe how random outcomes behave. NumPy provides distribution methods through np.random.default_rng().

The uniform distribution generates continuous values across an interval. The normal distribution is centered around its mean and controlled by its standard deviation.

The binomial distribution counts successes in a fixed number of trials. The Poisson distribution models event counts, while the exponential distribution is often used for waiting-time models.

Sample statistics and histograms help inspect generated data. A finite random sample may differ from theoretical expectations, and the selected distribution should match the assumptions of the problem.