Slide 1

Slide 1 text

 Python - Tutorial Eueung Mulyana http://eueung.github.io/EL5244/py-tut based on the material at CS231n@Stanford | Attribution-ShareAlike CC BY-SA 1 / 42

Slide 2

Slide 2 text

Agenda 1. Python Review 2. Numpy 3. SciPy 4. Matplotlib 2 / 42

Slide 3

Slide 3 text

 Python Review 3 / 42

Slide 4

Slide 4 text

Agenda 1. Python Review Basic Data Types Containers Functions Classes 2. Numpy 3. SciPy 4. Matplotlib 4 / 42

Slide 5

Slide 5 text

x = 3 p r i n t t y p e ( x ) # P r i n t s " < t y p e ' i n t ' > " p r i n t x # P r i n t s " 3 " p r i n t x + 1 # A d d i t i o n ; p r i n t s " 4 " p r i n t x - 1 # S u b t r a c t i o n ; p r i n t s " 2 " p r i n t x * 2 # M u l t i p l i c a t i o n ; p r i n t s " 6 " p r i n t x * * 2 # E x p o n e n t i a t i o n ; p r i n t s " 9 " x + = 1 p r i n t x # P r i n t s " 4 " x * = 2 p r i n t x # P r i n t s " 8 " y = 2 . 5 p r i n t t y p e ( y ) # P r i n t s " < t y p e ' f l o a t ' > " p r i n t y , y + 1 , y * 2 , y * * 2 # P r i n t s " 2 . 5 3 . 5 5 . 0 6 . 2 5 " t = T r u e f = F a l s e p r i n t t y p e ( t ) # P r i n t s " < t y p e ' b o o l ' > " p r i n t t a n d f # L o g i c a l A N D ; p r i n t s " F a l s e " p r i n t t o r f # L o g i c a l O R ; p r i n t s " T r u e " p r i n t n o t t # L o g i c a l N O T ; p r i n t s " F a l s e " p r i n t t ! = f # L o g i c a l X O R ; p r i n t s " T r u e " Basic Data Types Numbers Integers and floats work as you would expect from other languages Does not have unary increment (x + + ) or decrement (x - - ) operators Other: built-in types for long integers and complex numbers Booleans Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols (& & , | | , etc.): 5 / 42

Slide 6

Slide 6 text

Basic Data Types Strings String objects have a lot of useful methods. h e l l o = ' h e l l o ' # S t r i n g l i t e r a l s c a n u s e s i n g l e q u o t e s w o r l d = " w o r l d " # o r d o u b l e q u o t e s ; i t d o e s n o t m a t t e r . p r i n t h e l l o # P r i n t s " h e l l o " p r i n t l e n ( h e l l o ) # S t r i n g l e n g t h ; p r i n t s " 5 " h w = h e l l o + ' ' + w o r l d # S t r i n g c o n c a t e n a t i o n p r i n t h w # p r i n t s " h e l l o w o r l d " h w 1 2 = ' % s % s % d ' % ( h e l l o , w o r l d , 1 2 ) # s p r i n t f s t y l e s t r i n g p r i n t h w 1 2 # p r i n t s " h e l l o w o r l d 1 2 " s = " h e l l o " p r i n t s . c a p i t a l i z e ( ) # C a p i t a l i z e a s t r i n g ; p r i n t s " H e l l o " p r i n t s . u p p e r ( ) # C o n v e r t a s t r i n g t o u p p e r c a s e ; p r i n t s p r i n t s . r j u s t ( 7 ) # R i g h t - j u s t i f y a s t r i n g , p a d d i n g w i t h s p r i n t s . c e n t e r ( 7 ) # C e n t e r a s t r i n g , p a d d i n g w i t h s p a c e s ; p r i n t s . r e p l a c e ( ' l ' , ' ( e l l ) ' ) # R e p l a c e a l l i n s t a n c e s o f o n e # p r i n t s " h e ( e l l ) ( e l l ) o " p r i n t ' w o r l d ' . s t r i p ( ) # S t r i p l e a d i n g a n d t r a i l i n g w h i t e s p 6 / 42

Slide 7

Slide 7 text

x s = [ 3 , 1 , 2 ] # C r e a t e a l i s t p r i n t x s , x s [ 2 ] # P r i n t s " [ 3 , 1 , 2 ] 2 " p r i n t x s [ - 1 ] # N e g a t i v e i n d i c e s c o u n t f r o m t h e e n d o f t h e l i s t ; p r i n t s " 2 " x s [ 2 ] = ' f o o ' # L i s t s c a n c o n t a i n e l e m e n t s o f d i f f e r e n t t y p e s p r i n t x s # P r i n t s " [ 3 , 1 , ' f o o ' ] " x s . a p p e n d ( ' b a r ' ) # A d d a n e w e l e m e n t t o t h e e n d o f t h e l i s t p r i n t x s # P r i n t s x = x s . p o p ( ) # R e m o v e a n d r e t u r n t h e l a s t e l e m e n t o f t h e l i s t p r i n t x , x s # P r i n t s " b a r [ 3 , 1 , ' f o o ' ] " Container Types Python includes several built-in container types: lists, dictionaries, sets, and tuples. Lists A list is the Python equivalent of an array, but is resizeable and can contain elements of different types. 7 / 42

Slide 8

Slide 8 text

