You are on page 1of 46

Why Python?

Introduction to Python

Further Info

Scientic Programming in Python

Eric Christiansen
UCSD CSE

September 16, 2008

This work is licensed under the Creative Commons Attribution 3.0 License. Based on a MATLAB tutorial by Tim Marks

Scientic Programming in Python 1 / 22

Why Python?

Introduction to Python

Further Info

What is Python?

Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:

Scientic Programming in Python 2 / 22

Why Python?

Introduction to Python

Further Info

What is Python?

Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform

Scientic Programming in Python 2 / 22

Why Python?

Introduction to Python

Further Info

What is Python?

Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform object oriented

Scientic Programming in Python 2 / 22

Why Python?

Introduction to Python

Further Info

What is Python?

Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform object oriented open source

Scientic Programming in Python 2 / 22

Why Python?

Introduction to Python

Further Info

Why should I care?

Scientic Programming in Python 3 / 22

Why Python?

Introduction to Python

Further Info

Why should I care?

You may need to use a computer to


run simulations crunch data display data...

Scientic Programming in Python 3 / 22

Why Python?

Introduction to Python

Further Info

Why should I care?

You may need to use a computer to


run simulations crunch data display data...

Pythons 3rd -party libraries can help you with these tasks.

Scientic Programming in Python 3 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries

Python is enhanced by a large set of scientic libraries that are being actively developed.

Scientic Programming in Python 4 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries

Python is enhanced by a large set of scientic libraries that are being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib

Scientic Programming in Python 4 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries

Python is enhanced by a large set of scientic libraries that are being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib

a computer algebra system (Mathematica)


SAGE

Scientic Programming in Python 4 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries


data processing
Modular toolkit for Data Processing (MDP)

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries


data processing
Modular toolkit for Data Processing (MDP)

bioinformatics functions
Biopython

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries


data processing
Modular toolkit for Data Processing (MDP)

bioinformatics functions
Biopython

machine learning functions


PyML, mlpy, SHOGUN

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries


data processing
Modular toolkit for Data Processing (MDP)

bioinformatics functions
Biopython

machine learning functions


PyML, mlpy, SHOGUN

neural nets
Fast Articial Neural Network (FANN) Library

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Pythons Scientic Libraries


data processing
Modular toolkit for Data Processing (MDP)

bioinformatics functions
Biopython

machine learning functions


PyML, mlpy, SHOGUN

neural nets
Fast Articial Neural Network (FANN) Library

articial intelligence or robotics routines


Python Robotics (Pyro)

Scientic Programming in Python 5 / 22

Why Python?

Introduction to Python

Further Info

Is it hard to learn?

Scientic Programming in Python 6 / 22

Why Python?

Introduction to Python

Further Info

Is it hard to learn?

Scientic Programming in Python 6 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Advantages of Python:

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Advantages of Python:
open source means no limits on use

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality modern language with support for object orientation

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box

Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality modern language with support for object orientation support for calling functions in other languages

Scientic Programming in Python 7 / 22

Why Python?

Introduction to Python

Further Info

Basics
To get information on an object from the interpreter
h e l p <o b j e c t >

Commenting:
Inline comments are preceded with # Block comments are surrounded with

Code blocks are denoted with indentation:


i f x == 2 : print x

Python is dynamically typed:


a = hello # a is a string a = 4 # a i s now an i n t e g e r

Scientic Programming in Python 8 / 22

Why Python?

Introduction to Python

Further Info

Vectors
Many of these functions come from SciPy.
from s c i p y import

Vectors:
N v v v v v = = = = = = 5 # [1 ,2 ,3] # array ([1 ,2 ,3]) # a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] )# array ([[1 ,2 ,3]]) # transpose (v) # # # a r a n g e ( 4 ,4) # # p i a r a n g e ( 4 ,4)/4 arange ( 4 ,4 ,.5) # [] # a scalar a list a column v e c t o r a column v e c t o r a column v e c t o r transpose a vector ( row t o column o r column t o row ) a vector in a s p e c i f i e d range : arange ( s t a r t , stop , s t e p ) empty l i s t
Scientic Programming in Python 9 / 22

