Python 7 & 8 - Numpy
Python 7 & 8 - Numpy
Numpy
... it’s that library you always load, without even thinking
about it.
Intr
o
Structur
e
>>>
[1 ,2 ,3 ,4 ,5]
Topic 2: Indexing
Reminder of basic python
indexes
>>>
[1 ,2 ,3 ,4 ,5]
1 print( my_list [2])
Topic 2:
Reminder
Indexing of basic python
indexes
>>>
[1 ,2 ,3 ,4 ,5]
1 print( my_list [2])
>>>
3
Topic 2:
Indexing
Reminder of basic python
indexes
What is
this?
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list [2:4])
Topic 2:
Indexing
Reminder of basic python
indexes
What is
this?
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list [2:4])
>>> [3 ,4]
... it’s a
slice!
Topic 2:
Indexing
Reminder of basic python
indexes
1 my_list = [1 ,2 ,3 ,4 ,5]
>>>
[3 ,4]
Topic 2:
Indexing
Reminder of basic python
indexes
1 my_list = [1 ,2 ,3 ,4 ,5]
>>> [1 , 2, 3, 4, 5] at 0 x127885500
>>> [1 , 2, 9, 8, 5] at 0 x127885500
Topic 1: Numpy Arrays
actually, what is a
list?
IAI, UET
17/48
Topic 1: Numpy Arrays
actually, what is a
list?
IAI, UET
18/48
Topic 1: Numpy Arrays
actually, what is a
list?
IAI, UET
19/48
Topic 1: Numpy
Arrays
This is an Array!
IAI, UET
20/48
Topic 1: Numpy
Arrays
so what’s the
difference?
Python Numpy Array
List
Dimensions 1 n
Elements can be different types must be homogeneous
Memory Stores addresses at which One contiguous block of
the ele- ment data is memory
Layout
located
Memory direct data
Access address, then
Access Size access Not
access data Expandable
Content expandable
Mutable
s Good Mutable
variable sequences
for... large amounts of data; data
IAI, UET
21/48
> Exercise 2
1. Make a random 10x10 numpy array. Print the element in the fourth
column and the fifth row.
2. Print the entire fifth row.
IAI, UET
22/48
Topic 2: Indexing
12/49
Topic 2: Indexing
Ways to index a numpy
array
IAI, UET
24/48
Topic 2:
Indexing
Ways to index a numpy
array
IAI, UET
25/48
Topic 2:
Indexing
Ways to index a numpy
array
Numpy arrays are compatible with lists of lists in pure python and they can
be accessed like this:
1 # 3- dimensional array
2 a = zeros(3 ,3 ,3)
3 z = x = y = 1
4 a[z][ x][ y]
14/49
Topic 2:
Indexing
Regular
Indexing
The more common usage is with just one set of square brackets:
Dimensions are represented by commas. To index explicitly, you need
as many values separated by commas as dimensions.
1 a = np. random. randint(1 , 10 , (5 ,5))
2 row = 2
3 col = 3
4 a[row , col]
>>>
2
15/49
Topic 2: Indexing
Common pitfall: don’t think of the index as coordinates (x and y): They
are interpreted in the reverse order!
1 a = np. random. 10 , (2 , 3, 4))
2 randint(1 , print(a)
1 >>> x = y = z = 1
2 >>> print(a[z, y, x])
>>>
4 16/49
Topic 2:
Indexing
Numpy Slice
17/49
Topic 2:
Indexing
Slicing
Shorthands
There are some convenient shorthands/default expansions like:
► If you want all indexes from a dimension use ”:”
► Ellipses [...] can be used to replace zero or more ”:” terms
► If you give fewer comma-separated values than dimensions, it assumes
you provided the first dimension’s indexes.
1 # 3- dimensional array
2 a = zeros(3 ,3 ,3)
3 # access the second element of the first dimension
4 a[1 , :, :] # explicit
5 a[1 , ...] # equivalent
6 a[1] # equivalent
18/49
Topic 2:
Indexing
What Dimensionality is my
output?
>>>
(3 ,3)
> Dimension Guessing
see file
Exercises_Notebook_1.py
Topic 2: Indexing
What Dimensionality is my
output?
>>>
(3 ,)
20/49
Topic 2:
Indexing
What Dimensionality is my
output?
>>>
(2 ,)
Topic 2: Indexing
Fancy
Indexing
When indexing with multiple lists, they are interpreted as
coordinates to look up.
... actually thats just a useful lie to believe for now. Let’s talk about it later!
1 a = np. array( list(" ABCDEFGHIJKLMNOPQRSTUVWX")).
2 reshape(4 ,6) print(a)
23/49
TOPIC 3: at the heart of
Numpy
Fancy Indexing
Topic 2:
IndexingIndexing with
Fancy
Bools
If you pass an array of booleans, Numpy will return the values at the
True positions.
1 a = np. random. randint(1 , 10 , (3 ,3))
2 print(a)
>>> [9 , 2, 7],
array([
>>> [2 , 6, 9],
>>> [1 , 7, 3]])
1 print(a[a<5])
see file
Exercises_Notebook_2.py
TOPIC 3: at the heart of
Numpy and
Views
Copies
26/49
TOPIC 3: at the heart of Numpy
Views and Copies
>>> [3 4
5]
TOPIC 3: at the heart of Numpy
Views and Copies
>>> [3 4
5]
1 v[1] = 999
2 print(v)
TOPIC 3: at the heart of Numpy
Views and Copies
>>> [3 4
5]
1 v[1] = 999
2 print(v)
>>> [3 999
5]
TOPIC 3: at the heart of
Numpy
Views and Copies
1 a = np. array([1 ,2 ,3 ,4 ,5 ,6])
2 v = a[2:5]
3 print(v)
>>> [3 4
5]
1 v[1] = 999
2 print(v)
>>> [3 999
5]
1 print(a)
TOPIC 3: at the heart of
Numpy
Views and Copies
1 a = np. array([1 ,2 ,3 ,4 ,5 ,6])
2 v = a[2:5]
3 print(v)
>>> [3 4
5]
1 v[1] = 999
2 print(v)
>>> [3 999
5]
1 print(a)
>>> [1 2 3 999 5 6]
27/49
TOPIC 3: at the heart of
Numpy
Views and Copies
1 a = np. zeros([4 ,4])
2 print(a)
29/49
TOPIC 3: at the heart of Numpy
ndarray
>>> (3 , 3)
>>> dtype(' int64
')
30/49
TOPIC 3: at the heart of
Numpy
ndarray
properties
31/49
TOPIC 3: at the heart of
Numpy
ndarray properties
32/49
TOPIC 3: at the heart of
Numpy
ndarray properties
33/49
TOPIC 3: at the heart of
Numpy
ndarray properties
34/49
TOPIC 3: at the heart of
Numpy
Question
Stride operations
What kind of operations are strides useful for?
35/49
TOPIC 3: at the heart of
Numpy
ndarray properties
36/49
TOPIC 3: at the heart of
Numpy
Broadcastin
g... allows ufuncs to deal with inputs that do not have exactly the
same shape.
— plots: https://github.com/rougier/numpy-
tutorial
TOPIC 3: at the heart of
Numpy
Broadcastin
g... allows ufuncs to deal with inputs that do not have exactly the
same shape.
— plots: https://github.com/rougier/numpy-
tutorial
TOPIC 3: at the heart of
Numpy
Broadcastin
g... allows ufuncs to deal with inputs that do not have exactly the
same shape.
— plots: https://github.com/rougier/numpy-
tutorial
TOPIC 3: at the heart of
Numpy
Broadcastin
g... allows ufuncs to deal with inputs that do not have exactly the
same shape.
— plots: https://github.com/rougier/numpy-
tutorial
TOPIC 3: at the heart of
Numpy
Broadcastin
g
Broadcasting can be understood by four rules:
1. All input arrays with ndim smaller than the input array of largest
ndim have 1’s pre-pended to their shapes.
2. The size in each dimension of the output shape is the maximum of all the
input shapes in that dimension.
3. An input can be used in the calculation if it’s shape in a particular
dimension either matches the output shape or has value
exactly 1.
4. If an input has a dimension size of 1 in its shape, the first data entry in
that dimension will be used for all calculations along that
dimension.
>>> (3 ,)
>>> (3 ,
1)
1 np. broadcast_arrays(a, b)
The .npy format is the standard binary file format in NumPy for persisting a
single arbitrary NumPy array on disk.
40/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors
41/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
divide
1 a
>>> inf
42/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
over
43/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
under
>>> 0.0
Default consequence:
Ignore
44/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
invalid
Invalid operation: result is not an expressible number, typically
indicates that a NaN was produced.
1 a = np. arange(5)
2 b = np. array([4 , 3, 2, np.NaN 9])
3 b ,
1 a<=b
46/49
TOPIC 4: Tricks, Conventions, and
Applications
Reshaping
47/49
TOPIC 4: Tricks, Conventions, and
Applications
Reshapin
g