Containers Lists - Slicing In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing Lists - Looping You can loop over the elements of a list. If you want access to the index of each element within the body of a loop, use the built-in e n u m e r a t e function. n u m s = r a n g e ( 5 ) # r a n g e i s a b u i l t - i n f u n c t i o n t h a t c r e a t e s p r i n t n u m s # P r i n t s " [ 0 , 1 , 2 , 3 , 4 ] " p r i n t n u m s [ 2 : 4 ] # G e t a s l i c e f r o m i n d e x 2 t o 4 ( e x c l u s i v e ) p r i n t n u m s [ 2 : ] # G e t a s l i c e f r o m i n d e x 2 t o t h e e n d ; p r i n p r i n t n u m s [ : 2 ] # G e t a s l i c e f r o m t h e s t a r t t o i n d e x 2 ( e x p r i n t n u m s [ : ] # G e t a s l i c e o f t h e w h o l e l i s t ; p r i n t s [ " 0 p r i n t n u m s [ : - 1 ] # S l i c e i n d i c e s c a n b e n e g a t i v e ; p r i n t s [ " 0 n u m s [ 2 : 4 ] = [ 8 , 9 ] # A s s i g n a n e w s u b l i s t t o a s l i c e p r i n t n u m s # P r i n t s " [ 0 , 1 , 8 , 9 , 4 ] " a n i m a l s = [ ' c a t ' , ' d o g ' , ' m o n k e y ' ] f o r a n i m a l i n a n i m a l s : p r i n t a n i m a l # P r i n t s " c a t " , " d o g " , " m o n k e y " , e a c h o n i t s o w n l i n e . # - - - - - - a n i m a l s = [ ' c a t ' , ' d o g ' , ' m o n k e y ' ] f o r i d x , a n i m a l i n e n u m e r a t e ( a n i m a l s ) : p r i n t ' # % d : % s ' % ( i d x + 1 , a n i m a l ) # P r i n t s " # 1 : c a t " , " # 2 : d o g " , " # 3 : m o n k e y " , e a c h o n i t s o w n l 8 / 42

Slide 9

Slide 9 text

n u m s = [ 0 , 1 , 2 , 3 , 4 ] s q u a r e s = [ ] f o r x i n n u m s : s q u a r e s . a p p e n d ( x * * 2 ) p r i n t s q u a r e s # P r i n t s [ 0 , 1 , 4 , 9 , 1 6 ] n u m s = [ 0 , 1 , 2 , 3 , 4 ] s q u a r e s = [ x * * 2 f o r x i n n u m s ] p r i n t s q u a r e s # P r i n t s [ 0 , 1 , 4 , 9 , 1 6 ] n u m s = [ 0 , 1 , 2 , 3 , 4 ] e v e n _ s q u a r e s = [ x * * 2 f o r x i n n u m s i f x % 2 = = 0 ] p r i n t e v e n _ s q u a r e s # P r i n t s " [ 0 , 4 , 1 6 ] " Containers Lists - List Comprehensions When programming, frequently we want to transform one type of data into another - > LC. List comprehensions can also contain conditions. 9 / 42

Slide 10

Slide 10 text

Containers Dictionaries A dictionary stores (key, value) pairs, similar to a M a p in Java or an o b j e c t in Javascript. Dicts - Looping It is easy to iterate over the keys in a dictionary. If you want access to keys and their corresponding values, use the i t e r i t e m s method. d = { ' c a t ' : ' c u t e ' , ' d o g ' : ' f u r r y ' } # C r e a t e a n e w d i c t i o n a r y p r i n t d [ ' c a t ' ] # G e t a n e n t r y f r o m a d i c t i o n a r y ; p r i n t s p r i n t ' c a t ' i n d # C h e c k i f a d i c t i o n a r y h a s a g i v e n k e y ; d [ ' f i s h ' ] = ' w e t ' # S e t a n e n t r y i n a d i c t i o n a r y p r i n t d [ ' f i s h ' ] # P r i n t s " w e t " # p r i n t d [ ' m o n k e y ' ] # K e y E r r o r : ' m o n k e y ' n o t a k e y o f d p r i n t d . g e t ( ' m o n k e y ' , ' N / A ' ) # G e t a n e l e m e n t w i t h a d e f a u l t ; p r i n t d . g e t ( ' f i s h ' , ' N / A ' ) # G e t a n e l e m e n t w i t h a d e f a u l t ; d e l d [ ' f i s h ' ] # R e m o v e a n e l e m e n t f r o m a d i c t i o n a r y p r i n t d . g e t ( ' f i s h ' , ' N / A ' ) # " f i s h " i s n o l o n g e r a k e y ; p r i n t s d = { ' c h i c k e n ' : 2 , ' c a t ' : 4 , ' s p i d e r ' : 8 } f o r a n i m a l i n d : l e g s = d [ a n i m a l ] p r i n t ' A % s h a s % d l e g s ' % ( a n i m a l , l e g s ) # P r i n t s " A c h i c k e n h a s 2 l e g s " , " A s p i d e r h a s 8 l e g s " , " A c a t d = { ' c h i c k e n ' : 2 , ' c a t ' : 4 , ' s p i d e r ' : 8 } f o r a n i m a l , l e g s i n d . i t e r i t e m s ( ) : p r i n t ' A % s h a s % d l e g s ' % ( a n i m a l , l e g s ) # P r i n t s " A c h i c k e n h a s 2 l e g s " , " A s p i d e r h a s 8 l e g s " , " A c a t 10 / 42

Slide 11

Slide 11 text

n u m s = [ 0 , 1 , 2 , 3 , 4 ] e v e n _ n u m _ t o _ s q u a r e = { x : x * * 2 f o r x i n n u m s i f x % 2 = = 0 } p r i n t e v e n _ n u m _ t o _ s q u a r e # P r i n t s " { 0 : 0 , 2 : 4 , 4 : 1 6 } " Containers Dicts - Dictionary Comprehensions These are similar to list comprehensions, but allow you to easily construct dictionaries. 11 / 42

Slide 12

Slide 12 text

