In [1]:
name = '2017-09-25-numpy-intro'
title = 'Intro to NumPy'
tags = 'numpy, basics'
author = 'Denis Sergeev'
In [2]:
from nb_tools import connect_notebook_to_post
from IPython.core.display import HTML
html = connect_notebook_to_post(name, title, tags, author)
Basic data containers in Python include
In [3]:
my_collection = [1, 4, 6, 10]
In [4]:
my_collection.append(100000)
In [5]:
my_collection.remove(1)
In [6]:
my_collection[1] = 'abcdef'
In [7]:
my_collection
Out[7]:
In [8]:
a = [10, 20, 30, 40, 50, 60, 70]
In [9]:
low, high = 2, 4
In [10]:
a[:low]
Out[10]:
In [11]:
a[low:high]
Out[11]:
In [12]:
a[high:]
Out[12]:
Works with any index-supporting objects, including strings:
In [13]:
s = 'qwerty'
In [14]:
s[1:-1]
Out[14]:
Given a 2D image, img, stored in row-major order, we want to find the linear position in the array of the element at
position (x, y). Using zero-based indexing, that linear position is simply img[y * width + x], whereas with one-based indexing it is img((y - 1) * width + x). Now there is a -1 in there!
Lists in Python are quite general, can have arbitrary objects as elements.
Addition and scalar multiplication are defined for lists, but not what we want for numerical computation, e.g.
Addition results in concatenation
In [15]:
x = [1, 2, 3]
y = [10, 20, 30]
x + y
Out[15]:
And multiplication results in repeating:
In [16]:
x = [2, 3]
x * 3
Out[16]:
In [17]:
import numpy as np
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank.
Why it is useful: Memory-efficient container that provides fast numerical operations.
In [18]:
l = list(range(1000))
In [19]:
%timeit [i**2 for i in l]
In [20]:
a = np.arange(1000)
In [21]:
%timeit a**2
In [22]:
a = np.array([3, 4, 5, 6])
In [23]:
a
Out[23]:
The class is called
In [24]:
type(a)
Out[24]:
In [25]:
a.ndim
Out[25]:
Scalar array
In [26]:
a0 = np.array(7)
In [27]:
a0.ndim
Out[27]:
In [28]:
b = np.array([[10, 20, 30], [9, 8, 7]])
In [29]:
b
Out[29]:
In [30]:
c = np.array([[[1], [2]], [[3], [4]]])
In [31]:
c.shape
Out[31]:
In [32]:
c.max()
Out[32]:
Equivalent to size(c) in MATLAB.
In [33]:
try:
a = np.array(1,2,3,4) # WRONG, throws ValueError
except ValueError as e:
print(e)
In [34]:
a = [1,2,3,4]
In [35]:
a = np.array(a) # RIGHT
In [36]:
b = a.copy()
Do not use np.ndarray function to create an array
In [37]:
np.arange(1, 9, 2) # start, end (exclusive), step
Out[37]:
In [38]:
np.linspace(0, 1, 6) # start, end, num-points
Out[38]:
In [39]:
np.logspace(-3,2,7)
Out[39]:
In [40]:
np.zeros((2, 3))
Out[40]:
In [41]:
np.ones((3, 2))
Out[41]:
In [42]:
np.empty([2,3])
Out[42]:
The function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.
In [43]:
np.random.seed(1234)
In [44]:
np.random.rand(4) # uniform in [0, 1]
Out[44]:
In [45]:
np.random.randn(4) # Gaussian
Out[45]:
In [46]:
np.eye(3)
Out[46]:
In [47]:
np.diag(np.array([1, 2, 3, 4]))
Out[47]:
In [48]:
a = np.array([1, 2, 3, np.nan])
In [49]:
b = list(a)
In [50]:
a = np.array([1, 2, 3])
In [51]:
b = np.ma.masked_less(a, 2)
In [52]:
b
Out[52]:
In [53]:
b.data
Out[53]:
In [54]:
b.mask
Out[54]:
We will have a separate session on Masked Arrays in NumPy.
In [55]:
np.rollaxis??
In [56]:
np.*space*?
In [57]:
np.lookfor('create array')
In [58]:
HTML(html)
Out[58]: