In [2]:
from import ConfigManager
from IPython.paths import locate_profile
cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))
cm.update('livereveal', {
              'theme': 'sky',
              'transition': 'zoom',
              'start_slideshow_at': 'selected',
{u'start_slideshow_at': 'selected', u'theme': 'sky', u'transition': 'zoom'}

Python crash course


  • Download and install Anaconda Python Distribution
  • (Optional) Download Academic license email is sufficient. It gives you access to the Intel MKL linear algebra package.
  • Run command in the terminal (if you have one, if you are on Windows, ask Evgeny Frolov what to do)

    jupyter 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 [25]:
#This how their import looks like
%matplotlib inline 
#To show everything in a browser
import numpy as np
import matplotlib.pyplot as plt 
import scipy as sp

Notice the namespaces

In [ ]:
import this

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 [6]:
#Some print 'Hi' code
for i in range(5):
In [7]:
from IPython.display import YouTubeVideo
#Long-long tutorial


Numpy arrays is something that you need to know.

It is typically a bad idea to write your own array processing code!

In [17]:
#Create some numpy array and print it
import numpy as np
a = [1, 2, 3, 4]
np_a = np.array(a)
print np_a
print a
[1 2 3 4]
[1, 2, 3, 4]


Let us do some arithmetics demo

In [21]:
#Do some arithmetics
b = 1.0
a = 1
c = a + b
print c, c ** 2, a/b
2.0 4.0 1.0


Let us do some loops demo

In [26]:
#Do some loop demo (i.e, create two lists and add them)
a = [1.0, 2.0, 3.0, 4.0]
b = [3.0, 4.0, 5.0, 6.0]
c = [0.0, 0.0, 0.0, 0.0]
for i in range(4):
    c[i] = a[i] + b[i]
c = a + b
print c
[1.0, 2.0, 3.0, 4.0, 3.0, 4.0, 5.0, 6.0]

Adding two lists....

In [20]:
#Now we can add two lists

Adding two numpy arrays...

In [31]:
#Now we want to add two arrays
print np.array(a) * np.array(b) ** 2
[   9.   32.   75.  144.]
In [37]:
#Do some matrix-by-vector operations 

a = np.array([[1.0, 0.5, 0.5], [0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
v = np.random.randn(3)
print #@
print a * v
[-1.63638585 -0.41324712 -6.42705844]
[[-1.26816793 -0.52981229  0.16159436]
 [-0.         -1.05962458  0.64637746]
 [-3.80450379 -4.23849831  1.61594365]]


Let us create some functions!

In [39]:
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 [40]:
M = 10 ** 5
a = np.ones(M) #All ones
%timeit c = mysum(a, a) #IPython magic function for timing
%timeit c = a + a ** 2 #Numpy built-in function
10 loops, best of 3: 35.1 ms per loop
10000 loops, best of 3: 73.6 µs per loop
  • Why is the difference?
  • Actually, there are many ways to solve the problem (Numpy, Cython, f2py, numba)


One of the interesting tools is Numba
Let us write some code...

In [44]:
from numba import jit
def mysum_nb(a, b):
    c = np.zeros(len(a))
    for i in range(len(a)):
        c[i] = a[i] + b[i] ** 3 + a[i] * b[i]

M = 10 ** 5
a = np.ones(M) #All ones
%timeit c = mysum_nb(a, a) #IPython magic function for timing
%timeit c = a + a ** 3 + a * a#Numpy built-in function
The slowest run took 1203.02 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 133 µs per loop
1000 loops, best of 3: 1.01 ms per loop


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

In [45]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns #For prettier plots
import numpy as np

Some matrix stuff

Our main object will be matrix.

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

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 [17]:
#Multiply matrix by vector here


  • Install & try to test Anaconda Python distribution
  • Bother TAs with questions
  • First homework to be delivered end of this week.

Stellar is still here.

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