lecture7 revised

Upload: lincol88

Post on 01-Jun-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Lecture7 Revised

    1/32

    Numerical Computing in PythonLecture

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    2/32

    Numerical Computing in Python: Review of the Basics

    Weve already seen how python can be used to compute some fairly basic mathematics.

    And that the math module provides some additional functionality.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    3/32

    Numerical Computing in Python: Review of the Basics

    Weve already seen how python can be used to compute some fairly basic mathematics.

    And that the math module provides some additional functionality.

    Today were going to talk about python modules that extend its standard numerical computingabilities.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    4/32

    rom numpy import*

    NumPy provides fast numerical computing methodsfor Python.

    Tuesday, October 20, 2009

    http://numpy.scipy.org/http://numpy.scipy.org/
  • 8/9/2019 Lecture7 Revised

    5/32

  • 8/9/2019 Lecture7 Revised

    6/32

    rom numpy import*

    NumPy provides fast numerical computing methodsfor Python.

    Multi-dimensional arrays Standard matrix/array operations

    Sophisticated random number functionality Access to the fortran-based LINPACK numerical linear algebra software library Ability to integrate Fortran & C/C++ code

    NumPy can also be used as an efcient multi-dimensional container, and has threefundamental objects:

    [1] the ndarray itself [2] the data-type objects describing the layoutof a single xed-size element of the array.[3] the array-scalar Python object that isreturned when a single element is accessed.

    Tuesday, October 20, 2009

    http://numpy.scipy.org/http://numpy.scipy.org/
  • 8/9/2019 Lecture7 Revised

    7/32

    rom numpy import*

    NumPy is primarily used for array functionality:

    Create array like a nested list

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    8/32

    rom numpy import*

    NumPy is primarily used for array functionality:

    Create array like a nested list

    Obtain dimensions of a (rows, columns)

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    9/32

    rom numpy import*

    NumPy is primarily used for array functionality:

    Create array like a nested list

    Obtain dimensions of a (rows, columns)

    Array slicing is a natural extension of list slicing

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    10/32

    rom numpy import*

    Vectors, matrices, and tensors are all arrays.

    vector: [1, 2, 3]matrix:

    [[1, 2, 3],[ 4, 5, 6]]

    tensor:[[[1, 2, 3], [ 4, 5, 6]],[[1, 2, 3], [ 4, 5, 6]]]

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    11/32

    rom numpy import*

    Vectors, matrices, and tensors are all arrays.

    ...but why would we want our data in an array, rather than something else?

    vector: [1, 2, 3]matrix:

    [[1, 2, 3],[ 4, 5, 6]]

    tensor:[[[1, 2, 3], [ 4, 5, 6]],[[1, 2, 3], [ 4, 5, 6]]]

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    12/32

    rom numpy import*

    Main Advantage : a single command can be used to perform basic math operations.

    This can substantially increase the efciency of your code.

    NumPy arrays haveseveral attributes:

    dot(a, b) Matrix product of a and b

    More in the linalg submodule...

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    13/32

    rom numpy import*

    In addition to normal slicing, there are several other array methods for item selection& manipulation

    an order for the indices that would lead to sorting

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    14/32

    rom numpy import*

    abbreviated command summary

    Tuesday, October 20, 2009

    http://numpy.scipy.org/http://numpy.scipy.org/
  • 8/9/2019 Lecture7 Revised

    15/32

    Random numbers

    abbreviated summary of array methods

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    16/32

    Random numbers

    Two solutions:

    >>> import random

    >>> import numpy.random

    One often needs to generaterandom numbers in scientic

    scripting.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    17/32

    Random numbers

    Two solutions:

    >>> import random

    One often needs to generaterandom numbers in scientic

    scripting. We already saw that therandom module can do this.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    18/32

    Random numbers

    Two solutions:

    >>> import random

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    19/32

    Random numbers

    Two solutions: The random module within NumPyProvides vectorized drawing of randomnumbers, in addition to a variety ofadditional methods.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    20/32

    Random numbers

    Two solutions:

    The NumPy random module allowsus to take advantage of powerfularray methods!

    The random module within NumPyProvides vectorized drawing of randomnumbers, in addition to a variety ofadditional methods.

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    21/32

    Random numbers

    Watch out for namespace collision!

    Tuesday, October 20, 2009

  • 8/9/2019 Lecture7 Revised

    22/32

    rom scipy import*

    SciPy provides a variety of scientic tools, and is built to specically work withNumPy arrays

    Tuesday, October 20, 2009

    l l

  • 8/9/2019 Lecture7 Revised

    23/32

    rom scipy import linalgSciPys linalg submodule provides most of the matrix algebra-related operations youll

    want.

    In linear algebra, one frequently needs to solve systems of equations taking the form

    Xb = yusing a procedure called Gaussian Elimination. This becomes verystraightforward with our Python library extensions. For example, if

    we could determine the values in b by

    Tuesday, October 20, 2009

    i i li l

    http://docs.scipy.org/doc/scipy/reference/linalg.htmlhttp://docs.scipy.org/doc/scipy/reference/linalg.htmlhttp://docs.scipy.org/doc/scipy/reference/linalg.html
  • 8/9/2019 Lecture7 Revised

    24/32

    rom scipy import linalg

    linalg.inv(a) Returns the inverse of matrix a

    linalg.pinv(a) Returns the pseudoinverse of singular matrix a linalg.svd(a) Returns U, s, V--results from singular value decomposition

    SciPys linalg submodule provides most of the matrix algebra-related operations youll want.

    Compute the inverse

    Singular Value Decomposition

    Matrix multiplication isincluded in the NumPy module

    Tuesday, October 20, 2009

    i l d

    http://docs.scipy.org/doc/scipy/reference/linalg.htmlhttp://docs.scipy.org/doc/scipy/reference/linalg.html
  • 8/9/2019 Lecture7 Revised

    25/32

    import xlrd

    http://www.python-excel.org/

    Sometimes we need to get dataout of/into excel directly.

    The xlrd & xlwt modules provide this functionality. Both have to be downloaded from their websites and installed.

    On macintosh, youll have toinstall it manually from the zip ortar.gz source le:

    Computery:~ ambertk$ cd DesktopComputery:~ ambertk$ gunzip xlrd-0.7.1.tar.gzComputery:~ ambertk$ tar -xvf xlrd-0.7.1.tarComputery:~ ambertk$ python xlrd-0.7.1/setup.py install

    (for .zip, just unzipand do this line)

    Tuesday, October 20, 2009

    i l d

    http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/
  • 8/9/2019 Lecture7 Revised

    26/32

    import xlrd

    http://www.python-excel.org/

    Lets say weneeded to

    work withan .xlsspreadsheet

    like thisone...

    Tuesday, October 20, 2009

    http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/http://www.python-excel.org/
  • 8/9/2019 Lecture7 Revised

    27/32

    Scenario

  • 8/9/2019 Lecture7 Revised

    28/32

    Scenario.

    Projecting from three dimensions to two is one type of linear transformation. We canuse a 2x3 matrix P whose columns dictate where the transformed x, y, and z will be

    located in two dimensions ( u, v).

    Kerl, 2009Tuesday, October 20, 2009

    Scenario

  • 8/9/2019 Lecture7 Revised

    29/32

    Scenario.

    Projecting from three dimensions to two is one type of linear transformation. We canuse a 2x3 matrix P whose columns dictate where the transformed x, y, and z will be

    located in two dimensions ( u, v).

    Lets say we have a 50 x 3 excel le containing a set of x, y, z coordinates that we want to map to two dimensions, and write to a tab-delimited text le.

    Kerl, 2009Tuesday, October 20, 2009

    Scenario

  • 8/9/2019 Lecture7 Revised

    30/32

    Scenario.

    Kerl, 2009Tuesday, October 20, 2009

    Scenario--better code

  • 8/9/2019 Lecture7 Revised

    31/32

    Scenario--better code

    Kerl, 2009Tuesday, October 20, 2009

    misc

  • 8/9/2019 Lecture7 Revised

    32/32

    misc.

    Others that may interest you...

    biopython (http://piopython.org) Bioinformatics et al.

    scipy (http://www.scipy.org) Symbolic mathematics--integration FFT

    matplotlib (http://matplotlib.sourceforge.net/)

    Interactive 2D plotting MATLAB-like interfaceSymPy (http://code.google.com/p/sympy/)

    Computer Alge bra System written in Pythonmlpy (https://mlpy.fbk.eu/)

    General-purpose machine learning using numpy arraysrpy2 (http://rpy.sourceforge.net/rpy2.html)

    python-R interfacing (well talk about this one on Friday...)

    https://mlpy.fbk.eu/https://mlpy.fbk.eu/https://mlpy.fbk.eu/http://www.scipy.org/http://www.scipy.org/http://piopython.org/http://piopython.org/