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>
You can customize loss functions in scikit learn, for this you need to apply the
make_scorerfactory to your custom loss function like:In your particular case with Random Forests though you can't customize the criterion, what you could do is to optimize the hyperparameters with
GridSearchCVand there you could use your custom loss.