Slide 1

Slide 1 text

Numpy: Vectorize your brain Ekaterina Tuzova

Slide 2

Slide 2 text

K nearest neighbors https://archive.ics.uci.edu/ml/datasets/Wine

Slide 3

Slide 3 text

NumPy

Slide 4

Slide 4 text

What is NumPy? Numpy is the fundamental package for scientific computing with Python.

Slide 5

Slide 5 text

IPython

Slide 6

Slide 6 text

Python and Performance

Slide 7

Slide 7 text

Python is fast

Slide 8

Slide 8 text

Python is slow

Slide 9

Slide 9 text

Euclidian distance

Slide 10

Slide 10 text

“Magic” timeit

Slide 11

Slide 11 text

Euclidian distance. C

Slide 12

Slide 12 text

Euclidian distance. C

Slide 13

Slide 13 text

Euclidian distance

Slide 14

Slide 14 text

line_profiler and “magic” lprun

Slide 15

Slide 15 text

Euclidian distance

Slide 16

Slide 16 text

Compiled languages

Slide 17

Slide 17 text

Interpreted languages

Slide 18

Slide 18 text

What can be done?

Slide 19

Slide 19 text

NumPy

Slide 20

Slide 20 text

Ufuncs

Slide 21

Slide 21 text

Universal functions Special type of function defined within a numpy library and it operate element-wise on arrays.

Slide 22

Slide 22 text

Arithmetic operations

Slide 23

Slide 23 text

Arithmetic operations

Slide 24

Slide 24 text

Arithmetic operations

Slide 25

Slide 25 text

Arithmetic operations

Slide 26

Slide 26 text

Ufuncs available - Arithmetic - Bitwise - Comparison - Trigonometric - Floating …

Slide 27

Slide 27 text

Slicing and indexing

Slide 28

Slide 28 text

Slicing and indexing

Slide 29

Slide 29 text

Slicing and indexing

Slide 30

Slide 30 text

Multidimensional arrays

Slide 31

Slide 31 text

Multidimensional arrays

Slide 32

Slide 32 text

Index arrays

Slide 33

Slide 33 text

Index arrays

Slide 34

Slide 34 text

Index arrays

Slide 35

Slide 35 text

Masking

Slide 36

Slide 36 text

Masking

Slide 37

Slide 37 text

Test train split

Slide 38

Slide 38 text

Test train split

Slide 39

Slide 39 text

Broadcasting

Slide 40

Slide 40 text

Broadcasting Broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations.

Slide 41

Slide 41 text

Broadcasting rules 1.  If two arrays differ in their number of dimension, the shape of the array with the fewer dimensions is padded with ones on it’s leading(left) size. 2.  If the shape of two arrays doesn’t match in any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape. 3.  If these conditions are not met, raise a ValueError: operands could not be broadcast together with shapes

Slide 42

Slide 42 text

Broadcasting. Example

Slide 43

Slide 43 text

np.newaxis

Slide 44

Slide 44 text

np.newaxis

Slide 45

Slide 45 text

np.newaxis

Slide 46

Slide 46 text

Aggregations

Slide 47

Slide 47 text

Aggregations

Slide 48

Slide 48 text

Aggregations

Slide 49

Slide 49 text

NumPy resume Basic ideas to make you code faster: - Ufuncs - Slicing and indexing - Broadcasting - Aggregations

Slide 50

Slide 50 text

k-means

Slide 51

Slide 51 text

Algorithm 1.  Select k points at random as cluster centers. 2.  Assign objects to their closest cluster center according to the Euclidean distance function. 3.  Calculate the centroid or mean of all objects in each cluster. 4.  Repeat steps 2, 3 and 4 until the same points are assigned to each cluster in consecutive rounds.

Slide 52

Slide 52 text

Synthetic data

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Vectorized euclidian distance

Slide 55

Slide 55 text

k-means

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Thank you. @ktisha