Sunday, April 27, 2014

Fitting data with SciPy using curve_fit

Suppose that you have a data set consisting of Throughput vs Latency data for your experiment. We’ll start by importing the needed libraries and defining a fitting function. in this example we define a quadratic function. you may define your own function:

from scipy.optimize import curve_fit
def fitFunc(x, a, b, c):
    return a*(x**2)+b*x + c

now we create some points:
y=[25.6, 31.21, 36.82, 42.43, 44.67, 46.91, 48.04, 49.15, 51.2] 
x=[26.0, 41.0, 50.0, 62.5, 69.0, 74.0, 75.050000000000182, 109.0, 98.5]

The scipy.optimize module contains a least squares curve fit routine that requires as input a user-defined fitting function (in our case fitFunc ), the x-axis data (in our case, t) and the y-axis data (in our case, noisy). The curve_fit routine returns an array of fit parameters, and a matrix of covariance data.

tt = np.linspace(min(x),max(x),100)
x=numpy.asarray(x)
y=numpy.asarray(y)
fitParams, fitCovariances = curve_fit(fitFunc, x, y)
print fitParams
print fitCovariances
plt.ylabel('Throughput (%)', fontsize = 16)
plt.xlabel('Latency' , fontsize = 16)
'''plot the real data'''
plt.plot(t,noisy,'b+',markersize=19)   
'''plot the best curving fit'''
plt.plot(tt, fitFunc(tt, fitParams[0], fitParams[1], fitParams[2]))

output:
[-0.00420386  0.88363413  3.96364659]
[[  4.78028309e-07  -6.52166801e-05   1.92964390e-03]
 [ -6.52166801e-05   9.24484129e-03  -2.86615224e-01]
 [  1.92964390e-03  -2.86615224e-01   9.57342339e+00]]

result:


No comments:

Post a Comment