Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Random sampling with numpy.random.choice()


Bootstrapping is a procedure similar to jackknifing. The basic bootstrapping method has the following steps:

  1. Generate samples from the original data of size N. Visualize the original data sample as a bowl of numbers. We create new samples by taking numbers at random from the bowl. After taking a number, we return it to the bowl.

  2. For each generated sample, we compute the statistical estimator of interest (for example, the arithmetic mean).

How to do it...

We will apply numpy.random.choice() to do bootstrapping:

  1. Generate a data sample following the binomial distribution that simulates flipping a fair coin five times:

    N = 400
    np.random.seed(28)
    data = np.random.binomial(5, .5, size=N)
  2. Generate 30 samples and compute their means (more samples will give a better result):

    bootstrapped = np.random.choice(data, size=(N, 30))
    means = bootstrapped.mean(axis=0)
  3. Visualize the arithmetic means distribution with a matplotlib box plot:

    plt.title('Bootstrapping demo')
    plt...