0% found this document useful (0 votes)
7 views

Python 7 & 8 - Numpy

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python 7 & 8 - Numpy

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 82

Intro

Numpy

... it’s that library you always load, without even thinking
about it.
Intr
o
Structur
e

1. Numpy Array Warmup Next Lecture


2. Indexing 3. At the heart of Numpy
- Python lists - Broadcasting
-Numpy indexes, slices, 4. Tricks, Conventions, and
and some fancy-ness Applications
3. At the heart of Numpy - Saving stuff
- Underneath the ndarray - Errors
- Views and copies - Reshaping
Topic 1: Numpy
Arrays
Making them
(1D)
Topic 1: Numpy
Arrays
Making them
(2D)
Topic 1: Numpy
Arrays
Making Grids
> Exercise 1

1. Make a random 10x10 numpy array and visualize it.


2. Simulate some roulette rounds in Python and using Numpy. Test
which is faster.
3. Use print(hex(id(mylist))) to look at tuples and lists and when you
change them.

see file Exercises_Notebook_1.py


Topic 2: Indexing
Reminder of basic python
indexes

Lists can be indexed with a single


number:
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list)
Topic 2: Indexing
Reminder of basic python
indexes

Lists can be indexed with a single


number:
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list)

>>>
[1 ,2 ,3 ,4 ,5]
Topic 2: Indexing
Reminder of basic python
indexes

Lists can be indexed with a single


number:
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list)

>>>
[1 ,2 ,3 ,4 ,5]
1 print( my_list [2])
Topic 2:
Reminder
Indexing of basic python
indexes

Lists can be indexed with a single


number:
1 my_list = [1 ,2 ,3 ,4 ,5]
2 print( my_list)

>>>
[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]

Indexing python lists also works using a slice object:


► it is made using the slice() function OR triggered when there is a ”:”
inside ”[ ]”
► sliced lists return a part of the original list
► can be used to modify in-place
1 print( my_list [2:4])
2 print( my_list [::2])
3 print( my_list[ slice(
0 ,4 ,2)]) #
equivalent
>>> [3 ,4]
>>>
[1 ,3 ,5]
>>>
[1 ,3 ,5]
Topic 2:
Indexing
Reminder of basic python
indexes
1 my_list = [1 ,2 ,3 ,4 ,5]

Indexing python lists also works using a slice object:


► it is made using the slice() function OR triggered when there is a ”:”
inside ”[ ]”
► sliced lists return a part of the original list
► can be used to modify in-place

1 print( my_list [2:4])

>>>
[3 ,4]
Topic 2:
Indexing
Reminder of basic python
indexes
1 my_list = [1 ,2 ,3 ,4 ,5]

Indexing python lists also works using a slice object:


► it is made using the slice() function OR triggered when there is a ”:”
inside ”[ ]”
► sliced lists return a part of the original list
► can be used to modify in-place
1 print( my_list , " at", hex( id( my_list)))
2 my_list [2:4] = [9 ,8]
3 print( my_list , " at", hex( id( my_list)))

>>> [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.

see file Exercises_Notebook_1.py

IAI, UET
22/48
Topic 2: Indexing

Numpy arrays can have multiple


dimensions
1 a = np. random. randint(1 , 10 , (5 ,5))
2 print(a)

>>> array([[8 , 2, 3, 1, 7],


>> [2 , 8, 1, 8, 2],
> [5 , 6, 3, 2, 4],
>> [5 , 2, 8, 2, 2],
> [6 , 9, 5, 5, 9]])
>>
>
>>
>

12/49
Topic 2: Indexing
Ways to index a numpy
array

1. field access indexing


‘a[1][2][3]‘
2. regular indexing ‘a[1,2,3]‘
3. slicing ‘a[1:3, 1:3]‘
4. fancy indexing ‘a[[1,2],
[1,2]]‘
if you want more: Check out the indexing.ipynb. It shows off all the different
kinds of indexing and combinations!

IAI, UET
24/48
Topic 2:
Indexing
Ways to index a numpy
array

1. field access indexing ‘a[1]


[2][3]‘
2. regular indexing ‘a[1,2,3]‘
3. slicing ‘a[1:3, 1:3]‘
4. fancy indexing ‘a[[1,2],
[1,2]]‘
if you want more: Check out the indexing.ipynb. It shows off all the different
kinds of indexing and combinations!

IAI, UET
25/48
Topic 2:
Indexing
Ways to index a numpy
array

1. field access indexing ‘a[1]


[2][3]‘
2. regular indexing ‘a[1,2,3]‘
3. slicing ‘a[1:3, 1:3]‘
4. fancy indexing ‘a[[1,2],
[1,2]]‘
if you want more: Check out the indexing.ipynb. It shows off all the different
kinds of indexing and combinations!
Topic 2:
Indexing
Ways to index a numpy
array

1. field access indexing ‘a[1]


[2][3]‘
2. regular indexing ‘a[1,2,3]‘
3. slicing ‘a[1:3, 1:3]‘
4. fancy indexing ‘a[[1,2],
[1,2]]‘
if you want more: Check out the indexing.ipynb. It shows off all the different
kinds of indexing and combinations!
Topic 2:
Indexing
Field Access
Indexing

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)

