2

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?

5
  • if by 'guess' you mean an estimate of the parameters, you can use those with curve_fit, which takes a guess as an (optional) fourth argument p0: curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw) If you were to use the sine+linear model suggested by @Alec, your p0 would be a tuple of the guesses for the parameters a,b, etc: p0 = (1, .2, -2, 200, 1000) Commented Dec 7, 2013 at 16:16
  • On the other hand, if you just want to do a nonlinear polynomial, you can replace z = numpy.polyfit(x, y, 1) with z = numpy.polyfit(x, y, 5) and voila. Commented Dec 7, 2013 at 16:20
  • Wow, your code works great. And it gives me the equation: 0.1171* x5 - 7.145* x4 + 153.6* x3 - 1369* x2 + 4595* x - 1527 Now this means I can make estimates with any x value I think? Commented Dec 7, 2013 at 21:58
  • Yes, you can evaluate the polynomial at any x. If all you wanted to do was to estimate the value at some other x, then interpolation is probably better for your purposes. scipy.interpolate Commented Dec 7, 2013 at 22:16
  • Thank you very much. Could you please give me a simple example code with a simple data (or my given data) for scipy.interpolate? Commented Dec 7, 2013 at 23:09

1 Answer 1

5

What you are referring to is the scipy module. You are right in that this is probably the module you want to be using.

Then, what you are interested in knowing is how curve_fit(func, x, y) works. The idea is that you want to minimize the difference between some function model (like y = m*x + b for a line) and the points on your model. The func argument represents this model: you are making a function that takes in as its first argument the dependent variable of the model (x in my example) and for all subsequent arguments the parameters of the model (those would be m and b in the case of the linear model). The x and y you have already figured out.

The real problem though, and yes I realize I'm not answering your question, is that you need to figure out manually some sort of model for your data (at least the type of model: exponential, linear, polynomial, etc.). There is no easy way out of that. Judging from your data, though I would go for a model of the form

y = a*sin(b*x + c) + d*x + e

or a 5 degree polynomial.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably you meant y = a*sin(b*x + c) + d*x + e
Sorry, yes. Thanks askewchan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.