Book review: Numpy Cookbook

I had the opportunity from Packt Publishing to review Numpy Cookbook. Many thanks to the publisher for this and let’s go to the review.

Content and opinions

Numpy Cookbook is a list of several dozens recipes to use Numpy. Or so the title and subtitle say, but in fact it’s nothing like that. More or less than 75% of the book are not about Numpy, but about the Numpy ecosystem, which is far better. This also means that the audience for the book is not Python beginners, but people who know about how the language works and are looking for advice on the scientific side.

The first chapter tackles IPython and more precisely a new feature called the notebook. The specific scientific side of it is the support of Matplotlib, the notebook being a good way of sharing scripts with plots between people. It is indeed a common problem that one may encounter. The next two chapters cover Numpy aspects: how arrays, indexing work and how functions can be applied on them. It is worth mentioning that the last recipe (the Sieve of Erathosthenes) from these chapters doesn’t work. I actually don’t know how the author expected it to work, the algorithm being wrong. There are also several installation recipes, but they tend to be slightly different each time. Why? No real explanation. This happens a lot with all the different installations. Sometimes there are even packages that don’t have such recipe. So no coherency here.

The next chapter handles communication between other Python modules, other languages or frameworks. A small overview of what is called the buffer protocol and the array interface is provided. They are both sides of the same coin, the inner Numpy way of working. Then other languages are covered: Matlab and Octave (but it seems that the author forgot going back from Matlab to Numpy and the fact that Matlab changed its I/O format and went to HDF5), R, Java and the Google App Engine. Mainly these recipes are about installing the interfaces, not about using them and then, there is only a small example of using them. I would have expecting something deeper. One still has to read the manual of these interfaces to use them even for something very basic.

Next two chapters are actual scientific recipes. Based on 1D and 2D signals, the first one tackles some modules in Scipy in addition to memmap, a way of using data from the disk without string it in memory. Unfortunately, this is not dealt in-depth, memmap may have needed more that just a few lines. The second chapter presents more Numpy classes that can be useful, especially masked arrays, as well as universal functions. It is advanced Numpy use and regularly helpful in complex algorithms.

Before the last chapter on Scikits, the author spends two chapters on something scientists often forget: profiling, debugging and quality insurance. Almost all the main modules in these domains are introduced, safe for nosetests, currently the main test framework. It’s a shame that it isn’t one of the recipes, as it supersedes all the modules of the tests recipes. Still, I really appreciate the presence of all these tips that all scientists should apply as they are too often overlooked on scientific books.

As I’ve said, the last chapter presents some scikits, application specific packages (machine learning, image), and pandas, a statistics module. I think of them as threads that readers can follow if they want to dig in a specific direction.

Conclusion

Let’s start with saying that the book is neither a good nor a bad book. It could have been a good book if the author had gone further into the content, and more precisely Numpy. My point of view is that the title, subtitle and everything on the cover cannot represent the book. It doesn’t solve common problems (finding prime number or a palindrome is not a common problem, it’s a pet problem but not an actual real-life problem) and too many recipes are installation recipes. One would have sufficed.

But the book is not a bad one. If the reader doesn’t know much of Numpy, it can help starting using it and as I’ve said, threads can be followed if a specific direction is of interest. It can also a sort of exercise book for a Numpy crash course, provided that some errors are fixed.

Final word: even if I would have liked more structure is the code (class and function), at least the infamous from pylab import * is not advocated in the book.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.