Containers Sets A set is an unordered collection of distinct elements. a n i m a l s = { ' c a t ' , ' d o g ' } p r i n t ' c a t ' i n a n i m a l s # C h e c k i f a n e l e m e n t i s i n a s e t ; p r p r i n t ' f i s h ' i n a n i m a l s # p r i n t s " F a l s e " a n i m a l s . a d d ( ' f i s h ' ) # A d d a n e l e m e n t t o a s e t p r i n t ' f i s h ' i n a n i m a l s # P r i n t s " T r u e " p r i n t l e n ( a n i m a l s ) # N u m b e r o f e l e m e n t s i n a s e t ; p r i n t s a n i m a l s . a d d ( ' c a t ' ) # A d d i n g a n e l e m e n t t h a t i s a l r e a d y i p r i n t l e n ( a n i m a l s ) # P r i n t s " 3 " a n i m a l s . r e m o v e ( ' c a t ' ) # R e m o v e a n e l e m e n t f r o m a s e t p r i n t l e n ( a n i m a l s ) # P r i n t s " 2 " 12 / 42

Slide 13

Slide 13 text

a n i m a l s = { ' c a t ' , ' d o g ' , ' f i s h ' } f o r i d x , a n i m a l i n e n u m e r a t e ( a n i m a l s ) : p r i n t ' # % d : % s ' % ( i d x + 1 , a n i m a l ) # P r i n t s " # 1 : f i s h " , " # 2 : d o g " , " # 3 : c a t " f r o m m a t h i m p o r t s q r t n u m s = { i n t ( s q r t ( x ) ) f o r x i n r a n g e ( 3 0 ) } p r i n t n u m s # P r i n t s " s e t ( [ 0 , 1 , 2 , 3 , 4 , 5 ] ) " Containers Sets - Looping Iterating over a set has the same syntax as iterating over a list; however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set. Sets - Set Comprehensions Like lists and dictionaries, we can easily construct sets using set comprehensions. 13 / 42

Slide 14

Slide 14 text

Containers Tuples A tuple is an (immutable) ordered list of values. A tuple is in many ways similar to a list. One of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. d = { ( x , x + 1 ) : x f o r x i n r a n g e ( 1 0 ) } # C r e a t e a d i c t i o n a r y t = ( 5 , 6 ) # C r e a t e a t u p l e p r i n t t y p e ( t ) # P r i n t s " < t y p e ' t u p l e ' > " p r i n t d [ t ] # P r i n t s " 5 " p r i n t d [ ( 1 , 2 ) ] # P r i n t s " 1 " 14 / 42

Slide 15

Slide 15 text

d e f s i g n ( x ) : i f x > 0 : r e t u r n ' p o s i t i v e ' e l i f x < 0 : r e t u r n ' n e g a t i v e ' e l s e : r e t u r n ' z e r o ' f o r x i n [ - 1 , 0 , 1 ] : p r i n t s i g n ( x ) # P r i n t s " n e g a t i v e " , " z e r o " , " p o s i t i v e " d e f h e l l o ( n a m e , l o u d = F a l s e ) : i f l o u d : p r i n t ' H E L L O , % s ' % n a m e . u p p e r ( ) e l s e : p r i n t ' H e l l o , % s ! ' % n a m e h e l l o ( ' B o b ' ) # P r i n t s " H e l l o , B o b " h e l l o ( ' F r e d ' , l o u d = T r u e ) # P r i n t s " H E L L O , F R E D ! " Functions Python functions are defined using the d e f keyword. We will often define functions to take optional keyword arguments. 15 / 42

Slide 16

Slide 16 text

Classes The syntax for defining classes in Python is straightforward. c l a s s G r e e t e r : # C o n s t r u c t o r d e f _ _ i n i t _ _ ( s e l f , n a m e ) : s e l f . n a m e = n a m e # C r e a t e a n i n s t a n c e v a r i a b l e # I n s t a n c e m e t h o d d e f g r e e t ( s e l f , l o u d = F a l s e ) : i f l o u d : p r i n t ' H E L L O , % s ! ' % s e l f . n a m e . u p p e r ( ) e l s e : p r i n t ' H e l l o , % s ' % s e l f . n a m e g = G r e e t e r ( ' F r e d ' ) # C o n s t r u c t a n i n s t a n c e o f t h e G r e e t e r c l g . g r e e t ( ) # C a l l a n i n s t a n c e m e t h o d ; p r i n t s " H e l l o , g . g r e e t ( l o u d = T r u e ) # C a l l a n i n s t a n c e m e t h o d ; p r i n t s " H E L L O , 16 / 42

Slide 17

Slide 17 text

 Numpy 17 / 42

Slide 18

Slide 18 text

Agenda 1. Python Review 2. Numpy Arrays Array Indexing Datatypes Array Math Broadcasting 3. SciPy 4. Matplotlib 18 / 42

Slide 19

Slide 19 text

