Book Image

Mastering SciPy

By : Francisco Javier Blanco-Silva, Francisco Javier B Silva
Book Image

Mastering SciPy

By: Francisco Javier Blanco-Silva, Francisco Javier B Silva

Overview of this book

Table of Contents (16 chapters)
Mastering SciPy
About the Author
About the Reviewers


The idea of writing Mastering SciPy arose but 2 months after publishing Learning SciPy for Numerical and Scientific Computing. During a presentation of that book at the University of South Carolina, I had the privilege of speaking about its contents to a heterogeneous audience of engineers, scientists, and students, each of them with very different research problems and their own set of preferred computational resources. In the weeks following that presentation, I helped a few professionals transition to a SciPy-based environment. During those sessions, we discussed how SciPy is, under the hood, the same set of algorithms (and often the same code) that they were already using. We experimented with some of their examples and systematically obtained comparable performance. We immediately saw the obvious benefit of a common environment based upon a robust scripting language. Through the SciPy stack, we discovered an easier way to communicate and share our results with colleagues, students, or employers. In all cases, the switch to the SciPy stack provided a faster setup for our groups, where newcomers could get up to speed quickly.

Everybody involved in the process went from novice to advanced user, and finally mastered the SciPy stack in no time. In most cases, the scientific background of the individuals with whom I worked made the transition seamless. The process toward mastering materialized when they were able to contrast the theory behind their research with the solutions offered. The aha moment always happened while replicating some of their experiments with a careful guidance and explanation of the process.

That is precisely the philosophy behind this book. I invite you to participate in similar sessions. Each chapter has been envisioned as a conversation with an individual with certain scientific needs expressed as numerical computations. Together, we discover relevant examples—the different possible ways to solve those problems, the theory behind them, and the pros and cons of each route.

The process of writing followed a similar path to obtain an engaging collection of examples. I entered into conversations with colleagues in several different fields. Each section clearly reflects these exchanges. This was crucial while engaged in the production of the most challenging chapters—the last four. To ensure the same quality throughout the book, always trying to commit to a rigorous set of standards, these chapters took much longer to be completed to satisfaction. Special mentions go to Aaron Dutle at NASA Langley Research Center, who helped shape parts of the chapter on computational geometry, and Parsa Bakhtary, a data analyst at Facebook, who inspired many of the techniques in the chapter on applications of statistical computing to data analysis.

It was an amazing journey that helped deepen my understanding of numerical methods, broadened my perspective in problem solving, and strengthened my scientific maturity. It is my wish that it has the same impact on you.

What this book covers

Chapter 1, Numerical Linear Algebra, presents an overview of the role of matrices to solve problems in scientific computing. It is a crucial chapter for understanding most of the processes and ideas of subsequent chapters. You will learn how to construct and store large matrices effectively in Python. We then proceed to reviewing basic manipulation and operations on them, followed by factorizations, solutions of matrix equations, and the computation of eigenvalues/eigenvectors.

Chapter 2, Interpolation and Approximation, develops advanced techniques to approximate functions, and their applications to scientific computing. This acts as a segway for the next two chapters.

Chapter 3, Differentiation and Integration, explores the different techniques to produce derivatives of functions and, more importantly, how to compute areas and volumes effectively by integration processes. This is the first of two chapters devoted to the core of numerical methods in scientific computing. This second part is also an introduction to Chapter 5, Initial Value Problems for Ordinary Differential Equations that mentions ordinary differential equations.

Chapter 4, Nonlinear Equations and Optimization, is a very technical chapter in which we discuss the best methods of obtaining the roots and extrema of systems of functions depending on the kinds of functions involved.

Chapter 5, Initial Value Problems for Ordinary Differential Equations, is the first of five chapters on applications to real-world problems. We show you, by example, the most popular techniques to solve systems of differential equations, as well as some applications.

Chapter 6, Computational Geometry, takes a tour of the most significant algorithms in this branch of computer science.

Chapter 7, Descriptive Statistics, is the first of two chapters on statistical computing and its applications to Data Analysis. In this chapter, we focus on probability and data exploration.

Chapter 8, Inference and Data Analysis, is the second chapter on Data Analysis. We focus on statistical inference, machine learning, and data mining.

Chapter 9, Mathematical Imaging, is the last chapter of this book. In it, we explore techniques for image compression, edition, restoration, and analysis.

What you need for this book

To work with the examples and try out the code of this book, all you need is a recent version of Python (2.7 or higher) with the SciPy stack: NumPy, the SciPy library, matplotlib, IPython, pandas, and SymPy. Although recipes to install all these independently are provided throughout the book, we recommend that you perform a global installation through a scientific Python distribution such as Anaconda.

Who this book is for

Although this book and technology are ultimately intended for applied mathematicians, engineers, and computer scientists, the material presented here is targeted at a broader audience. All that is needed is proficiency in Python, familiarity with iPython, some knowledge of the numerical methods in scientific computing, and a keen interest in developing serious applications in science, engineering, or data analysis.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

Any command-line input or output is written as follows:

In [7]: %time eigvals, v = spspla.eigsh(A, 5, which='SM')
CPU times: user 19.3 s, sys: 532 ms, total: 19.8 s
Wall time: 16.7 s
In [8]: print eigvals
[ 10.565523  10.663114  10.725135  10.752737  10.774503]


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you. You can also download the code files from GitHub repository at

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any li st of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.