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 SciPy from source on Linux

Since Linux is an umbrella name for a number of distinct operating system configurations, there is no binary distribution that fits all possible Linux flavors.

All modern distributions of Linux come with Python pre-installed. In this recipe, we describe a setup procedure for a local Python 3 installation from source that works on two of the most popular Linux distributions, Ubuntu and Debian. The installation will be located in the user's home directory, and will not conflict with any pre-installed version of Python that exists in the system.

How to do it...

The installation procedure is broken down in the following stages:

  • Installing Python 3
  • Installing the SciPy Stack

Installing Python 3

Start by opening a Terminal window and running the following commands, one at a time. You will be required to enter the administrative password for your system which, on a personal computer, is usually your own login password:

sudo apt-get install build-essential
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt-get install bzip2 libbz2-dev
sudo apt-get install libreadline-dev libncurses5-dev
sudo apt-get install libssl-dev tcl-dev tk-dev python3-tk
sudo apt-get install zlib1g-dev liblzma-dev

Next, download a source distribution of Python from the site https://python.org.

Make a note of the file name, which will look like the following: Python-3.x.x.tar.xz, where x.x is a pair of numbers that specify the build for this distribution. You should download the highest version available, which should be above 3.6.0.

Now, go to the directory where the downloaded file was saved and run the following command, replacing x.x with the corresponding build number of your file:

tar xvf Python-3.x.x.tar.xz

To build the distribution, execute the following commands, again replacing x.x with the correct build number:

cd Python-3.x.x
./configure --prefix=$HOME/python3
make

The build process will take a while to finish, depending on the configuration and speed of your system.

The following step is optional. If you want to run the battery of tests included with the source distribution, run the following command from the Terminal window:

make test

Depending on how fast your computer is, this may take a long time. At the end of the tests, a report of the build process will be generated. Do not worry if you get a few messages about skipped tests.

Next, install the code in its target directory, by running the following command:

make install

We now need to adjust the PATH environment variable. Start by making a backup copy of your shell configuration file by running the following commands:

cd
cp .bashrc .bashrc.python.bak

Start your text editor, open the .bashrc file, and add the following line at the end of the file:

export PATH=$HOME/python3/bin:$PATH

Save the file, close the editor and, back at the Terminal window, run the following command:

source ~/.bashrc

To test the installation, start Python from the command line by running the following command:

python3

If the installation is correct, Python will start and print information about the interpreter, as follows:

Python 3.x.x (default, Apr  4 2017, 09:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Check that the correct version of Python is being started by verifying that the version number 3.x.x coincides with that of the distribution you downloaded.

If you get an error when trying to start Python, or the wrong version of Python starts, the PATH variable was probably not set correctly in the .bash_rc file. You can check the current value of PATH by entering the echo $PATH command. If the path is not correct, edit .bashrc as indicated previously.

Exit Python by running the following command at the >>> Python prompt:

quit()

As a final test, run the following statement from the command line to check that Python's package manager was installed:

pip3 --version

This will print information about the version of pip3 that you are running. If no error message is issued, the setup was completed correctly and you can proceed to installing SciPy.

Installing the SciPy stack

Open a Terminal window and enter each of the following commands in succession:

pip3 install --user numpy scipy matplotlib
pip3 install --user ipython jupyter PyQt5
pip3 install --user pandas sympy nose

Make a backup copy of the .bashrc file and open it with a text editor. For example, to use nano, run the following commands in a Terminal window:

cd
cp .bashrc .bashrc.scipy.bak
nano .bashrc

Add the following line at the end of .bashrc:

export PATH="$HOME/.local/bin:$PATH"

Save the file, close the editor, and run the following command from the Terminal window:

source .bashrc

Let's now test the installation. Start Python by running the following command in a Terminal window:

python3

Execute the following lines at the >>> Python prompt. There will be no output and, as long as there are no errors, the installation is correct:

import numpy
import scipy
import matplotlib
import IPython
import pandas
import sympy
import nose

Exit Python by running the following at the prompt:

quit()

Back at the Terminal prompt, run the following command:

ipython

This will start IPython, an alternative Python shell with many added features, and print a startup message. You can check that the Python shell is running the expected version of Python from the top line of the startup message. Exit IPython by entering, at the prompt, the following command:

quit()

Back at the Command Prompt, run the following from the command line:

jupyter notebook

If all is correct, the Jupyter Notebook will start on your browser after a while. You have now concluded the installation of Python 3 and the SciPy stack in Linux.