1. 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
Scientific Programming in Python
1 / 22
2. 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:
Scientific Programming in Python
2 / 22
3. 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
Scientific Programming in Python
2 / 22
4. 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
Scientific Programming in Python
2 / 22
5. 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
Scientific Programming in Python
2 / 22
6. Why Python? Introduction to Python Further Info
Why should I care?
Scientific Programming in Python
3 / 22
7. 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...
Scientific Programming in Python
3 / 22
8. 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.
Scientific Programming in Python
3 / 22
9. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are
being actively developed.
Scientific Programming in Python
4 / 22
10. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are
being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
Scientific Programming in Python
4 / 22
11. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are
being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
Scientific Programming in Python
4 / 22
12. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
Scientific Programming in Python
5 / 22
13. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
Scientific Programming in Python
5 / 22
14. Why Python? Introduction to Python Further Info
Python’s Scientific Libraries
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
Scientific Programming in Python
5 / 22
15. 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
Scientific Programming in Python
5 / 22
16. 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
Scientific Programming in Python
5 / 22
17. 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)
Scientific Programming in Python
5 / 22
18. Why Python? Introduction to Python Further Info
Is it hard to learn?
Scientific Programming in Python
6 / 22
19. Why Python? Introduction to Python Further Info
Is it hard to learn?
Scientific Programming in Python
6 / 22
20. Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
Scientific Programming in Python
7 / 22
21. Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
Scientific Programming in Python
7 / 22
22. Why Python? Introduction to Python Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing
Scientific Programming in Python
7 / 22
23. 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
Scientific Programming in Python
7 / 22
24. 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”
Scientific Programming in Python
7 / 22
25. 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:
Scientific Programming in Python
7 / 22
26. 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
Scientific Programming in Python
7 / 22
27. 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
Scientific Programming in Python
7 / 22
28. 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
Scientific Programming in Python
7 / 22
29. 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
Scientific Programming in Python
7 / 22
30. 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
Scientific Programming in Python
8 / 22
31. Why Python? Introduction to Python Further Info
Vectors
Many of these functions come from SciPy.
from s c i p y import ∗
Vectors:
N = 5 # a scalar
v = [1 ,2 ,3] # a list
v = array ([1 ,2 ,3]) # a column v e c t o r
v = a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] )# a column v e c t o r
v = array ([[1 ,2 ,3]]) # a column v e c t o r
v = transpose (v) # transpose a vector
# ( row t o column
# o r column t o row )
v = a r a n g e ( −4 ,4) # a vector in
# a s p e c i f i e d range :
v = p i ∗ a r a n g e ( −4 ,4)/4
v = arange ( −4 ,4 ,.5) # arange ( s t a r t , stop , s t e p )
v = [] # empty l i s t
Scientific Programming in Python
9 / 22
32. Why Python? Introduction to Python Further Info
Matrices
v = array ([5 ,6 ,7]) # access a vector element
v [2] # vector ( index ) − arrays are
# z e r o −i n d e x e d
len (v) # number o f e l e m e n t s i n a v e c t o r
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 ]
Scientific Programming in Python
10 / 22
33. Why Python? Introduction to Python Further Info
Syntax and Special Functions
Matrices:
m = zeros ([2 ,3]) # a matrix of zeros
v = ones ( [ 1 , 3 ] ) # a matrix of ones
v = rand (3 ,1) # rand matrix ( see a l s o randn )
m = eye (3) # i d e n t i t y m a t r i x ( 3 x3 )
Scientific Programming in Python
11 / 22
34. Why Python? Introduction to Python Further Info
Syntax and Special Functions (cont)
m[ 1 , : ] # a c c e s s a m a t r i x row ( s e c o n d row )
m[ : , 0 ] # a c c e s s a m a t r i x column ( l e f t column )
m[ 1 : , 1 : ] # lower r i g h t submatrix
m. r e s h a p e ( [ 4 , 1 ] ) # 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 )
m. s h a p e # s i z e o f a m a t r i x [ rows , c o l s ]
m. s h a p e [ 0 ] # number o f rows
m. s h a p e [ 1 ] # number o f c o l u m n s
z e r o s (m. s h a p e ) # 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
Scientific Programming in Python
12 / 22
35. 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]) # vector
2 ∗ a # scalar multiplication
a / 4 # scalar division
b = array ([5 ,6 ,7 ,8]) # vector
a + b # pointwise vector addition
a − b # pointwise vector subtraction
a ∗∗ 2 # pointise vector squaring
a ∗ b # pointwise vector multiply
a / b # pointwise vector divide
log (a) # pointwise logarithm
around ( a r r a y ( [ [ . 6 ] ,
[.5]])) # pointwise rounding
# ( . 5 rounds to 0)
Scientific Programming in Python
13 / 22
36. Why Python? Introduction to Python Further Info
Vector Operations
a = array ([1 ,4 ,6 ,3]) # vector
sum ( a ) # sum o f v e c t o r e l e m e n t s
mean ( a ) # mean o f v e c t o r e l e m e n t s
var (a) # variance
std (a) # standard deviation
max ( a ) # maximum
a = array ([[1 ,2 ,3] ,
[4 ,5 ,6]]) # matrix
mean ( a , 0 ) # mean o f e a c h column
amax ( a , 1 ) # max o f e a c h row
amax ( a ) # 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
Scientific Programming in Python
14 / 22
37. 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
Scientific Programming in Python
15 / 22
38. 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
f = open ( ’ s a v e f i l e ’ , ’ r ’ ) # open archive f i l e
obj = p i c k l e . load ( f ) # read object
# from archive
f . close () # close archive f i l e
Scientific Programming in Python
16 / 22
39. 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 ]
Scientific Programming in Python
17 / 22
40. 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 configure
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 ( )
Scientific Programming in Python
18 / 22
41. 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 ( )
Scientific Programming in Python
19 / 22
42. 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 ( )
Scientific Programming in Python
20 / 22
43. Why Python? Introduction to Python Further Info
More Help?
Many guides and tutorials are available online:
Scientific Programming in Python
21 / 22
44. Why Python? Introduction to Python Further Info
More Help?
Many guides and tutorials are available online:
Dive Into Python
python introduction for programmers
Scientific Programming in Python
21 / 22
45. 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
Scientific Programming in Python
21 / 22
46. Why Python? Introduction to Python Further Info
Questions?
Scientific Programming in Python
22 / 22