【问题标题】:Python make_scorer giving incorrect outputs for Root Mean Square Logarithmic ErrorPython make_scorer 为均方根对数误差提供不正确的输出
【发布时间】:2021-01-16 21:04:25
【问题描述】:

我想生成一个网格搜索,我需要 scoring 参数来进行搜索。我定义了以下函数来为我提供Root Mean Squared Logarithmic Error。但我觉得得分手正在考虑更大的值是一个更好的分数,而它应该认为较低的值是更好的分数。如果我定义了正确的记分员,请告诉我。

RMSLE 函数

def score_func(y_true, y_pred, **kwargs):
  y_true = np.abs(y_true)
  y_pred = np.abs(y_pred)

  return np.sqrt(mean_squared_log_error(y_true, y_pred))

scorer = make_scorer(score_func)

我必须在上面的代码中使用np.abs,否则会报错,当Target为负值时RMSLE不能使用。

【问题讨论】:

标签: python numpy machine-learning scikit-learn


【解决方案1】:

给你:

import math

#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

【讨论】:

  • 您能否评论一下我为计算相同而创建的函数。对吗?
  • 是的@IshanDutta
  • 为了更好地了解底层计算,我建议使用我提到的解决方案。
猜你喜欢
  • 2016-06-23
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2021-06-15
  • 2016-12-28
相关资源
最近更新 更多