3 Running random simulations in NumPy

 

This section covers

  • Basic usage of the NumPy library
  • Simulating random observations using NumPy
  • Visualizing simulated data
  • Estimating unknown probabilities from simulated observations

NumPy, which stands for Numerical Python, is the engine that powers Pythonic data science. Python, despite its many virtues, is simply not suited for large-scale numeric analysis. Hence, data scientists must rely on the external NumPy library to efficiently manipulate and store numeric data. NumPy is an incredibly powerful tool for processing large collections of raw numbers. Thus, many of Python’s external data processing libraries are NumPy compatible. One such library is Matplotlib, which we introduced in the previous section. Other NumPy-driven libraries are discussed in later portions of the book. This section focuses on randomized numerical simulations. We will use NumPy to analyze billions of random data points; these random observations will allow us to learn hidden probabilities.

3.1 Simulating random coin flips and die rolls using NumPy

NumPy should already be installed in your working environment as one of the Matplotlib requirements. Let’s import NumPy as np based on common NumPy usage convention.

Note

NumPy can also be installed independently of Matplotlib by calling pip install numpy from the command line terminal.

Listing 3.1 Importing NumPy
import numpy as np

3.1.1 Analyzing biased coin flips

 
 
 
 

3.2 Computing confidence intervals using histograms and NumPy arrays

 
 

3.2.1 Binning similar points in histogram plots

 
 

3.2.2 Deriving probabilities from histograms

 

3.2.3 Shrinking the range of a high confidence interval

 
 
 

3.2.4 Computing histograms in NumPy

 
 
 

3.3  Using confidence intervals to analyze a biased deck of cards

 
 

3.4 Using permutations to shuffle cards

 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest