如何通过poly_fit numpy,pandas调整R2和R2

时间:2018-05-30 20:10:45

标签: python pandas numpy regression

我想确定它是更好的线性还是多项式第二次回归。

我使用此函数来确定新值:

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

请问,如何调整此代码以计算通过numpy和pandas调整的R2和R2?

我试过了,我不认为p = numpy.poly1d(coeffs)可以用于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

谢谢!

0 个答案:

没有答案