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

How to pre-process the data to calculate Root Mean Squared Logarithmic Error?

我正在计算均方根对数误差,因为我找到了几个选项,一个是使用 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 post 的解决方案:

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

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

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

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 并缩放),要么考虑您使用 RMSLE 的原因以及它是否真的是正确的指标。

特征缩放在这里应该是一个不错的选择,这样最小值 >= 0。

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

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

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

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

例如:

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

使用 min-max 缩放器在 (0, x] 之间缩放您的值,其中 x 是您选择的任何值。然后使用它以获得更好的结果。

我有一个类似的问题,其中一个预测是负面的,尽管所有的训练目标值都是正面的。我将其缩小到异常值并使用 sklearn 中的 RobustScaler 解决了它。这不仅可以扩展数据,还可以处理异常值

Scale features using statistics that are robust to outliers.