The IPython Notebook!

Press shift + enter to run a cell.

You can go back to previous cells, change them and re-run them.

In [ ]:
print("Hello World")
In [ ]:
X = 112
In [ ]:
print(X)
In [ ]:
range(10)

IPython notebook allows tab-completion and shows docstrings (by pressing tab [shift-tab in latest versions] after the opening parantheses), or using ?

In [ ]:
range?

Cells can be arbitrary long or short, and can define functions that will be available in other cells.

In [ ]:
def fib(n):
    if n in [0, 1]:
        return n
    return fib(n - 1) + fib(n - 2)

for x in range(5):
    print(fib(x))

Numpy

Numpy array are the most common numeric data type.

As in other environments, it is very beneficial to vectorize your code (array-based computing) to make use of fast C and Fortran implementations.

In [ ]:
import numpy as np
np.ones(10)
In [ ]:
np.ones((10, 10))
In [ ]:
np.arange(10)

Numpy allows broadcasting over rows, leading to practical short-hand notations.

In [ ]:
X = np.ones((10, 10)) + np.array([3, 5, 1, 10, 6, 12, 98, 1, 0, 3])
print(X)

Most libraries in Python use object oriented interfaces.

In [ ]:
X.mean(axis=0)

Numpy has all standard array functions, linear algebra, and fancy indexing.

In [ ]:
X[:3, 1:4]
In [ ]:
X[:, ::2]
In [ ]:
X = np.random.randint(10, size=(32, 103))
X
In [ ]:
X[np.random.randint(32, size=10)]
In [ ]:
X[np.random.randint(32, size=10)].shape

Matplotlib

For all of your plotting needs!

Enable in-line plotting (can be done in config file)

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
plt.plot(np.random.uniform(size=10))
In [ ]:
plt.bar(np.arange(10), np.random.uniform(size=10))
In [ ]:
plt.hist(np.random.normal(size=1000))
In [ ]:
x, y = np.random.uniform(size=(2, 10))
plt.scatter(x, y, marker="x")
In [ ]:
print(np.eye(5))
plt.matshow(np.eye(5))

Tasks

  1. Plot the function f(x) = x ** 2 using lines. How can you get a smooth plot?
  2. Visualize a two-dimensional gaussian distribution from samples. How does your approach look with very many samples?
In [ ]: