本文介绍了自定义损失函数 sklearn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个数据科学项目中做预测,误差是通过一个非对称函数计算出来的.
I want to make prediction in a data science project, and the error is calculate through an asymmetric function.
是否可以调整随机森林的损失函数或梯度提升(sklearn)?
Is it possible to tune the loss function of random forest or gradient boosting (of sklearn) ?
我读到需要修改 .pyx 文件,但我在 sklearn 文件夹中找不到任何文件(我使用的是 ubuntu 14.04 LTS).
I have read that it is required to modify a .pyx file but I cannot find any in my sklearn folder (I am on ubuntu 14.04 LTS).
你有什么建议吗?
推荐答案
是的,可以调整.例如:
Yes, it is possible to tune. For example:
class ExponentialPairwiseLoss(object):
def __init__(self, groups):
self.groups = groups
def __call__(self, preds, dtrain):
labels = dtrain.get_label().astype(np.int)
rk = len(np.bincount(labels))
plus_exp = np.exp(preds)
minus_exp = np.exp(-preds)
grad = np.zeros(preds.shape)
hess = np.zeros(preds.shape)
pos = 0
for size in self.groups:
sum_plus_exp = np.zeros((rk,))
sum_minus_exp = np.zeros((rk,))
for i in range(pos, pos + size, 1):
sum_plus_exp[labels[i]] += plus_exp[i]
sum_minus_exp[labels[i]] += minus_exp[i]
for i in range(pos, pos + size, 1):
grad[i] = -minus_exp[i] * np.sum(sum_plus_exp[:labels[i]]) +\
plus_exp[i] * np.sum(sum_minus_exp[labels[i] + 1:])
hess[i] = minus_exp[i] * np.sum(sum_plus_exp[:labels[i]]) +\
plus_exp[i] * np.sum(sum_minus_exp[labels[i] + 1:])
pos += size
return grad, hess
这篇关于自定义损失函数 sklearn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!