Getting Started With Python and AnacondaGetting Started With
Python and Anaconda1. Introduction2. Anaconda3. Downloading
Anaconda4. Installation on Linux5. Installation on Windows6.
Installation on Mac7. Spyder8. Getting Started: Variables and
Arrays9. Matrix Operations10. Importing Data11. IPython12. IPython
Notebooks13. Functions and Scripts14. References1.
IntroductionPython is a very popular general-purpose language, with
all the modern and classic constructs of a programming language
that every software developer appreciates. This is what makes
Python beneficial over MATLAB, besides the fact that it is not
proprietary and various open source python distributions are freely
and publicly available.However, the very fact that Python is a
general purpose language and not a software specific to scientific
computing may be considered a drawback of it, too. To address this
problem, several scientific computing packages (i.e. sets of
function,classes,...) have been developed and released for it so
far. These packages contain a large variety of functions which can
solve everyday computational problems of researchers in many fields
of engineering and science.But the remaining problem is to find,
install, maintain, manage updates and retain the consistency among
all such packages as well as the Python system itself. This is
where Anaconda comes in.2. AnacondaAnaconda is a completely free
Python distribution (i.e. set of packages) for scientific purposes,
as stated in their website. It contains more than 125 Python
packages for science, mathematics, engineering and data
analysis.Installing Anaconda will not only give you an
out-of-the-box ready python system as well as a fully-featured IDE
(Integrated Development Environment), but also it will release you
from the burden of manually installing and taking care of
dependency and consistency requirements between various packages.3.
Downloading AnacondaTo download the Anaconda, you can simply go to
the linkhttp://continuum.io/downloadsand download the zip file
compatible with your system. The download page looks like this:
It may ask for your e-mail as well. Please note that the Python
version Anaconda uses is Python 2.7.4. Installation on
LinuxInstalling Anaconda is pretty simple. On Linux-based systems,
all you need to do is running the following command.bash
Anaconda-1.x.x-Linux-x86[64].sh
, where you replaceAnaconda-1.x.x-Linux-x86[64].shwith the
proper name of the file you have downloaded. No root access is
required. However, you will need to manually add Python executable
files to your Path environment if you want to run them from every
folder. This can be done by adding the following line of code to
your ~/.bashrc file:export PATH=~/anaconda:
5. Installation on WindowsInstalling Anaconda on Windows should
be easy. It is automatically added to Path. In case of any
prospective problem, disabling your anti-virus can be a potential
solution.
6. Installation on MacOn MacOS, all you need to do is running
the graphical installer. Anaconda will be automatically added to
your path. However, in some cases an error message may appear at
the installation time which is not a big deal. You can simply
clickInstall for me onlyand go on:It seems to happens for older
versions of OS X that the following error is generated when
launching the ipython notebook (seesection 12)ValueError: unknown
locale: UTF-8In that case, run the commandlocalein the terminal and
inspect the value of the environment variableLC_CTYPE. It is
probably justUTF-8. Now open the file ~/.profile and add the
following line:export LC_CTYPE='us_EN.UTF-8'Then close the terminal
session and try again. You might need to replaceus_ENby a different
value matching your system configuration.7. SpyderSpyder is a
popular and very handy GUI for Python which is integrated in
Anaconda by default. It is very similar to MATLAB's GUI meaning
that if you have already worked with MATLB, you will not get lost
in spyder. You can run the spyder using the following command in
your command line:.spyder
When you run the command above, the following screen
appears.
TheConsolewindow on the bottom right is where you type your
commands and view possible results. The Object inspector window in
top right shows the help manual available for functions you type in
the console window. In the top bar of the application you can see
and change your working directory. This is the default location in
which spyder expects to find the files you read, the scripts you
run,... .Finally, theEditorwindow in the left is where you create
and edit your own functions. We will talk about functions later in
this tutorial.8. Getting Started: Variables and ArraysVariables are
where you store your values and results of your operations. Despite
some other programming languages, in Anaconda there is no need to
define a variable or its type prior to using it.The most important
variable type you will be working with is the array type. Please
note the very important fact that in Python, indices of an array
start from zero. This is in contrast to some other systems (the
most notable of them is MATLAB) in which arrays and matrices are
indexed starting from one.Initializing arrays is simple and can be
done using the following commands, for
example:In[1]:v1=[1,2,3,4]v2=[4,2,7,4]v3=[v1,v2]You may or may not
put semicolon at the end of the commands you type.The above
commands create three arrays. The first two are four-element
numerical arrays containing different numbers. The third one,v3, is
known as an array of arrays, a multi-dimensional (here
two-dimensional) array, or simply a matrix.v3is a two-element array
whose each element is a four-element array itself. Hence it is
a24matrix. The default type of the elements in an array is integer,
unless at least one of them is defined explicitly as a rational
number (i.e. having a fractional part, such as1.0).Please note that
hereafter, we use the terms "matrix" and "array" interchangeably
since in Python and in the programming language community in
general, a matrix is simply an array of arrays or a
multi-dimensional array.To see the value of an array (or any
variable in general), we can either simply write its name in the
console:In[2]:v2Out[2]:[4, 2, 7, 4]
or use the more elegant and more
flexibleprintcommand:In[3]:print v2[4, 2, 7, 4]
There are useful commands which facilitate creating and
initializing matrices. Two of them are the functionzerosandones,
which can create arbitrary arrays with values initialized
to0and1respectively. Consider the following commands for
example:In[4]:%pylab inlinez1=zeros(2)o1=ones((2,2))Populating the
interactive namespace from numpy and matplotlib
The first line creates a two-element arrayz1with all elements
initialized to0, whereas the second line creates a22matrixo1with
all elements initialized to1.Another useful group of functions are
those which query about the structure or shape of an array, i.e.
its number of elements and dimensions. The first one isshapewhich
returns the number of elements in each dimension of an array (like
MATLAB'ssize()).An example is the following line of code (assuming
you have already executed the above commands in your current
Anaconda session),
In[5]:shape(v3)Out[5]:(2L, 4L)which outputs(2L,4L)meaning that
v3 is a24matrix. The letterLin the output stands for "long",
stating that size of an array is by default a long integer
value.The second one issizewhich outputs the number of elements in
the whole array/matrix or a specific dimension of it (This is
partly similar to MATLAB'slength(), notsize(). Note the confusion).
Consider executing the following lines of code (again assuming you
have already executed the above commands in your current Anaconda
session):In[6]:print size(v3,0)print size(v3,1)24
You will see the outputs2and4respectively. These are the number
of elements in each column and row of v3, respectively.
Equivalently, these are the number of rows and columns ofv3.Please
note that the following command leads to an error, sincev3has only
two
dimensions.In[7]:size(v3,2)---------------------------------------------------------------------------IndexError
Traceback (most recent call last) in ()----> 1 size(v3,2)
E:\Programs\Anaconda\lib\site-packages\numpy\core\fromnumeric.pyc
in size(a, axis) 2536 return a.shape[axis] 2537 except
AttributeError:-> 2538 return asarray(a).shape[axis] 2539
2540
IndexError: tuple index out of range
9. Matrix OperationsMany functions are provided to manipulate an
array. Some useful ones are explained here.To flatten a
multi-dimensional array into a single-dimensional vector, we use a
combination of commandsflattenandlist. For example, the following
line of code flattens the arrayv3defined
above:In[8]:v4=list(flatten(v3))We can see the output by typing
"v4" in the Console window and viewing the
output:In[9]:v4Out[9]:[1, 2, 3, 4, 4, 2, 7, 4]The
functiontransposetransposesv3, similar to MATLAB's transpose
operator (single-quotation). This is shown
below:In[10]:v5=transpose(v3)print v5[[1 4] [2 2] [3 7] [4 4]]
The matrix multiplication and element-wise multiplication are
performed using the commandsdotandmultiply,
respectively:In[11]:v6=dot(v3,v5)v7=multiply(v3,v3)print v6print
v7[[30 45] [45 85]][[ 1 4 9 16] [16 4 49 16]]
Please note that the summation operator "+" has a completely
different meaning for arrays/matrices than for scalar numbers. The
output of the following line of code for example, is the
concatenation of the two vectorsv1andv2, rather than their
element-wise summation one may guess.In[12]:v1+v2Out[12]:[1, 2, 3,
4, 4, 2, 7, 4]For element-wise summation, we should use
thesumfunction. Please also note that the division operator on
integer values (which are default values in Python) acts as the
modulo operator. That means 2/4 outputs0for example. But 2.0/4
outputs0.5.addAddition
subtractSubtraction
dotMatrix multiplication
multiplyElement-wise multiplication
divideElement-wise division
np.powerElement-wise power
10. Importing DataVarious commands provide means to import data
in different formats and various modalities. A few of them are
explained here.To read the data stored in an ASCII text file, the
functionloadtxtis used. The following piece of code, for
example:In[14]:v11=loadtxt('data.txt')print v11[[ 1. 3. 5.] [ 2. 4.
6.]]
Reads the data in the filedata.txtinto the matrixv11and prints
the matrix. Each line of the text file is stored in a row
ofv11.Values in a line are assumed to be separated by white spaces
by default. To change this, for example in the case of
comma-separated CSV files, we use the following
form:In[15]:v12=loadtxt('data.csv',delimiter=',')print v12[[ 0. 3.
6.] [ 1. 5. 8.]]
We can also import data from MATLAB's specific.matfiles. This
can be done using the following lines of code:In[16]:import
scipy.io as siosio.loadmat('matlab.mat')Out[16]:{'M':
array([[-1.79467884, -0.19412354, -1.20784549, -2.0518163 ,
-0.29906603, 0.96422942, -0.58902903, 0.79141606, 0.86202161,
-0.06786555], [ 0.84037553, -2.13835527, 2.90800803, -0.35385 ,
0.02288979, 0.5200601 , -0.2937536 , -1.33200442, -1.36169447,
-0.1952212 ], [-0.88803208, -0.83958875, 0.82521889, -0.82358653,
-0.26199543, -0.02002785, -0.84792624, -2.32986716, 0.45502956,
-0.21760635], [ 0.10009283, 1.35459433, 1.37897198, -1.57705702,
-1.75021237, -0.03477109, -1.1201283 , -1.44909729, -0.84870938,
-0.30310762], [-0.54452893, -1.07215529, -1.05818026, 0.50797465,
-0.28565097, -0.79816358, 2.52599969, 0.33351083, -0.33488694,
0.02304562], [ 0.30352079, 0.96095387, -0.46861558, 0.28198406,
-0.83136651, 1.01868528, 1.65549759, 0.3913536 , 0.55278335,
0.05129036], [-0.60032656, 0.1240498 , -0.27246941, 0.03347988,
-0.97920631, -0.13321748, 0.30753516, 0.45167942, 1.03909065,
0.82606279], [ 0.48996532, 1.43669662, 1.09842462, -1.33367794,
-1.15640166, -0.71453016, -1.25711836, -0.13028465, -1.11763868,
1.52697669], [ 0.73936312, -1.9609 , -0.27787193, 1.12749228,
-0.53355711, 1.35138577, -0.86546803, 0.1836891 , 1.26065871,
0.46691444], [ 1.71188778, -0.19769823, 0.70154146, 0.35017941,
-2.00263574, -0.22477106, -0.17653411, -0.47615302, 0.66014314,
-0.20971334]]), 'N': array([[ 0.81472369, 0.15761308, 0.6557407 ,
0.70604609, 0.43874436, 0.27602508, 0.75126706, 0.84071726,
0.35165951, 0.07585429], [ 0.90579194, 0.97059278, 0.03571168,
0.03183285, 0.38155846, 0.67970268, 0.25509512, 0.25428218,
0.83082863, 0.05395012], [ 0.12698682, 0.95716695, 0.84912931,
0.27692298, 0.76551679, 0.655098 , 0.50595705, 0.81428483,
0.58526409, 0.53079755], [ 0.91337586, 0.48537565, 0.93399325,
0.04617139, 0.7951999 , 0.16261174, 0.69907672, 0.24352497,
0.54972361, 0.77916723], [ 0.63235925, 0.80028047, 0.67873515,
0.09713178, 0.1868726 , 0.11899768, 0.89090325, 0.92926362,
0.91719366, 0.93401068], [ 0.0975404 , 0.14188634, 0.75774013,
0.82345783, 0.4897644 , 0.49836405, 0.95929143, 0.34998377,
0.28583902, 0.12990621], [ 0.27849822, 0.42176128, 0.74313247,
0.69482862, 0.4455862 , 0.95974396, 0.54721553, 0.19659525,
0.75720023, 0.56882366], [ 0.54688152, 0.91573553, 0.39222702,
0.31709948, 0.64631301, 0.34038573, 0.13862444, 0.25108386,
0.75372909, 0.46939064], [ 0.95750684, 0.79220733, 0.65547789,
0.95022205, 0.70936483, 0.58526775, 0.14929401, 0.61604468,
0.38044585, 0.01190207], [ 0.96488854, 0.95949243, 0.17118669,
0.03444608, 0.75468668, 0.22381194, 0.25750825, 0.47328885,
0.56782164, 0.33712264]]), '__globals__': [], '__header__': 'MATLAB
5.0 MAT-file, Platform: PCWIN64, Created on: Tue Apr 22 00:06:25
2014', '__version__': '1.0'}The first line is required since the
package containing theloadmatfunction is not loaded by default when
spyder is started. So we need to import it manually.Finally, to
load an image into the spyder environment, the functionimreadis
used:In[17]:I=imread('David.bmp')print shape(I)(96L, 96L, 4L)
The command in the first line is used to read the
imageDavid.bmpinto a matrix, hereI. Supported formats may vary
depending on the device and the operating system. The second line
prints the size and the number of color channels of the image.11.
IPythonIPython is an interactive shell for Python which offers much
more functionality then the default Python command line. These vary
from more direct operating system support (e.g. for manipulation of
data files, interacting with external processes, ...) to logged
system state (i.e. storing results of all operations performed so
far). IPython is installed by Anaconda and can be used as an
alternative to the default spyder Console, although some of its
properties exist there too.To run IPython you should type the
following command in the command prompt of your operating
system:ipython
You will see a prompt asIn [1]. This means that it is your first
command. When you run something, the result is shown in an output
line starting withOut [1]. As you go on and execute more commands,
the number increases. You can always refer to the output of the
command in linensimply by the variable named_n(wherenis replaced by
the command number). This is a very useful property of the
IPython.One other feature of IPython is its more direct connection
to the underlying operating system. By preceding the character "!"
in front of a command line, that line is passed to the operating
system to be executed directly. You will find this specially useful
when you want to run OS commands or execute external files from
within Python. As an example, consider the following code:!vlc
test.avi
This will try to execute the vlc media player from the operating
system. More information about IPython and its features can be
found inthispaper.In recent versions of the IPython, a very
interesting feature has been added to it, called the IPython
notebooks. We will talk about these in the next section.12. IPython
NotebooksIPython notebooks are very interesting novel features
added to recent versions of IPython. Notebooks are interactive
documents that allow running Python code and reading (or writing)
notes and documentationsin the same place. Therefore, one can not
only see the results he is reading about, but also can produce
different results by changing the documented code.
A notebook is actually an extended HTML file which contains
specific markup to distinguish Python codes inside the page. When
displayed using a custom web server, it allows interactive
execution and editing of the code inside the document. However, it
can also be viewed as a usual, nicely-formatted HTML page. The
document you are currently reading is itself an IPython
notebook.The command to run the IPython Notebooks web server is the
following:ipython notebook
When you execute the command above, a new browser window is
opened which shows the notebooks in the current folder. The IPython
notebook files have the ".ipynb" extension. You can download the
".ipynb" files from a variety of sources on the internet, including
ourGitHub Repositoryfor numerical examples we will see throughout
this course. After downloading the notebook files (and their
additional material such as images,...) you should put all of them
in a folder, open a command window in that folder and run the
command above.12.1. Downloading and Running the Numerical Examples
of the Course in the Notebook FormatWe maintain the IPython
notebooks related to this course on theGitHubwebsite. We will not
explain here what GitHub is and what is it used for. In the scope
of this course, you can consider it simply as a storage
service.There are two main repositories on the GitHub related to
this course. One is the for thenumerical examplesand the other
containssignals of the day.To run our numerical examples, you first
need to download the notebooks from their corresponding
repositories. To do so, open the root page of the respository (the
appropriate link in the paragraph above). You will see a page like
the following:
Notice theDownload ZIPbutton in the bottom right. You have to
click on it to download the whole repository as a.zipfile. After
unzipping the repository in a folder of your choice in your
computer, you have to open your command line (terminal in Mac and
Linux) in or navigate in the commandline mode to the folder
containing your desired notebook (e.g.NumEx2):
The final step is running teh commandipython notebook. This will
pop your browser window up with the following page:
You then simply click on the name of the notebook of your choice
(Herenumex_module_2) in order to to execute it. Please note that in
order to see the desired output out of a code cell, you should have
executed the code cells above that (by pressing the
triangle-shapedplaybutton of the toolbar while on that cell).12.2.
Notebooks on the WebThere are a lot of notebooks available on the
web which you can see and read. The GitHub repository available
inherecontains many useful and interesting ones. The source code of
these notebooks is also available through the GitHub version
control system.Another interesting source is the book "Python For
Signal Processing" which is publicly available as a series of
IPython notebooks availableat this address.
13. Functions and ScriptsYou can define your own functions and
scripts in Python as well. We start with scripts here.You can
create, edit, save and execute your scripts through the Editor
panel in the left side of the spyder window. A script can be any
piece of code that you may want to run together and more than once.
For example, the following code draws 1000 random samples from the
standard Gaussian distribution and plots a histogram of them. You
can verify the statistical distribution of the samples by looking
at the histogram.In[18]:x = randn(10000);hist(x, 100);
To save the above code as a script, you should first create a
new file using the menu item "File> New File", type your script
in the file and then save it with your preferred name. the default
folder to save the scripts is your working folder we talked about
earlier. You can then execute the script by clicking the green play
button in the toolbar. This runs the currently open script.Looking
at the console, you will notice that clicking the play button is
equivalent to running the following function in
console:runfile('C:/Users/mrazavi/Documents/Python
Scripts/untitled1.py', wdir=r'C:/Users/mrazavi/Documents/Python
Scripts')
The first argument is the script file and the second one is the
working directory.You can also define your own functions in a
script. Functions are defined by a startingdefcommand. Consider the
following code for example:In[30]:def
celsius_to_fahrenheit(c_temp): return 9.0 / 5.0 * c_temp + 32print
celsius_to_fahrenheit(0)32.0
The code above first defines a function which receives a degree
in Celsius and returns its Fahrenheit equivalent. Then it prints
the result of applying the function to the value0.After the first
time you run the script containing a function, it is added to the
current spyder session meaning that you can later run it
independently from the console. However, if you change the
function, you should run the scripts for the changes to take
effect.
14. ReferencesThere is a lot of documentation available on the
web, both about Anaconda and its packages and also about Python
itself. A few of them are listed below.
http://docs.continuum.io/anaconda/install.html
http://wiki.scipy.org/NumPy_for_Matlab_Users
http://mathesaurus.sf.net/matlab-numpy.html
http://mathesaurus.sf.net/matlab-python-xref.pdf