Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Introduction to Python
Luis Pedro Coelho
Institute for Molecular Medicine (Lisbon)
Lisbon Machine Learning School II
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (1 / 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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (2 / 78)
Python Versions
Python VersionsThere are two major versions, currently: 2.7 and 3.2.We are going to be using 2.7 (but 2.6 should be OK too).
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (3 / 78)
Python Example
pr in t ” He l lo World”
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (4 / 78)
Task
AverageCompute the average of the following numbers:
...1 10
...2 7
...3 22
...4 14
...5 17
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (5 / 78)
Python example
numbers = [ 10 , 7 , 22 , 14 , 17 ]
sum = 0 . 0n = 0 . 0f o r va l in numbers :
sum = sum + valn = n + 1
return sum / n
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (6 / 78)
“Python is executable pseudo-code.”—Python lore (often attributed to Bruce Eckel)
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (7 / 78)
Programming Basics
numbers = [ 10 , 7 , 22 , 14 , 17 ]
sum = 0 . 0n = 0 . 0f o r va l in numbers :
sum = sum + valn = n + 1
return sum / n
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (8 / 78)
Python Types
Basic TypesNumbers (integers and floating point)StringsLists and tuplesDictionaries
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (9 / 78)
Python Types: Numbers I: Integers
A = 1B = 2C = 3pr in t A+B*C
Outputs 7.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (10 / 78)
Python Types: Numbers II: Floats
A = 1 . 2B = 2 . 4C = 3 . 6p r in t A + B*C
Outputs 9.84.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (11 / 78)
Python Types: Numbers III: Integers & Floats
A = 2B = 2 . 5C = 4 . 4p r in t A + B*C
Outputs 22.0.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (12 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (13 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (14 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (14 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (15 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (16 / 78)
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 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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (17 / 78)
Python Types: Lists
mixed = [ ’Banana ’ , 100 , [ ’ Another ’ , ’ L i s t ’ ] , [ ] ]p r i n t l en (mixed )
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (18 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (19 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (20 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (21 / 78)
Python Blocks
Unlike almost all other modern programming languages,Python uses indentation to delimit blocks!i f <cond i t i on>:
statement 1statement 2statement 3
next statement
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (22 / 78)
Convention...1 Use 4 spaces to indent....2 Other things will work, but confuse people.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (23 / 78)
Conditionals
Examplesx == yx != yx < yx < y < zx in lstx not in lst
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (24 / 78)
Nested Blocks
i f <cond i t i on 1>:do somethingi f cond i t i on 2>:
nested blocke l s e :
nested e l s e b locke l i f <cond i t i on 1b>:
do something
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (25 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (26 / 78)
While Loop
whi le <cond i t i on>:statement1statement2
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (27 / 78)
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].
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (28 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (29 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (30 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (31 / 78)
Numbers
Two Types of Numbers...1 Integers...2 Floating-point
Operations...1 Unary Minus: -x...2 Addition: x + y...3 Subtraction: x - y...4 Multiplication: x * y...5 Exponentiation: x ** y
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (32 / 78)
Division
DivisionWhat is 9 divided by 3?What is 10 divided by 3?
Two types of division...1 Integer division: x // y
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (33 / 78)
Division
DivisionWhat is 9 divided by 3?What is 10 divided by 3?
Two types of division...1 Integer division: x // y
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (33 / 78)
Functions
de f double ( x ) :’ ’ ’y = double ( x )
Returns the double o f x’ ’ ’r e turn 2*x
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (34 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (35 / 78)
Linear Algebra Recap
VectorsMatrices (operators)Multiplication of vectors & Matrices
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (36 / 78)
Linear Algebra Recap
Vectors[0, 1.2,−1.2, 4] ∈ R4
Matrices (operators) 1 0 00 1 00 0 1
Multiplication of vectors & Matrices
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (36 / 78)
Vector Space
Addition operation
[1, 2] + [2, 3] = [3, 5]
Multiplication by a scalar
4 · [2, 0, 1] = [8, 0, 4]
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (37 / 78)
Matrices (Linear Operators)
I =
1 0 00 1 00 0 1
This is the identity matrix
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (38 / 78)
Matrix as an Operator
A00 A01 A02
A10 A11 A12
A20 A21 A22
x0x1x2
=
x0A00 + x1A01 + x2A02
x0A10 + x1A11 + x2A12
x0A20 + x1A21 + x2A22
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (39 / 78)
Matrix as an Operator
1 0 00 1 00 0 1
x0x1x2
=
x0x1x2
This is identity.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (40 / 78)
Matrix Transpose
A00 A01 A02
A10 A11 A12
A20 A21 A22
T
=
A00 A10 A20
A01 A11 A21
A02 A12 A22
1 2 34 5 67 8 9
T
=
1 4 72 5 83 6 9
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (41 / 78)
Matrix Transpose
A00 A01 A02
A10 A11 A12
A20 A21 A22
T
=
A00 A10 A20
A01 A11 A21
A02 A12 A22
1 2 3
4 5 67 8 9
T
=
1 4 72 5 83 6 9
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (41 / 78)
Numeric Python: Numpy
Numpy
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (42 / 78)
Basic Type
numpy.array or numpy.ndarray.
Multi-dimensional array of numbers.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (43 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (44 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (44 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (45 / 78)
A Python List of Numbers
..
float
.
0.0
.
float
.
1.0
.
float
.
2.0
.
float
.
3.0
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (46 / 78)
A Numpy Array of Numbers
..float
.0.0
.1.0
.2.0
.3.0
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (47 / 78)
Numpy Arrays
AdvantagesLess memory consumptionFasterWork with (or write) code in other languages (C, C++, Fortran…)
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (48 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (49 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (49 / 78)
Matrix-Matrix and Dot Products
(1 11 −1
)(0 11 0
)=
(1 1−1 1
)
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (50 / 78)
Matrix-Matrix and Dot Products
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (51 / 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⃗ = x⃗Ty⃗.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (52 / 78)
Dot Products: Norms
x⃗Tx⃗ =?
x⃗Tx⃗ =∑
ix2i
This is the squared norm of the vector (size).
∥x⃗∥ =√x⃗Tx⃗
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (53 / 78)
Dot Products: Norms
x⃗Tx⃗ =?
x⃗Tx⃗ =∑
ix2i
This is the squared norm of the vector (size).
∥x⃗∥ =√x⃗Tx⃗
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (53 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (54 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (55 / 78)
Matrix Operation Properties
A+ B = B+AAB ̸= BA (in general)A(BC) = (AB)C
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (56 / 78)
Dot Product Properties
x⃗Ty⃗ ≥ 0
x⃗Tx⃗ = 0 iff x⃗ = 0⃗
x⃗Ty⃗ = y⃗Tx⃗ (for reals)α(⃗xTy⃗) = (αx⃗)Ty⃗x⃗Tz⃗+ y⃗Tz⃗ = (⃗x+ y⃗)T z⃗
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (57 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (58 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (59 / 78)
Other Functions
np.expnp.sin…
All of these work element-wise!
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (60 / 78)
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
Prints
array([1, 2, 4, 5])
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (61 / 78)
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
Prints
array([1, 2, 4, 5])
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (61 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (62 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (63 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (64 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (64 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (65 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (66 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (66 / 78)
Logical Arrays
A = np . array ( [ - 1 , 0 , 1 , 2 , - 2 , 3 , 4 , - 2 ] )p r i n t (A > 0 )
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (67 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (68 / 78)
Logical Indexing
A[A < 0 ] = 0
orA *= (A > 0 )
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (69 / 78)
Logical Indexing
pr in t ’Mean o f p o s i t i v e s ’ , A[A > 0 ] .mean( )
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (70 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (71 / 78)
Documentation
http://docs.scipy.org/doc/
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (72 / 78)
Last Section
Matplotlib & Spyder
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (73 / 78)
Matplotlib
Matplotlib is a plotting library.Very flexible.Very active project.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (74 / 78)
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 (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (75 / 78)
Example I
4 3 2 1 0 1 2 3 420
15
10
5
0
5
10
15
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (76 / 78)
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/2012I’m available at [email protected]
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (77 / 78)
Thank you.
Luis Pedro Coelho (IMM) ⋆ Introduction to Python ⋆ Lisbon Machine Learning School II (78 / 78)