UCSD March 9, 2009
What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is: cross platform
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
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
Why should I care?
Why should I care? You may need to use a computer to run simulations crunch data display data...
Why should I care? You may need to use a computer to run simulations crunch data display data... Python s 3 rd -party libraries can help you with these tasks.
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want:
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP)
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython machine learning functions PyML, mlpy, SHOGUN
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE 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
Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE 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)
Is it hard to learn?
Is it hard to learn?
Python vs MATLAB Advantages of MATLAB:
Python vs MATLAB Advantages of MATLAB: already widely used
Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing
Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation
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
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:
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
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
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
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
Python Intro - 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 g a = 4 # a i s now an i n t e g e r
Python Intro - Vectors and Matrices Many of these functions come from SciPy. from s c i p y import Vectors: N = 5 # a s c a l a r v = [ 1, 2, 3 ] # a l i s t v = a r r a y ( [ 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 = a r r a y ( [ [ 1, 2, 3 ] ] ) # a column v e c t o r v = t r a n s p o s e ( v ) # t r a n s p o s e a v e c t o r # ( row to column # or column to row ) v = arange ( 4,4) # a v e c t o r i n # a s p e c i f i e d range : v = p i arange ( 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
Python Intro Matrices: m = z e r o s ( [ 2, 3 ] ) # a m a t r i x o f z e r o s v = ones ( [ 1, 3 ] ) # a m a t r i x o f ones v = rand ( 3, 1 ) # rand m a t r i x ( s e e a l s o randn ) m = eye ( 3 ) # i d e n t i t y m a t r i x (3 x3 )
Python Intro - Vectors and Matrices v = a r r a y ( [ 5, 6, 7 ] ) # a c c e s s a v e c t o r element v [ 2 ] # v e c t o r ( i n d e x ) a r r a y s a r e # zero i n d e x e d l e n ( v ) # number o f e l e m e n t s i n # a v e c t o r m = a r r a y ( [ [ 1, 2, 3 ], \ [ 4, 5, 6 ] ] ) # a 2 x3 m a t r i x m[ 1, 2 ] == m[ 1 ] [ 2 ] # a c c e s s a m a t r i x element # m a t r i x [ row, column ]
blah m[ 1, : ] # a c c e s s a m a t r i x row # ( bottom row ) m[ :, 0 ] # a c c e s s a m a t r i x column # ( l e f t column ) 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. shape # s i z e o f a m a t r i x [ rows, c o l s ] m. shape [ 0 ] # number o f rows m. shape [ 1 ] # number o f columns z e r o s (m. shape ) # c r e a t e a new m a t r i x with # 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 y m[ 0, 1 ] (m[ 1 ] ) # c a l l sum on bottom row o f m
Python Intro - Vectors and Matrices Arithmetic operations performed on arrays are done element by element. a = a r r a y ( [ 1, 2, 3, 4 ] ) # v e c t o r 2 a # s c a l a r m u l t i p l i c a t i o n a / 4 # s c a l a r d i v i s i o n b = a r r a y ( [ 5, 6, 7, 8 ] ) # v e c t o r a + b # p o i n t w i s e v e c t o r a d d i t i o n a b # p o i n t w i s e v e c t o r s u b t r a c t i o n a 2 # p o i n t i s e v e c t o r s q u a r i n g a b # p o i n t w i s e v e c t o r m u l t i p l y a / b # p o i n t w i s e v e c t o r d i v i d e l o g ( a ) # p o i n t w i s e l o g a r i t h m around ( a r r a y ( [ [. 6 ], \ [. 5 ] ] ) ) # p o i n t w i s e r o u n d i n g # (. 5 rounds to 0)
Vector Operations a = a r r a y ( [ 1, 4, 6, 3 ] ) # v e c t o r 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 v a r ( a ) # v a r i a n c e s t d ( a ) # s t a n d a r d d e v i a t i o n max( a ) # maximum a = a r r a y ( [ [ 1, 2, 3 ], \ [ 4, 5, 6 ] ] ) # m a t r i x mean ( a, 0 ) # mean o f each column amax ( a, 1 ) # max o f each row amax ( a ) # to o b t a i n max o f m a t 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
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 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 # matrix, a l s o known # as o u t e r p r oduct 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
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 # 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( obj, f ) # dump o b j e c t to a r c h i v e f. 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 e o b j = p i c k l e. l o a d ( f ) # read o b j e c t # from a r c h i v e f. c l o s e ( ) # c l o s e a r c h i v e f i l e
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 ] l e n ( v ) # u i s a l l z e r o s f o r i i n range ( 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 ]
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 configure PYTHONPATH. from mylib import myfunc myfunc ( 1, 2 ) myfunc ( b=2, a=1) # same a s 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 ( )
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 ( xs ) ) show ( )
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 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 # e x t r a c t s p i x e l v a l u e s imr = 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 ( ) ) # c o n v e r t a r r a y i n t o image img = ima. mean ( 2 ) # a v e r a g e 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 ( ) # s e t d e f a u l t colormap to autumn show ( )
How do I start? Many guides and tutorials are available online:
How do I start? Many guides and tutorials are available online: Dive Into Python python introduction for programmers
How do I start? 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
Questions?