Book Image

Practical Data Science Cookbook

By : Tony Ojeda, Sean Patrick Murphy, Benjamin Bengfort, Abhijit Dasgupta
Book Image

Practical Data Science Cookbook

By: Tony Ojeda, Sean Patrick Murphy, Benjamin Bengfort, Abhijit Dasgupta

Overview of this book

<p>As increasing amounts of data is generated each year, the need to analyze and operationalize it is more important than ever. Companies that know what to do with their data will have a competitive advantage over companies that don't, and this will drive a higher demand for knowledgeable and competent data professionals.</p> <p>Starting with the basics, this book will cover how to set up your numerical programming environment, introduce you to the data science pipeline (an iterative process by which data science projects are completed), and guide you through several data projects in a step-by-step format. By sequentially working through the steps in each chapter, you will quickly familiarize yourself with the process and learn how to apply it to a variety of situations with examples in the two most popular programming languages for data analysis—R and Python.</p>
Table of Contents (18 chapters)
Practical Data Science Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Profiling Python code using IPython's %timeit function


Often, when we are working in the Python REPL, we would like to be able to quickly and easily benchmark a line of code or a function. IPython makes this possible through a magic function called timeit.

How to do it…

Perform the following set of steps to try out %timeit to profile the code:

  1. Open up a new terminal and change to the direction of the asa.py source code.

  2. Fire up IPython by typing the following:

    ipython
    
  3. Let's see how fast or slow the built-in square root function is, as follows:

    In [1]: import math
    In [2]: %timeit math.sqrt(10000)
    
  4. This should produce output similar to the following:

    10000000 loops, best of 3: 166 ns per loop
    
  5. We see that %timeit, due to the rapid execution of the math.sqrt() method, tested the function execution 10,000,000 times to get a more accurate measurement.

  6. Next, we will use %timeit to test the main loop of the asa code calculation. For this, we must first import the relevant functions:

    In [2]: from asa import...