Vous êtes sur la page 1sur 32

7

Lecture

Numerical Computing in Python

Tuesday, October 20, 2009

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

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 computing
abilities.

Tuesday, October 20, 2009

from numpy import *


NumPy provides fast numerical computing methods
for Python.
<!--Array/matrix computations are done in native code,
allowing numerical operations to proceed as fast as natively compiled C-->

Tuesday, October 20, 2009

from numpy import *


NumPy provides fast numerical computing methods
for Python.
<!--Array/matrix computations are done in native code,
allowing numerical operations to proceed as fast as natively compiled C-->
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

Tuesday, October 20, 2009

from numpy import *


NumPy provides fast numerical computing methods
for Python.
<!--Array/matrix computations are done in native code,
allowing numerical operations to proceed as fast as natively compiled C-->
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 efficient multi-dimensional container, and has three
fundamental objects:
[1] the ndarray itself
[2] the data-type objects describing the layout
of a single fixed-size element of the array.
[3] the array-scalar Python object that is
returned when a single element is accessed.

Tuesday, October 20, 2009

from numpy import *


NumPy is primarily used for array functionality:
Create array like a nested list

Tuesday, October 20, 2009

from 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

from 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

from numpy import *


Vectors, matrices, and tensors are all arrays.

vector:
matrix:
tensor:

Tuesday, October 20, 2009

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

from numpy import *


Vectors, matrices, and tensors are all arrays.

vector:
matrix:
tensor:

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

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

Tuesday, October 20, 2009

from numpy import *


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

This can substantially increase the efficiency of your code.

NumPy arrays have


several attributes:

dot(a, b)
Matrix product of a and b
More in the linalg submodule...

Tuesday, October 20, 2009

from 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

from numpy import *

abbreviated command summary


Tuesday, October 20, 2009

Random numbers

abbreviated summary of array methods

Tuesday, October 20, 2009

Random numbers

Two solutions:
>>> import random
>>> import numpy.random

Tuesday, October 20, 2009

One often needs to generate


random numbers in scientific
scripting.

Random numbers

Two solutions:
>>> import random

One often needs to generate


random numbers in scientific
scripting.
We already saw that the
random module can do this.

Tuesday, October 20, 2009

Random numbers

Two solutions:
>>> import random

Tuesday, October 20, 2009

Random numbers

Two solutions:

Tuesday, October 20, 2009

The random module within NumPy


Provides vectorized drawing of random
numbers, in addition to a variety of
additional methods.

Random numbers

Two solutions:

The random module within NumPy


Provides vectorized drawing of random
numbers, in addition to a variety of
additional methods.

The NumPy random module allows


us to take advantage of powerful
array methods!

Tuesday, October 20, 2009

Random numbers

Watch out for namespace collision!

Tuesday, October 20, 2009

from scipy import *


SciPy provides a variety of scientific tools, and is built to specifically work with
NumPy arrays

Tuesday, October 20, 2009

from scipy import linalg


SciPys 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 = y
using a procedure called Gaussian Elimination. This becomes very
straightforward with our Python library extensions. For example, if

we could determine the values in b by

Tuesday, October 20, 2009

from scipy import linalg


SciPys linalg submodule provides most of the matrix algebra-related operations youll
want.
linalg.inv(a)
linalg.pinv(a)
linalg.svd(a)

Returns the inverse of matrix a


Returns the pseudoinverse of singular matrix a
Returns U, s, V--results from singular value decomposition

Compute the inverse


Singular Value Decomposition

Matrix multiplication is
included in the NumPy module

Tuesday, October 20, 2009

import xlrd

Sometimes we need to get data


out of/into excel directly.
The xlrd & xlwt modules provide
this functionality. Both have to
be downloaded from their web
sites and installed.

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

On macintosh, youll have to


install it manually from the zip or
tar.gz source file:
Computery:~
Computery:~
Computery:~
Computery:~
(for .zip, just unzip
and do this line)

Tuesday, October 20, 2009

ambertk$
ambertk$
ambertk$
ambertk$

cd Desktop
gunzip xlrd-0.7.1.tar.gz
tar -xvf xlrd-0.7.1.tar
python xlrd-0.7.1/setup.py install

import xlrd

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

Lets say we
needed to
work with
an .xls
spreadsheet
like this
one...
Tuesday, October 20, 2009

import xlrd
xlrd example

denotes a unicode string

get information about a specific sheet

get data from a specific sheet

http://www.python-excel.org/
Tuesday, October 20, 2009

Scenario.
Projecting from three dimensions to two is one type of linear transformation. We can
use a 2x3 matrix P whose columns dictate where the transformed x, y, and z will be
located in two dimensions (u, v).

Kerl, 2009
Tuesday, October 20, 2009

Scenario.
Projecting from three dimensions to two is one type of linear transformation. We can
use 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 file containing a set of x, y, z coordinates that we


want to map to two dimensions, and write to a
tab-delimited text file.

Kerl, 2009
Tuesday, October 20, 2009

Scenario.

Kerl, 2009
Tuesday, October 20, 2009

Scenario--better code

Kerl, 2009
Tuesday, October 20, 2009

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 interface
SymPy (http://code.google.com/p/sympy/)
Computer Algebra System written in Python
mlpy (https://mlpy.fbk.eu/)
General-purpose machine learning using numpy arrays
rpy2 (http://rpy.sourceforge.net/rpy2.html)
python-R interfacing (well talk about this one on Friday...)

Tuesday, October 20, 2009

Vous aimerez peut-être aussi