我想使用该函数编写一个自定义评分函数make_scorer,其中我custom_function(y_true, y_pred)计算和输出的 DAILY sumproduct y_true,y_pred例如平均值。问题是时间戳在我的X矩阵中作为一项功能可用,我无法访问当前文件夹的索引。
有谁知道如何使这成为可能?
我想使用该函数编写一个自定义评分函数make_scorer,其中我custom_function(y_true, y_pred)计算和输出的 DAILY sumproduct y_true,y_pred例如平均值。问题是时间戳在我的X矩阵中作为一项功能可用,我无法访问当前文件夹的索引。
有谁知道如何使这成为可能?
你应该能够做到这一点,但没有make_scorer.
用于 sklearn 中的超参数搜索的“评分对象”(由 产生的那些make_scorer)具有签名(estimator, X, y)。make_scorer与具有签名的指标/分数/损失进行比较,例如用作 的输入的那些(y_true, y_pred)。
所以解决方案就是直接定义自己的“评分对象”,并引用传递X来做你想要的计算。有关更多详细信息,请参阅用户指南。
一种选择是创建一个自定义评分函数,按天计算损失和分组。
这是一个粗略的开始:
import numpy as np
from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV
def custom_loss_function(model, X, y):
y_pred = clf.predict(X)
y_true = y
difference = y_pred-y_true
group_timestamp = X[0] # Timestamp column
score_by_day = np.array([difference[group_timestamp==i].sum() for i in np.unique(group_timestamp)]) # Groupby
score_overall = np.mean(score_by_day)
return score_overall
custom_scorer = make_scorer(custom_loss_function, greater_is_better=True)
GridSearchCV(model,
param_grid=param_grid,
scoring=custom_scorer)