SlideShare a Scribd company logo
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
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
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
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
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
Why Python?       Introduction to Python                             Further Info


              Why should I care?




                                           Scientific 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...




                                                             Scientific 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...
     Python’s 3rd -party libraries can help you with these tasks.




                                                             Scientific Programming in Python
                                                                                               3 / 22
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
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
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
Why Python?            Introduction to Python                             Further Info


              Python’s Scientific Libraries




                                                Scientific Programming in Python
                                                                                  5 / 22
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
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
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
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
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
Why Python?        Introduction to Python                             Further Info


              Is it hard to learn?




                                            Scientific Programming in Python
                                                                              6 / 22
Why Python?        Introduction to Python                             Further Info


              Is it hard to learn?




                                            Scientific Programming in Python
                                                                              6 / 22
Why Python?                  Introduction to Python                             Further Info


                    Python vs MATLAB

     Advantages of MATLAB:




                                                      Scientific Programming in Python
                                                                                        7 / 22
Why Python?                         Introduction to Python                             Further Info


                             Python vs MATLAB

     Advantages of MATLAB:
              already widely used




                                                             Scientific Programming in Python
                                                                                               7 / 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Why Python?                     Introduction to Python                             Further Info


                              More Help?




     Many guides and tutorials are available online:




                                                         Scientific 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




                                                             Scientific 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




                                                             Scientific Programming in Python
                                                                                               21 / 22
Why Python?   Introduction to Python                             Further Info




               Questions?




                                       Scientific Programming in Python
                                                                         22 / 22

More Related Content

PDF
Introduction to Python
DrMohammed Qassim
 
DOCX
Obj report
Manish Raghav
 
PDF
Python programming for beginners
Benishchoco
 
PDF
Python, the Language of Science and Engineering for Engineers
Boey Pak Cheong
 
PDF
introduction of python in data science
bhavesh lande
 
DOCX
Seminar report On Python
Shivam Gupta
 
DOCX
Python(1)
sambhajimeher
 
PDF
Python – The Fastest Growing Programming Language
IRJET Journal
 
Introduction to Python
DrMohammed Qassim
 
Obj report
Manish Raghav
 
Python programming for beginners
Benishchoco
 
Python, the Language of Science and Engineering for Engineers
Boey Pak Cheong
 
introduction of python in data science
bhavesh lande
 
Seminar report On Python
Shivam Gupta
 
Python(1)
sambhajimeher
 
Python – The Fastest Growing Programming Language
IRJET Journal
 

What's hot (17)

PDF
pycon-2015-liza-daly
Liza Daly
 
PDF
summer training report on python
Shubham Yadav
 
PDF
Introduction to Python
MuhammadBakri13
 
PPTX
Python Class 1
arijit banerjee
 
PPTX
Chapter 8 getting started with python
Praveen M Jigajinni
 
PDF
Machine Learning empowered by Python April2009
Francis Piéraut
 
PDF
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
pythoncharmers
 
PDF
开源沙龙第一期 Python intro
fantasy zheng
 
PDF
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Edureka!
 
DOCX
Seminar report on python 3 course
HimanshuPanwar38
 
PDF
Intro to Machine Learning Enpowered by Python (Montreal Python)
Francis Piéraut
 
PDF
python for linguists
shukaihsieh
 
PPTX
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
PPTX
Benefits & features of python |Advantages & disadvantages of python
paradisetechsoftsolutions
 
PPTX
First python project
Neetu Jain
 
PDF
Python: the Project, the Language and the Style
Juan-Manuel Gimeno
 
PPTX
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
pycon-2015-liza-daly
Liza Daly
 
summer training report on python
Shubham Yadav
 
Introduction to Python
MuhammadBakri13
 
Python Class 1
arijit banerjee
 
Chapter 8 getting started with python
Praveen M Jigajinni
 
Machine Learning empowered by Python April2009
Francis Piéraut
 
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
pythoncharmers
 
开源沙龙第一期 Python intro
fantasy zheng
 
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Edureka!
 
Seminar report on python 3 course
HimanshuPanwar38
 
Intro to Machine Learning Enpowered by Python (Montreal Python)
Francis Piéraut
 
python for linguists
shukaihsieh
 
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
Benefits & features of python |Advantages & disadvantages of python
paradisetechsoftsolutions
 
First python project
Neetu Jain
 
Python: the Project, the Language and the Style
Juan-Manuel Gimeno
 
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
Ad

Viewers also liked (7)

ODP
Python 3 Intro Presentation for NEWLUG
NEWLUG
 
ODP
Stackless Python 101
guest162fd90
 
PDF
Python arsenal for re
geeksec80
 
PPTX
Reverse engineering with python
n|u - The Open Security Community
 
PPT
Python for pentesters
Rashid feroz
 
PPTX
DefCamp 2013 - MSF Into The Worm Hole
DefCamp
 
PPT
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Python 3 Intro Presentation for NEWLUG
NEWLUG
 
Stackless Python 101
guest162fd90
 
Python arsenal for re
geeksec80
 
Reverse engineering with python
n|u - The Open Security Community
 
