Intro to Numpy With Examples
Intro to Numpy With Examples
/ tools_numpy.ipynb (/github/ageron/handson-ml2/tree/master/tools_numpy.ipynb)
Tools - NumPy
NumPy is the fundamental library for scientific computing with Python. NumPy is centered around a
powerful N-dimensional array object, and it also contains useful linear algebra, Fourier transform,
and random number functions.
Creating Arrays
Now let's import numpy . Most people import it as np :
In [1]:
import numpy as np
np.zeros
In [2]:
np.zeros(5)
Out[2]:
In [3]:
np.zeros((3,4))
Out[3]:
Some vocabulary
In NumPy, each dimension is called an axis.
The number of axes is called the rank.
For example, the above 3x4 matrix is an array of rank 2 (it is 2-dimensional).
The first axis has length 3, the second has length 4.
An array's list of axis lengths is called the shape of the array.
For example, the above matrix's shape is (3, 4) .
The rank is equal to the shape's length.
The size of an array is the total number of elements, which is the product of all axis lengths
(eg. 3*4=12)
In [4]:
a = np.zeros((3,4))
a
Out[4]:
In [5]:
a.shape
Out[5]:
(3, 4)
In [6]:
Out[6]:
In [7]:
a.size
Out[7]:
12
N-dimensional arrays
You can also create an N-dimensional array of arbitrary rank. For example, here's a 3D array
(rank=3), with shape (2,3,4) :
In [8]:
np.zeros((2,3,4))
Out[8]:
Array type
NumPy arrays have the type ndarray s:
In [9]:
type(np.zeros((3,4)))
Out[9]:
numpy.ndarray
np.ones
Many other NumPy functions create ndarrays .
In [10]:
np.ones((3,4))
Out[10]:
np.full
Creates an array of the given shape initialized with the given value. Here's a 3x4 matrix full of π .
In [11]:
np.full((3,4), np.pi)
Out[11]:
np.empty
An uninitialized 2x3 array (its content is not predictable, as it is whatever is in memory at that
point):
In [12]:
np.empty((2,3))
Out[12]:
np.array
Of course you can initialize an ndarray using a regular python array. Just call the array
function:
In [13]:
Out[13]:
array([[ 1, 2, 3, 4],
[10, 20, 30, 40]])
np.arange
You can create an ndarray using NumPy's arange function, which is similar to python's built-
in range function:
In [14]:
np.arange(1, 5)
Out[14]:
array([1, 2, 3, 4])
np.arange(1.0, 5.0)
Out[15]:
In [16]:
np.arange(1, 5, 0.5)
Out[16]:
However, when dealing with floats, the exact number of elements in the array is not always
predictible. For example, consider this:
In [17]:
np.linspace
For this reason, it is generally preferable to use the linspace function instead of arange when
working with floats. The linspace function returns an array containing a specific number of
points evenly distributed between two values (note that the maximum value is included, contrary to
arange ):
In [18]:
In [19]:
np.random.rand(3,4)
Out[19]:
Here's a 3x4 matrix containing random floats sampled from a univariate normal distribution
(https://en.wikipedia.org/wiki/Normal_distribution) (Gaussian distribution) of mean 0 and variance 1:
In [20]:
np.random.randn(3,4)
Out[20]:
To give you a feel of what these distributions look like, let's use matplotlib (see the matplotlib
tutorial (tools_matplotlib.ipynb) for more details):
In [21]:
%matplotlib inline
import matplotlib.pyplot as plt
In [23]:
np.fromfunction
You can also initialize an ndarray using a function:
In [23]:
Out[23]:
array([[[ 0., 1., 2., 3., 4., 5., 6., 7., 8.,
9.],
[ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.]],
[[100., 101., 102., 103., 104., 105., 106., 107., 108., 109.],
[110., 111., 112., 113., 114., 115., 116., 117., 118., 119.]],
[[200., 201., 202., 203., 204., 205., 206., 207., 208., 209.],
[210., 211., 212., 213., 214., 215., 216., 217., 218., 219.]]])
NumPy first creates three ndarrays (one per dimension), each of shape (3, 2, 10) . Each
array has values equal to the coordinate along a specific axis. For example, all elements in the z
array are equal to their z-coordinate:
[[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
[[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]]]
In [25]:
c = np.arange(1, 5)
print(c.dtype, c)
int64 [1 2 3 4]
In [26]:
c = np.arange(1.0, 5.0)
print(c.dtype, c)
float64 [ 1. 2. 3. 4.]
Instead of letting NumPy guess what data type to use, you can set it explicitly when creating an
array by setting the dtype parameter:
In [27]:
d = np.arange(1, 5, dtype=np.complex64)
print(d.dtype, d)
itemsize
The itemsize attribute returns the size (in bytes) of each item:
In [28]:
e = np.arange(1, 5, dtype=np.complex64)
e.itemsize
Out[28]:
data buffer
An array's data is actually stored in memory as a flat (one dimensional) byte buffer. It is available via
the data attribute (you will rarely need it, though).
In [29]:
Out[29]:
In [30]:
if (hasattr(f.data, "tobytes")):
data_bytes = f.data.tobytes() # python 3
else:
data_bytes = memoryview(f.data).tobytes() # python 2
data_bytes
Out[30]:
'\x01\x00\x00\x00\x02\x00\x00\x00\xe8\x03\x00\x00\xd0\x07\x00\x
00'
Several ndarrays can share the same data buffer, meaning that modifying one will also modify
the others. We will see an example in a minute.
Reshaping an array
In place
Changing the shape of an ndarray is as simple as setting its shape attribute. However, the
array's size must remain the same.
In [31]:
g = np.arange(24)
print(g)
print("Rank:", g.ndim)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23]
Rank: 1
In [32]:
g.shape = (6, 4)
print(g)
print("Rank:", g.ndim)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
Rank: 2
In [33]:
g.shape = (2, 3, 4)
print(g)
print("Rank:", g.ndim)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Rank: 3
reshape
The reshape function returns a new ndarray object pointing at the same data. This means
that modifying one array will also modify the other.
In [34]:
g2 = g.reshape(4,6)
print(g2)
print("Rank:", g2.ndim)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
Rank: 2
In [35]:
g2[1, 2] = 999
g2
Out[35]:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 999, 9, 10, 11],
[ 12, 13, 14, 15, 16, 17],
[ 18, 19, 20, 21, 22, 23]])
Out[36]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[999, 9, 10, 11]],
ravel
Finally, the ravel function returns a new one-dimensional ndarray that also points to the
same data:
In [37]:
g.ravel()
Out[37]:
Arithmetic operations
All the usual arithmetic operators ( + , - , * , / , // , ** , etc.) can be used with ndarray s.
They apply elementwise:
In [38]:
a + b = [19 27 35 43]
a - b = [ 9 19 29 39]
a * b = [70 92 96 82]
a / b = [ 2.8 5.75 10.66666667 20.5 ]
a // b = [ 2 5 10 20]
a % b = [4 3 2 1]
a ** b = [537824 279841 32768 1681]
Note that the multiplication is not a matrix multiplication. We will discuss matrix operations below.
The arrays must have the same shape. If they do not, NumPy will apply the broadcasting rules.
Broadcasting
In general, when NumPy expects arrays of the same shape but finds that this is not the case, it
applies the so-called broadcasting rules:
First rule
If the arrays do not have the same rank, then a 1 will be prepended to the smaller ranking arrays
until their ranks match.
In [39]:
h = np.arange(5).reshape(1, 1, 5)
h
Out[39]:
array([[[0, 1, 2, 3, 4]]])
Now let's try to add a 1D array of shape (5,) to this 3D array of shape (1,1,5) . Applying the
first rule of broadcasting!
In [40]:
h + [10, 20, 30, 40, 50] # same as: h + [[[10, 20, 30, 40, 50]]]
Out[40]:
Second rule
Arrays with a 1 along a particular dimension act as if they had the size of the array with the largest
shape along that dimension. The value of the array element is repeated along that dimension.
In [41]:
k = np.arange(6).reshape(2, 3)
k
Out[41]:
array([[0, 1, 2],
[3, 4, 5]])
Let's try to add a 2D array of shape (2,1) to this 2D ndarray of shape (2, 3) . NumPy will
apply the second rule of broadcasting:
In [42]:
k + [[100], [200]] # same as: k + [[100, 100, 100], [200, 200, 200]]
Out[42]:
k + [100, 200, 300] # after rule 1: [[100, 200, 300]], and after rule 2:
[[100, 200, 300], [100, 200, 300]]
Out[43]:
In [44]:
Out[44]:
Third rule
After rules 1 & 2, the sizes of all arrays must match.
In [45]:
try:
k + [33, 44]
except ValueError as e:
print(e)
Broadcasting rules are used in many NumPy operations, not just arithmetic operations, as we will
see below. For more details about broadcasting, check out the documentation
(https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html).
Upcasting
When trying to combine arrays with different dtype s, NumPy will upcast to a type capable of
handling all possible values (regardless of what the actual values are).
In [46]:
k1 = np.arange(0, 5, dtype=np.uint8)
print(k1.dtype, k1)
uint8 [0 1 2 3 4]
In [47]:
int16 [ 5 7 9 11 13]
Note that int16 is required to represent all possible int8 and uint8 values (from -128 to
255), even though in this case a uint8 would have sufficed.
In [48]:
k3 = k1 + 1.5
print(k3.dtype, k3)
Conditional operators
The conditional operators also apply elementwise:
In [49]:
Out[49]:
In [50]:
Out[50]:
In [51]:
Out[51]:
array([20, -5])
ndarray methods
Some functions are simply ndarray methods, for example:
In [52]:
[[ -2.5 3.1 7. ]
[ 10. 11. 12. ]]
mean = 6.76666666667
Note that this computes the mean of all elements in the ndarray , regardless of its shape.
min = -2.5
max = 12.0
sum = 40.6
prod = -71610.0
std = 5.08483584352
var = 25.8555555556
These functions accept an optional argument axis which lets you ask for the operation to be
performed on elements along the given axis. For example:
In [54]:
c=np.arange(24).reshape(2,3,4)
c
Out[54]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
In [55]:
Out[55]:
In [56]:
Out[56]:
In [57]:
Out[57]:
In [58]:
Out[58]:
Universal functions
NumPy also provides fast elementwise functions called universal functions, or ufunc. They are
vectorized wrappers of simple functions. For example square returns a new ndarray which is
a copy of the original ndarray except that each element is squared:
In [59]:
Out[59]:
In [60]:
print("Original ndarray")
print(a)
for func in (np.abs, np.sqrt, np.exp, np.log, np.sign, np.ceil, np.modf,
np.isnan, np.cos):
print("\n", func.__name__)
print(func(a))
Original ndarray
Original ndarray
[[ -2.5 3.1 7. ]
[ 10. 11. 12. ]]
absolute
[[ 2.5 3.1 7. ]
[ 10. 11. 12. ]]
sqrt
[[ nan 1.76068169 2.64575131]
[ 3.16227766 3.31662479 3.46410162]]
exp
[[ 8.20849986e-02 2.21979513e+01 1.09663316e+03]
[ 2.20264658e+04 5.98741417e+04 1.62754791e+05]]
log
[[ nan 1.13140211 1.94591015]
[ 2.30258509 2.39789527 2.48490665]]
sign
[[-1. 1. 1.]
[ 1. 1. 1.]]
ceil
[[ -2. 4. 7.]
[ 10. 11. 12.]]
modf
(array([[-0.5, 0.1, 0. ],
[ 0. , 0. , 0. ]]), array([[ -2., 3., 7.],
[ 10., 11., 12.]]))
isnan
[[False False False]
[False False False]]
cos
[[-0.80114362 -0.99913515 0.75390225]
[-0.83907153 0.0044257 0.84385396]]
-c:5: RuntimeWarning: invalid value encountered in sqrt
-c:5: RuntimeWarning: invalid value encountered in log
Binary ufuncs
There are also many binary ufuncs, that apply elementwise on two ndarray s. Broadcasting rules
are applied if the arrays do not have the same shape:
In [61]:
Out[61]:
array([ 3, 6, 2, 11])
In [62]:
Out[62]:
In [63]:
np.maximum(a, b)
Out[63]:
array([2, 8, 3, 7])
In [64]:
np.copysign(a, b)
Out[64]:
Array indexing
One-dimensional arrays
One-dimensional NumPy arrays can be accessed more or less like regular python arrays:
In [65]:
Out[65]:
19
In [66]:
a[2:5]
Out[66]:
In [67]:
a[2:-1]
Out[67]:
In [68]:
a[:2]
Out[68]:
array([1, 5])
In [69]:
a[2::2]
Out[69]:
In [70]:
a[::-1]
Out[70]:
In [71]:
a[3]=999
a
Out[71]:
In [72]:
Out[72]:
In [73]:
a[2:5] = -1
a
Out[73]:
try:
a[2:5] = [1,2,3,4,5,6] # too long
except ValueError as e:
print(e)
In [75]:
try:
del a[2:5]
except ValueError as e:
print(e)
Last but not least, ndarray slices are actually views on the same data buffer. This means that if
you create a slice and modify it, you are actually going to modify the original ndarray as well!
In [76]:
a_slice = a[2:6]
a_slice[1] = 1000
a # the original array was modified!
Out[76]:
In [77]:
a[3] = 2000
a_slice # similarly, modifying the original array modifies the slice!
Out[77]:
If you want a copy of the data, you need to use the copy method:
In [78]:
another_slice = a[2:6].copy()
another_slice[1] = 3000
a # the original array is untouched
Out[78]:
In [79]:
a[3] = 4000
another_slice # similary, modifying the original array does not affect t
he slice copy
Out[79]:
Multi-dimensional arrays
Multi-dimensional arrays can be accessed in a similar way by providing an index or slice for each
axis, separated by commas:
In [80]:
b = np.arange(48).reshape(4, 12)
b
Out[80]:
In [81]:
Out[81]:
14
In [82]:
Out[82]:
array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
In [83]:
Out[83]:
In [84]:
b[1, :]
Out[84]:
array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
In [85]:
b[1:2, :]
Out[85]:
array([[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
The first expression returns row 1 as a 1D array of shape (12,) , while the second returns that
same row as a 2D array of shape (1, 12) .
Fancy indexing
You may also specify a list of indices that you are interested in. This is referred to as fancy indexing.
In [86]:
Out[86]:
array([[ 2, 3, 4],
[26, 27, 28]])
In [87]:
b[:, (-1, 2, -1)] # all rows, columns -1 (last), 2 and -1 (again, and in
this order)
Out[87]:
array([[11, 2, 11],
[23, 14, 23],
[35, 26, 35],
[47, 38, 47]])
If you provide multiple index arrays, you get a 1D ndarray containing the values of the elements
at the specified coordinates.
In [88]:
b[(-1, 2, -1, 2), (5, 9, 1, 9)] # returns a 1D array with b[-1, 5], b[2,
9], b[-1, 1] and b[2, 9] (again)
Out[88]:
Higher dimensions
Everything works just as well with higher dimensional arrays, but it's useful to look at a few
examples:
In [89]:
c = b.reshape(4,2,6)
c
Out[89]:
array([[[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]],
In [90]:
Out[90]:
34
In [91]:
Out[91]:
array([27, 33])
If you omit coordinates for some axes, then all elements in these axes are returned:
In [92]:
Out[92]:
In [93]:
c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2
, :, :]
Out[93]:
In [94]:
Out[94]:
In [95]:
Out[95]:
array([27, 33])
In [96]:
Out[96]:
array([[ 3, 9],
[15, 21],
[27, 33],
[39, 45]])
Boolean indexing
You can also provide an ndarray of boolean values on one axis to specify the indices that you
want to access.
In [97]:
b = np.arange(48).reshape(4, 12)
b
Out[97]:
In [98]:
Out[98]:
In [99]:
Out[99]:
array([[ 1, 4, 7, 10],
[13, 16, 19, 22],
[25, 28, 31, 34],
[37, 40, 43, 46]])
np.ix_
You cannot use boolean indexing this way on multiple axes, but you can work around this by using
the ix_ function:
In [100]:
b[np.ix_(rows_on, cols_on)]
Out[100]:
array([[ 1, 4, 7, 10],
[25, 28, 31, 34]])
In [101]:
np.ix_(rows_on, cols_on)
Out[101]:
(array([[0],
[2]]), array([[ 1, 4, 7, 10]]))
If you use a boolean array that has the same shape as the ndarray , then you get in return a 1D
array containing all the values that have True at their coordinate. This is generally used along
with conditional operators:
In [102]:
b[b % 3 == 1]
Out[102]:
array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40,
43, 46])
Iterating
Iterating over ndarray s is very similar to iterating over regular python arrays. Note that iterating
over multidimensional arrays is done with respect to the first axis.
In [103]:
Out[103]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
In [104]:
for m in c:
print("Item:")
print(m)
Item:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Item:
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
In [105]:
Item:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Item:
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
If you want to iterate on all elements in the ndarray , simply iterate over the flat attribute:
In [106]:
for i in c.flat:
print("Item:", i)
Item: 0
Item: 1
Item: 2
Item: 3
Item: 4
Item: 5
Item: 6
Item: 7
Item: 8
Item: 9
Item: 10
Item: 11
Item: 12
Item: 13
Item: 14
Item: 15
Item: 16
Item: 17
Item: 18
Item: 19
Item: 20
Item: 21
Item: 22
Item: 23
Stacking arrays
It is often useful to stack together different arrays. NumPy offers several functions to do just that.
Let's start by creating a few arrays.
In [107]:
q1 = np.full((3,4), 1.0)
q1
Out[107]:
q2 = np.full((4,4), 2.0)
q2
Out[108]:
In [109]:
q3 = np.full((3,4), 3.0)
q3
Out[109]:
vstack
Now let's stack them vertically using vstack :
In [110]:
Out[110]:
q4.shape
Out[111]:
(10, 4)
This was possible because q1, q2 and q3 all have the same shape (except for the vertical axis, but
that's ok since we are stacking on that axis).
hstack
We can also stack arrays horizontally using hstack :
In [112]:
q5 = np.hstack((q1, q3))
q5
Out[112]:
In [113]:
q5.shape
Out[113]:
(3, 8)
This is possible because q1 and q3 both have 3 rows. But since q2 has 4 rows, it cannot be
stacked horizontally with q1 and q3:
In [114]:
try:
q5 = np.hstack((q1, q2, q3))
except ValueError as e:
print(e)
all the input array dimensions except for the concatenation axi
s must match exactly
concatenate
The concatenate function stacks arrays along any given existing axis.
In [115]:
Out[115]:
In [116]:
q7.shape
Out[116]:
(10, 4)
In [117]:
q8 = np.stack((q1, q3))
q8
Out[117]:
In [118]:
q8.shape
Out[118]:
(2, 3, 4)
Splitting arrays
Splitting is the opposite of stacking. For example, let's use the vsplit function to split a matrix
vertically.
r = np.arange(24).reshape(6,4)
r
Out[119]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
In [120]:
Out[120]:
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
In [121]:
r2
Out[121]:
In [122]:
r3
Out[122]:
There is also a split function which splits an array along any given axis. Calling vsplit is
equivalent to calling split with axis=0 . There is also an hsplit function, equivalent to
calling split with axis=1 :
In [123]:
r4, r5 = np.hsplit(r, 2)
r4
Out[123]:
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13],
[16, 17],
[20, 21]])
In [124]:
r5
Out[124]:
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15],
[18, 19],
[22, 23]])
Transposing arrays
The transpose method creates a new view on an ndarray 's data, with axes permuted in the
given order.
t = np.arange(24).reshape(4,2,3)
t
Out[125]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
Now let's create an ndarray such that the axes 0, 1, 2 (depth, height, width) are re-ordered
to 1, 2, 0 (depth→width, height→depth, width→height):
In [126]:
t1 = t.transpose((1,2,0))
t1
Out[126]:
[[ 3, 9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]]])
In [127]:
t1.shape
Out[127]:
(2, 3, 4)
Out[128]:
[[ 1, 7, 13, 19],
[ 4, 10, 16, 22]],
[[ 2, 8, 14, 20],
[ 5, 11, 17, 23]]])
In [129]:
t2.shape
Out[129]:
(3, 2, 4)
NumPy provides a convenience function swapaxes to swap two axes. For example, let's create
a new view of t with depth and height swapped:
In [130]:
Out[130]:
array([[[ 0, 1, 2],
[ 6, 7, 8],
[12, 13, 14],
[18, 19, 20]],
[[ 3, 4, 5],
[ 9, 10, 11],
[15, 16, 17],
[21, 22, 23]]])
In [131]:
t3.shape
Out[131]:
(2, 4, 3)
Linear algebra
NumPy 2D arrays can be used to represent matrices efficiently in python. We will just quickly go
through some of the main matrix operations available. For more details about Linear Algebra,
vectors and matrics, go through the Linear Algebra tutorial (math_linear_algebra.ipynb).
Matrix transpose
The T attribute is equivalent to calling transpose() when the rank is ≥2:
In [132]:
m1 = np.arange(10).reshape(2,5)
m1
Out[132]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [133]:
m1.T
Out[133]:
array([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
m2 = np.arange(5)
m2
Out[134]:
array([0, 1, 2, 3, 4])
In [135]:
m2.T
Out[135]:
array([0, 1, 2, 3, 4])
We can get the desired transposition by first reshaping the 1D array to a single-row matrix (2D):
In [136]:
m2r = m2.reshape(1,5)
m2r
Out[136]:
array([[0, 1, 2, 3, 4]])
In [137]:
m2r.T
Out[137]:
array([[0],
[1],
[2],
[3],
[4]])
Matrix multiplication
Let's create two matrices and execute a matrix multiplication
(https://en.wikipedia.org/wiki/Matrix_multiplication) using the dot() method.
In [138]:
n1 = np.arange(10).reshape(2, 5)
n1
Out[138]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [139]:
n2 = np.arange(15).reshape(5,3)
n2
Out[139]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
In [140]:
n1.dot(n2)
Out[140]:
m3 = np.array([[1,2,3],[5,7,11],[21,29,31]])
m3
Out[141]:
array([[ 1, 2, 3],
[ 5, 7, 11],
[21, 29, 31]])
In [142]:
linalg.inv(m3)
Out[142]:
In [143]:
linalg.pinv(m3)
Out[143]:
Identity matrix
The product of a matrix by its inverse returns the identiy matrix (with small floating point errors):
In [144]:
m3.dot(linalg.inv(m3))
Out[144]:
In [145]:
np.eye(3)
Out[145]:
QR decomposition
The qr function computes the QR decomposition
(https://en.wikipedia.org/wiki/QR_decomposition) of a matrix:
In [146]:
q, r = linalg.qr(m3)
q
Out[146]:
Out[147]:
In [148]:
Out[148]:
Determinant
The det function computes the matrix determinant (https://en.wikipedia.org/wiki/Determinant):
In [149]:
Out[149]:
43.999999999999972
In [150]:
Out[150]:
eigenvectors # v
Out[151]:
In [152]:
Out[152]:
In [153]:
Out[153]:
array([[1, 0, 0, 0, 2],
[0, 0, 3, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0]])
In [154]:
U, S_diag, V = linalg.svd(m4)
U
Out[154]:
In [155]:
S_diag
Out[155]:
array([ 3. , 2.23606798, 2. , 0. ])
The svd function just returns the values in the diagonal of Σ, but we want the full Σ matrix, so
let's create it:
In [156]:
S = np.zeros((4, 5))
S[np.diag_indices(4)] = S_diag
S # Σ
Out[156]:
array([[ 3. , 0. , 0. , 0. , 0.
],
[ 0. , 2.23606798, 0. , 0. , 0. ],
[ 0. , 0. , 2. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ]])
In [157]:
Out[157]:
array([[-0. , 0. , 1. , -0. , 0.
],
[ 0.4472136 , 0. , 0. , 0. , 0.89442719],
[-0. , 1. , 0. , -0. , 0. ],
[ 0. , 0. , 0. , 1. , 0. ],
[-0.89442719, 0. , 0. , 0. , 0.4472136 ]])
In [158]:
U.dot(S).dot(V) # U.Σ.V == m4
Out[158]:
Out[159]:
array([ 1, 7, 31])
In [160]:
Out[160]:
39
2x + 6y = 6
5x + 3y = −9
In [161]:
Out[161]:
array([-3., 2.])
In [162]:
Out[162]:
In [163]:
np.allclose(coeffs.dot(solution), depvars)
Out[163]:
True
Vectorization
Instead of executing operations on individual array items, one at a time, your code is much more
efficient if you try to stick to array operations. This is called vectorization. This way, you can benefit
from NumPy's many optimizations.
For example, let's say we want to generate a 768x1024 array based on the formula sin(xy/40.5) . A
bad option would be to do the math in python using nested loops:
In [164]:
import math
data = np.empty((768, 1024))
for y in range(768):
for x in range(1024):
data[y, x] = math.sin(x*y/40.5) # BAD! Very inefficient.
Sure, this works, but it's terribly inefficient since the loops are taking place in pure python. Let's
vectorize this algorithm. First, we will use NumPy's meshgrid function which generates
coordinate matrices from coordinate vectors.
In [165]:
Out[165]:
Out[166]:
As you can see, both X and Y are 768x1024 arrays, and all values in X correspond to the
horizontal coordinate, while all values in Y correspond to the the vertical coordinate.
In [167]:
data = np.sin(X*Y/40.5)
Now we can plot this data using matplotlib's imshow function (see the matplotlib tutorial
(tools_matplotlib.ipynb)).
In [168]:
In [169]:
a = np.random.rand(2,3)
a
Out[169]:
np.save("my_array", a)
Done! Since the file name contains no file extension was provided, NumPy automatically added
.npy . Let's take a peek at the file content:
In [171]:
content
Out[171]:
In [172]:
a_loaded = np.load("my_array.npy")
a_loaded
Out[172]:
Text format
Let's try saving the array in text format:
In [173]:
np.savetxt("my_array.csv", a)
This is a CSV file with tabs as delimiters. You can set a different delimiter:
In [175]:
np.savetxt("my_array.csv", a, delimiter=",")
In [176]:
Out[176]:
b = np.arange(24, dtype=np.uint8).reshape(2, 3, 4)
b
Out[177]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
In [178]:
Again, let's take a peek at the file content. Note that the .npz file extension was automatically
added.
In [179]:
repr(content)[:180] + "[...]"
Out[179]:
u'"PK\\x03\\x04\\x14\\x00\\x00\\x00\\x00\\x00x\\x94cH\\xb6\\x96
\\xe4{h\\x00\\x00\\x00h\\x00\\x00\\x00\\x08\\x00\\x00\\x00my_b.
npy\\x93NUMPY\\x01\\x00F\\x00{\'descr\': \'|u1\', \'fortran_ord
er\': False, \'shape\': (2,[...]'
In [180]:
my_arrays = np.load("my_arrays.npz")
my_arrays
Out[180]:
<numpy.lib.npyio.NpzFile at 0x10fa4d4d0>
This is a dict-like object which loads the arrays lazily:
In [181]:
my_arrays.keys()
Out[181]:
['my_b', 'my_a']
In [182]:
my_arrays["my_a"]
Out[182]:
What next?
Now you know all the fundamentals of NumPy, but there are many more options available. The best
way to learn more is to experiment with NumPy, and go through the excellent reference
documentation (http://docs.scipy.org/doc/numpy/reference/index.html) to find more functions and
features you may be interested in.