i m p o r t n u m p y a s n p a = n p . a r r a y ( [ 1 , 2 , 3 ] ) # C r e a t e a r a n k 1 a r r a y p r i n t t y p e ( a ) # P r i n t s " < t y p e ' n u m p y . n d a r r a y ' > " p r i n t a . s h a p e # P r i n t s " ( 3 , ) " p r i n t a [ 0 ] , a [ 1 ] , a [ 2 ] # P r i n t s " 1 2 3 " a [ 0 ] = 5 # C h a n g e a n e l e m e n t o f t h e a r r a y p r i n t a # P r i n t s " [ 5 , 2 , 3 ] " b = n p . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] ) # C r e a t e a r a n k 2 a r r a y p r i n t b . s h a p e # P r i n t s " ( 2 , 3 ) " p r i n t b [ 0 , 0 ] , b [ 0 , 1 ] , b [ 1 , 0 ] # P r i n t s " 1 2 4 " # - - - - - a = n p . z e r o s ( ( 2 , 2 ) ) # C r e a t e a n a r r a y o f a l l z e r o s p r i n t a # P r i n t s " [ [ 0 . 0 . ] # [ 0 . 0 . ] ] " b = n p . o n e s ( ( 1 , 2 ) ) # C r e a t e a n a r r a y o f a l l o n e s p r i n t b # P r i n t s " [ [ 1 . 1 . ] ] " c = n p . f u l l ( ( 2 , 2 ) , 7 ) # C r e a t e a c o n s t a n t a r r a y p r i n t c # P r i n t s " [ [ 7 . 7 . ] # [ 7 . 7 . ] ] " d = n p . e y e ( 2 ) # C r e a t e a 2 x 2 i d e n t i t y m a t r i x p r i n t d # P r i n t s " [ [ 1 . 0 . ] # [ 0 . 1 . ] ] " e = n p . r a n d o m . r a n d o m ( ( 2 , 2 ) ) # C r e a t e a n a r r a y f i l l e d w i t h r a n d o m v a l u e s p r i n t e # M i g h t p r i n t " [ [ 0 . 9 1 9 4 0 1 6 7 0 . 0 8 1 4 3 9 4 1 ] # [ 0 . 6 8 7 4 4 1 3 4 0 . 8 7 2 3 6 6 8 7 ] ] " Numpy Numpy is the core library for scientific computing in Python. It provides a high- performance multidimensional array object (MATLAB style), and tools for working with these arrays. Arrays A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension. We can initialize numpy arrays from nested Python lists, and access elements using square brackets. Numpy also provides many functions to create arrays. 19 / 42

Slide 20

Slide 20 text

Numpy Array Indexing - Slicing Numpy offers several ways to index into arrays. Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array. i m p o r t n u m p y a s n p # C r e a t e t h e f o l l o w i n g r a n k 2 a r r a y w i t h s h a p e ( 3 , 4 ) # [ [ 1 2 3 4 ] # [ 5 6 7 8 ] # [ 9 1 0 1 1 1 2 ] ] a = n p . a r r a y ( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 1 0 , 1 1 , 1 2 ] ] ) # U s e s l i c i n g t o p u l l o u t t h e s u b a r r a y c o n s i s t i n g o f t h e f i r s t # a n d c o l u m n s 1 a n d 2 ; b i s t h e f o l l o w i n g a r r a y o f s h a p e ( 2 , 2 # [ [ 2 3 ] # [ 6 7 ] ] b = a [ : 2 , 1 : 3 ] # A s l i c e o f a n a r r a y i s a v i e w i n t o t h e s a m e d a t a , s o m o d i f y i # w i l l m o d i f y t h e o r i g i n a l a r r a y . p r i n t a [ 0 , 1 ] # P r i n t s " 2 " b [ 0 , 0 ] = 7 7 # b [ 0 , 0 ] i s t h e s a m e p i e c e o f d a t a a s a [ 0 , 1 ] p r i n t a [ 0 , 1 ] # P r i n t s " 7 7 " 20 / 42

Slide 21

Slide 21 text

i m p o r t n u m p y a s n p # C r e a t e t h e f o l l o w i n g r a n k 2 a r r a y w i t h s h a p e ( 3 , 4 ) # [ [ 1 2 3 4 ] # [ 5 6 7 8 ] # [ 9 1 0 1 1 1 2 ] ] a = n p . a r r a y ( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 1 0 , 1 1 , 1 2 ] ] ) # T w o w a y s o f a c c e s s i n g t h e d a t a i n t h e m i d d l e r o w o f t h e a r r a y . # M i x i n g i n t e g e r i n d e x i n g w i t h s l i c e s y i e l d s a n a r r a y o f l o w e r r a n k , # w h i l e u s i n g o n l y s l i c e s y i e l d s a n a r r a y o f t h e s a m e r a n k a s t h e # o r i g i n a l a r r a y : r o w _ r 1 = a [ 1 , : ] # R a n k 1 v i e w o f t h e s e c o n d r o w o f a r o w _ r 2 = a [ 1 : 2 , : ] # R a n k 2 v i e w o f t h e s e c o n d r o w o f a p r i n t r o w _ r 1 , r o w _ r 1 . s h a p e # P r i n t s " [ 5 6 7 8 ] ( 4 , ) " p r i n t r o w _ r 2 , r o w _ r 2 . s h a p e # P r i n t s " [ [ 5 6 7 8 ] ] ( 1 , 4 ) " # W e c a n m a k e t h e s a m e d i s t i n c t i o n w h e n a c c e s s i n g c o l u m n s o f a n a r r a y : c o l _ r 1 = a [ : , 1 ] c o l _ r 2 = a [ : , 1 : 2 ] p r i n t c o l _ r 1 , c o l _ r 1 . s h a p e # P r i n t s " [ 2 6 1 0 ] ( 3 , ) " p r i n t c o l _ r 2 , c o l _ r 2 . s h a p e # P r i n t s " [ [ 2 ] # [ 6 ] # [ 1 0 ] ] ( 3 , 1 ) " Numpy Array Indexing - Slicing You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array. Note that this is quite different from the way that MATLAB handles array slicing. 21 / 42

Slide 22

Slide 22 text

Numpy Array Indexing - Integer Array Indexing When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. i m p o r t n u m p y a s n p a = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] ) # A n e x a m p l e o f i n t e g e r a r r a y i n d e x i n g . # T h e r e t u r n e d a r r a y w i l l h a v e s h a p e ( 3 , ) a n d p r i n t a [ [ 0 , 1 , 2 ] , [ 0 , 1 , 0 ] ] # P r i n t s " [ 1 4 5 ] " # T h e a b o v e e x a m p l e o f i n t e g e r a r r a y i n d e x i n g i s e q u i v a l e n t t o p r i n t n p . a r r a y ( [ a [ 0 , 0 ] , a [ 1 , 1 ] , a [ 2 , 0 ] ] ) # P r i n t s " [ 1 4 5 ] # W h e n u s i n g i n t e g e r a r r a y i n d e x i n g , y o u c a n r e u s e t h e s a m e # e l e m e n t f r o m t h e s o u r c e a r r a y : p r i n t a [ [ 0 , 0 ] , [ 1 , 1 ] ] # P r i n t s " [ 2 2 ] " # E q u i v a l e n t t o t h e p r e v i o u s i n t e g e r a r r a y i n d e x i n g e x a m p l e p r i n t n p . a r r a y ( [ a [ 0 , 1 ] , a [ 0 , 1 ] ] ) # P r i n t s " [ 2 2 ] " 22 / 42

