Set sklearn function loss function

I want to make a forecast in a data science project, and the error is calculated through an asymmetric function.

Is it possible to adjust the loss function of a random increase or increase in the gradient (sklearn)?

I read that I need to modify the .pyx file, but I cannot find it in my sklearn folder (I am on ubuntu 14.04 LTS).

Do you have any suggestions?

+4
source share
2 answers

Yes, you can customize. For instance:

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 
+3
source

You do not need to change anything from any file.

Changing the .py file is usually bad and should be avoided.

If you want to create your own scoring function, here is a link to the sklearn documentation that shows how to do it.

+1
source

All Articles