I have a simple data;
x = numpy.array([1,2,3,
4,5,6,
7,8,9,
10,11,12,
13,14,15,
16,17,18,
19,20,21,
22,23,24])
y = numpy.array([2149,2731,3397,
3088,2928,2108,
1200,659,289,
1141,1726,2910,
4410,5213,5851,
5817,5307,4314,
3656,3081,3103,
3535,4512,5584])
I can create linear regression and make guess with this code:
z = numpy.polyfit(x, y, 1)
p = numpy.poly1d(z)
But I want to create non linear regression of this data and draw graph with code like this:
import matplotlib.pyplot as plt
xp1 = numpy.linspace(1,24,100)
plt.plot(x, y, 'r--', xp1, p(xp1))
plt.show()
I saw a code like this but that couldn't help me:
def func(x, a, b, c):
return a*np.exp(-b*x) + c
...
popt, pcov = curve_fit(func, x, y)
...
So what's the code for making non linear regression and what can i make some guesses with non linear equation?
curve_fit, which takes aguessas an (optional) fourth argumentp0:curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw)If you were to use the sine+linear model suggested by @Alec, yourp0would be a tuple of the guesses for the parametersa,b, etc:p0 = (1, .2, -2, 200, 1000)z = numpy.polyfit(x, y, 1)withz = numpy.polyfit(x, y, 5)and voila.x. If all you wanted to do was to estimate the value at some otherx, then interpolation is probably better for your purposes.scipy.interpolate