Numbers and numpy

Creating numpy arrays

  • Creating an array from a list
  • Properties: shape, size, dtype, stride
  • fromstring, fromregex, fromfunc
  • arange, empty, zeros, ones
  • eye, diag
  • empty_like, zeros_like, ones_like
  • linspace, logspace
  • meshgrid, mgrid
  • Changing order and dtype with astype

In [1]:
import numpy as np
import numpy.random as npr

In [4]:
x = np.array([1,2,3])

In [8]:
x2 = np.array([[1,2,3],[4,5,6]])

In [5]:
x.max()


Out[5]:
3

In [6]:
np.max(x)


Out[6]:
3

In [9]:
x2.shape


Out[9]:
(2, 3)

In [10]:
x2.size


Out[10]:
6

In [11]:
x2.dtype


Out[11]:
dtype('int64')

In [12]:
x2.strides


Out[12]:
(24, 8)

In [18]:
x3 = np.fromstring('1-2-3', sep='-', dtype='int')

In [19]:
x3.dtype


Out[19]:
dtype('int64')

In [20]:
x3


Out[20]:
array([1, 2, 3])

In [22]:
x3.astype('complex')


Out[22]:
array([ 1.+0.j,  2.+0.j,  3.+0.j])

In [15]:
%%file foo.txt
123-456-789abc
abc234-23-99x


Writing foo.txt

In [23]:
np.fromregex('foo.txt', r'[a-c]*(\d+)-(\d+)-(\d+)[abcx]*', dtype='int')


Out[23]:
array([[123, 456, 789],
       [234,  23,  99]])

In [24]:
np.fromfunction(lambda i, j: i**2 + j**2, (4,5))


Out[24]:
array([[  0.,   1.,   4.,   9.,  16.],
       [  1.,   2.,   5.,  10.,  17.],
       [  4.,   5.,   8.,  13.,  20.],
       [  9.,  10.,  13.,  18.,  25.]])

In [26]:
np.arange(8,10, 0.2)


Out[26]:
array([ 8. ,  8.2,  8.4,  8.6,  8.8,  9. ,  9.2,  9.4,  9.6,  9.8])

In [27]:
np.zeros((3,4))


Out[27]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [28]:
np.ones((2,3))


Out[28]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

In [29]:
np.eye(3)


Out[29]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [30]:
np.eye(3, k=1)


Out[30]:
array([[ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  0.]])

In [32]:
np.diag([1,2,3,4])


Out[32]:
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

In [38]:
x = np.ones((3,4), dtype='float')

In [39]:
x


Out[39]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

In [41]:
np.empty_like(x)


Out[41]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [42]:
np.linspace(0, 10, 11)


Out[42]:
array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

In [44]:
np.logspace(0, 10, num=11, base=2).astype('int')


Out[44]:
array([   1,    2,    4,    8,   16,   32,   64,  128,  256,  512, 1024])

In [45]:
np.meshgrid(range(3), range(4))


Out[45]:
[array([[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]), array([[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]])]

Creating random arrays

  • np.choice
  • Discrete distributions
  • Continuous distributions
  • shuffle and permutation

In [46]:
npr.choice(['H', 'T'], 10, p=[0.2, 0.8])


Out[46]:
array(['T', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'H'], 
      dtype='<U1')

In [50]:
npr.choice(10, 20, replace=True)


Out[50]:
array([9, 7, 2, 0, 3, 7, 3, 3, 0, 6, 6, 8, 2, 0, 3, 6, 1, 1, 3, 9])

In [51]:
npr.binomial(10, 0.5, 3)


Out[51]:
array([6, 4, 7])

In [52]:
npr.beta(5,1, 10)


Out[52]:
array([ 0.93907755,  0.94605278,  0.87580729,  0.89036244,  0.85595685,
        0.82103471,  0.71948532,  0.89582272,  0.98625894,  0.70513201])

In [53]:
npr.seed(123)
npr.beta(5,1, 10)


Out[53]:
array([ 0.77061245,  0.92874247,  0.46661652,  0.98406551,  0.97827394,
        0.81830982,  0.88390493,  0.93435773,  0.7106378 ,  0.85743991])

In [54]:
npr.seed(123)
npr.beta(5,1, 10)


Out[54]:
array([ 0.77061245,  0.92874247,  0.46661652,  0.98406551,  0.97827394,
        0.81830982,  0.88390493,  0.93435773,  0.7106378 ,  0.85743991])

In [55]:
x = np.arange(10)
x


Out[55]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [56]:
npr.shuffle(x)

In [57]:
x


Out[57]:
array([8, 0, 1, 7, 3, 4, 9, 5, 6, 2])

In [58]:
npr.permutation(x)


Out[58]:
array([2, 0, 6, 5, 8, 1, 7, 4, 9, 3])

In [59]:
x = np.arange(1,17).reshape(8,2)
x


Out[59]:
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12],
       [13, 14],
       [15, 16]])

