Search code examples
pythonpandasmatplotliblinear-regressioncurve-fitting

I get the following error when trying to plot modelled data: ValueError: Length of passed values is 2, index implies 9. How to fix this?


I am trying to plot observed and modelled data. This is an example of dataframe and below the code. I get this error #### ValueError: Length of passed values is 2, index implies 9####. I don't know how else to plot the modelled data.

all = [treatment1, treatment2]

 [    x     y   intercept 
0     25  0.171144     1.6
1     50  1.859164     1.6
2    100  4.407867     1.6
      x     y       intercept 
14    25  0.997440     1.3
15    50  1.823077     1.3  
16   100  4.412220     1.3]



def model(x, slope):
 return  (slope*x) + intercept 

def func_fit(x):
 return model(x,  
            popt)


from scipy.optimize import curve_fit

for g in all: #this loop allows to fit the model for each treatment

popt = curve_fit(model, g['x'], g['y'])

new_row = {'treatment':treatment, 'slope': popt[0], 'intercept':intercept}

results=results.append(new_row, ignore_index=True)

plt.plot(g['x'],  g['y'], '.', label='data')

plt.plot(g['x'], func_fit(g['x']), 'r-', tuple(popt)) # this is the line that gives the error. 

#### ValueError: Length of passed values is 2, index implies 9####

#I tried to overcome the error but what I get with these lines below is no line
#t= np.linspace(0,200,1)
#a = model(t,results.iloc[0,1])
#plt.plot(t, a, 'r') # plotting t, a separately

These are the observations and I would like to plot the modelled data as well:

observations

modelled data


Solution

  • I assume you want to plot the function your model learned?
    If this is the case here is a simple implementation using the code snippets from the Official docs scipy.optimize.curve_fit:

    data = [
        [25, 0.171144, 1.6],
        [50, 1.859164, 1.6],
        [100, 4.407867, 1.6]]
    
    df = pd.DataFrame(data, columns=['x', 'y', 'intercept'])
    
    def model(x, slope, intercept):
        return  (slope*x) + intercept 
    
    
    
    xdata=df['x']
    ydata=df['y']
    popt, _ = curve_fit(model, xdata, ydata)
    
    plt.plot(xdata, ydata, 'o', label='data')
    plt.plot(xdata, model(xdata, *popt), 'r-',
             label='fit: slope=%5.3f, intercept=%5.3f' % tuple(popt))
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.show()
    

    Leads to this result: Result