Introduction to Python Luis Pedro Coelho [email protected] @luispedrocoelho European Molecular Biology Laboratory Lisbon Machine Learning School 2014 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (1 / 78) Today’s Lecture in Context Today: basic introduction to Python & Numpy During LxMLS, you will implement algorithms “by hand” Tomorrow: scikit-learn by Andreas Mueller Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (2 / 78) Python Language History Python was started in the late 80’s. It was intended to be both easy to teach and industrial strength. It is (has always been) open-source. It has become one of the most widely used languages (top 10). Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (3 / 78) Popularity Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (4 / 78) Popularity Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (5 / 78) Python Versions Python Versions There are two major versions, currently: 2.7 and 3.4. We are going to be using 2.7 (but 2.6 should be OK too). Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (6 / 78) Python Example p r i n t ” H e l l o World ” Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (7 / 78) Task Average Compute the average of the following numbers: 1. 10 . 7 3. 22 2 . 14 5. 17 4 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (8 / 78) Python example numbers = [ 10 , 7 , 22 , 14 , 17 ] total = 0 . 0 n = 0.0 f o r v a l i n numbers : total = total + val n = n + 1 print total / n Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (9 / 78) “Python is executable pseudo-code.” —Python lore (often attributed to Bruce Eckel) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (10 / 78) Programming Basics numbers = [ 10 , 7 , 22 , 14 , 17 ] total = 0 . 0 n = 0.0 f o r v a l i n numbers : total = total + val n = n + 1 print total / n Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (11 / 78) Python Types Basic Types Numbers (integers and floating point) Strings Lists and tuples Dictionaries Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (12 / 78) Python Types: Numbers I: Integers A = 1 B = 2 C = 3 p r i n t A + B*C Outputs 7. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (13 / 78) Python Types: Numbers II: Floats A = 1.2 B = 2.4 C = 3.6 p r i n t A + B*C Outputs 9.84. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (14 / 78) Python Types: Numbers III: Integers & Floats A = 2 B = 2.5 C = 4.4 p r i n t A + B*C Outputs 22.0. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (15 / 78) Composite Assignment total = total + n Can be abbreviated as t o t a l += n Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (16 / 78) Python Types: Strings f i r s t = ’ John ’ l a s t = ”Doe” full = first + ” ” + last print f u l l Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (17 / 78) Python Types: Strings f i r s t = ’ John ’ l a s t = ”Doe” full = first + ” ” + last print f u l l Outputs John Doe. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (17 / 78) Python Types: String Rules What is a String Literal Short string literals are delimited by (”) or (’). Short string literals are one line only. Special characters are input using escape sequences. (\n for newline,…) m u l t i p l e = ’ He : May I ?\ nShe : No , you may not . ’ a l t e r n a t i v e = ”He : May I ?\ nShe : No , you may not . ” Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (18 / 78) Python Types: Long Strings We can input a long string using triple quotes (”’ or ”””) as delimiters. l o n g = ’ ’ ’ T e l l me , i s l o v e S t i l l a popular suggestion Or merely an o b s o l e t e a r t ? F o r g i v e me , f o r a s k i n g , This s i m p l e q u e s t i o n , I am u n f a m i l i a r with h i s h e a r t . ’ ’ ’ Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (19 / 78) Python Types: Lists c o u r s e s = [ ’ PfS ’ , ’ P o l i t i c a l P h i l o s o p h y ’ ] p r i n t ”The t h e f i r s t c o u r s e i s ” , c o u r s e s [ 0 ] p r i n t ”The s e c o n d c o u r s e i s ” , c o u r s e s [ 1 ] Notice that list indices start at 0! Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (20 / 78) Python Types: Lists mixed = [ ’ Banana ’ , 100 , [ ’ Another ’ , ’ L i s t ’ ] , [ ] ] p r i n t l e n ( mixed ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (21 / 78) Python Types: Lists f r u i t s = [ ’ Banana ’ , ’ Apple ’ , ’ Orange ’ ] fruits . sort ( ) print f r u i t s Prints [’Apple’, ’Banana’, ’Orange’] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (22 / 78) Python Types: Dictionaries e m a i l s = { ’ L u i s ’ : ’ lpc@cmu . edu ’ , ’ Mark ’ : ’ mark@cmu . edu ’ } print ” Luis ’ s email i s ” , emails [ ’ Luis ’ ] e m a i l s [ ’ Rita ’ ] = ’ rita@cmu . edu ’ Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (23 / 78) Python Control Structures s t u d e n t = ’ Rita ’ a v e r a g e = gradeavg ( s t u d e n t ) i f average > 0 . 7 : p r i n t student , ’ passed ! ’ print ’ Congratulations ! ! ’ else : p r i n t student , ’ f a i l e d . Sorry . ’ Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (24 / 78) Python Blocks Unlike almost all other modern programming languages, Python uses indentation to delimit blocks! i f <c o n d i t i o n>: statement 1 statement 2 statement 3 next s t a t e m e n t Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (25 / 78) Convention . Use 4 spaces to indent. 2. Other things will work, but confuse people. 1 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (26 / 78) Conditionals Examples x == y x != y x<y x<y<z x in lst x not in lst Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (27 / 78) Nested Blocks i f <c o n d i t i o n 1>: do something i f c o n d i t i o n 2>: nested block else : nested e l s e block e l i f <c o n d i t i o n 1b>: do something Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (28 / 78) For loop s t u d e n t s = [ ’ L u i s ’ , ’ Rita ’ , ’ Sabah ’ , ’ Mark ’ ] for st in students : print st Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (29 / 78) While Loop w h i l e <c o n d i t i o n>: statement1 statement2 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (30 / 78) Other Loopy Stuff f o r i in range ( 5 ) : print i prints 0 1 2 3 4 This is because range(5) is the list [0,1,2,3,4]. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (31 / 78) Break rita_enrolled = False for st in students : i f s t == ’ Rita ’ : r i t a _ e n r o l l e d = True break Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (32 / 78) Conditions & Booleans Booleans Just two values: True and False. Comparisons return booleans (e.g., x < 2) Conditions When evaluating a condition, the condition is converted to a boolean: Many things are converted to False: . . 3. 4. 5. 1 2 [] (the empty list) {} (the empty dictionary) ”” (the empty string) 0 or 0.0 (the value zero) … Everything else is True or not convertible to boolean. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (33 / 78) Conditions Example A B C D = = = = [] [1,2] 2 0 i f A: print i f B: print i f C: print i f D: print ’A i s t r u e ’ ’B i s t r u e ’ ’C i s t r u e ’ ’D i s t r u e ’ Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (34 / 78) Numbers Two Types of Numbers . Integers 2. Floating-point 1 Operations . Unary Minus: -x 2. Addition: x + y 3. Subtraction: x - y 1 . Multiplication: x * y 5. Exponentiation: x ** y 4 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (35 / 78) Division Division What is 9 divided by 3? What is 10 divided by 3? Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (36 / 78) Division Division What is 9 divided by 3? What is 10 divided by 3? Two types of division . Integer division: x // y 2. Floating-point division: x / float(y) 1 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (36 / 78) Functions def double ( x ) : ’’’ y = double ( x ) Returns t h e d o u b l e o f x ’’’ r e t u r n 2 *x Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (37 / 78) Functions A=4 p r i n t d o u b l e (A) p r i n t double ( 2 . 3 ) p r i n t d o u b l e ( d o u b l e (A) ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (38 / 78) Functions II d e f g r e e t ( name , g r e e t i n g= ’ H e l l o ’ ) : p r i n t g r e e t i n g , name g r e e t ( ’ Mario ’ ) g r e e t ( ’ Mario ’ , ’ Goodbye ’ ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (39 / 78) Defining a class Boat Class We define a Boat class, with two values, latitude & longitude, and five methods: 1. move_north, move_south, move_east, move_west . distance 2 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (40 / 78) Defining & Calling Methods Defining a method c l a s s Boat ( o b j e c t ) : d e f __init__ ( s e l f , l a t=0 , l o n g=0 ) : self . latitude = lat s e l f . longitude = long d e f move_north ( s e l f , d l a t ) : s e l f . l a t i t u d e += d l a t Calling a Method o b j = Boat ( ) o b j . method ( arg1 , a r g 2 ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (41 / 78) Defining classes c l a s s Boat ( o b j e c t ) : d e f __init__ ( s e l f , l a t=0 , l o n g=0 ) : self . latitude = lat s e l f . longitude = long d e f move_north ( s e l f , d l a t ) : s e l f . l a t i t u d e += d l a t __init__: special name (constructor) self: the object itself (this in many other languages) Instance variables are defined at first use Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (42 / 78) ScientificBoat class ScientificBoat ( object ) : d e f __init_ _( s e l f , l a t=0 , l o n g=0 ) : self . latitude = lat s e l f . longitude = long d e f move_north ( s e l f , d l a t ) : ... Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (43 / 78) Numeric Python: Numpy Numpy Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (44 / 78) Basic Type numpy.array or numpy.ndarray. Multi-dimensional array of numbers. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (45 / 78) numpy example import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) p r i n t A[ 0 , 0 ] p r i n t A[ 0 , 1 ] p r i n t A[ 1 , 0 ] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (46 / 78) numpy example import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) p r i n t A[ 0 , 0 ] p r i n t A[ 0 , 1 ] p r i n t A[ 1 , 0 ] 0 1 2 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (46 / 78) Why Numpy? Why do we need numpy? import numpy a s np lst = [0. ,1. ,2. ,3. ] a r r = np . a r r a y ( [ 0 . , 1 . , 2 . , 3 . ] ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (47 / 78) A Python List of Numbers . float float float float 0.0 1.0 2.0 3.0 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (48 / 78) A Numpy Array of Numbers float 0.0 . Luis Pedro Coelho (@luispedrocoelho) ⋆ 1.0 2.0 3.0 Introduction to Python ⋆ #LxMLS (49 / 78) Numpy Arrays Advantages Less memory consumption Faster Work with (or write) code in other languages (C, C++, Fortran…) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (50 / 78) Matrix-vector multiplication A = np . a r r a y ( [ [1, 0, 0] , [0, 1, 0] , [0, 0, 1] ] ) v = np . a r r a y ( [ 1 , 5 , 2 ] ) p r i n t np . dot (A, v ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (51 / 78) Matrix-vector multiplication A = np . a r r a y ( [ [1, 0, 0] , [0, 1, 0] , [0, 0, 1] ] ) v = np . a r r a y ( [ 1 , 5 , 2 ] ) p r i n t np . dot (A, v ) [1 5 2] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (51 / 78) Matrix-Matrix and Dot Products ( 1 1 1 −1 Luis Pedro Coelho (@luispedrocoelho) )( ⋆ 0 1 1 0 ) ( = 1 1 −1 1 Introduction to Python ) ⋆ #LxMLS (52 / 78) Matrix-Matrix and Dot Products ( 1 2 ) ( · 3 −1 ) = 1 · 3 + (−1) · 2 = 1. This is a vector inner product (aka dot product) < ⃗x, ⃗y >= ⃗x · ⃗y = ⃗xT⃗y. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (53 / 78) v0 = np . a r r a y ( [ 1 , 2 ] ) v1 = np . a r r a y ( [ 3 , - 1 ] ) r = 0.0 f o r i i n xrange ( 2 ) : r += v0 [ i ] * v1 [ i ] print r p r i n t np . dot ( v0 , v1 ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (54 / 78) A0 = np . a r r a y ( [ [ 1 , 2 ] , [ 2 , 3 ] ] ) A1 = np . a r r a y ( [ [ 0 , 1 ] , [ 1 , 0 ] ] ) p r i n t np . dot (A0 , A1) Luis Pedro Coelho (@luispedrocoelho) ( 0 2 2 3 ⋆ )( 0 1 1 0 ) Introduction to Python ⋆ #LxMLS (55 / 78) Some Array Properties import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) p r i n t A. shape p r i n t A. s i z e Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (56 / 78) Some Array Functions ... p r i n t A. max( ) p r i n t A. min ( ) max(): maximum min(): minimum ptp(): spread (max - min) sum(): sum std(): standard deviation … Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (57 / 78) Other Functions np.exp np.sin … All of these work element-wise! Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (58 / 78) Arithmetic Operations import numpy a s np A = np . a r r a y ( [ 0 , 1 , 2 , 3 ] ) B = np . a r r a y ( [ 1 , 1 , 2 , 2 ] ) print A + B print A * B print A / B Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (59 / 78) Arithmetic Operations import numpy a s np A = np . a r r a y ( [ 0 , 1 , 2 , 3 ] ) B = np . a r r a y ( [ 1 , 1 , 2 , 2 ] ) print A + B print A * B print A / B [1 2 4 5] [0 1 4 6] [0 1 1 1] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (59 / 78) Numpy Dtypes All members of an array have the same type Either integer or floating point Defined when you first create the array A = np . a r r a y ( [ 0 , 1 , 2 ] ) B = np . a r r a y ( [ 0 . 5 , 1 . 1 , 2 . 1 ] ) A *= 2 . 5 B *= 2 . 5 print A print B [0 2 5] [ 1.25 2.75 5.25] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (60 / 78) A = np . a r r a y ( [ 0 , 1 , 2 ] , dtype=np . i n t 1 6 ) B = np . a r r a y ( [ 0 , 1 , 2 ] , dtype=np . f l o a t 3 2 ) np.int8, np.int16, np.int32 np.uint8, np.uint16, np.uint32 np.float32, np.float64 np.bool Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (61 / 78) Object Construction import numpy a s np A = np . a r r a y ( [ 0 , 1 , 1 ] , np . f l o a t 3 2 ) A = np . a r r a y ( [ 0 , 1 , 1 ] , f l o a t ) A = np . a r r a y ( [ 0 , 1 , 1 ] , b o o l ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (62 / 78) Reduction A = np . a r r a y ( [ [0,0,1] , [1,2,3] , [2,4,2] , [1,0,1] ] ) p r i n t A. max( 0 ) p r i n t A. max( 1 ) p r i n t A. max( ) prints [2,4,3] [1,3,4,1] 4 The same is true for many other functions. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (63 / 78) Slicing import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) p r i n t A[ 0 ] p r i n t A[ 0 ] . shape p r i n t A[ 1 ] p r i n t A[ : , 2 ] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (64 / 78) Slicing import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) p r i n t A[ 0 ] p r i n t A[ 0 ] . shape p r i n t A[ 1 ] p r i n t A[ : , 2 ] [0, 1, 2] (3,) [2, 3, 4] [2, 4, 6, 8] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (64 / 78) Slices Share Memory! import numpy a s np A = np . a r r a y ( [ [0,1,2] , [2,3,4] , [4,5,6] , [6,7,8] ] ) B = A[ 0 ] B[ 0 ] = - 1 p r i n t A[ 0 , 0 ] Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (65 / 78) Pass is By Reference d e f d o u b l e (A) : A *= 2 A = np . a r a n g e ( 20 ) d o u b l e (A) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (66 / 78) Pass is By Reference d e f d o u b l e (A) : A *= 2 A = np . a r a n g e ( 20 ) d o u b l e (A) A = np . a r a n g e ( 20 ) B = A. copy ( ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (66 / 78) Logical Arrays A = np . a r r a y ( [ - 1 , 0 , 1 , 2 , - 2 , 3 , 4 , - 2 ] ) p r i n t (A > 0 ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (67 / 78) Logical Arrays II A = np . a r r a y ( [ - 1 , 0 , 1 , 2 , - 2 , 3 , 4 , - 2 ] ) p r i n t ( (A > 0 ) & (A < 3 ) ) . mean ( ) What does this do? Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (68 / 78) Logical Indexing A[A < 0 ] = 0 or A *= (A > 0 ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (69 / 78) Logical Indexing p r i n t ’ Mean o f p o s i t i v e s ’ , A[A > 0 ] . mean ( ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (70 / 78) Some Helper Functions Constructing Arrays A = np . z e r o s ( ( 10 , 10 ) , dtype=np . i n t 8 ) B = np . o n e s ( 10 ) C = np . a r a n g e ( 100 ) . r e s h a p e ( ( 10 , 10 ) ) ... Multiple Dimensions img = np . z e r o s ( ( 1024 , 1024 , 3 ) , dype=np . u i n t 8 ) Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (71 / 78) Documentation http://docs.scipy.org/doc/ Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (72 / 78) Last Section Matplotlib & Spyder Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (73 / 78) Matplotlib Matplotlib is a plotting library. Very flexible. Very active project. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (74 / 78) Example I import numpy a s np import m a t p l o t l i b . p y p l o t a s p l t X = np . l i n s p a c e ( - 4 , 4 , 1000 ) p l t . p l o t (X, X* * 2*np . c o s (X* * 2 ) ) p l t . s a v e f i g ( ’ s i m p l e . pdf ’ ) ( ) y = x2 cos x2 Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (75 / 78) Example I 15 10 5 0 5 10 15 20 4 3 Luis Pedro Coelho (@luispedrocoelho) 1 2 ⋆ 0 1 2 Introduction to Python 3 ⋆ 4 #LxMLS (76 / 78) Resources Numpy+scipy docs: http://docs.scipy.org Matplotlib: http://matplotlib.sf.net Python docs: http://docs.python.org These slides are available at http://luispedro.org/talks/2014 I’m available at [email protected] @luispedrocoelho on twitter Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (77 / 78) Thank you. Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (78 / 78)