Monday, February 3, 2014

Post 13: Getting Started With Analytical Python Ecosystem


Python has been widely used in Artificial Intelligence and Machine Learning tasks, and it comes with a great set of libraries to help us in scientific and numerical computing. In this post, we would talk about:
1. how to obtain the software
2. explore the Python command line from IPython
3. experiment in IPython notebook

1.       Obtaining the software:

We would focus on getting the package that contains the libraries for numerical and scientific computing. So basically the packages that we would be interested in are (SciPy+NumPy, MatplotLib etc.)

For getting the SciPy stuff, you can use Continuum Anaconda or Enthought Canopy installers. The links for downloading the software are:


Let's start with Continuum Anaconda. We would download the windows installer from the list of installers for different operating systems.



After you have downloaded the installer, double click and follow the installation instruction.



























Once you have installed the software, you would be able to go to Start menu and see the launcher. You can click on the launcher and you can use the launcher to launch IPython notebook, qtconsole, and spyder GUI etc., as shown below.


















Let's look at downloading and installing canopy. Go to the link for enthought as given above, and download the installer (downloaded for windows).
























Click the installer and follow the installation instruction to install the software.

















You can launch its editor once installed, as shown below. Within the editor, you can create the new IPython notebook.


































You can also use http://wakari.io for web based editor, if you are unwilling to download and install the software in your machine.

2.  Explore the Python command line from IPython

We would open the IPython command line by clicking Start -> Anaconda (64-bit) -> IPython (Py 2.7) QTConsole. The command line would open up as shown below.

a) For seeing command history: history -n
b) Tab completion:  Suppose we create a list x = [3, 4, 5]. To know what commands you can fire against the list, just type the list variable, typing the dot, and then hitting tab. If you want to see the hidden ones, type _ and hit the tab again.






















c) Documentation - If we have an object instantiated, all we do is type the object name followed by the question mark and hit enter. In that case, we would get the doc string for that object.
d) shell commands - ls
If you want to access the underlying shell, use the ! mark and now you have access to your underlying shell.

x = !echo "hello world";

e) magic functions
%timeit [x for x in range(100)]
%run error.py - gives the detailed trace back.
%debug - IPython debugger, showing the exact place where the error occured


3.    IPython Noteback

Use command

>> IPython Notebook

from the command line or from the start menu -> Anaconda (64-bit) -> IPython (Py 2.7) Notebook to start the IPython notebook.

You can use the command,

>> ipython notebook --pylab inline

to start the notebook in pylab mode. This loads Scipy, and Matplotlib modules. Inline flag allows us to display the inline Matplotlib plots.

You can type %pylab inline in IPython notebook command line to load the pylab module.

You can also use namespace import statements like

>> import matplotlib.pyplot as plt

to bring in the Matplotlib pyplot module.

Once the modules are imported, you can plot as shown in the example below:

You can plot 1000 random samples over a uniform distribution into a 100 bins

>> plt.hist(np.random.randn(1000), bins=100)

By default, matplotlib would will return all the bins that were returned and ipython would print that for us. If you want to suppress that output, we can add a semicolon to the end of the statement
























If we are working with mathematics, we can insert mathematical formulae using markdown cell from the drop drown in the tool bar.

So if we select markdown and type $e^{i \pi}+1 = 0$ and then leave the edit view of the cell by clicking on some outside area of the browser, we would get to see the equation being displayed. as shown in the above picture.

No comments:

Post a Comment