Lesson Overview and Goal Setting

Welcome to another fascinating session! Today, we will delve into probability distributions and learn how Python expedites the exploration of data patterns. We will examine different types of probability distributions, specifically the Uniform and Normal distributions, and use Python libraries to visualize them.

Understanding the Basics of Probability

Probability measures the likelihood that an event will occur from all possible outcomes. If we flip a coin, the probability of getting a head is 50% or 0.5. Essentially, probability distributions map out each outcome of a random variable and its corresponding probability.

Visualization?

We will use visualization with python's powerful module, matplotlib, to have a glance at distributions we study. The whole visualization course is covered within the course path, but for now you may treat matplotlib as a magic black box that helps us. Remember that the focus of this lesson is exploring statistical distributions, so your focus should be on this part.

Exploring Uniform Distribution

Consider a scenario in which all outcomes have an equal chance of occurring. This phenomenon is described by a Uniform Distribution. For instance, if we draw a card suit from a deck, the probabilities of drawing a heart, club, diamond, or spade are equal. Let's generate and plot a Uniform Distribution using numpy and matplotlib.

import numpy as np
import matplotlib.pyplot as plt

# Generate random numbers uniformly distributed between -1 and 1
uniform_data = np.random.uniform(-1, 1, 1000)

# Plot a Histogram of the distribution
plt.hist(uniform_data, bins=20, density=True)
plt.title("Uniform Distribution")
plt.show()

Output:

Here, np.random.uniform(-1, 1, 1000) generates 1000 random numbers uniformly distributed between -1 and 1. plt.hist(uniform_data, bins=20, density=True) creates a histogram of the distribution, and plt.show() displays the plot.

Exploring Normal Distribution

Next, we will explore the Normal Distribution, a statistical function that describes a symmetrical, bell-shaped curve, prevalent in statistical analysis. A key characteristic of the Normal Distribution is that it is entirely defined by its mean (average) and standard deviation (spread). Let's simulate and plot a Normal Distribution:

# Generate Normal Distribution data
normal_data = np.random.normal(loc=0, scale=1, size=1000)

# Plot a Histogram of the distribution
plt.hist(normal_data, bins=20, density=True)
plt.title("Normal Distribution")
plt.show()

Output:

The function np.random.normal(loc=0, scale=1, size=1000) generates 1000 data points following a Normal Distribution with a mean of 0 and a standard deviation of 1.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal