I want to determine if it is better linear or polynom 2nd regression.
I use this function to determine new values:
def poly_fit(id,x, y,z,p,n):
#id- id for matching with orginal table
# x- sample for polyfit
# y- sample for polyfit
# z- all data to get coplete fit , x is subset of best samples to get proper fit, z is entire dataset
# p- all prices in case of wrong polifit use original data
# n- polifit determinant
df_poly = pd.DataFrame()
df_poly.empty
df_poly['id']=id
df_poly=df_poly.set_index('id')
results = {}
coeffs = numpy.polyfit(x, y, n)
# Polynomial Coefficients
results['polynomial'] = coeffs.tolist()
# r-squared
p = numpy.poly1d(coeffs)
try:
poly= np.polyfit(x, y,n)
df_poly['new_fit']=np.poly1d(poly)(z)
except Exception as e:
df_poly['new_fit']=z
return df_poly
Please, how can be this code adjusted to calculate R2 and R2 adjusted via numpy and pandas?
I tried, bu I dont think p = numpy.poly1d(coeffs) is ok for R2...
def poly_fit_r2(id,x, y,z,p,n):
#id- id for matching with orginal table
# x- sample for polyfit
# y- sample for polyfit
# z- all data to get complete fit , x is subset of best samples to get proper fit, z is entire dataset
# p- all prices in case of wrong polyfit use original data
# n- polifit determinant
df_poly = pd.DataFrame()
df_poly.empty
df_poly['id']=id
df_poly=df_poly.set_index('id')
results = {}
try:
poly= np.polyfit(x,y,n)
df_poly['new_fit']=np.poly1d(poly)(z)
coeffs = numpy.polyfit(x, y, n)
# Polynomial Coefficients
results['polynomial'] = coeffs.tolist()
# r-squared
p = numpy.poly1d(coeffs)
coefficient_of_dermination = sklearn.r2_score(y, p(x))
df['poly'] = coefficient_of_dermination
except Exception as e:
df_poly['new_fit']=z
df['poly'] = 0
return df_poly
Thanks!