Slide 23

Slide 23 text

i m p o r t n u m p y a s n p a = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] ) b o o l _ i d x = ( a > 2 ) # F i n d t h e e l e m e n t s o f a t h a t a r e b i g g e r t h a n 2 ; # t h i s r e t u r n s a n u m p y a r r a y o f B o o l e a n s o f t h e s a m e # s h a p e a s a , w h e r e e a c h s l o t o f b o o l _ i d x t e l l s # w h e t h e r t h a t e l e m e n t o f a i s > 2 . p r i n t b o o l _ i d x # P r i n t s " [ [ F a l s e F a l s e ] # [ T r u e T r u e ] # [ T r u e T r u e ] ] " # W e u s e b o o l e a n a r r a y i n d e x i n g t o c o n s t r u c t a r a n k 1 a r r a y # c o n s i s t i n g o f t h e e l e m e n t s o f a c o r r e s p o n d i n g t o t h e T r u e v a l u e s # o f b o o l _ i d x p r i n t a [ b o o l _ i d x ] # P r i n t s " [ 3 4 5 6 ] " # - - - # W e c a n d o a l l o f t h e a b o v e i n a s i n g l e c o n c i s e s t a t e m e n t : p r i n t a [ a > 2 ] # P r i n t s " [ 3 4 5 6 ] " Numpy Array Indexing - Boolean Array Indexing Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition. 23 / 42

Slide 24

Slide 24 text

Numpy Datatypes Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype. i m p o r t n u m p y a s n p x = n p . a r r a y ( [ 1 , 2 ] ) # L e t n u m p y c h o o s e t h e d a t a t y p e p r i n t x . d t y p e # P r i n t s " i n t 6 4 " x = n p . a r r a y ( [ 1 . 0 , 2 . 0 ] ) # L e t n u m p y c h o o s e t h e d a t a t y p e p r i n t x . d t y p e # P r i n t s " f l o a t 6 4 " x = n p . a r r a y ( [ 1 , 2 ] , d t y p e = n p . i n t 6 4 ) # F o r c e a p a r t i c u l a r d a t p r i n t x . d t y p e # P r i n t s " i n t 6 4 " 24 / 42

Slide 25

Slide 25 text

i m p o r t n u m p y a s n p x = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] , d t y p e = n p . f l o a t 6 4 ) y = n p . a r r a y ( [ [ 5 , 6 ] , [ 7 , 8 ] ] , d t y p e = n p . f l o a t 6 4 ) # E l e m e n t w i s e s u m ; b o t h p r o d u c e t h e a r r a y # [ [ 6 . 0 8 . 0 ] # [ 1 0 . 0 1 2 . 0 ] ] p r i n t x + y p r i n t n p . a d d ( x , y ) # E l e m e n t w i s e d i f f e r e n c e ; b o t h p r o d u c e t h e a r r a y # [ [ - 4 . 0 - 4 . 0 ] # [ - 4 . 0 - 4 . 0 ] ] p r i n t x - y p r i n t n p . s u b t r a c t ( x , y ) # E l e m e n t w i s e p r o d u c t ; b o t h p r o d u c e t h e a r r a y # [ [ 5 . 0 1 2 . 0 ] # [ 2 1 . 0 3 2 . 0 ] ] p r i n t x * y p r i n t n p . m u l t i p l y ( x , y ) # E l e m e n t w i s e d i v i s i o n ; b o t h p r o d u c e t h e a r r a y # [ [ 0 . 2 0 . 3 3 3 3 3 3 3 3 ] # [ 0 . 4 2 8 5 7 1 4 3 0 . 5 ] ] p r i n t x / y p r i n t n p . d i v i d e ( x , y ) # E l e m e n t w i s e s q u a r e r o o t ; p r o d u c e s t h e a r r a y # [ [ 1 . 1 . 4 1 4 2 1 3 5 6 ] # [ 1 . 7 3 2 0 5 0 8 1 2 . ] ] p r i n t n p . s q r t ( x ) Numpy Array Math Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module 25 / 42

Slide 26

Slide 26 text

Numpy Array Math Note that unlike MATLAB, * is elementwise multiplication, not matrix multiplication. We instead use the dot function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices. dot is available both as a function in the numpy module and as an instance method of array objects i m p o r t n u m p y a s n p x = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) y = n p . a r r a y ( [ [ 5 , 6 ] , [ 7 , 8 ] ] ) v = n p . a r r a y ( [ 9 , 1 0 ] ) w = n p . a r r a y ( [ 1 1 , 1 2 ] ) # I n n e r p r o d u c t o f v e c t o r s ; b o t h p r o d u c e 2 1 9 p r i n t v . d o t ( w ) p r i n t n p . d o t ( v , w ) # M a t r i x / v e c t o r p r o d u c t ; b o t h p r o d u c e t h e r a n k 1 a r r a y [ 2 9 6 p r i n t x . d o t ( v ) p r i n t n p . d o t ( x , v ) # M a t r i x / m a t r i x p r o d u c t ; b o t h p r o d u c e t h e r a n k 2 a r r a y # [ [ 1 9 2 2 ] # [ 4 3 5 0 ] ] p r i n t x . d o t ( y ) p r i n t n p . d o t ( x , y ) 26 / 42

