How to fix the error: too many values to unpack (expected3)

1.1k views Asked by At

I am trying to fit a polynomial regression model to a dataset. When I run the following code I get the error: too many values to unpack (3). How can I solve this problem?

    return (a * x) + (b * x**2) + c

x,y = xdata, ydata
popt, _ = curve_fit(objective, x, y, maxfev = 2000)
a,b,c  = popt
print('y = %.5f * x + %.5f * x^2 + %.5f' % (a, b, c))
pyplot.scatter(x,y)
x_line = arrange(min(x), max(x), 1)
y_line = objective(x_line, a, b, c)
pyplot.plot(x_line, y_line, '--', color='red')
pyplot.show()

As you can see below in the screenshot I get the error when I run the code.

Screenshot of the error in Jupyter

Any suggestions?

1

There are 1 answers

0
Homer512 On
  1. Please include your imports or make them obvious. Searching for all possible curve_fit functions that you may have called is kind of annoying

  2. As someone else remarked, your problem is that you try to unpack 3 values from a tuple or list that has fewer entries.

  3. Specifically, popt is the set of optimized parameters. scipy.optimize.curve_fit determines the number of parameters through introspection. I assume that your objective function has only one or two parameters.