Book Image

SciPy Recipes

By : V Kishore Ayyadevara, Ruben Oliva Ramos
Book Image

SciPy Recipes

By: V Kishore Ayyadevara, Ruben Oliva Ramos

Overview of this book

With the SciPy Stack, you get the power to effectively process, manipulate, and visualize your data using the popular Python language. Utilizing SciPy correctly can sometimes be a very tricky proposition. This book provides the right techniques so you can use SciPy to perform different data science tasks with ease. This book includes hands-on recipes for using the different components of the SciPy Stack such as NumPy, SciPy, matplotlib, and pandas, among others. You will use these libraries to solve real-world problems in linear algebra, numerical analysis, data visualization, and much more. The recipes included in the book will ensure you get a practical understanding not only of how a particular feature in SciPy Stack works, but also of its application to real-world problems. The independent nature of the recipes also ensure that you can pick up any one and learn about a particular feature of SciPy without reading through the other recipes, thus making the book a very handy and useful guide.
Table of Contents (11 chapters)

Installing optional packages with conda

Sooner or later, you will need a package that is not installed by default in your Anaconda distribution.

If you are using the Anaconda distribution, this recipe describes the preferred method of installing and uninstalling packages. Anaconda is built on top of conda, a flexible and easy-to-use package manager.

Anaconda allows direct access to vast collections of add-on packages. For example, it makes it easy to install SciKits, which are SciPy packages developed by independent developers that are not part of the main distribution. A complete list of SciKits can be found at the following site: http://scikits.appspot.com/scikits.

Getting ready

This recipe assumes that you have a working installation of Anaconda. If you don't, follow the recipe for installing Anaconda for your operating system presented previously in this chapter.

How to do it...

As an example, lets install scikit-image, a package for image processing available at the site http://scikit-image.org:

To install scikit-image, first update conda and Anaconda by running the following two commands in your Terminal:

conda update conda
conda update anaconda

Now, scikit-image can be installed by running the following command at the system prompt:

conda install scikit-image

After the installation is finished, use a text editor to create a file called skimage_test.py containing the following code:

from skimage import data, io, filters

image = data.coins()
edges = filters.sobel(image)
io.imshow(edges)
io.show()

This code first constructs an image object from one of the sample images contained in scikit-image, which in this example is a photograph of a collection of old coins. It then applies a Sobel filter, which emphasizes the edges of objects present in the image. Finally, it displays the image of the detected coin edges.

Save the file and run the script by entering the following statement at the command line:

python3 skimage_test.py

Once the script runs, an image of the coin edges is displayed. Close the image to exit the script.

Let's now suppose that you don't need scikit-image any longer. It is easy to uninstall the package with conda using the following command:

conda uninstall scikit-image

If, after uninstalling, you attempt to run the skimage_test.py script, you will get an error message stating that Python can't import the skimage library.

Finally, what if a package is not available in Anaconda? In this case, it is still possible to install the package with pip, as outlined in the next section.

An alternative to installing packages directly in the global Anaconda distribution is to create a virtual environment with conda. Later in this chapter, we present several recipes for creating virtual environments with different requirements.