Slide 27

Slide 27 text

i m p o r t n u m p y a s n p x = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) p r i n t n p . s u m ( x ) # C o m p u t e s u m o f a l l e l e m e n t s ; p r i n t s " 1 0 " p r i n t n p . s u m ( x , a x i s = 0 ) # C o m p u t e s u m o f e a c h c o l u m n ; p r i n t s " [ 4 6 ] " p r i n t n p . s u m ( x , a x i s = 1 ) # C o m p u t e s u m o f e a c h r o w ; p r i n t s " [ 3 7 ] " i m p o r t n u m p y a s n p x = n p . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) p r i n t x # P r i n t s " [ [ 1 2 ] # [ 3 4 ] ] " p r i n t x . T # P r i n t s " [ [ 1 3 ] # [ 2 4 ] ] " # N o t e t h a t t a k i n g t h e t r a n s p o s e o f a r a n k 1 a r r a y d o e s n o t h i n g : v = n p . a r r a y ( [ 1 , 2 , 3 ] ) p r i n t v # P r i n t s " [ 1 2 3 ] " p r i n t v . T # P r i n t s " [ 1 2 3 ] " Numpy Array Math Numpy provides many useful functions for performing computations on arrays; one of the most useful is s u m . Apart from computing mathematical functions using arrays, we frequently need to reshape or otherwise manipulate data in arrays. The simplest example of this type of operation is transposing a matrix; to transpose a matrix, simply use the T attribute of an array object. 27 / 42

Slide 28

Slide 28 text

Numpy Broadcasting Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array. For example, suppose that we want to add a constant vector to each row of a matrix ... i m p o r t n u m p y a s n p # W e w i l l a d d t h e v e c t o r v t o e a c h r o w o f t h e m a t r i x x , # s t o r i n g t h e r e s u l t i n t h e m a t r i x y x = n p . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 1 0 , 1 1 , 1 2 ] ] ) v = n p . a r r a y ( [ 1 , 0 , 1 ] ) y = n p . e m p t y _ l i k e ( x ) # C r e a t e a n e m p t y m a t r i x w i t h t h e s a m e # A d d t h e v e c t o r v t o e a c h r o w o f t h e m a t r i x x w i t h a n e x p l i c i f o r i i n r a n g e ( 4 ) : y [ i , : ] = x [ i , : ] + v # N o w y i s t h e f o l l o w i n g # [ [ 2 2 4 ] # [ 5 5 7 ] # [ 8 8 1 0 ] # [ 1 1 1 1 1 3 ] ] p r i n t y 28 / 42

Slide 29

Slide 29 text

i m p o r t n u m p y a s n p # W e w i l l a d d t h e v e c t o r v t o e a c h r o w o f t h e m a t r i x x , # s t o r i n g t h e r e s u l t i n t h e m a t r i x y x = n p . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 1 0 , 1 1 , 1 2 ] ] ) v = n p . a r r a y ( [ 1 , 0 , 1 ] ) v v = n p . t i l e ( v , ( 4 , 1 ) ) # S t a c k 4 c o p i e s o f v o n t o p o f e a c h o t h e r p r i n t v v # P r i n t s " [ [ 1 0 1 ] # [ 1 0 1 ] # [ 1 0 1 ] # [ 1 0 1 ] ] " y = x + v v # A d d x a n d v v e l e m e n t w i s e p r i n t y # P r i n t s " [ [ 2 2 4 ] # [ 5 5 7 ] # [ 8 8 1 0 ] # [ 1 1 1 1 1 3 ] ] " Numpy Broadcasting This works... however when the matrix x is very large, computing an explicit loop in Python could be slow. Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix v v by stacking multiple copies of v vertically, then performing elementwise summation of x and v v . 29 / 42

Slide 30

Slide 30 text

Numpy Broadcasting Numpy broadcasting allows us to perform this computation without actually creating multiple copies of v . Consider this version, using broadcasting. The line y = x + v works even though x has shape (4, 3) and v has shape (3,) due to broadcasting. This line works as if v actually had shape (4, 3), where each row was a copy of v , and the sum was performed elementwise. i m p o r t n u m p y a s n p # W e w i l l a d d t h e v e c t o r v t o e a c h r o w o f t h e m a t r i x x , # s t o r i n g t h e r e s u l t i n t h e m a t r i x y x = n p . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 1 0 , 1 1 , 1 2 ] ] ) v = n p . a r r a y ( [ 1 , 0 , 1 ] ) y = x + v # A d d v t o e a c h r o w o f x u s i n g b r o a d c a s t i n g p r i n t y # P r i n t s " [ [ 2 2 4 ] # [ 5 5 7 ] # [ 8 8 1 0 ] # [ 1 1 1 1 1 3 ] ] " 30 / 42

Slide 31

Slide 31 text

i m p o r t n u m p y a s n p # C o m p u t e o u t e r p r o d u c t o f v e c t o r s v = n p . a r r a y ( [ 1 , 2 , 3 ] ) # v h a s s h a p e ( 3 , ) w = n p . a r r a y ( [ 4 , 5 ] ) # w h a s s h a p e ( 2 , ) # T o c o m p u t e a n o u t e r p r o d u c t , w e f i r s t r e s h a p e v t o b e a c o l u m n # v e c t o r o f s h a p e ( 3 , 1 ) ; w e c a n t h e n b r o a d c a s t i t a g a i n s t w t o y i e l d # a n o u t p u t o f s h a p e ( 3 , 2 ) , w h i c h i s t h e o u t e r p r o d u c t o f v a n d w : # [ [ 4 5 ] # [ 8 1 0 ] # [ 1 2 1 5 ] ] p r i n t n p . r e s h a p e ( v , ( 3 , 1 ) ) * w # A d d a v e c t o r t o e a c h r o w o f a m a t r i x x = n p . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] ) # x h a s s h a p e ( 2 , 3 ) a n d v h a s s h a p e ( 3 , ) s o t h e y b r o a d c a s t t o ( 2 , 3 ) , # g i v i n g t h e f o l l o w i n g m a t r i x : # [ [ 2 4 6 ] # [ 5 7 9 ] ] p r i n t x + v # . . . . . Broadcasting two arrays together follows these rules: If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length. The two arrays are said to be compatible in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension. The arrays can be broadcast together if they are compatible in all dimensions. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays. In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension. 31 / 42

