machkev / dsc-normal-distribution-lab-onl01-dtsc-pt-030220

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The Normal Distribution - Lab

Introduction

In this lab, you'll learn how to generate random normal distributions in Python. You'll learn how to visualize a histogram and build a density function using the formula.

Objectives

You will be able to:

  • Use numpy to generate a random normal distribution
  • Calculate the density function for normal distributions with a Python function
  • Plot and interpret density plots and comment on the shape of the plot

A quick refresher!

Here's the formula for the normal distribution density function once more:

$$ \large N(x) = \dfrac{1}{\sigma \sqrt {2\pi }}e^{-\dfrac{(x-\mu)^2}{2\sigma^2}}$$

Here,

  • $\mu$ is the mean
  • $\sigma$ is the standard deviation
  • $\pi \approx 3.14159 $
  • $ e \approx 2.71828 $

First generate a normal distribution containing 5000 values with $\mu=14$ and $\sigma = 2.8$

# Generate a random normal variable with given parameters , n=5000

Calculate a normalized histogram for this distribution in matplotlib, with bin size = 20

Make sure to get the bin positions and counts for each of the obtained bins. You can use official documentation to view input and output options for plt.hist()

# Calculate a histogram for above data distribution

png

Use the formula to calculate the density function with $\mu$, $\sigma$ and bin information obtained before

# Calculate the normal Density function 
density = None

Plot the histogram and density function

# Plot histogram along with the density function

png

Visualize the distribution using seaborn and plot the KDE

# Use seaborn to plot the histogram with KDE
<matplotlib.axes._subplots.AxesSubplot at 0x1a121adac8>

png

Summary

In this lab, you learned how to generate random normal distributions in Python using Numpy. You also calculated the density for normal distributions using the general formula as well as seaborn's KDE. Next, you'll move on to learn about the standard normal distribution and how normal distributions are used to answer analytical questions.

About

License:Other


Languages

Language:Jupyter Notebook 100.0%