>>> array([[[8 , 2, 6, 7],


>>> [7 , 5, 6, 2],
>>> [1 , 8, 6, 1]],
>>>
>>> [[7 , 3, 7, 9],
>>> [2 , 4, 2, 1],
>>> [1 , 1, 8, 3]]])

1 >>> x = y = z = 1
2 >>> print(a[z, y, x])

>>>
4 16/49
Topic 2:
Indexing
Numpy Slice

► Numpy slicing expands the slice syntax into n dimensions


► you pass one slice object per dimension in the array
► There are three big differences to base python slicing:
1. slicing can be done over multiple dimensions
2. exactly one ellipsis object ”...” can be used to indicate several
dimensions at once,
3. slicing cannot be used to expand the size of an array (unlike lists).

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?

When slicing Dimensionality of the output is determined by the combined


dimensionality of the indexes
1 a = np. zeros((5 ,5 ,5))
2 a[1 , ::2 , ::2]. shape
Topic 2: Indexing
What Dimensionality is my
output?

When slicing Dimensionality of the output is determined by the combined


dimensionality of the indexes
1 a = np. zeros((5 ,5 ,5))
2 a[1 , ::2 , ::2]. shape

>>>
(3 ,3)
> Dimension Guessing

see file
Exercises_Notebook_1.py
Topic 2: Indexing
What Dimensionality is my
output?

Single element dimensions


disappear
1 a = np. zeros((3 ,3))
2 a[1 ,:]. shape

>>>
(3 ,)

20/49
Topic 2:
Indexing
What Dimensionality is my
output?

What if I can’t use slices, because I want some irregular subset of


the data?
1 a = np. zeros((5 ,5))

I want every second row and the second and


third column!
Topic 2: Indexing
What Dimensionality is my
output?

What if I can’t use slices, because I want some irregular subset of


the data?
1 a = np. zeros((5 ,5))

I want every second row and the second and third


column!
1 a[::2 , [2 ,3]]. shape

Answer: using a list of indices


also works
Topic 2: Indexing
What Dimensionality is my
output?

What if I can’t use slices, because I want some irregular subset of


the data?
1 a = np. zeros((5 ,5))

I want every second row and the second and


third column!
1 a[::2 , [2 ,3]]. shape

Answer: using a list of indices


also works
>>> (3 ,2)
Topic 2: Indexing
What Dimensionality is my
output?

Before we indexed with a slice and a list. Now how


about two lists? What do you think happens here?
1 a = np. zeros((5 ,5))
2 a[[2 ,3], [2 ,3]]. shape
Topic 2:
Indexing
What Dimensionality is my
output?
Before we indexed with a slice and a list. Now how
about two lists? What do you think happens here?
1 a = np. zeros((5 ,5))
2 a[[2 ,3], [2 ,3]]. shape

>>>
(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)

>>> array([['A', 'B', 'C', 'D', 'E', 'F'],


>> ['G', 'H', 'I', 'J', 'K', 'L'],
> ['M', 'N', 'O', 'P', 'Q', 'R'],
>> ['S', 'T', 'U', 'V', 'W', 'X']], dtype='<U1 ')
>
1
>>
print(a[[3 ,3 ,0],[4 ,1 ,5]])
>

>>> array(['W', 'T', 'F'], dtype='<U1 ')

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)

>>> array([[9 , 2, 7],


>> [2 , 6, 9],
> [1 , 7, 3]])
>>
>
Topic 2:
Indexing
Fancy Indexing with
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)

>>> array([[9 , 2, 7],


>> [2 , 6, 9],
> [1 , 7, 3]])
>>
1
>
print(a<5)

>>> array([[ False , True , False],


>> [ True , False , False],
> [ True , False , True]])
>>
>
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])

>>> array([2 , 2, 1, 3])


> Exercises 3

see file
Exercises_Notebook_2.py
TOPIC 3: at the heart of
Numpy and
Views
Copies

[...] a regular indexing expression on an ndarray can always produce an


ndarray object without copying any data. This is sometimes referred to as
the “view” feature of array indexing

— Guide to Numpy, pg. 30

► Slicing gives a view


► In-place operations can be done on views
► All fancy indexing returns a copy
► the a.copy() command gives a copy when specifically requested

26/49
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)
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]
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)
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]
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)

>>> [[0. 0. 0. 0.]


>>> [0. 0. 0. 0.]
>>> [0. 0. 0. 0.]
>>> [0. 0. 0. 0.]]
1 v = a[2:4 , 2] 1 v = a[[2 ,3], 2]
2 v[1] = 9 2 v[1] = 9
3 print(a) 3 print(a)

>>> [[0. 0. 0. 0.] >>> [[0. 0. 0. 0.]


>>> [0. 0. 0. 0.] >>> [0. 0. 0. 0.]
>>> [0. 0. 0. 0.] >>> [0. 0. 0. 0.]
>>> [0. 0. 9. 0.]] >>> [0. 0. 0. 0.]]
28/49
TOPIC 3: at the heart of
Numpy
ndarra
y

