Introduction to Python
Luis Pedro [email protected]@luispedrocoelho
European Molecular Biology Laboratory
Lisbon Machine Learning School 2015
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (1 / 79)
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 (2 / 79)
Popularity
source: TIOBE (July 2015)Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (3 / 79)
Python Versions
Python VersionsThere 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, althoughit’s very old by now).
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (4 / 79)
Python Example
pr in t ” He l lo World”
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (5 / 79)
Task
AverageCompute the average of the following numbers:
1 102 73 224 145 17
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (6 / 79)
Python example
numbers = [ 10 , 7 , 22 , 14 , 17 ]
t o t a l = 0 . 0n = 0 . 0f o r va l in numbers :
t o t a l = t o t a l + va ln = n + 1
pr in t t o t a l / n
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (7 / 79)
“Python is executable pseudo-code.”—Python lore (often attributed to Bruce Eckel)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (8 / 79)
Programming Basics
numbers = [ 10 , 7 , 22 , 14 , 17 ]
t o t a l = 0 . 0n = 0 . 0f o r va l in numbers :
t o t a l = t o t a l + va ln = n + 1
pr in t t o t a l / n
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (9 / 79)
Python Types
Basic TypesNumbers (integers and floating point)StringsLists and tuplesDictionaries
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (10 / 79)
Python Types: Numbers I: Integers
A = 1B = 2C = 3pr in t A + B*C
Outputs 7.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (11 / 79)
Python Types: Numbers II: Floats
A = 1 . 2B = 2 . 4C = 3 . 6p r in t A + B*C
Outputs 9.84.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (12 / 79)
Python Types: Numbers III: Integers & Floats
A = 2B = 2 . 5C = 4 . 4p r in t A + B*C
Outputs 22.0.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (13 / 79)
Composite Assignment
t o t a l = t o t a l + n
Can be abbreviated ast o t a l += n
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (14 / 79)
Python Types: Strings
f i r s t = ’ John ’l a s t = ”Doe”f u l l = f i r s t + ” ” + l a s t
p r i n t f u l l
Outputs John Doe.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (15 / 79)
Python Types: Strings
f i r s t = ’ John ’l a s t = ”Doe”f u l l = f i r s t + ” ” + l a s t
p r i n t f u l l
Outputs John Doe.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (15 / 79)
Python Types: String Rules
What is a String LiteralShort string literals are delimited by (”) or (’).Short string literals are one line only.Special characters are input using escape sequences.(\n for newline,…)
mul t ip 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 (16 / 79)
String formatting
Old Stylepr in t ’Username : %s ’ % ’ l u i s p ed r o ’p r i n t ’Username : %s (%s l o g i n s ) ’ % ( ’ l u i s p ed r o ’ , 15 )
New Stylepr in t ’Username : {0} ’ . format ( ’ l u i s p ed r o ’ )p r i n t ’Username : {0} ( {1} l o g i n s ) ’ . format ( ’ l u i s p ed r o ’ , 15 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (17 / 79)
Python Types: Long Strings
We can input a long string using triple quotes (”’ or ”””) as delimiters.long = ’ ’ ’ Te l l me , i s l oveS t i l l a popular sugge s t i onOr merely an ob so l e t e a r t ?
Forgive me, f o r asking ,This s imple quest ion ,I am un fami l i a r with h i s heart . ’ ’ ’
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (18 / 79)
Python Types: Lists
cour s e s = [ ’ PfS ’ , ’ P o l i t i c a l Phi losophy ’ ]
p r i n t ”The f i r s t course i s ” , c ou r s e s [ 0 ]p r i n t ”The second course i s ” , cour s e s [ 1 ]
Notice that list indices start at 0!
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (19 / 79)
Python Types: Lists
mixed = [ ’Banana ’ , 100 , [ ’ Another ’ , ’ L i s t ’ ] , [ ] ]p r i n t l en (mixed )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (20 / 79)
Python Types: Lists
f r u i t s = [ ’Banana ’ , ’ Apple ’ , ’ Orange ’ ]f r u i t s . s o r t ( )p r i n t f r u i t s
Prints [’Apple’, ’Banana’, ’Orange’]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (21 / 79)
Python Types: Dictionaries
emai l s = { ’ Luis ’ : ’ lpc@cmu . edu ’ ,’Mark ’ : ’mark@cmu . edu ’ }
p r i n t ” Luis ’ s emai l i s ” , emai l s [ ’ Luis ’ ]
ema i l s [ ’ Rita ’ ] = ’ rita@cmu . edu ’
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (22 / 79)
Python Control Structures
student = ’ Rita ’average = gradeavg ( student )i f average > 0 . 7 :
p r i n t student , ’ passed ! ’p r i n t ’ Congratu lat ions ! ! ’
e l s e :p r i n t student , ’ f a i l e d . Sorry . ’
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (23 / 79)
Python Blocks
Unlike almost all other modern programming languages,Python uses indentation to delimit blocks!i f <cond i t i on>:
<statement 1><statement 2><statement 3>
<statement a f t e r i f>
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (24 / 79)
Convention1 Use 4 spaces to indent.2 Other things will work, but confuse people.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (25 / 79)
Conditionals
Examplesx == yx != yx < yx < y < zx in lstx not in lst
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (26 / 79)
Nested Blocks
i f <cond i t i on 1>:<do something>i f <cond i t i on 2>:
<nested block>e l s e :
<nested e l s e b lock>e l i f <cond i t i on 1b>:
<do something>
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (27 / 79)
For loop
s tudents = [ ’ Luis ’ , ’ Rita ’ , ’ Sabah ’ , ’Mark ’ ]f o r s t in s tudents :
p r i n t s t
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (28 / 79)
While Loop
whi le <cond i t i on>:<statement1><statement2>
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (29 / 79)
Other Loopy Stuff
f o r i in range ( 5 ) :p r i n t i
prints
01234
This is because range(5) is the list [0,1,2,3,4].For looping, you can use xrange(5) which works the same withoutbuilding a list.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (30 / 79)
Break
r i t a_en r o l l e d = Falsef o r s t in s tudents :
i f s t == ’ Rita ’ :r i t a_en r o l l e d = Truebreak
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (31 / 79)
Conditions & BooleansBooleansJust two values: True and False.Comparisons return booleans (e.g., x < 2)
ConditionsWhen evaluating a condition, the condition is converted to aboolean:Many things are converted to False:
1 [] (the empty list)2 {} (the empty dictionary)3 ”” (the empty string)4 0 or 0.0 (the value zero)5 …
Everything else is True or not convertible to boolean.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (32 / 79)
Conditions Example
A = [ ]B = [ 1 , 2 ]C = 2D = 0
i f A:p r i n t ’A i s t rue ’
i f B:p r i n t ’B i s t rue ’
i f C:p r i n t ’C i s t rue ’
i f D:p r i n t ’D i s t rue ’
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (33 / 79)
Numbers
Two Types of Numbers1 Integers2 Floating-point
Operations1 Unary Minus: -x2 Addition: x + y3 Subtraction: x - y4 Multiplication: x * y5 Exponentiation: x ** y
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (34 / 79)
Division
DivisionWhat is 9 divided by 3?What is 10 divided by 3?
Two types of division1 Integer division: x // y2 Floating-point division: x / float(y)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (35 / 79)
Division
DivisionWhat is 9 divided by 3?What is 10 divided by 3?
Two types of division1 Integer division: x // y2 Floating-point division: x / float(y)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (35 / 79)
Functions
de f double ( x ) :’ ’ ’y = double ( x )
Returns the double o f x’ ’ ’r e turn 2*x
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (36 / 79)
Functions
A=4pr in t double (A)p r in t double ( 2 . 3 )p r i n t double ( double (A) )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (37 / 79)
Functions II
de f g r e e t (name , g r e e t i n g=’ He l lo ’ ) :p r i n t g ree t ing , name
g r e e t ( ’Mario ’ )g r e e t ( ’Mario ’ , ’Goodbye ’ )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (38 / 79)
Defining a class
(This is not used in the LxMLS code, but presented here forcompleteness).
Boat ClassWe define a Boat class, with two values, latitude & longitude, and fivemethods:
1 move_north, move_south, move_east, move_west2 distance
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (39 / 79)
Defining a class
Defining a methodc l a s s Boat ( ob j e c t ) :
de f __init__( s e l f , l a t=0 , long=0 ) :s e l f . l a t i t u d e = l a ts e l f . l ong i tude = long
de f move_north ( s e l f , d l a t ) :s e l f . l a t i t u d e += dla t
Calling a Methodt i t a n i c = Boat ( )
t i t a n i c . move_north ( 12 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (40 / 79)
Defining classes
c l a s s Boat ( ob j e c t ) :de f __init__( s e l f , l a t=0 , long=0 ) :
s e l f . l a t i t u d e = l a ts e l f . l ong i tude = long
de f move_north ( s e l f , d l a t ) :s e l f . l a t i t u d e += dla 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 (41 / 79)
ScientificBoat
c l a s s S c i e n t i f i cBo a t (Boat ) :de f takeSample ( s e l f ) :
. . .
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (42 / 79)
Saddle Point
Before we (1) move on to numpy (numeric computation) and (2) ademo session; does anyone have any questions?
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (43 / 79)
Numeric Python: Numpy
Numpy
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (44 / 79)
Unlike R/MATLAB, Python relies on libraries for numerics
No builtin types for numeric computationHowever, packages like numpy are quasi-standard
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (45 / 79)
Basic Type
numpy.array or numpy.ndarray.
Multi-dimensional array of numbers.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (46 / 79)
numpy example
import numpy as npA = np . array ( [
[ 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 ]
012
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (47 / 79)
numpy example
import numpy as npA = np . array ( [
[ 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 ]
012
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (47 / 79)
Why Numpy?
Why do we need numpy?import numpy as npl s t = [ 0 . , 1 . , 2 . , 3 . ]a r r = np . array ( [ 0 . , 1 . , 2 . , 3 . ] )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (48 / 79)
A Python List of Numbers
float
0.0
float
1.0
float
2.0
float
3.0
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (49 / 79)
A Numpy Array of Numbers
float 0.0 1.0 2.0 3.0
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (50 / 79)
Numpy Arrays
AdvantagesA block of memoryLess memory consumptionFasterWork with (or write) code in other languages (C, C++, Fortran…)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (51 / 79)
Matrix-vector multiplication
A = np . array ( [[ 1 , 0 , 0 ] ,[ 0 , 1 , 0 ] ,[ 0 , 0 , 1 ] ] )
v = np . array ( [ 1 , 5 , 2 ] )
p r i n t np . dot (A, v )
[1 5 2]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (52 / 79)
Matrix-vector multiplication
A = np . array ( [[ 1 , 0 , 0 ] ,[ 0 , 1 , 0 ] ,[ 0 , 0 , 1 ] ] )
v = np . array ( [ 1 , 5 , 2 ] )
p r i n t np . dot (A, v )
[1 5 2]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (52 / 79)
Matrix-Matrix and Dot Products
(1 11 −1
)(0 11 0
)=
(1 1−1 1
)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (53 / 79)
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⃗ = x⃗Ty⃗.
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (54 / 79)
v0 = np . array ( [ 1 , 2 ] )v1 = np . array ( [ 3 , - 1 ] )
r = 0 . 0f o r i in xrange ( 2 ) :
r += v0 [ i ] *v1 [ i ]p r i n t r
p r i n t np . dot ( v0 , v1 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (55 / 79)
A0 = np . array ( [ [ 1 , 2 ] , [ 2 , 3 ] ] )A1 = np . array ( [ [ 0 , 1 ] , [ 1 , 0 ] ] )
p r i n t np . dot (A0 ,A1) (0 22 3
)(0 11 0
)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (56 / 79)
Some Array Properties
import numpy as npA = np . array ( [
[ 0 , 1 , 2 ] ,[ 2 , 3 , 4 ] ,[ 4 , 5 , 6 ] ,[ 6 , 7 , 8 ] ] )
p r i n t A. shapep r in t A. s i z e
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (57 / 79)
Some Array Functions
. . .p r i n t A.max( )p r i n t A. min ( )
max(): maximummin(): minimumptp(): spread (max - min)sum(): sumstd(): standard deviation…
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (58 / 79)
Other Functions
np.expnp.sin…
All of these work element-wise!
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (59 / 79)
Arithmetic Operations
import numpy as npA = np . array ( [ 0 , 1 , 2 , 3 ] )B = np . array ( [ 1 , 1 , 2 , 2 ] )
p r i n t A + Bpr in t A * Bpr in t A / B
[1 2 4 5][0 1 4 6][0 1 1 1]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (60 / 79)
Arithmetic Operations
import numpy as npA = np . array ( [ 0 , 1 , 2 , 3 ] )B = np . array ( [ 1 , 1 , 2 , 2 ] )
p r i n t A + Bpr in t A * Bpr in t A / B
[1 2 4 5][0 1 4 6][0 1 1 1]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (60 / 79)
Numpy Dtypes
All members of an array have the same typeEither integer or floating pointDefined when you first create the array
A = np . array ( [ 0 , 1 , 2 ] )B = np . array ( [ 0 . 5 , 1 . 1 , 2 . 1 ] )
A *= 2 . 5B *= 2 . 5
p r in t Apr in t B
[0 2 5][ 1.25 2.75 5.25]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (61 / 79)
A = np . array ( [ 0 , 1 , 2 ] , dtype=np . in t16 )B = np . array ( [ 0 , 1 , 2 ] , dtype=np . f l o a t 3 2 )
np.int8, np.int16, np.int32np.uint8, np.uint16, np.uint32np.float32, np.float64np.bool
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (62 / 79)
Object Construction
import numpy as npA = np . array ( [ 0 , 1 , 1 ] , np . f l o a t 3 2 )A = np . array ( [ 0 , 1 , 1 ] , f l o a t )A = np . array ( [ 0 , 1 , 1 ] , bool )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (63 / 79)
Reduction
A = np . array ( [[ 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 (64 / 79)
Slicing
import numpy as npA = np . array ( [
[ 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 ] . shapep r in 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 (65 / 79)
Slicing
import numpy as npA = np . array ( [
[ 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 ] . shapep r in 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 (65 / 79)
Slices Share Memory!
import numpy as npA = np . array ( [
[ 0 , 1 , 2 ] ,[ 2 , 3 , 4 ] ,[ 4 , 5 , 6 ] ,[ 6 , 7 , 8 ] ] )
B = A[ 0 ]B[ 0 ] = - 1p r in t A[ 0 , 0 ]
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (66 / 79)
Pass is By Reference
de f double (A) :A *= 2
A = np . arange ( 20 )double (A)
A = np . arange ( 20 )B = A. copy ( )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (67 / 79)
Pass is By Reference
de f double (A) :A *= 2
A = np . arange ( 20 )double (A)
A = np . arange ( 20 )B = A. copy ( )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (67 / 79)
Logical Arrays
A = np . array ( [ - 1 , 0 , 1 , 2 , - 2 , 3 , 4 , - 2 ] )p r i n t (A > 0 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (68 / 79)
Logical Arrays II
A = np . array ( [ - 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 (69 / 79)
Logical Indexing
A[A < 0 ] = 0
orA *= (A > 0 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (70 / 79)
Logical Indexing
pr in 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 (71 / 79)
Some Helper Functions
Constructing ArraysA = np . z e ro s ( ( 10 , 10 ) , dtype=np . in t8 )B = np . ones ( 10 )C = np . arange ( 100 ) . reshape ( ( 10 , 10 ) ). . .
Multiple Dimensionsimg = np . z e r o s ( ( 1024 , 1024 , 3 ) , dype=np . u int8 )
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (72 / 79)
Documentation
http://docs.scipy.org/doc/
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (73 / 79)
Last Section
Matplotlib & Spyder
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (74 / 79)
Matplotlib
Matplotlib is a plotting library.Very flexible.Very active project.Ugly plots by default(subjective, but the project is trying to change;also, it’s possible to change styling).
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (75 / 79)
Example I
import numpy as npimport matp lo t l i b . pyplot as p l tX = np . l i n s p a c e ( - 4 , 4 , 1000 )p l t . p l o t (X, X**2*np . cos (X**2 ) )p l t . s a v e f i g ( ’ s imple . pdf ’ )
y = x2 cos(x2)
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (76 / 79)
Example I
4 3 2 1 0 1 2 3 420
15
10
5
0
5
10
15
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (77 / 79)
Resources
Numpy+scipy docs: http://docs.scipy.orgMatplotlib: http://matplotlib.sf.netPython docs: http://docs.python.org
These slides are available at http://luispedro.org/talks/2015I’m available at [email protected]@luispedrocoelho on twitter
Luis Pedro Coelho (@luispedrocoelho) ⋆ Introduction to Python ⋆ #LxMLS (78 / 79)
Top Related