Customising loss functions in scikit learn

3.8k views Asked by At

How can I customise loss functions in scikit learn? For example, instead of using mean square error, I want to use MSE multiplied by the true value of the sample. I have used the following code snippet:

def my_custom_loss_func(y_true,y_pred):
    diff3=(abs(y_true-y_pred))*y_true
    return diff3

clf=RandomForestRegressor(criterion=my_custom_loss_func)
knn=clf.fit(feam,labm)

I get the following error:

KeyError: <function my_custom_loss_func at 0x000000002EA9CA60>
1

There are 1 answers

5
Franco Piccolo On

You can customize loss functions in scikit learn, for this you need to apply the make_scorer factory to your custom loss function like:

from sklearn.metrics import make_scorer
score = make_scorer(my_custom_loss_func, greater_is_better=False)

In your particular case with Random Forests though you can't customize the criterion, what you could do is to optimize the hyperparameters with GridSearchCV and there you could use your custom loss.