Scientific Programming in Python
Transcript of Scientific Programming in Python
Why Python? Introduction to Python Further Info
Scientific 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
1 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
What is Python?
Python in a very high level (scripting) language which has gainedwidespread popularity in recent years.It is:
cross platform
object oriented
open source
2 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
What is Python?
Python in a very high level (scripting) language which has gainedwidespread popularity in recent years.It is:
cross platform
object oriented
open source
2 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
What is Python?
Python in a very high level (scripting) language which has gainedwidespread popularity in recent years.It is:
cross platform
object oriented
open source
2 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
What is Python?
Python in a very high level (scripting) language which has gainedwidespread popularity in recent years.It is:
cross platform
object oriented
open source
2 / 22
Scientific Programming in Python
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...
Python’s 3rd-party libraries can help you with these tasks.
3 / 22
Scientific Programming in Python
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...
Python’s 3rd-party libraries can help you with these tasks.
3 / 22
Scientific Programming in Python
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...
Python’s 3rd-party libraries can help you with these tasks.
3 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that arebeing actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
4 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that arebeing actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
4 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that arebeing actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
4 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python Robotics (Pyro)
5 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Is it hard to learn?
6 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Is it hard to learn?
6 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation
support for calling functions in other languages
7 / 22
Scientific Programming in Python
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 :p r i n t x
Python is dynamically typed:
a = ” h e l l o ” # a i s a s t r i n ga = 4 # a i s now an i n t e g e r
8 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
VectorsMany of these functions come from SciPy.
from s c i p y import ∗
Vectors:
N = 5 # a s c a l a rv = [ 1 , 2 , 3 ] # a l i s tv = a r r a y ( [ 1 , 2 , 3 ] ) # a column v e c t o rv = a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] )# a column v e c t o rv = a r r a y ( [ [ 1 , 2 , 3 ] ] ) # a column v e c t o rv = t r a n s p o s e ( v ) # t r an s p o s e a v e c t o r
# ( row to column# or column to row )
v = a ran ge (−4 ,4) # a ve c t o r i n# a s p e c i f i e d range :
v = p i ∗ ar an ge (−4 ,4)/4v = a ran ge (−4 ,4 , .5) # arange ( s t a r t , stop , s t e p )v = [ ] # empty l i s t
9 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Matrices
v = a r r a y ( [ 5 , 6 , 7 ] ) # acc e s s a v e c t o r e l ementv [ 2 ] # ve c t o r ( i nd ex ) − a r r a y s a r e
# zero−i ndexedl e n ( v ) # number o f e l ement s i n a v e c t o r
m = a r r a y ( [ [ 1 , 2 , 3 ] , \[ 4 , 5 , 6 ] ] ) # a 2x3 mat r i x
m[ 1 , 2 ] == m[ 1 ] [ 2 ] # acc e s s a mat r i x e l ement# mat r i x [ row , column ]
10 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Syntax and Special Functions
Matrices:
m = z e r o s ( [ 2 , 3 ] ) # a mat r i x o f z e r o sv = ones ( [ 1 , 3 ] ) # a mat r i x o f onesv = rand ( 3 , 1 ) # rand mat r i x ( s e e a l s o randn )m = eye ( 3 ) # i d e n t i t y mat r i x (3 x3 )
11 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Syntax and Special Functions (cont)
m[ 1 , : ] # acc e s s a mat r i x row ( second row )m[ : , 0 ] # acc e s s a mat r i x column ( l e f t column )m[ 1 : , 1 : ] # lowe r r i g h t submat r i xm. r e s h a p e ( [ 4 , 1 ] ) # turn mat r i x i n t o a column
# ve c t o r ( conca t ena t e rows )m. shape # s i z e o f a mat r i x [ rows , c o l s ]m. shape [ 0 ] # number o f rowsm. shape [ 1 ] # number o f columnsz e r o s (m. shape ) # c r e a t e a new mat r i x w i th
# s i z e o f m
m = a r r a y ( [ [ ’ h e l l o ’ , sum ] ,\[ 1 , 2 ] ] ) # put whatever you want
# i n t o an a r r a ym[ 0 , 1 ] (m[ 1 ] ) # c a l l sum on bottom row o f m
12 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Syntax and Special Functions (cont)
Arithmetic operations performed on arrays are done “element byelement”.
a = a r r a y ( [ 1 , 2 , 3 , 4 ] ) # ve c t o r2 ∗ a # s c a l a r m u l t i p l i c a t i o na / 4 # s c a l a r d i v i s i o nb = a r r a y ( [ 5 , 6 , 7 , 8 ] ) # ve c t o ra + b # po i n tw i s e v e c t o r a d d i t i o na − b # po i n tw i s e v e c t o r s u b t r a c t i o na ∗∗ 2 # p o i n t i s e v e c t o r s qu a r i n ga ∗ b # po i n tw i s e v e c t o r mu l t i p l ya / b # po i n tw i s e v e c t o r d i v i d el o g ( a ) # po i n tw i s e l o g a r i t hmaround ( a r r a y ( [ [ . 6 ] , \
[ . 5 ] ] ) ) # po i n tw i s e round ing# ( . 5 rounds to 0)
13 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Vector Operations
a = a r r a y ( [ 1 , 4 , 6 , 3 ] ) # ve c t o rsum ( a ) # sum of v e c t o r e l ement smean ( a ) # mean o f v e c t o r e l ement sv a r ( a ) # va r i a n c es t d ( a ) # standa rd d e v i a t i o nmax ( a ) # maximum
a = a r r a y ( [ [ 1 , 2 , 3 ] , \[ 4 , 5 , 6 ] ] ) # mat r i x
mean ( a , 0 ) # mean o f each columnamax ( a , 1 ) # max o f each rowamax ( a ) # to ob t a i n max o f mat r i x
# note we use 2# d i f f e r e n t max f u n c t i o n s
14 / 22
Scientific Programming in Python
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 ] ) ) , \a r r a y ( [ 4 , 5 , 6 ] ) ) # row v e c t o r 1x3 t imes column
# ve c t o r 3x1 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 p roduc t
dot ( a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , \a r r a y ( [ [ 4 , 5 , 6 ] ] ) ) # column v e c t o r 3x1 t imes row
# ve c t o r 1x3 r e s u l t s i n 3x3# matr ix , a l s o known# as ou t e r p roduc t
a = rand ( 3 , 2 ) # 3x2 mat r i xb = rand ( 2 , 4 ) # 2x4 mat r i xdot ( a , b ) # 3x4 mat r i x
15 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Saving Your Work
import c P i c k l e as p i c k l e # t h i s module l e t s you# save 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 ep i c k l e . dump( obj , f ) # dump ob j e c t to a r c h i v ef . c l o s e ( ) # c l o s e a r c h i v e f i l e
del o b j # c l e a r o b j e c t# from memory
f = open ( ’ s a v e f i l e ’ , ’ r ’ ) # open a r c h i v e f i l eo b j = p i c k l e . l o a d ( f ) # read ob j e c t
# from a r c h i v ef . c l o s e ( ) # c l o s e a r c h i v e f i l e
16 / 22
Scientific Programming in Python
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 ifthey are greater than 0, and equal to 0 if they less than or equal to0.Using a for loop:
v = [3 ,5 ,−2 ,5 ,−1 ,0]u = [ 0 ] ∗ l e n ( v ) # u i s a l l z e r o sf o r i i n ra ng e ( l e n ( 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 ]
17 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Importing Functions
Save the following code to “mylib.py”:
def myfunc ( a , b ) :return a+b∗∗2
Import and use myfunc. Note, we might need to configurePYTHONPATH.
from m y l i b import myfuncmyfunc ( 1 , 2 )myfunc ( b=2,a=1) # same as above
Python also supports class creation:
c l a s s MyClass :def i n i t ( s e l f ) :
p r i n t ” h e l l o ! ”m y c l a s s = MyClass ( )
18 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Plotting
The library matplotlib / pylab is your friend:
from p y l a b import ∗xs = ar ang e (−2 ,2 , .01)p l o t ( xs , s i n ( xs ) )show ( )
19 / 22
Scientific Programming in Python
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 the imageima = a r r a y ( im ) # ty p e c a s t i n g to a r r a y
# e x t r a c t s p i x e l v a l u e simr = Image . f r o m s t r i n g ( ’RGB ’ ,\
( ima . shape [ 1 ] , ima . shape [ 0 ] ) , \ima . t o s t r i n g ( ) ) # conve r t a r r a y i n t o image
img = mean ( ima , 2 ) # ave rage c o l o r i n t e n s i t i e s# f o r each p i x e l
imshow ( img )autumn ( ) # se t d e f a u l t co lormap to autumnshow ( )
20 / 22
Scientific Programming in Python
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 befound at http://www.awaretek.com/tutorials.html
21 / 22
Scientific Programming in Python
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 befound at http://www.awaretek.com/tutorials.html
21 / 22
Scientific Programming in Python
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 befound at http://www.awaretek.com/tutorials.html
21 / 22
Scientific Programming in Python
Why Python? Introduction to Python Further Info
Questions?
22 / 22
Scientific Programming in Python