Menu

[r6320]: / trunk / py4science / examples / fitting.py  Maximize  Restore  History

Download this file

76 lines (58 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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 np
import matplotlib as mpl
import matplotlib.pyplot as plt
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
best, mesg = leastsq(errfunc, guess)
print 'Least-squares fit to the data'
print 'true', pars_true
print 'best', best
print '|err|_l2 =',np.linalg.norm(pars_true-best)
# scipy's splrep uses FITPACK's curfit (B-spline interpolation)
print
print 'Spline smoothing of the data'
sp = splrep(x_vals,y_noisy)
smooth = splev(x_vals,sp)
print 'Spline information (see splrep and splev for details):',sp
# Polynomial fitting
def plot_polyfit(x,y,n,fignum=None):
""" """
if fignum is None:
fignum = plt.figure().number
plt.plot(x,y,label='Data')
fit_coefs = np.polyfit(x,y,n)
fit_val = np.polyval(fit_coefs,x)
plt.plot(x,fit_val,label='Polynomial fit, $n=%d$' % n)
plt.legend()
return fignum
# Now use pylab to plot
plt.figure()
plt.plot(x_vals,y_noisy,label='Noisy data')
plt.plot(x_vals,func(best),lw=2,label='Least-squares fit')
plt.legend()
plt.figure()
plt.plot(x_vals,y_noisy,label='Noisy data')
plt.plot(x_vals,smooth,lw=2,label='Spline-smoothing')
plt.legend()
fignum = plot_polyfit(x_vals,y_noisy,1)
plot_polyfit(x_vals,y_noisy,2,fignum)
plot_polyfit(x_vals,y_noisy,3,fignum)
plt.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.