Python crash course

Installation

  • Download and install Anaconda Python Distribution
  • (Optional) Download Academic license .skolkovotech.ru email is sufficient. It gives you access to the Intel MKL linear algebra package.
  • Run command in the terminal

    ipython notebook
    

    (on Mac & Linux, on Windows should work somehow as well).
    You will get a new browser window with the list of your notebooks. If there none, you can create one!

Notebook structure

  • Notebook consists of cells of different types
  • Code in the cells is executed by pressing Shift + Enter
  • To add a cell, use Insert in the menu
  • To edit the cell, double-click on it

Important points

  • Notebooks are good for short snipplets of the code
  • Big codes are typically written in a separate .py files and used as packages
  • It is a good idea to use Notebooks for the presentation of the final results of your computation

Importing packages

You have to import several basic packages to start doing basic stuff for linear algebra and plotting.
There are a lot of useful Python packages in the world for almost every task: always look on the web
before writing your own code!
Basic libraries:

  • numpy (for doing matrix & vector stuff)
  • matplotlib (for doing plotting), prettyplotlib (for nice plotting)
  • scipy (a huge math library, we will use it for sparse matrices)

In [3]:
import numpy as np
import matplotlib.pyplot as plt 
import scipy as sp

Notice the namespaces


In [4]:
import this


The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python vs MATLAB

MATLAB is a well-known tool for doing numerical linear algebra computations, but it has many disadvantages.
If you are an experienced MATLAB user, then you can look at Numpy for MATLAB users.

  • Indexing from 0
  • [ ] instead of ( ) for array element
  • Python is a powerful and flexible programming language :)

Python syntax

Best to learn is practice!

  • Use 4 whitespaces to denote a codeblock
    Not like this!!
    for (i = 0, i < 5, i++){
     printf("Hi! \n");
    }
    but like this:

In [10]:
for i in xrange(5):
    print 'Hi'
    print 'Hi2'
print 'end of Hi'


Hi
Hi2
Hi
Hi2
Hi
Hi2
Hi
Hi2
Hi
Hi2
end of Hi

In [11]:
from IPython.display import YouTubeVideo
#Long-long tutorial
YouTubeVideo('3Fp1zn5ao2M')


Out[11]:

Arrays


In [14]:
import numpy as np
a = np.linspace(0, 1, 2)
print a


[ 0.  1.]

Variables

Let us do some arithmetics demo


In [15]:
a = 5
b = -7 
c = a / b
print c
c = a * 1.0 / b
print c


-1
-0.714285714286

Loops

Let us do some loops demo


In [20]:
a = [5, 6, 7, 8]
b  = [9, 10, 11, 12]
c = [0, 0, 0, 0]
m = len(a)
for i in xrange(m):      #Notice the whitespace!!!!! Notice the : sign!!!
    c[i] = a[i] + b[i]
print c


[14, 16, 18, 20]

Adding two lists....


In [21]:
c = a + b
print c


[5, 6, 7, 8, 9, 10, 11, 12]

Adding two numpy arrays...


In [23]:
a = np.array(a)
b = np.array(b)
c = a * b
print c


[45 60 77 96]

In [29]:
#Matrix-by-vector
a = np.eye(10)
v = np.ones(10)

Functions

Let us create some functions!


In [31]:
def mysum(a, b):
    c = np.zeros(len(a)) #All zeros
    for i in xrange(len(c)):
        c[i] = a[i] + b[i]
    return c

And add two large arrays!


In [32]:
M = 10 ** 5
a = np.ones(M) #All ones
%timeit c = mysum(a, a) #IPython magic function for timing
%timeit c = a + a #Numpy built-in function


10 loops, best of 3: 77.7 ms per loop
10000 loops, best of 3: 70.2 µs per loop
  • Why is the difference?
  • Actually, there are many ways to solve the problem (Numpy, Cython, f2py, numba)

Plotting

To do plotting, you have to put in a preamble.


In [38]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
#import matplotlib
x = np.linspace(-10, 10, 1024)
plt.plot(x, np.sinc(x))


Out[38]:
[<matplotlib.lines.Line2D at 0x10643dd90>]

Some matrix stuff


In [39]:
from IPython.display import Image
i = Image(filename='TheMatrix.jpg', retina = True)
i


Out[39]:

Matrix-by-vector product

$A$ is an $n \times m$ matrix. $v$ is an $m \times 1$ matrix (vector).
$$ y = A x, $$ y is a vector of length $n$.


In [43]:
n = 100
m = 50
A = np.random.randn(n, m)
x = np.ones(m)
y = np.dot(A, x) #Unfortunately, no short operator for matmul
plt.plot(y)


Out[43]:
[<matplotlib.lines.Line2D at 0x107071f50>]

Summary

  • Install & try to test Anaconda Python distribution
  • Bother TAs with questions
  • Thursday & Friday plan: bring laptops with installed Anaconda for live problem solving.
  • First homework to be delivered today.

Stellar.


In [2]:
from IPython.core.display import HTML
def css_styling():
    styles = open("./styles/custom.css", "r").read()
    return HTML(styles)
css_styling()


Out[2]:

In [ ]: