我已经通过以下方式编写了rmsse自定义损失函数
def wrmsse(preds, y_true,store_name):
'''
preds - Predictions: pd.DataFrame of size (30490 rows, N day columns)
y_true - True values: pd.DataFrame of size (30490 rows, N day columns)
sequence_length - np.array of size (42840,)
sales_weight - sales weights based on last 28 days: np.array (42840,)
'''
preds = preds[-(30490 * 28):]
y_true = y_true.get_label()[-(30490 * 30490):]
preds = preds.reshape(28, 30490).T
y_true = y_true.reshape(28, 30490).T
sw = list(SW_store.keys())[key]
return 'wrmsse', np.sum(np.sqrt(np.mean(np.square(rollup(preds-y_true)),axis=1)) * sw)/12,False #<-used
我正在训练如下模式
model =
store_name = 'CA_1 lgbm.train(params,train_set=train_set,num_boost_round=2500,early_stopping_rounds=50,valid_sets=val_set,verbose_eval = 100, feval= wrmsse)
我想将商店名称作为参数传递,怎么办?
答案 0 :(得分:0)
您可以通过将您的自定义 ndarray
附加到数据集来实现,
例如,在声明数据集后,设置自定义类属性,
dtrain = lgb.Dataset(X_train, y_train, feature_name =feature_names, categorical_feature=categorical_feature, free_raw_data=False)
dval = lgb.Dataset(X_val, y_val, reference=dtrain, feature_name =feature_names, categorical_feature=categorical_feature, free_raw_data=False)
dtrain.indexes = np.arange(0, X_train.shape[0])
dval.indexes = np.arange(0, X_val.shape[0])
这里的索引是我想在度量中使用的自定义数组,
然后,在您的度量函数中,将您的自定义数组作为闭包传递并使用索引访问它们,
def utility_score(weight, resp, date_):
def func(preds, train_data):
score = 0.
labels = train_data.get_label()
indexes = train_data.indexes
y_pred = preds.reshape(-1, 1)
weight_ = weight[indexes, :]
resp_ = resp[indexes, :]
date__ = date_[indexes, :]
# do whatever with ur custom vars and calculate score....
return 'utility', score, True
return func
像这样使用它,
feval=utility_score(weight, resp, date_)