Python for pentesters
Rashid feroz
 
DefCamp 2013 - MSF Into The Worm Hole
DefCamp
 
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Ad

Similar to Scientific Programming in Python (20)

PPTX
What is Python? An overview of Python for science.
Nicholas Pringle
 
PPTX
First of all, what is Python? According t
bhatamith15
 
PPTX
Introduction to Python Basics Programming
Raveendra R
 
PDF
Python For Scientists
aeberspaecher
 
PPTX
CS301_Detailed_Overview_MATLAB_Mathematica_Python.pptx
fn723290
 
PPTX
1.Introductiontshdbsyh3h5hh4hbhu4b4h.pptx
harshpately63
 
PDF
Introduction to Python and how to use python
pranjalchoubisa046
 
PDF
Python in Scientific Computing.pdf
AmirKhan811717
 
PPTX
Introduction_to_Python.pptx
Vinay Chowdary
 
PPTX
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
PPTX
Python Basics for python understanding.pptx
ShubhamNain11
 
PDF
Py tut-handout
Ramachandra Dama
 
PPTX
Machine learning session 1
NirsandhG
 
PDF
Python Mastery Made Easy.pdf
SathishKumar235656
 
PDF
Python for MATLAB Programmers
Michael Patterson
 
PPTX
We19 Learn Python! Why and How
Linda Ruetz
 
PDF
Introduction-To-Python- a guide to master
ImadM4
 
PPTX
_python Raunak.pptx
RaunakKumar33449
 
PDF
PYTHON: A brief introduction for beginners
sushanbairy
 
PDF
London level39
Travis Oliphant
 
What is Python? An overview of Python for science.
Nicholas Pringle
 
First of all, what is Python? According t
bhatamith15
 
Introduction to Python Basics Programming
Raveendra R
 
Python For Scientists
aeberspaecher
 
CS301_Detailed_Overview_MATLAB_Mathematica_Python.pptx
fn723290
 
1.Introductiontshdbsyh3h5hh4hbhu4b4h.pptx
harshpately63
 
Introduction to Python and how to use python
pranjalchoubisa046
 
Python in Scientific Computing.pdf
AmirKhan811717
 
Introduction_to_Python.pptx
Vinay Chowdary
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
Python Basics for python understanding.pptx
ShubhamNain11
 
Py tut-handout
Ramachandra Dama
 
Machine learning session 1
NirsandhG
 
Python Mastery Made Easy.pdf
SathishKumar235656
 
Python for MATLAB Programmers
Michael Patterson
 
We19 Learn Python! Why and How
Linda Ruetz
 
Introduction-To-Python- a guide to master
ImadM4
 
_python Raunak.pptx
RaunakKumar33449
 
PYTHON: A brief introduction for beginners
sushanbairy
 
London level39
Travis Oliphant
 

More from zukun (20)

PDF
My lyn tutorial 2009
zukun
 
PDF
ETHZ CV2012: Tutorial openCV
zukun
 
PDF
ETHZ CV2012: Information
zukun
 
PDF
Siwei lyu: natural image statistics
zukun
 
PDF
Lecture9 camera calibration
zukun
 
PDF
Brunelli 2008: template matching techniques in computer vision
zukun
 
PDF
Modern features-part-4-evaluation
zukun
 
PDF
Modern features-part-3-software
zukun
 
PDF
Modern features-part-2-descriptors
zukun
 
PDF
Modern features-part-1-detectors
zukun
 
PDF
Modern features-part-0-intro
zukun
 
PDF
Lecture 02 internet video search
zukun
 
PDF
Lecture 01 internet video search
zukun
 
PDF
Lecture 03 internet video search
zukun
 
PDF
Icml2012 tutorial representation_learning
zukun
 
PPT
Advances in discrete energy minimisation for computer vision
zukun
 
PDF
Gephi tutorial: quick start
zukun
 
PDF
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
PDF
Object recognition with pictorial structures
zukun
 
PDF
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 
My lyn tutorial 2009
zukun
 
ETHZ CV2012: Tutorial openCV
zukun
 
ETHZ CV2012: Information
zukun
 
Siwei lyu: natural image statistics
zukun
 
Lecture9 camera calibration
zukun
 
Brunelli 2008: template matching techniques in computer vision
zukun
 
Modern features-part-4-evaluation
zukun
 
Modern features-part-3-software
zukun
 
Modern features-part-2-descriptors
zukun
 
Modern features-part-1-detectors
zukun
 
Modern features-part-0-intro
zukun
 
Lecture 02 internet video search
zukun
 
Lecture 01 internet video search
zukun
 
Lecture 03 internet video search
zukun
 
Icml2012 tutorial representation_learning
zukun
 
Advances in discrete energy minimisation for computer vision
zukun
 
Gephi tutorial: quick start
zukun
 
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
Object recognition with pictorial structures
zukun
 
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 

Recently uploaded (20)

PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Landforms and landscapes data surprise preview
jpinnuck
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Understanding operators in c language.pptx
auteharshil95
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 

Scientific Programming in Python

  • 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