In [60]:
npr.shuffle(x)
x


Out[60]:
array([[ 1,  2],
       [15, 16],
       [11, 12],
       [ 5,  6],
       [13, 14],
       [ 3,  4],
       [ 7,  8],
       [ 9, 10]])

Indexing, reshaping and concatenation

  • Simple, boolean and fancy indexing
  • ix_
  • reshape and transpose
  • A 1D-array is neither a row nor a column vector!
  • concat, stack, `split``
  • r_, c_

In [63]:
x


Out[63]:
array([[ 1,  2],
       [15, 16],
       [11, 12],
       [ 5,  6],
       [13, 14],
       [ 3,  4],
       [ 7,  8],
       [ 9, 10]])

In [65]:
x[2:5, :]


Out[65]:
array([[11, 12],
       [ 5,  6],
       [13, 14]])

In [68]:
x[x % 2 == 0]


Out[68]:
array([ 2, 16, 12,  6, 14,  4,  8, 10])

In [69]:
x[[0,3,5],:]


Out[69]:
array([[1, 2],
       [5, 6],
       [3, 4]])

In [74]:
x = np.arange(1, 17).reshape((4,4))

In [75]:
x


Out[75]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

In [76]:
x[np.ix_([0,2], [1,3])]


Out[76]:
array([[ 2,  4],
       [10, 12]])

In [77]:
x


Out[77]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

In [78]:
np.transpose(x)


Out[78]:
array([[ 1,  5,  9, 13],
       [ 2,  6, 10, 14],
       [ 3,  7, 11, 15],
       [ 4,  8, 12, 16]])

In [79]:
x.T


Out[79]:
array([[ 1,  5,  9, 13],
       [ 2,  6, 10, 14],
       [ 3,  7, 11, 15],
       [ 4,  8, 12, 16]])

In [80]:
x = np.array([1,2,3])

In [81]:
x


Out[81]:
array([1, 2, 3])

In [82]:
x.shape


Out[82]:
(3,)

In [83]:
x @ x


Out[83]:
14

In [86]:
xc = x.reshape((3,1))
xc


Out[86]:
array([[1],
       [2],
       [3]])

In [87]:
xr = x.reshape((1,3))
xr


Out[87]:
array([[1, 2, 3]])

In [88]:
xc.T @ xc


Out[88]:
array([[14]])

In [89]:
xc @ xc.T


Out[89]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [90]:
np.dot(xc, xc.T)


Out[90]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [91]:
x = np.arange(12).reshape(3,4)

In [92]:
x


Out[92]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [97]:
np.concatenate([[1,1,1], x])



ValueErrorTraceback (most recent call last)
<ipython-input-97-cb9d871f1482> in <module>()
----> 1 np.concatenate([[1,1,1], x])

ValueError: all the input arrays must have same number of dimensions

In [98]:
x0 = np.ones(3)

In [99]:
x0


Out[99]:
array([ 1.,  1.,  1.])

In [100]:
np.c_[x0, x]


Out[100]:
array([[  1.,   0.,   1.,   2.,   3.],
       [  1.,   4.,   5.,   6.,   7.],
       [  1.,   8.,   9.,  10.,  11.]])

In [107]:
np.vstack((x, x))


Out[107]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [108]:
y = np.r_[x, x]

In [109]:
y


Out[109]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [116]:
np.split(y, 2, axis=1)


Out[116]:
[array([[0, 1],
        [4, 5],
        [8, 9],
        [0, 1],
        [4, 5],
        [8, 9]]), array([[ 2,  3],
        [ 6,  7],
        [10, 11],
        [ 2,  3],
        [ 6,  7],
        [10, 11]])]

Vectorization and universal functions (ufuncs)

  • unary and binary ufucns
  • cumsum and cumprod
  • vectorize
  • dot, @, kron

In [126]:
a = np.arange(10)

In [119]:
a**2


Out[119]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

In [123]:
%timeit a**2


The slowest run took 16.45 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 14.4 µs per loop

In [124]:
%timeit [i**2 for i in a]


100 loops, best of 3: 2.18 ms per loop

In [127]:
np.exp(a)


Out[127]:
array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,
         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,
         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,
         8.10308393e+03])

In [128]:
a + a


Out[128]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

In [129]:
def f(a, b):
    return a + 2*b

In [130]:
f(a, a)


Out[130]:
array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27])

In [131]:
a = list(range(10))

In [132]:
f(a, a)


Out[132]:
[0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 0,
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9]

In [133]:
fv = np.vectorize(f)

In [134]:
fv(a,a)


Out[134]:
array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27])

In [136]:
np.kron([1,2,3], [2,3,4])


Out[136]:
array([ 2,  3,  4,  4,  6,  8,  6,  9, 12])

In [137]:
np.kron(np.diag([1,2]), np.ones((2,2)))


Out[137]:
array([[ 1.,  1.,  0.,  0.],
       [ 1.,  1.,  0.,  0.],
       [ 0.,  0.,  2.,  2.],
       [ 0.,  0.,  2.,  2.]])

In [138]:
np.diag([1,2])


Out[138]:
array([[1, 0],
       [0, 2]])

In [139]:
np.ones((2,2))


Out[139]:
array([[ 1.,  1.],
       [ 1.,  1.]])

Array reductions

  • Global reductions
  • Using axis

In [143]:
a = np.arange(1, 17).reshape((-1,2))

In [144]:
a


Out[144]:
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12],
       [13, 14],
       [15, 16]])

In [147]:
np.mean(a, axis=0)


Out[147]:
array([ 8.,  9.])

In [148]:
a.mean(axis=1)


Out[148]:
array([  1.5,   3.5,   5.5,   7.5,   9.5,  11.5,  13.5,  15.5])

In [149]:
a.var(axis=0)


Out[149]:
array([ 21.,  21.])

Broadcasting rules

  • Compatible shapes for broadcasting
  • Using newaxis to enable broadcasting
  • Examples: multiplication table, normalization and distance matrix

In [151]:
x = np.zeros((3,4), dtype='int')
x


Out[151]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

In [152]:
x.shape


Out[152]:
(3, 4)

In [153]:
a = np.array(1)
b = np.array([1,2,3,4])
c = np.array([10,20,30])

In [154]:
a.shape, b.shape, c.shape


Out[154]:
((), (4,), (3,))

In [155]:
x + a


Out[155]:
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])

In [156]:
x + b


Out[156]:
array([[1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4]])

In [158]:
x + c[:, np.newaxis]


Out[158]:
array([[10, 10, 10, 10],
       [20, 20, 20, 20],
       [30, 30, 30, 30]])

In [159]:
x = np.arange(1, 13)

In [160]:
x


Out[160]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

In [161]:
x[:, np.newaxis] * x[np.newaxis, :]


Out[161]:
array([[  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12],
       [  2,   4,   6,   8,  10,  12,  14,  16,  18,  20,  22,  24],
       [  3,   6,   9,  12,  15,  18,  21,  24,  27,  30,  33,  36],
       [  4,   8,  12,  16,  20,  24,  28,  32,  36,  40,  44,  48],
       [  5,  10,  15,  20,  25,  30,  35,  40,  45,  50,  55,  60],
       [  6,  12,  18,  24,  30,  36,  42,  48,  54,  60,  66,  72],
       [  7,  14,  21,  28,  35,  42,  49,  56,  63,  70,  77,  84],
       [  8,  16,  24,  32,  40,  48,  56,  64,  72,  80,  88,  96],
       [  9,  18,  27,  36,  45,  54,  63,  72,  81,  90,  99, 108],
       [ 10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120],
       [ 11,  22,  33,  44,  55,  66,  77,  88,  99, 110, 121, 132],
       [ 12,  24,  36,  48,  60,  72,  84,  96, 108, 120, 132, 144]])

In [162]:
x = npr.random((5, 2))

In [163]:
x


Out[163]:
array([[ 0.51948512,  0.61289453],
       [ 0.12062867,  0.8263408 ],
       [ 0.60306013,  0.54506801],
       [ 0.34276383,  0.30412079],
       [ 0.41702221,  0.68130077]])

In [164]:
x[:,np.newaxis,:].shape


Out[164]:
(5, 1, 2)

In [165]:
x[np.newaxis, :,:].shape


Out[165]:
(1, 5, 2)

In [167]:
np.sum((x[:,np.newaxis,:] - x[np.newaxis, :,:])**2, axis=2)


Out[167]:
array([[ 0.        ,  0.20464578,  0.01158522,  0.12657163,  0.01517806],
       [ 0.20464578,  0.        ,  0.3118545 ,  0.32205777,  0.10888575],
       [ 0.01158522,  0.3118545 ,  0.        ,  0.12580972,  0.05316947],
       [ 0.12657163,  0.32205777,  0.12580972,  0.        ,  0.14777904],
       [ 0.01517806,  0.10888575,  0.05316947,  0.14777904,  0.        ]])

Miscellaneous

  • set_printoptions
  • I/O with save, load, savetxt, loadtxt

In [168]:
np.set_printoptions(precision=2, suppress=True)

In [170]:
M = np.sum((x[:,np.newaxis,:] - x[np.newaxis, :,:])**2, axis=2)

In [171]:
np.save('M.npy', M)

In [172]:
np.load('M.npy')


Out[172]:
array([[ 0.  ,  0.2 ,  0.01,  0.13,  0.02],
       [ 0.2 ,  0.  ,  0.31,  0.32,  0.11],
       [ 0.01,  0.31,  0.  ,  0.13,  0.05],
       [ 0.13,  0.32,  0.13,  0.  ,  0.15],
       [ 0.02,  0.11,  0.05,  0.15,  0.  ]])