Slide 32

Slide 32 text

Numpy Broadcasting Functions that support broadcasting are known as universal functions. Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible. # . . . . . # A d d a v e c t o r t o e a c h c o l u m n o f a m a t r i x # x h a s s h a p e ( 2 , 3 ) a n d w h a s s h a p e ( 2 , ) . # I f w e t r a n s p o s e x t h e n i t h a s s h a p e ( 3 , 2 ) a n d c a n b e b r o a d c # a g a i n s t w t o y i e l d a r e s u l t o f s h a p e ( 3 , 2 ) ; t r a n s p o s i n g t h i # y i e l d s t h e f i n a l r e s u l t o f s h a p e ( 2 , 3 ) w h i c h i s t h e m a t r i x # t h e v e c t o r w a d d e d t o e a c h c o l u m n . G i v e s t h e f o l l o w i n g m a t r i # [ [ 5 6 7 ] # [ 9 1 0 1 1 ] ] p r i n t ( x . T + w ) . T # A n o t h e r s o l u t i o n i s t o r e s h a p e w t o b e a r o w v e c t o r o f s h a p e # w e c a n t h e n b r o a d c a s t i t d i r e c t l y a g a i n s t x t o p r o d u c e t h e s # o u t p u t . p r i n t x + n p . r e s h a p e ( w , ( 2 , 1 ) ) # M u l t i p l y a m a t r i x b y a c o n s t a n t : # x h a s s h a p e ( 2 , 3 ) . N u m p y t r e a t s s c a l a r s a s a r r a y s o f s h a p e # t h e s e c a n b e b r o a d c a s t t o g e t h e r t o s h a p e ( 2 , 3 ) , p r o d u c i n g t # f o l l o w i n g a r r a y : # [ [ 2 4 6 ] # [ 8 1 0 1 2 ] ] p r i n t x * 2 32 / 42

Slide 33

Slide 33 text

 SciPy 33 / 42

Slide 34

Slide 34 text

Agenda 1. Python Review 2. Numpy 3. SciPy Image Operations MATLAB Files Distance between Points 4. Matplotlib 34 / 42

Slide 35

Slide 35 text

f r o m s c i p y . m i s c i m p o r t i m r e a d , i m s a v e , i m r e s i z e # R e a d a n J P E G i m a g e i n t o a n u m p y a r r a y i m g = i m r e a d ( ' a s s e t s / c a t . j p g ' ) p r i n t i m g . d t y p e , i m g . s h a p e # P r i n t s " u i n t 8 ( 4 0 0 , 2 4 8 , 3 ) " # W e c a n t i n t t h e i m a g e b y s c a l i n g e a c h o f t h e c o l o r c h a n n e l s # b y a d i f f e r e n t s c a l a r c o n s t a n t . T h e i m a g e h a s s h a p e ( 4 0 0 , 2 4 8 , 3 ) ; # w e m u l t i p l y i t b y t h e a r r a y [ 1 , 0 . 9 5 , 0 . 9 ] o f s h a p e ( 3 , ) ; # n u m p y b r o a d c a s t i n g m e a n s t h a t t h i s l e a v e s t h e r e d c h a n n e l u n c h a n g e d , # a n d m u l t i p l i e s t h e g r e e n a n d b l u e c h a n n e l s b y 0 . 9 5 a n d 0 . 9 # r e s p e c t i v e l y . i m g _ t i n t e d = i m g * [ 1 , 0 . 9 5 , 0 . 9 ] # R e s i z e t h e t i n t e d i m a g e t o b e 3 0 0 b y 3 0 0 p i x e l s . i m g _ t i n t e d = i m r e s i z e ( i m g _ t i n t e d , ( 3 0 0 , 3 0 0 ) ) # W r i t e t h e t i n t e d i m a g e b a c k t o d i s k i m s a v e ( ' a s s e t s / c a t _ t i n t e d . j p g ' , i m g _ t i n t e d ) SciPy Numpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. SciPy builds on this, and provides a large number of functions that operate on numpy arrays and are useful for different types of scientific and engineering applications. Image Operations SciPy provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images. 35 / 42

Slide 36

Slide 36 text

SciPy MATLAB Files The functions s c i p y . i o . l o a d m a t and s c i p y . i o . s a v e m a t allow you to read and write MATLAB files. Distance between Points SciPy defines some useful functions for computing distances between sets of points. The function s c i p y . s p a t i a l . d i s t a n c e . p d i s t computes the distance between all pairs of points in a given set. A similar function (s c i p y . s p a t i a l . d i s t a n c e . c d i s t ) computes the distance between all pairs across two sets of points. i m p o r t n u m p y a s n p f r o m s c i p y . s p a t i a l . d i s t a n c e i m p o r t p d i s t , s q u a r e f o r m # C r e a t e t h e f o l l o w i n g a r r a y w h e r e e a c h r o w i s a p o i n t i n 2 D s # [ [ 0 1 ] # [ 1 0 ] # [ 2 0 ] ] x = n p . a r r a y ( [ [ 0 , 1 ] , [ 1 , 0 ] , [ 2 , 0 ] ] ) p r i n t x # C o m p u t e t h e E u c l i d e a n d i s t a n c e b e t w e e n a l l r o w s o f x . # d [ i , j ] i s t h e E u c l i d e a n d i s t a n c e b e t w e e n x [ i , : ] a n d x [ j , : # a n d d i s t h e f o l l o w i n g a r r a y : # [ [ 0 . 1 . 4 1 4 2 1 3 5 6 2 . 2 3 6 0 6 7 9 8 ] # [ 1 . 4 1 4 2 1 3 5 6 0 . 1 . ] # [ 2 . 2 3 6 0 6 7 9 8 1 . 0 . ] ] d = s q u a r e f o r m ( p d i s t ( x , ' e u c l i d e a n ' ) ) p r i n t d 36 / 42

