gpt4 book ai didi

python - 如何预处理数据来计算均方根对数误差?

转载 作者:行者123 更新时间:2023-11-30 09:26:34 27 4
gpt4 key购买 nike

我正在尝试计算均方根对数误差,但我发现了几个选项,一种是使用 sklearn 指标:mean_squared_log_error 并取其平方根

np.sqrt(mean_squared_log_error( target, predicted_y ))

但我收到以下错误:

Mean Squared Logarithmic Error cannot be used when targets contain negative values

我还尝试了 Kaggle 帖子中的解决方案:

import math

#A function to calculate Root Mean Squared Logarithmic Error (RMSLE)
def rmsle(y, y_pred):
assert len(y) == len(y_pred)
terms_to_sum = [(math.log(y_pred[i] + 1) - math.log(y[i] + 1)) ** 2.0 for i,pred in enumerate(y_pred)]
return (sum(terms_to_sum) * (1.0/len(y))) ** 0.5

同样的问题,这次我收到域错误。

在同一篇文章中,他们对负面日志问题发表了以下评论:

You're right. You have to transform y_pred and y_test to make sure they don't carry negative values.

In my case, when predicting weather temperature (originally in Celsius degrees), the solution was to convert them to Kelvin degrees before calculating the RMSLE:

rmsle(data.temp_pred + 273.15, data.temp_real + 273.15)

是否有任何使用此指标的标准形式可以使用负值?

最佳答案

将两个数组标准化为 0 到 1 范围

如果您使用 scikit,则可以使用sklearn.preprocessing.minmax_scale:

minmax_scale(arr, feature_range=(0,1))

在执行此操作之前,请保存 arr 的最大值和最小值。您可以取回实际值。

例如:

normalized = (value - arr.min()) / (arr.max() - arr.min()) # Illustration

关于python - 如何预处理数据来计算均方根对数误差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com