代码之家  ›  专栏  ›  技术社区  ›  gabboshow

关于回归的均方根误差的计算

  •  2
  • gabboshow  · 技术社区  · 6 年前

    假设我得到了下面的熊猫数据框架用于回归分析。

    import pandas
    import math
    import numpy
    
    df = pandas.DataFrame(numpy.random.randint(0,100,size=(100, 2)), columns=['labels','predictions'])
    

    我现在想把RMSE计算为

    math.sqrt(numpy.mean((df["predictions"] - df["lables"]) ** 2)) 
    

    对于间隔为7的标签值

    这是一个非常难看的代码,能完成这项工作……如果你能帮我把它弄脏,那就太好了……

    # define step
    step = 7
    # initialize counter
    idx = 0
    # initialize empty dataframe
    rmse = pandas.DataFrame(columns=['bout' , 'rmse'],index=range(0,len(range(int(df['labels'].min())+step,int(df['labels'].max()),step))))
    
    # start loop to calculate rmse every 7 units
    for i in range(int(df['labels'].min())+step,int(df['labels'].max()),step):
    
        # select values in interval
        df_bout = df[(df['labels']>=i-step) & (df['labels']<i)]
    
        # calculate rmse in interval
        rmse.loc[idx] = [str(i-step)+'-'+str(i),math.sqrt(numpy.mean((df_bout.predictions - df_bout.labels) ** 2))]
    
        # increment counter
        idx = idx + 1
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Lukas Thaler    6 年前

    我为一开始的误会道歉。下面的代码片段给出了您希望的结果

    from sklearn.metrics import mean_squared_error
    import pandas
    import math
    import numpy
    
    df = pandas.DataFrame(numpy.random.randint(0, 100, size = (100, 2)), columns = ['labels','predictions']).sort_values(by = 'labels', ascending = True)
    def rmse(df):
        return numpy.sqrt(mean_squared_error(df['labels'], df['predictions']))
    
    output = df.groupby(numpy.floor(numpy.array(df['labels'] / 7))).apply(rmse)
    rmse_df = pandas.DataFrame({'bout': [str(int(output.index[i] * 7)) + ' - ' + str(int(output.index[i + 1] * 7)) for i in range(output.shape[0] - 1)], 'rmse': output.values[:-1]})
    

    您可以为变量更改我的代码中的7s step 如果要动态更改步骤大小