Slide 37

Slide 37 text

 Matplotlib 37 / 42

Slide 38

Slide 38 text

Agenda 1. Python Review 2. Numpy 3. SciPy 4. Matplotlib Plotting Subplots Images 38 / 42

Slide 39

Slide 39 text

i m p o r t n u m p y a s n p i m p o r t m a t p l o t l i b . p y p l o t a s p l t # C o m p u t e t h e x a n d y c o o r d i n a t e s f o r p o i n t s o n a s i n e c u r v e x = n p . a r a n g e ( 0 , 3 * n p . p i , 0 . 1 ) y = n p . s i n ( x ) # P l o t t h e p o i n t s u s i n g m a t p l o t l i b p l t . p l o t ( x , y ) p l t . s h o w ( ) # Y o u m u s t c a l l p l t . s h o w ( ) t o m a k e g r a p h i c s a p p e a r . i m p o r t n u m p y a s n p i m p o r t m a t p l o t l i b . p y p l o t a s p l t # C o m p u t e t h e x a n d y c o o r d i n a t e s f o r p o i n t s o n s i n e a n d c o s i n e c u r v e s x = n p . a r a n g e ( 0 , 3 * n p . p i , 0 . 1 ) y _ s i n = n p . s i n ( x ) y _ c o s = n p . c o s ( x ) # P l o t t h e p o i n t s u s i n g m a t p l o t l i b p l t . p l o t ( x , y _ s i n ) p l t . p l o t ( x , y _ c o s ) p l t . x l a b e l ( ' x a x i s l a b e l ' ) p l t . y l a b e l ( ' y a x i s l a b e l ' ) p l t . t i t l e ( ' S i n e a n d C o s i n e ' ) p l t . l e g e n d ( [ ' S i n e ' , ' C o s i n e ' ] ) p l t . s h o w ( ) Matplotlib Matplotlib is a plotting library. In this section give a brief introduction to the m a t p l o t l i b . p y p l o t module, which provides a plotting system similar to that of MATLAB. Plotting The most important function in matplotlib is p l o t , which allows you to plot 2D data. With just a little bit of extra work we can easily plot multiple lines at once, and add a title, legend, and axis labels. 39 / 42

Slide 40

Slide 40 text

Matplotlib Subplots You can plot different things in the same figure using the subplot function. i m p o r t n u m p y a s n p i m p o r t m a t p l o t l i b . p y p l o t a s p l t # C o m p u t e t h e x a n d y c o o r d i n a t e s f o r p o i n t s o n s i n e a n d c o s i n x = n p . a r a n g e ( 0 , 3 * n p . p i , 0 . 1 ) y _ s i n = n p . s i n ( x ) y _ c o s = n p . c o s ( x ) # S e t u p a s u b p l o t g r i d t h a t h a s h e i g h t 2 a n d w i d t h 1 , # a n d s e t t h e f i r s t s u c h s u b p l o t a s a c t i v e . p l t . s u b p l o t ( 2 , 1 , 1 ) # M a k e t h e f i r s t p l o t p l t . p l o t ( x , y _ s i n ) p l t . t i t l e ( ' S i n e ' ) # S e t t h e s e c o n d s u b p l o t a s a c t i v e , a n d m a k e t h e s e c o n d p l o t . p l t . s u b p l o t ( 2 , 1 , 2 ) p l t . p l o t ( x , y _ c o s ) p l t . t i t l e ( ' C o s i n e ' ) # S h o w t h e f i g u r e . p l t . s h o w ( ) 40 / 42

Slide 41

Slide 41 text

i m p o r t n u m p y a s n p f r o m s c i p y . m i s c i m p o r t i m r e a d , i m r e s i z e i m p o r t m a t p l o t l i b . p y p l o t a s p l t i m g = i m r e a d ( ' a s s e t s / c a t . j p g ' ) i m g _ t i n t e d = i m g * [ 1 , 0 . 9 5 , 0 . 9 ] # S h o w t h e o r i g i n a l i m a g e p l t . s u b p l o t ( 1 , 2 , 1 ) p l t . i m s h o w ( i m g ) # S h o w t h e t i n t e d i m a g e p l t . s u b p l o t ( 1 , 2 , 2 ) # A s l i g h t g o t c h a w i t h i m s h o w i s t h a t i t m i g h t g i v e s t r a n g e r e s u l t s # i f p r e s e n t e d w i t h d a t a t h a t i s n o t u i n t 8 . T o w o r k a r o u n d t h i s , w e # e x p l i c i t l y c a s t t h e i m a g e t o u i n t 8 b e f o r e d i s p l a y i n g i t . p l t . i m s h o w ( n p . u i n t 8 ( i m g _ t i n t e d ) ) p l t . s h o w ( ) Matplotlib Images You can use the i m s h o w function to show images. 41 / 42

Slide 42

Slide 42 text

 END Eueung Mulyana http://eueung.github.io/EL5244/py-tut based on the material at CS231n@Stanford | Attribution-ShareAlike CC BY-SA 42 / 42