v = v = v = v =

Why Python?

Introduction to Python

Further Info

Matrices

v = array ([5 ,6 ,7]) v [2]

len (v) m = array ([[1 ,2 ,3] ,\ [4 ,5 ,6]]) # a 2 x3 m a t r i x m[ 1 , 2 ] == m [ 1 ] [ 2 ] # access a matrix element # m a t r i x [ row , column ]

# # # #

access a vector element vector ( index ) arrays are z e r o i n d e x e d number o f e l e m e n t s i n a v e c t o r

Scientic Programming in Python 10 / 22

Why Python?

Introduction to Python

Further Info

Syntax and Special Functions

Matrices:
m v v m = = = = zeros ([2 ,3]) ones ( [ 1 , 3 ] ) rand (3 ,1) eye (3) # # # # a matrix of zeros a matrix of ones rand matrix ( see a l s o randn ) i d e n t i t y m a t r i x ( 3 x3 )

Scientic Programming in Python 11 / 22

Why Python?

Introduction to Python

Further Info

Syntax and Special Functions (cont)


m[ 1 , : ] m[ : , 0 ] m[ 1 : , 1 : ] m. r e s h a p e ( [ 4 , 1 ] ) m. s h a p e m. s h a p e [ 0 ] m. s h a p e [ 1 ] z e r o s (m. s h a p e ) # # # # # # # # # # a c c e s s a m a t r i x row ( s e c o n d row ) a c c e s s a m a t r i x column ( l e f t column ) lower r i g h t submatrix t u r n m a t r i x i n t o a column v e c t o r ( c o n c a t e n a t e rows ) s i z e o f a m a t r i x [ rows , c o l s ] number o f rows number o f c o l u m n s c r e a t e a new m a t r i x w i t h s i z e of m

m = a r r a y ( [ [ h e l l o , sum ] , \ [1 ,2]]) # p u t w h a t e v e r you want # i n t o an a r r a y m[ 0 , 1 ] (m[ 1 ] ) # c a l l sum on bottom row o f m


Scientic Programming in Python 12 / 22

Why Python?

Introduction to Python

Further Info

Syntax and Special Functions (cont)


Arithmetic operations performed on arrays are done element by element.
a = array ([1 ,2 ,3 ,4]) 2 a a / 4 b = array ([5 ,6 ,7 ,8]) a + b a b a 2 a b a / b log (a) around ( a r r a y ( [ [ . 6 ] , \ [.5]])) # # # # # # # # # # vector scalar multiplication scalar division vector pointwise vector addition pointwise vector subtraction pointise vector squaring pointwise vector multiply pointwise vector divide pointwise logarithm

# pointwise rounding # ( . 5 rounds to 0)


Scientic Programming in Python 13 / 22

Why Python?

Introduction to Python

Further Info

Vector Operations
a = array ([1 ,4 ,6 ,3]) sum ( a ) mean ( a ) var (a) std (a) max ( a ) a = array ([[1 ,2 ,3] ,\ [4 ,5 ,6]]) mean ( a , 0 ) amax ( a , 1 ) amax ( a ) # # # # # # vector sum o f v e c t o r e l e m e n t s mean o f v e c t o r e l e m e n t s variance standard deviation maximum

# # # # # #

matrix mean o f e a c h column max o f e a c h row t o o b t a i n max o f m a t r i x n o t e we u s e 2 d i f f e r e n t max f u n c t i o n s

Scientic Programming in Python 14 / 22

Why Python?

Introduction to Python

Further Info