An N-dimensional array is a homogeneous collection of “items” indexed


using N integers. There are two essential pieces of information that define
an N-dimensional array:
1. the shape of the array
2. the kind of item the array is composed of

— Guide to Numpy, pg. 18

29/49
TOPIC 3: at the heart of Numpy
ndarray

1 a = np. random. randint(1 , 10 , (3 ,3))


2 print(a. shape)
3 print(a. dtype)

>>> (3 , 3)
>>> dtype(' int64
')

30/49
TOPIC 3: at the heart of
Numpy
ndarray
properties

Which information does the ndarray


need?
► data: where to start reading
► strides/item size: by how many
bits to move
► size: when to stop reading
► data type: how to interpret the
bits
► order: which reading convention
to use

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

Views and Copies


Why does fancy indexing return copies and slices return
views?

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.

— Guide to Numpy, pg. 154


38/49
TOPIC 3: at the heart of
Numpy
Broadcasting
1 a = np. array([1 ,2 ,3]) # row vector
2 b = np. array ([[4] ,[5] ,[6]]) # column vector
3 print(a. shape)
4 print(b. shape)

>>> (3 ,)
>>> (3 ,
1)
1 np. broadcast_arrays(a, b)

>>> [ array([[1 , 2, 3],


>>> [1 , 2, 3],
>>> [1 , 2, 3]]),
>>> array([ [4 , 4, 4],
>>> [5 , 5, 5],
>>> [6 , 6, 6]])]
> Exercise 4

Calculate out the between subject variance, so that we can get a


better estimate of the variance for each subject!
see file Exercises_Notebook_3.ipynb
TOPIC 4: Tricks, Conventions, and
Applications
Save and
Load

The .npy format is the standard binary file format in NumPy for persisting a
single arbitrary NumPy array on disk.

► for one numpy array


► when called on something other than a pure np array, will use python
pickle
► in that case the flag ”allow_pickle” needs to be True
1 a = np. random. rand((5 ,5))
2 np. save(a, " my_file. npy")
3
b = np. load(" my_file. npy")

40/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors

What happens when things go


wrong?
1 >>> np. geterr()
2 {' divide': 'warn',
3 'over': 'warn',
4 ' under': ' ignore',
5 ' invalid': 'warn'}

When something goes wrong, but no error is raised, typically


NaN is returned.

41/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
divide

Division by zero: infinite result obtained from finite


numbers.
1 a = np. divide(3 ,0)

>>> <stdin >:1: RuntimeWarning: divide by zero encountered in true_divide

1 a

>>> inf

Default consequence: Warning

42/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
over

Overflow: result too large to be


expressed.
1 np. exp(1234.1)

>>> <stdin >:1: RuntimeWarning: overflow encountered in


exp
>>> inf

Default consequence: Warning

43/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors:
under

Underflow: result so close to zero that some precision


was lost.
1 a = np. exp(-(40)**2)
2 a

>>> 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 ,

>>> array([ 4., 3., 2., nan , 9.])

1 a<=b

>>> <stdin >:1: RuntimeWarning: invalid value encountered in


less_equal
>>> array([ True , True , True , False , True])
Default consequence: Warn
45/49
TOPIC 4: Tricks, Conventions, and
Applications
Errors

You can configure your own safety net, by escalating particularly


relevant errors
1 np. seterr( under=' raise', over=' ignore')

it can also be useful to use context


managers
1 a = np. arange(5)
2 b = np. array([4 , 3, 2, np.NaN , 9])
3 with np. errstate( invalid=' ignore'):
4 c = a <= b

46/49
TOPIC 4: Tricks, Conventions, and
Applications
Reshaping

1 np. arange(8). reshape(2 ,4)

>>> array([[0 , 1, 2, 3],


>> [4 , 5, 6, 7]])
>
► reshaping is very fast (only changes string
information)
► -1 means ”just make it fit with the other
dimensions”
► reshape gives a new view. resize is the same,
but in place.

47/49
TOPIC 4: Tricks, Conventions, and
Applications
Reshapin
g

► the corresponding opposites are flatten (copy) and ravel(in-place)


► also useful is squeeze(): return an array with all single dimensions
squeezed out.
1 np. arange(8). reshape(2 ,4). flatten()

>>> array([0 , 1, 2, 3, 4, 5, 6, 7])


> Excersizes
5
1. Go insanity mode: switch off all the errors and do all the things
you never thought you could!
2. Chose one of every two elements in a 1D array.

see file Exercises_Notebook_4.ipynb


Summar
y

In short Numpy introduces:


1. the Numpy array
► at the core of Numpy functionality
► it expands the Python List into n dimensions
► it cleverly uses memory to avoid time-consuming copying and
looping
► is a homogeneous container for various datatypes
2. the ufunc
► a standard class for functions
► common interface to mathematical functions that operate on
▶ uses broadcasting to deal with arrays that are not the
scalars
► same
can beshape
made to operate on arrays in an element-by-element
fashion
Thank you

You might also like