计算回归的根均方误差

2024-04-27 03:01:14 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我得到了下面的数据框,用于回归分析。你知道吗

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的标签值

这是一个非常难看的代码,它做的工作…这将是很好的,如果你帮我把它pythonize。。。你知道吗

# 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

Tags: inimportnumpydataframepandasdflabelsstep
1条回答
网友
1楼 · 发布于 2024-04-27 03:01:14

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

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]})

如果要动态更改步长,可以更改变量step的“我的代码”中的7

相关问题 更多 >