Matrix Operations
dot ( t r a n s p o s e ( a r r a y ( [ 1 , 2 , 3 ] ) ) , \ array ([4 ,5 ,6])) # row v e c t o r 1 x3 t i m e s column # v e c t o r 3 x1 r e s u l t s i n a # s i n g l e number , a l s o known # as dot / i n n e r product dot ( a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , \ a r r a y ( [ [ 4 , 5 , 6 ] ] ) ) # column v e c t o r 3 x1 t i m e s row # v e c t o r 1 x3 r e s u l t s i n 3 x3 # m a t r i x , a l s o known # as outer product a = rand (3 ,2) # 3 x2 m a t r i x b = rand (2 ,4) # 2 x4 m a t r i x dot ( a , b ) # 3 x4 m a t r i x

Scientic Programming in Python 15 / 22

Why Python?

Introduction to Python

Further Info

Saving Your Work


import c P i c k l e a s p i c k l e # t h i s module l e t s you # s a v e and r e l o a d o b j e c t s

f = open ( s a v e f i l e , w ) # open a r c h i v e f i l e p i c k l e . dump ( o b j , f ) # dump o b j e c t t o a r c h i v e f . close () # close archive f i l e del obj # clear object # from memory archive f i l e object archive archive f i l e

f = open ( s a v e f i l e , r ) # open obj = p i c k l e . load ( f ) # read # from f . close () # close

Scientic Programming in Python 16 / 22

Why Python?

Introduction to Python

Further Info

Relations and Control


Example: given a list v, create a new list u with values equal to v if they are greater than 0, and equal to 0 if they less than or equal to 0. Using a for loop:
v = [3 ,5 , 2 ,5 , 1 ,0] u = [0] len (v) # u is a l l zeros for i in range ( len ( v ) ) : i f v [ i ] > 0: u[ i ] = v[ i ]

Using list comprehension:


v = [3 ,5 , 2 ,5 , 1 ,0] u = [ max ( e , 0 ) f o r e i n v ]

Scientic Programming in Python 17 / 22

Why Python?

Introduction to Python

Further Info

Importing Functions
Save the following code to mylib.py:
def myfunc ( a , b ) : r e t u r n a+b 2

Import and use myfunc. Note, we might need to congure PYTHONPATH.


from m y l i b import myfunc myfunc ( 1 , 2 ) myfunc ( b=2, a=1) # same a s a bo v e

Python also supports class creation:


c l a s s MyClass : def init ( self ): print hello ! m y c l a s s = MyClass ( )
Scientic Programming in Python 18 / 22

Why Python?

Introduction to Python

Further Info

Plotting

The library matplotlib / pylab is your friend:


from p y l a b import xs = arange ( 2 ,2 ,.01) p l o t ( xs , s i n ( x s ) ) show ( )

Scientic Programming in Python 19 / 22

Why Python?

Introduction to Python

Further Info

Imaging
We use the Python Imaging Library as well as matplotlib / pylab.
from p y l a b import import Image im = Image . open ( my image . j p g ) im . show ( ) # we can d i s p l a y t h e image ima = a r r a y ( im ) # t y p e c a s t i n g to a r r a y # extracts pixel values i m r = Image . f r o m s t r i n g ( RGB , \ ( ima . s h a p e [ 1 ] , ima . s h a p e [ 0 ] ) , \ ima . t o s t r i n g ( ) ) # c o n v e r t a r r a y i n t o image img = mean ( ima , 2 ) # average color i n t e n s i t i e s # f o r each p i x e l imshow ( img ) autumn ( ) # s e t d e f a u l t c o l o r m a p t o autumn show ( )
Scientic Programming in Python 20 / 22

Why Python?

Introduction to Python

Further Info

More Help?

Many guides and tutorials are available online:

Scientic Programming in Python 21 / 22

Why Python?

Introduction to Python

Further Info

More Help?

Many guides and tutorials are available online: Dive Into Python
python introduction for programmers

Scientic Programming in Python 21 / 22

Why Python?

Introduction to Python

Further Info

More Help?

Many guides and tutorials are available online: Dive Into Python
python introduction for programmers

A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html

Scientic Programming in Python 21 / 22

Why Python?

Introduction to Python

Further Info

Questions?

Scientic Programming in Python 22 / 22

You might also like