Differences between revisions 5 and 6
Revision 5 as of 2010-11-01 10:22:44
Size: 3190
Editor: anonymous
Comment:
Revision 6 as of 2010-11-01 10:25:31
Size: 4145
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 77: Line 77:
= Arrays =
Line 78: Line 79:
= Links and References Numpy provides a multidimensional '''array''' data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types. We will learn more about arrays in the next chapter.

== Array creation ==

 * {{{empty((d1,d2),dtype)}}} returns uninitialized array of shape d1,d2.
 * {{{zeros((d1,d2),dtype)}}} returns array of shape d1,d2 filled with zeros
 * {{{ones((d1,d2),dtype)}}} returns array of shape d1,d2 filled with ones
 * {{{array(object,dtype)}}} returns an array from an object, e.g. a list
 * {{{dtype}}} fundamental C data type e.g. uint8, int16, int64, float32, float64

== Array indexing ==

 * {{{A[y,x]}}} returns (y,x) element of the array
 * {{{A[:,x]}}} returns all elements of the y-dimension at x
 * {{{A[y1:y2,x]}}}
 * {{{A[:,:]}}} returns a copy of two dimensional array A
 * {{{A[:,:,0]}}} returns the first sub-image of a 3-dimensional array

  
 



= Links and References =

Python modules

There a several modules which are already included in the standard Python distribution:

Other modules have to be installed seperately, e.g. Numpy, Scipy, Pylab, etc.

Over 8000 special purpose modules and scripts can be found in the Python Package Index:

It is important to know where to find the functions that you need. We will go through some useful examples.

Numpy, Scipy and Pylab

Numpy

Numpy is the core library for multidimensional array objects (ndarray) and linear algebra. Most other scientific modules use the numpy array object. Numpy arrays and standard Python sequences have important differences:

  • Numpy arrays have a fixed size at creation, unlike Python lists
  • Numpy arrays facilitate mathematical operations on large numbers of data efficiently

The User's guide PDF provides a good introduction.

Scipy

The Scipy module is built on Numpy and offers a collection of mathematical algorithms such as

  • Clustering algorithms
  • Fast Fourier Transform routines
  • Integration and ordinary differential equation solvers
  • Interpolation and smoothing splines
  • Linear algebra
  • Maximum entropy methods
  • N-dimensional image processing and signal processing
  • Optimization and root-finding routines
  • Statistical distributions and functions

Furthermore it includes very handy routines for data input and output.

Pylab

Pylab (aka Matplotlib) uses Numpy and Scipy and offers high-level functions that are similar in the name and syntax to those offered by Matlab. Matplotlib is the name of the core library and pylab provides the Matlab similarity. Pylab produces figures of great quality suitable for publications.

Making plots is easy. Start reading the User's guide. For a specific problem look at the Gallery for a similar plot you would like to have and learn from the source code.

Importing Numpy, Scipy, Pylab

The statement

from pylab import *

imports the most important functions/objects for numerical computation and plotting.

For more complex applications it is useful but not necessary to follow the conventions that the community has adopted

   1 import numpy as np
   2 import scipy as sp
   3 import matplotlib as mpl
   4 import matplotlib.pyplot as plt

Arrays

Numpy provides a multidimensional array data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types. We will learn more about arrays in the next chapter.

Array creation

  • empty((d1,d2),dtype) returns uninitialized array of shape d1,d2.

  • zeros((d1,d2),dtype) returns array of shape d1,d2 filled with zeros

  • ones((d1,d2),dtype) returns array of shape d1,d2 filled with ones

  • array(object,dtype) returns an array from an object, e.g. a list

  • dtype fundamental C data type e.g. uint8, int16, int64, float32, float64

Array indexing

  • A[y,x] returns (y,x) element of the array

  • A[:,x] returns all elements of the y-dimension at x

  • A[y1:y2,x]

  • A[:,:] returns a copy of two dimensional array A

  • A[:,:,0] returns the first sub-image of a 3-dimensional array

Links and References

LehreWiki: OpenSource2010/Lesson3 (last edited 2010-11-01 14:03:52 by anonymous)