Numpy

In [1]:
import numpy as np
In [2]:
vector = np.arange(12)
In [3]:
vector.shape
Out[3]:
(12,)
In [4]:
vector.dtype
Out[4]:
dtype('int64')

Another way of making the same array is:

In [5]:
vector_again = np.array([0,1,2,3,4,5,6,7,8,9,10,11]) # array from a list

Addition and so on work on elements, rather like the MATLAB dot syntax for element-wise operations:

In [6]:
vector + 1
Out[6]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
In [7]:
vector * 2
Out[7]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22])
In [8]:
vector + vector_again
Out[8]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22])

You can make 2 or more dimensions:

In [9]:
arr2d = vector.reshape((4, 3))
In [10]:
arr2d
Out[10]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
In [11]:
a_transpose_a = arr2d.T.dot(arr2d)
a_transpose_a
Out[11]:
array([[126, 144, 162],
       [144, 166, 188],
       [162, 188, 214]])
In [12]:
np.linalg.inv(a_transpose_a)
Out[12]:
array([[ -1.18515780e+13,   2.37031559e+13,  -1.18515780e+13],
       [  2.37031559e+13,  -4.74063119e+13,   2.37031559e+13],
       [ -1.18515780e+13,   2.37031559e+13,  -1.18515780e+13]])

Get help with:

In [13]:
np.lookfor('correlation')
Search results for 'correlation'
--------------------------------
numpy.corrcoef
    Return correlation coefficients.
numpy.correlate
    Cross-correlation of two 1-dimensional sequences.
numpy.ma.corrcoef
    Return correlation coefficients of the input array.
numpy.cov
    Estimate a covariance matrix, given data.

Scipy

Scipy is a large library. Check out what is in there by doing:

In [14]:
import scipy

and then doing tab completion. In fact, there is so much in there, that when you import scipy, it does not load all its submodules, as this would take too long. So, you have to import them individually before using them.

In [15]:
import scipy.io
import scipy.ndimage
import scipy.optimize
import scipy.stats
In [16]:
scipy.io.savemat('for_matlab.mat', {'arr2d': arr2d, 'vector': vector})
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/io/matlab/mio.py:266: FutureWarning: Using oned_as default value ('column') This will change to 'row' in future versions
  oned_as=oned_as)
In [17]:
import glob
glob.glob('*.mat')
Out[17]:
['for_matlab.mat']
In [18]:
mat_contents = scipy.io.loadmat('for_matlab.mat')
In [19]:
mat_contents
Out[19]:
{'__globals__': [],
 '__header__': 'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri Feb 22 09:36:00 2013',
 '__version__': '1.0',
 'arr2d': array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]]),
 'vector': array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11]])}
In [20]:
random_arr = np.random.normal(size=(128, 128))
In [21]:
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [22]:
import matplotlib.pyplot as plt
plt.imshow(random_arr)
Out[22]:
<matplotlib.image.AxesImage at 0x106f67990>
In [23]:
smoothed_arr = scipy.ndimage.gaussian_filter(random_arr, 8)
In [24]:
plt.imshow(smoothed_arr)
Out[24]:
<matplotlib.image.AxesImage at 0x1071dd250>
In [25]:
def func(x):
    return x ** 2 - 5 * x + 4
x = np.linspace(-3, 7)
plt.plot(x, func(x)) 
Out[25]:
[<matplotlib.lines.Line2D at 0x1075d0750>]
In [26]:
scipy.optimize.fmin_powell(func, 0)
Optimization terminated successfully.
         Current function value: -2.250000
         Iterations: 2
         Function evaluations: 39
Out[26]:
array(2.5000000105713474)
In [27]:
rv = scipy.stats.norm(0, 1)
x = np.linspace(-3, 3)
pdf = rv.pdf(x)
pdf
Out[27]:
array([ 0.00443185,  0.00635135,  0.00896675,  0.01247075,  0.01708592,
        0.02306069,  0.03066159,  0.04016108,  0.05182083,  0.0658706 ,
        0.08248352,  0.10174921,  0.12364689,  0.1480211 ,  0.17456307,
        0.20280069,  0.2320998 ,  0.26167871,  0.29063661,  0.31799518,
        0.34275126,  0.36393672,  0.38068082,  0.39226937,  0.39819528,
        0.39819528,  0.39226937,  0.38068082,  0.36393672,  0.34275126,
        0.31799518,  0.29063661,  0.26167871,  0.2320998 ,  0.20280069,
        0.17456307,  0.1480211 ,  0.12364689,  0.10174921,  0.08248352,
        0.0658706 ,  0.05182083,  0.04016108,  0.03066159,  0.02306069,
        0.01708592,  0.01247075,  0.00896675,  0.00635135,  0.00443185])
In [28]:
plt.plot(pdf)
Out[28]:
[<matplotlib.lines.Line2D at 0x107846f90>]

Get help with:

In [29]:
scipy.lookfor('convolution')
Search results for 'convolution'
--------------------------------
numpy.convolve
    Returns the discrete, linear convolution of two one-dimensional sequences.
numpy.bartlett
    Return the Bartlett window.
numpy.correlate
    Cross-correlation of two 1-dimensional sequences.

Matplotlib

You've already seen matplotlib in action above, displaying images and plots. Here a little more.

In [30]:
x = np.linspace(-np.pi, np.pi)
y = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y)
plt.plot(x, y2, 'g:')
Out[30]:
[<matplotlib.lines.Line2D at 0x10784f650>]

Cython

Cython allows you to write code that can make use of C compilation speed from within Python

In [31]:
def sum_to(n): # A python function
    result = 0
    for val in range(1, n):
        result += val
    return result
In [32]:
sum_to(100)
Out[32]:
4950
In [33]:
%load_ext cythonmagic
In [34]:
%%cython
def cython_sum_to(int n): # A Cython function
    cdef int result = 0 # Tell Cython that result is a C int
    cdef int val # ditto val
    for val in range(1, n): # Now this loop gets compiled to C
        result += val
    return result
In [35]:
cython_sum_to(100)
Out[35]:
4950
In [36]:
%timeit sum_to(10000)
1000 loops, best of 3: 1.1 ms per loop
In [37]:
%timeit cython_sum_to(10000)
100000 loops, best of 3: 8.7 us per loop

Sympy

In [38]:
import sympy
In [39]:
X = sympy.Symbol('X')
In [40]:
my_formula = X**2 + X / 2 + sympy.sin(X)
my_formula
Out[40]:
X**2 + X/2 + sin(X)
In [41]:
my_formula.diff()
Out[41]:
2*X + cos(X) + 1/2
In [42]:
my_formula.integrate()
Out[42]:
X**3/3 + X**2/4 - cos(X)