Scipy tutorial

Interactive documentation

>>> import scipy
>>> from scipy.interpolate import splrep


>>> scipy.info(splrep)
 splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None,
        full_output=0, per=0, quiet=1)

Find the B-spline representation of 1-D curve.

Description:

  Given the set of data points (x[i], y[i]) determine a smooth spline
  approximation of degree k on the interval xb <= x <= xe.  The coefficients,
  c, and the knot points, t, are returned.  Uses the FORTRAN routine
  curfit from FITPACK.

Inputs:

  x, y -- The data points defining a curve y = f(x).
  w -- Strictly positive rank-1 array of weights the same length as x and y.
       The weights are used in computing the weighted least-squares spline
...
>>> help(splrep)
Help on function splrep in module scipy.interpolate.fitpack:

splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, full_output=0, per=0, quiet=1)
    Find the B-spline representation of 1-D curve.

    Description:

      Given the set of data points (x[i], y[i]) determine a smooth spline
      approximation of degree k on the interval xb <= x <= xe.  The coefficients,
      c, and the knot points, t, are returned.  Uses the FORTRAN routine
      curfit from FITPACK.

    Inputs:

      x, y -- The data points defining a curve y = f(x).
      w -- Strictly positive rank-1 array of weights the same length as x and y.
           The weights are used in computing the weighted least-squares spline
           fit. If the errors in the y values have standard-deviation given by the
...
>>> scipy.source(splrep)
In file: /usr/lib64/python2.6/site-packages/scipy/interpolate/fitpack.py

def splrep(x,y,w=None,xb=None,xe=None,k=3,task=0,s=None,t=None,
           full_output=0,per=0,quiet=1):
    """Find the B-spline representation of 1-D curve.
...

Packages

Packages name description
cluster clustering algorithms
common functions which are common***
cow cluster of workstations code for parallel programming
fftpack fft based on fftpack
ga genetic algorithms
gplt plotting (Gnuplot)
helpmod help module
integrate integration***
interpolate interpolation***
io input output
linalg linear algebra***
optimize optimization and root-finding routines***
pilutil functions which need the PIL
plt plotting package (wxPython)
scipy_version scipy version
signal signal processing
sparse sparse matrix
special special functions
xplt plotting (gist)

Integration

Load modules:

>>> from numpy import *
>>> from scipy import integrate
help:
>>> help(scipy.integrate)
...

Methods for Integrating Functions given function object:

>>> integrate.quad(cos,0,pi/2.)
(0.99999999999999989, 1.1102230246251564e-14)

Methods for Integrating Functions given fixed samples:

>>> x = arange(0,pi/2.,pi/100.)
>>> y = cos(x)
>>> integrate.trapz(y,x)                  # trapeze
0.9994243528939043
>>> integrate.simps(y,x)                  # simpson
0.99950521309271634

Optimization

Load modules:

>>> from numpy import *
>>> from scipy.optimize import leastsq

Compute residuals:

>>> def residuals(p, x, y):
...   a,b = p
...   err = y-(a*x + b)
...   return err
...
>>> x = array([1,2,3,4],float)
>>> y = array([2.1,3,3.9,5.2],float)
>>> p0 = 1.,1.
>>> plsq,cmt = leastsq(residuals, p0, args=(x, y))
>>> a = plsq[0]
>>> b = plsq[1]

Plot values:

>>> import pylab as pt
>>> pt.plot(x,y,'x',x,a*x+b)
>>> pt.show()

Root finding routines:

>>> from scipy.optimize import newton
>>> def fct(x):
...   return x+1
...
>>> x0 = 0
>>> newton(fct,x0)
-1.0

>>> from scipy.optimize import bisect
>>> xmin=-5
>>> xmax=5
>>> bisect(fct,xmin,xmax)
-1.0000000000003979

Interpolation

Load modules:

>>> from numpy import *
>>> from scipy import interpolate
help:
>>> help(interpolate)
...

Interpolate points:

>>> x = arange(10)
>>> y = cos(x)
>>>
>>> ay = interpolate.splrep(x,y,s=1)
>>>
>>> xi = arange(0,10,0.1)
>>> yi = interpolate.splev(xi,ay)
>>>
>>> import pylab as pt
>>> pt.plot(x,y,'x',xi,yi)
>>> pt.show()