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

Optimizing the innermost loop with NumPy


In this recipe, we are going to rewrite the calculate_asa function, replacing a double-nested loop with NumPy and SciPy functions that have been heavily optimized for performance.

Getting ready

Take a moment and review the original calculate_asa function in the asa.py source file. Note the three levels of for loops present. Copy the asa_np_v1.py file to a new file named asa_np_v2.py, and open this in your editor of choice. Again, if you are proficient with the code-versioning tool of your choice, feel free to use it to handle the versioning for you.

How to do it…

The following steps complete this chapter:

  1. First, we heavily modify the calculate_asa function as shown in the following code:

    def calculate_asa(atoms, probe, n_sphere_point=960):
    """
    Returns accessible surface areas for atoms, using the probe
    and atom radius to define the surface.
    """
    sphere_points = np.array(generate_sphere_points(n_sphere_point))
    points = np.array([ [a.pos.x, a.pos.y, a.pos...