introduction to python - instituto de telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf ·...

77
Introduction to Python Luis Pedro Coelho European Molecular Biology Laboratory Lisbon Machine Learning School 2013 Luis Pedro Coelho (EMBL) Introduction to Python #LxMLS (1 / 70)

Upload: others

Post on 03-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Introduction to Python

Luis Pedro Coelho

European Molecular Biology Laboratory

Lisbon Machine Learning School 2013

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (1 / 70)

Page 2: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (2 / 70)

Page 3: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Versions

Python VersionsThere are two major versions, currently: 2.7 and 3.3.We are going to be using 2.7 (but 2.6 should be OK too).

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (3 / 70)

Page 4: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Example

pr in t ” He l lo World”

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (4 / 70)

Page 5: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Task

AverageCompute the average of the following numbers:

...1 10

...2 7

...3 22

...4 14

...5 17

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (5 / 70)

Page 6: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (6 / 70)

Page 7: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

“Python is executable pseudo-code.”—Python lore (often attributed to Bruce Eckel)

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (7 / 70)

Page 8: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (8 / 70)

Page 9: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Types

Basic TypesNumbers (integers and floating point)StringsLists and tuplesDictionaries

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (9 / 70)

Page 10: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Types: Numbers I: Integers

A = 1B = 2C = 3pr in t A + B*C

Outputs 7.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (10 / 70)

Page 11: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Types: Numbers II: Floats

A = 1 . 2B = 2 . 4C = 3 . 6p r in t A + B*C

Outputs 9.84.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (11 / 70)

Page 12: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Types: Numbers III: Integers & Floats

A = 2B = 2 . 5C = 4 . 4p r in t A + B*C

Outputs 22.0.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (12 / 70)

Page 13: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (13 / 70)

Page 14: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (14 / 70)

Page 15: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (14 / 70)

Page 16: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (15 / 70)

Page 17: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (16 / 70)

Page 18: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (17 / 70)

Page 19: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Python Types: Lists

mixed = [ ’Banana ’ , 100 , [ ’ Another ’ , ’ L i s t ’ ] , [ ] ]p r i n t l en (mixed )

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (18 / 70)

Page 20: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (19 / 70)

Page 21: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (20 / 70)

Page 22: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (21 / 70)

Page 23: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (22 / 70)

Page 24: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Convention...1 Use 4 spaces to indent....2 Other things will work, but confuse people.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (23 / 70)

Page 25: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Conditionals

Examplesx == yx != yx < yx < y < zx in lstx not in lst

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (24 / 70)

Page 26: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (25 / 70)

Page 27: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (26 / 70)

Page 28: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

While Loop

whi le <cond i t i on>:statement1statement2

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (27 / 70)

Page 29: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (28 / 70)

Page 30: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (29 / 70)

Page 31: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (30 / 70)

Page 32: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (31 / 70)

Page 33: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (32 / 70)

Page 34: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Division

DivisionWhat is 9 divided by 3?What is 10 divided by 3?

Two types of division...1 Integer division: x // y...2 Floating-point division: x / float(y)

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (33 / 70)

Page 35: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Division

DivisionWhat is 9 divided by 3?What is 10 divided by 3?

Two types of division...1 Integer division: x // y...2 Floating-point division: x / float(y)

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (33 / 70)

Page 36: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Functions

de f double ( x ) :’ ’ ’y = double ( x )

Returns the double o f x’ ’ ’r e turn 2*x

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (34 / 70)

Page 37: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (35 / 70)

Page 38: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Numeric Python: Numpy

Numpy

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (36 / 70)

Page 39: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Basic Type

numpy.array or numpy.ndarray.

Multi-dimensional array of numbers.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (37 / 70)

Page 40: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (38 / 70)

Page 41: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (38 / 70)

Page 42: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (39 / 70)

Page 43: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

A Python List of Numbers

..

float

.

0.0

.

float

.

1.0

.

float

.

2.0

.

float

.

3.0

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (40 / 70)

Page 44: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

A Numpy Array of Numbers

..float

.0.0

.1.0

.2.0

.3.0

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (41 / 70)

Page 45: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Numpy Arrays

AdvantagesLess memory consumptionFasterWork with (or write) code in other languages (C, C++, Fortran…)

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (42 / 70)

Page 46: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (43 / 70)

Page 47: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (43 / 70)

Page 48: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Matrix-Matrix and Dot Products

(1 11 −1

)(0 11 0

)=

(1 1−1 1

)

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (44 / 70)

Page 49: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (45 / 70)

Page 50: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (46 / 70)

Page 51: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (47 / 70)

Page 52: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (48 / 70)

Page 53: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (49 / 70)

Page 54: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Other Functions

np.expnp.sin…

All of these work element-wise!

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (50 / 70)

Page 55: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (51 / 70)

Page 56: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (51 / 70)

Page 57: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Numpy DtypesAll 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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (52 / 70)

Page 58: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (53 / 70)

Page 59: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (54 / 70)

Page 60: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (55 / 70)

Page 61: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (56 / 70)

Page 62: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (56 / 70)

Page 63: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (57 / 70)

Page 64: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (58 / 70)

Page 65: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (58 / 70)

Page 66: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Logical Arrays

A = np . array ( [ - 1 , 0 , 1 , 2 , - 2 , 3 , 4 , - 2 ] )p r i n t (A > 0 )

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (59 / 70)

Page 67: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (60 / 70)

Page 68: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Logical Indexing

A[A < 0 ] = 0

orA *= (A > 0 )

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (61 / 70)

Page 69: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Logical Indexing

pr in t ’Mean o f p o s i t i v e s ’ , A[A > 0 ] .mean( )

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (62 / 70)

Page 70: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (63 / 70)

Page 71: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Documentation

http://docs.scipy.org/doc/

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (64 / 70)

Page 72: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Last Section

Matplotlib & Spyder

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (65 / 70)

Page 73: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Matplotlib

Matplotlib is a plotting library.Very flexible.Very active project.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (66 / 70)

Page 74: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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 (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (67 / 70)

Page 75: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Example I

4 3 2 1 0 1 2 3 420

15

10

5

0

5

10

15

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (68 / 70)

Page 76: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

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/2013I’m available at [email protected]

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (69 / 70)

Page 77: Introduction to Python - Instituto de Telecomunicaçõeslxmls.it.pt/2013/introduction_python.pdf · 2013-07-24 · Python Versions Python Versions Therearetwomajorversions,currently:

Thank you.

Luis Pedro Coelho (EMBL) ⋆ Introduction to Python ⋆ #LxMLS (70 / 70)