Search code examples
pythonpandasnumpylinear-regression

Trying to calculate de equation for linear regression with numpy.polyfit but getting too many values to unpack (expected 2)


I'm expected to read a column "Horas Estudo" and a number(x) = 5.95 then calculate the y= ax + b.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv("https://www.dropbox.com/s/k3owtgaywmjhk66/fake-classrooms-correl03.csv?dl=1")

col = input()
x = round(float(input()),2)

(a,b) = np.polyfit(df[col],df["Nota Final"], deg=2)
print(a,b)

ValueError                                Traceback (most recent call last)

<ipython-input-81-7e077f926863> in <module>()
      7 x = round(float(input()),2)
      8 
----> 9 (a,b) = np.polyfit(df[col],df["Nota Final"], deg=2)
     10 print(a,b)

ValueError: too many values to unpack (expected 2)





Solution

  • np.polyfit returns the polynomial coefficients in the form of an ndarray and not a tuple. So put a single variable on the left and print it. Also if you are trying to find a linear fit (y=ax+b), put the deg=1 in the function call