I asked this question in stack Overflow, but no one gave me an answer.I managed to optimize a line in order to get a line of best fit using curve_fit, but I can't seem to get the R squared value the way I can for linear regression, this is my code:
import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline from scipy.optimize import * from scipy.integrate import * from scipy.interpolate import * df=pd.read_csv('F:/Data32.csv') df2=df['Temperature'] df3=df['CO2-Rh'] def f(x,a,b,c) : return a*np.exp(b*x)+c params, extras = curve_fit(f, df2, df3) print('a=%g,b=%g, c=%g' %(params[0],df2[1],df3[2])) plt.plot(df2,df3,'o') plt.plot(df2,f(df2,params[0],params[1],params[2])) plt.legend(['data','fit'],loc='best') plt.show()
Contents
hide
Best Answer
You can obtain an estimate of $R^2$ for non linear regression by calculate the square of the correlation value between the fitted values and the real values of the response variable.