Code Search for Developers
 
 
  

fitting_skel.py from matplotlib at Krugle


Show fitting_skel.py syntax highlighted

#!/usr/bin/env python
"""Simple data fitting and smoothing example"""

from numpy import exp,arange,array,linspace
from numpy.random import normal

from scipy.optimize import leastsq
from scipy.interpolate import splrep,splev

import numpy as N
import scipy as S
import pylab as P

def func(pars):
    a, alpha, k = pars
    return a*exp(alpha*x_vals) + k

def errfunc(pars):
    """Return the error between the function func() evaluated""" 
    return y_noisy - func(pars)  #return the error

# Use globals for the x values and true parameters
pars_true = array([2.0, -.76, 0.1])
x_vals = linspace(0, 4, 1000)

# some pseudo data; add some noise
y_noisy = func(pars_true) + normal(0.0, 0.1, x_vals.shape)

# the intial guess of the params
guess = 1.0, -.4, 0.0

# now solve for the best fit paramters

#XXX - use leastsq() here, call the output 'best' for code below to use

print 'Least-squares fit to the data'
print 'true', pars_true
print 'best', best
print '|err|_l2 =',P.l2norm(pars_true-best)

# scipy's splrep uses FITPACK's curfit (B-spline interpolation)
print
print 'Spline smoothing of the data'
sp = # XXX - use splrep()
smooth = # XXX use splev()

print 'Spline information (see splrep and splev for details):',sp

# Polynomial fitting
def plot_polyfit(x,y,n,fignum=None):
    """Do a polynomial fit of order n and plot it."""
    if fignum is None:
        fignum = P.figure().number
        P.plot(x,y,label='Data')
        
    fit_coefs = # XXX- use N.polyfit here
    fit_val = # XXX - use N.polyval
    P.plot(x,fit_val,label='Polynomial fit, $n=%d$' % n)
    P.legend()
    return fignum

# Now use pylab to plot
P.figure()
# Plot the least-squares fit here...
P.plot(x_vals,y_noisy,label='Noisy data')
P.plot(x_vals,func(best),lw=2,label='Least-squares fit')
P.legend()

P.figure()
# Plot the splines fit here...

# Plot the polynomials fits with this:
fignum = plot_polyfit(x_vals,y_noisy,1)
plot_polyfit(x_vals,y_noisy,2,fignum)
plot_polyfit(x_vals,y_noisy,3,fignum)

P.show()




See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  faces/
    faces_skel0.tgz
    fmatch_skel1.py
  fortran_wrap/
    Makefile
    fib3.f
    test.py
  distributions_skel.py
  erathostenes_skel.py
  fft_imdenoise_skel.py
  fit_synapse_skel.py
  fitting_skel.py
  montecarlo_pi_skel.py
  polyroots1d_skel.py
  qsort_skel.py
  quad_newton_skel.py
  recarray_demo_skel.py
  regress_demo_skel.py
  scrape_key_stats_skel.py
  shoot_skel.py
  spline_demo_skel.py
  stats_descriptives_skel.py
  stats_distributions_skel.py
  trapezoid_skel.py
  wallis_pi_skel.py
  wordfreqs_skel.py