In [1]:
import scipy.optimize as spicy
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def myfunction(x, a, b):
    return a*x + b

t = np.linspace(-1, 1, 20)
sigma = 0.3*np.ones_like(t)
y = myfunction(t, 1.0, 10.) + sigma*np.random.normal(0, 1, 20)

In [2]:
fit = spicy.curve_fit(myfunction, t, y, p0=[1.0, 1.0], sigma=sigma)

In [3]:
abest, bbest = fit[0]
plt.scatter(t, y)
plt.plot(t, myfunction(t, abest, bbest))


Out[3]:
[<matplotlib.lines.Line2D at 0x7fa50443e250>]

In [5]:
print fit[0]


[ 0.97365672  9.94589991]

In [ ]: