1

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!

HeadOverFeet
  • 768
  • 6
  • 13
  • 33
  • 1
    Look at this [question](https://stackoverflow.com/questions/893657/how-do-i-calculate-r-squared-using-python-and-numpy?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – it's-yer-boy-chet May 30 '18 at 20:21

0 Answers0