Python Numpy Primer
Python Numpy Primer
IC-CVLab
2.3.2021
1
Python
2
About Python
• High-level, focus on readable and concise code - fast prototyping.
3
Working with Python
Installation
• Install the interpreter and packages manually
• OSX and some Linux distributions already include Python
• pip - package installer
Running
• Terminal (interactive or script mode)
$ python
>>> print('hello CS322')
'hello CS322'
>>> # Use Ctrl-D, Ctrl-Z or quit() to exit
$ python my_script.py
'hello 'CS322'
• Jupyter Notebook
4
Data Types
• Python is dynamically typed language.
• Data type inferred at runtime.
• Can change during runtime.
>>> a = 1
>>> print(type(a))
<class 'int'>
>>> a = 42 int
>>> a = 3.142 float 64 bit
>>> a = 3. float 64 bit
>>> a = 2+3j Complex
>>> a = True bool
>>> a = None None
5
Data Types
• Strings can be single, double or triple quoted.
6
ADT(Abstract Data Type) - list
• Contains a series of values.
>>> a = [] # empty
>>> b = [1, 2, 3, 4] # 4 elements
>>> c = [1, 'cat', 0.23] # mixed types
>>> d = [1, ['cat', 'dog'], 2, 3] # list in list
7
ADT - list
• Length of a list.
>>> a = ['great','minds','think','alike']
>>> print(len(a)) #length of list
4
8
ADT - tuple
• Similar as list but fixed in size and immutable = change not allowed.
• Declaration with parentheses.
• Tuple unpacking
>>> a, b = (1, 2)
>>> print(b)
2
9
ADT - dictionary
• Collection of key:value pairs
Access value with keys
Key must be immutable and unique
10
Type Casting
>>> a = 42 # int -> float
>>> a = float(a)
>>> print(a, type(a))
42.0 <class 'float'>
11
Type Casting
>>> a = [42, 21.2, 'black'] #list -> tuple
>>> a = tuple(a)
>>> print(a)
(42, 21.2, 'black')
12
Functions
>>> def print_hello():
print("hello world”)
13
Builtin Functions
• Python comes with various useful builtin functions.
• range, zip, type, str, int, sorted, len, sum, max, min, abs, any, all, …
>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> type('hello')
str
>>> sorted((2, 1, 3))
[1, 2, 3]
>>> sorted('eba')
['a', 'b', 'e']
14
Builtin Functions
>>>len('abc')
3
>>>sum([1, 2, 3])
6
>>>max([1, 2, 3])
3
>>> min([1, 2, 3])
1
>>> abs(-2)
2
>>> a = [False, False, True]
>>> any(a)
True
>>> b = [False, False, False]
>>> any(b)
False
>>> c = [True, True, True]
>>> all(c)
True
15
List Comprehensions
• List comprehensions provide a concise way to create lists.
{2x | x ∈ {0...10}}
>>> powers2 = [2**x for x in range(11)]
>>> print(powers2)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
16
Import
• Gains access to code in another module by importing it.
17
Getting help
• In Jupiter Notebook - function name with “?”.
>>> sum?
Signature: sum(iterable, start=0, /)
Docstring:
Return the sum of a 'start' value (default: 0)
plus an iterable of numbers
18
NumPy
19
About NumPy
• Core library for scientific computing in Python.
NumPy Arrays
• high-performance multidimensional arrays.
20
Creating Arrays I
# import numpy
>>> import numpy as np
21
Creating Arrays II
>>> np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.ones((2,3))
array([[1., 1., 1.],
[1., 1., 1.]])
>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
22
Creating Arrays III
>>> np.linspace(1, 5, 9)
array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ,4.5, 5. ])
23
Inspecting Arrays
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6]])
>>> x.shape
(2, 3)
>>> x.ndim
2
>>> x.size
6
>>> x.dtype
dtype(‘int64')
24
Indexing
• For a rank 2 array, the first index refers to the row & the second
to the column.
25
Indexing multidimensional arrays
>>> m[0]
array([0, 1, 2])
>>> m[1]
array([3, 4, 5])
26
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly
example: A[0, 2]
• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension
>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]
27
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly
example: A[0, 2]
example: A[:, 1]
• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension
>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]
28
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly
example: A[0, 2]
example: A[:, 1]
example: A[1, :]
• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension
>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]
29
Indexing multidimensional arrays
Structural Indexing
• Syntactic sugar used to add an extra dimension to an existing array.
>>> x = np.array([1, 2, 3])
x.shape
(3,)
>>> x[np.newaxis].shape
(1, 3)
30
Indexing multidimensional arrays
Structural Indexing
• Useful for broadcasting.
• Explicit shape, e.g. row/column vectors.
inner product
outer product
(dot product)
31
Indexing multidimensional arrays
Structural Indexing
>>> x2 = x2[np.newaxis]
>>> x2.shape
(1, 3)
>>> x1 @ x2
array([[ 2, 4, 6],
[ 4, 8, 12],
[ 6, 12, 18]])
32
Indexing multidimensional arrays
Structural Indexing
>>> x1 = x1[np.newaxis]
>>> x1.shape
(1, 3)
>>> x1 @ x2
array([[28]])
33
Slicing: Basics
• Slicing is indexing of the form [start : stop : step]
• start including
• stop excluding
>>> x = np.arange(1, 11)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> x[1:10:2]
array([ 2, 4, 6, 8, 10])
>>> x[9:0:-1]
array([10, 9, 8, 7, 6, 5, 4, 3, 2])
>>> x[::-1]
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
34
Slicing: Basics
• Slicing provides a view of the original array.
>>> y = x[0:2]
>>> y[:] = 42
>>> x
array([42, 42, 3, 4, 5, 6, 7, 8, 9, 10])
35
Slicing: Example
syntax: [start : stop : step]
36
Masking: Basics
• Masking is indexing an array with an identically shaped Boolean array.
>>> x = np.arange(1, 6)
>>> x
array([1, 2, 3, 4, 5])
37
Masking: Example
• Get all even numbers in [1, 6] which are multiplies of 3.
>>> m = np.arange(1, 7)
array([1, 2, 3, 4, 5, 6])
>>> div2 = (m % 2 == 0)
array([False, True, False, True, False, True])
>>> div3 = (m % 3 == 0)
array([False, False, True, False, False, True])
>>> m[mask]
array([6])
38
Type casting
>>> m = np.arange(0, 5)
array([ 0, 1, 2, 3, 4])
>>> m.astype(np.float32)
array([ 0., 1., 2., 3., 4.])
>>> m.astype(np.bool)
array([False, True, True, True])
>>> m.dtype
dtype('bool')
39
Broadcasting
40
How broadcast works
• Smaller array is “broadcast” across the larger array so that they have
compatible shapes
41
Broadcasting: Basic Example
• Add scalar to every element of a vector.
42
Broadcasting: Advanced Example
• Convert binary numbers (in rows) to decimal
43
Broadcasting Rules
• The corresponding dimensions of 2 arrays must satisfy one of the following:
• Same dimension
• One of the dimensions is 1
• Non-existent dimensions are treated as 1
A (2d array): 5 x 4 A (3d array): 15 x 3 x 5
B (1d array): 1 B (3d array): 15 x 1 x 5
Result (2d array): 5 x 4 Result (3d array): 15 x 3 x 5
44
Common Functions in Numpy
1 2 3
a=
<latexit sha1_base64="oUHV7uuCKEIPOD6evmzUyUZzaEY=">AAACG3icbVDLSgMxFM34rPVVdekmWBRXZaatr4VQcOOygn1AZyiZzG0bmskMSUYsQ//Djb/ixoUirgQX/o3pA9HWC4cczrmX3Hv8mDOlbfvLWlhcWl5Zzaxl1zc2t7ZzO7t1FSWSQo1GPJJNnyjgTEBNM82hGUsgoc+h4fevRn7jDqRikbjVgxi8kHQF6zBKtJHauSK5dH3oMpH6IdGS3Q8dfISLBiXXLZvnxODUBRH8NLRzebtgjwvPE2dK8mha1Xbuww0imoQgNOVEqZZjx9pLidSMchhm3URBTGifdKFlqCAhKC8d3zbEh0YJcCeSBkLjsfp7IiWhUoPQN51mv56a9Ubif14r0Z1zL2UiTjQIOvmok3CsIzwKCgdMAtV8YAihkpldMe0RSag2cWZNCM7syfOkXiw4pYJ9U85XLqZxZNA+OkDHyEFnqIKuURXVEEUP6Am9oFfr0Xq23qz3SeuCNZ3ZQ3/K+vwGfAOejQ==</latexit>
4 5 6
45
Common Functions in Numpy
• Sum along the columns:
>>> sum_along_cols = np.sum(a, axis=1)
>>> print(sum_along_cols)
[ 6 15 ]
1 2 3 6
<latexit sha1_base64="5lnxL8zySiaH1KTLC07IsuJUu4M=">AAACGXicbVDLSgMxFM3UV62vqks3waK4KjNtfe0KblxWsA/oDCWTuW1DM5khyYhl6G+48VfcuFDEpa78G9MHoq0XDjmccy+59/gxZ0rb9peVWVpeWV3Lruc2Nre2d/K7ew0VJZJCnUY8ki2fKOBMQF0zzaEVSyChz6HpD67GfvMOpGKRuNXDGLyQ9ATrMkq0kTp52/Whx0Tqh0RLdj9y8DEuGZRdt2KeU4MzF0Tw09DJF+yiPSm8SJwZKaBZ1Tr5DzeIaBKC0JQTpdqOHWsvJVIzymGUcxMFMaED0oO2oYKEoLx0ctkIHxklwN1IGgiNJ+rviZSESg1D33Sa/fpq3huL/3ntRHcvvJSJONEg6PSjbsKxjvA4JhwwCVTzoSGESmZ2xbRPJKHahJkzITjzJy+SRqnolIv2TaVQvZzFkUUH6BCdIAedoyq6RjVURxQ9oCf0gl6tR+vZerPep60Zazazj/6U9fkNI7ed2w==</latexit>
4 5 6 <latexit sha1_base64="bvlLQhDy5WrfSD62omUTYZFOrT8=">AAACDHicbVDLSsNAFJ3UV62vqks3g0VwVRLf7gpuXFawD2hCmUxu2qGTSZiZiCX0A9z4K25cKOLWD3Dn3zhtg2jrgYHDOecy9x4/4Uxp2/6yCguLS8srxdXS2vrG5lZ5e6ep4lRSaNCYx7LtEwWcCWhopjm0Ewkk8jm0/MHV2G/dgVQsFrd6mIAXkZ5gIaNEG6lbrrg+9JjI/Ihoye5HZ9h1sXPqggh+NJOyq/YEeJ44OamgHPVu+dMNYppGIDTlRKmOYyfay4jUjHIYldxUQULogPSgY6ggESgvmxwzwgdGCXAYS/OExhP190RGIqWGkW+SZr++mvXG4n9eJ9XhhZcxkaQaBJ1+FKYc6xiPm8EBk0A1HxpCqGRmV0z7RBKqTX8lU4Ize/I8aR5VneOqfXNSqV3mdRTRHtpHh8hB56iGrlEdNRBFD+gJvaBX69F6tt6s92m0YOUzu+gPrI9vxUCbaA==</latexit>
15
• Sum along the rows:
>>> sum_along_rows = np.sum(a, axis=0)
>>> print(sum_along_rows)
[ 5 7 9 ]
1 2 3
4 5 6
⇥ ⇤
<latexit sha1_base64="5lnxL8zySiaH1KTLC07IsuJUu4M=">AAACGXicbVDLSgMxFM3UV62vqks3waK4KjNtfe0KblxWsA/oDCWTuW1DM5khyYhl6G+48VfcuFDEpa78G9MHoq0XDjmccy+59/gxZ0rb9peVWVpeWV3Lruc2Nre2d/K7ew0VJZJCnUY8ki2fKOBMQF0zzaEVSyChz6HpD67GfvMOpGKRuNXDGLyQ9ATrMkq0kTp52/Whx0Tqh0RLdj9y8DEuGZRdt2KeU4MzF0Tw09DJF+yiPSm8SJwZKaBZ1Tr5DzeIaBKC0JQTpdqOHWsvJVIzymGUcxMFMaED0oO2oYKEoLx0ctkIHxklwN1IGgiNJ+rviZSESg1D33Sa/fpq3huL/3ntRHcvvJSJONEg6PSjbsKxjvA4JhwwCVTzoSGESmZ2xbRPJKHahJkzITjzJy+SRqnolIv2TaVQvZzFkUUH6BCdIAedoyq6RjVURxQ9oCf0gl6tR+vZerPep60Zazazj/6U9fkNI7ed2w==</latexit>
<latexit sha1_base64="wpkfcu6511qhvhdRK5sII0cc6rc=">AAACDnicbVDLSsNAFJ34rPUVdelmsFRclcQHtbuCG5cV7APaUCaT23boZBJmJmIJ/QI3/oobF4q4de3Ov3HaBtHWAxcO59zLvff4MWdKO86XtbS8srq2ntvIb25t7+zae/sNFSWSQp1GPJItnyjgTEBdM82hFUsgoc+h6Q+vJn7zDqRikbjVoxi8kPQF6zFKtJG6drHjQ5+J1A+Jlux+fIGPcdlUpQMi+FG7dsEpOVPgReJmpIAy1Lr2ZyeIaBKC0JQTpdquE2svJVIzymGc7yQKYkKHpA9tQwUJQXnp9J0xLholwL1ImhIaT9XfEykJlRqFvuk09w3UvDcR//Paie5deikTcaJB0NmiXsKxjvAkGxwwCVTzkSGESmZuxXRAJKHaJJg3IbjzLy+SxmnJPSs5N+eFaiWLI4cO0RE6QS4qoyq6RjVURxQ9oCf0gl6tR+vZerPeZ61LVjZzgP7A+vgG5nibWQ==</latexit>
5 7 9
46
Common Functions in Numpy
1 2 3
a=
4 5 6
<latexit sha1_base64="oUHV7uuCKEIPOD6evmzUyUZzaEY=">AAACG3icbVDLSgMxFM34rPVVdekmWBRXZaatr4VQcOOygn1AZyiZzG0bmskMSUYsQ//Djb/ixoUirgQX/o3pA9HWC4cczrmX3Hv8mDOlbfvLWlhcWl5Zzaxl1zc2t7ZzO7t1FSWSQo1GPJJNnyjgTEBNM82hGUsgoc+h4fevRn7jDqRikbjVgxi8kHQF6zBKtJHauSK5dH3oMpH6IdGS3Q8dfISLBiXXLZvnxODUBRH8NLRzebtgjwvPE2dK8mha1Xbuww0imoQgNOVEqZZjx9pLidSMchhm3URBTGifdKFlqCAhKC8d3zbEh0YJcCeSBkLjsfp7IiWhUoPQN51mv56a9Ubif14r0Z1zL2UiTjQIOvmok3CsIzwKCgdMAtV8YAihkpldMe0RSag2cWZNCM7syfOkXiw4pYJ9U85XLqZxZNA+OkDHyEFnqIKuURXVEEUP6Am9oFfr0Xq23qz3SeuCNZ3ZQ3/K+vwGfAOejQ==</latexit>
• Along cols:
>>> col_min = np.min(a, axis=1)
>>> print(col_min)
[ 1 4 ]
• Along rows:
>>> row_min = np.min(a, axis=0)
>>> print(row_min)
[ 1 2 3 ]
• Along columns:
>>> col_mean = np.mean(a, axis=1)
>>> print(col_mean)
[2. 5.]
You can use np.std similarly to find the standard deviation and
np.var to find the variance
49
Random
>>> x = np.random.random()
>>> print(x)
0.22402776143655379
50
Random
• How to fix the pseudo-random generator?
>>> np.random.random()
0.4353
>>> np.random.random()
0.4204
• Use seed.
>>> np.random.seed(2)
>>> np.random.random()
0.4360
>>> np.random.seed(2)
>>> np.random.random()
0.4360
51
Random: Useful Functions
• Getting random integer n such that a <= n <= b:
>>> minim = 3
>>> maxim = 10
>>> x = (maxim - minim) * random.random() + minim
print(x)
3.1815
52
Random: Useful Stuff
• How to shuffle (in-place) randomly list of indices?
>>> x = np.arange(10)
>>> print(x)
[0 1 2 3 4 5 6 7 8 9]
>>> np.random.shuffle(x)
>>> print(x)
[9 5 4 7 1 8 0 3 2 6]
53
References
• Python Documentation:
https://docs.python.org/3/
• NumPy Basics:
https://docs.scipy.org/doc/numpy/user/basics.html
54