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 packages with pip

The main tool to set up packages in a standalone installation of Python 3 is the pip3 command. This command will fetch a package from the Python Package Index (PyPI), a standard public repository located at https://pypi.python.org/pypi.

PyPI is somewhat hard to navigate due to the large number of packages from different application areas that are available. A list restricted to packages related to SciPy is available at: https://www.scipy.org/topical-software.html.

The existence of a uniform package manager makes it is straightforward to add and remove packages to a Python installation.

Python has a standard package distribution system, documented at the site https://packaging.python.org.
The procedures outlined in this site are the preferred methods to distribute Python software. This is recommended even in the case of software intended for internal use in an institution.
If you are using Anaconda, the preferred method for installing packages is to use conda, as explained in the previous section. Only use pip3 if the package is not available in Anaconda.

How to do it...

As an example, let's say we want to install the Bokeh package, located at http://bokeh.pydata.org/.

Bokeh is a visualization library that targets modern browsers for presentation. It produces high-quality plots in a variety of formats for inclusion in web pages. To install Bokeh in macOS or Linux, all you need to do is to run the following command in a Terminal window:

pip3 install bokeh

To install on Windows, run the following command at the Command Prompt:

pip install bokeh

This will remotely access PyPI, fetch Bokeh and all its dependencies, build the package, and make it accessible in the current Python 3 installation. To test the package, use a text editor to enter the following code in the bokeh_test.py file:

import numpy as np
from bokeh.plotting import figure, show

xvalues = np.linspace(-np.pi, np.pi, 50)
y1values = np.sin(xvalues)
y2values = np.cos(xvalues)

p = figure(
tools='pan,box_zoom,reset',
title='Trigonometric functions',
x_axis_label='x',
y_axis_label='y'
)

p.line(xvalues, y1values,
legend='y=sin(x)', line_color='blue')
p.circle(xvalues, y2values,
legend='y=cos(x)', fill_color='green',
line_color='green', size=3)

show(p)

print('Plot generated. Open file bokeh_test.html in your browser.')

This code uses the np.sin() and np.cos() NumPy functions to generate data points for the trigonometric functions sin(x) and cos(x). After that, it constructs a Bokeh figure object and adds two plots to the figure with the p.line() and p.circle() functions. Finally, the figure is displayed with a call to show(p).

Save the file in a convenient folder and open a Terminal window from the same folder. Run the following statement at the command line:

python3 bokeh_test.py

Running the script will create an output file named bokeh_test.html, with an HTML code that generates the figure described in the Python script. If you have a default browser defined for your system, Bokeh will automatically open the HTML file in the browser. Otherwise, you will need to open the file in the browser manually.

Notice that the graph displays control buttons at the top right. By clicking on the Pan button, the user can drag the graphed region with the mouse. With the Box Zoom tool, it is possible to draw a box on the graph, and the plot will be zoomed in on that box. Finally, Reset brings the figure back to its original state.

Let's say that Bokeh does not satisfy your needs and you do not want to keep it in your Python installation. The package can be easily uninstalled by issuing the following command in the Terminal, for Linux and macOS:

pip3 uninstall bokeh

On Windows, use the following command:

pip uninstall bokeh

The uninstaller will list the changes to be made and ask for confirmation. If you answer yes, the package and its dependencies will be removed from the system.

The pip3 package manager is a wonderful and well designed tool. However, it is a complex tool, since it has to carefully keep track of the changes and dependencies in the Python installation. Furthermore, it depends on developers to structure distributed packages with the correct dependencies. If all the reader wants is to test a package, I recommend the use of a virtual environment, as explained in the next section.