You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HistGradientBoosting: Implement custom loss function like LightGBM permits it #15841
I think this is something we could consider supporting, at least in a private API. A loss class is simply a bunch of methods and a bunch of attributes.
We would need to wait for #14830 to be merged since it slightly changes the API
@flamby we have implemented support for this and this should be out in the next version. Please keep in mind however that this is part of a private API so no backward compatibility is guaranteed:
we might not support this anymore at some point
the current loss API is subject to change, which means that you might have to adjust your own the custom losses in the future
See how it's inheriting from ForestClassifier? Right there in the class definition. Click that word to jump to it's parent definition.
See how this new object is inheriting from ClassifierMixin? Click that.
See how the bottom of that ClassifierMixin class says this?
from .metrics import accuracy_score
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
That's your model being trained on accuracy. You need to inject at this point if you want to train your model to be a "recall model" or a "precision model" or whatever model. This accuracy metric is baked into SKlearn. Some day, a better man than I will make this a parameter which models accept, however in the mean time, you gotta go into your sklearn installation, and tweak this accuracy_score to be whatever you want.
Please avoid posting the same post in 3 different places in the repo. You also posted in #16908 (comment) and in #17659 (comment). If you have a question or something cool to show, please open a new issue or a new discussion
is it possible to add custom loss function to classification tree as well?
You mean regular DecisionTreeClassifier here. They don't use loss functions, they just have a splitting criterion. It's not possible to define a custom splitting criterion for those trees: #17436
This has absolutely nothing to do with the loss of gradient boosting estimators, which is what this issue was about.
Also, the instructions you copy pasted from stack overflow are completely wrong (the question of OP itself is underspecified). The fact that accuracy score is used in ClassifierMixins is just because by default models will be scored with accuracy, or evaluated with that, but never trained with accuracy.
Also you don't need to go deep in the skleran code to change the accuracy scorer. Everywhere where it's relevant, sklearn tools like GridSearchCV will accept a "scoring" parameter which will allow to replace accuracy_score with an arbitrary callable.
Activity
NicolasHug commentedon Dec 9, 2019
I think this is something we could consider supporting, at least in a private API. A loss class is simply a bunch of methods and a bunch of attributes.
We would need to wait for #14830 to be merged since it slightly changes the API
WDYT @ogrisel @adrinjalali ?
ogrisel commentedon Dec 9, 2019
+1 as a private API (no API stability guaranteed from one version of scikit-learn to the next).
adrinjalali commentedon Dec 14, 2019
I don't mind if it's public. It's still experimental, we can still change the API in the future.
NicolasHug commentedon Dec 14, 2019
Let's keep it private for now. Even if the estimators are experimental, it's still not great to make something available and then remove it
NicolasHug commentedon Mar 30, 2020
@gbolmier might be an easy one?
gbolmier commentedon Mar 30, 2020
With pleasure @NicolasHug!
NicolasHug commentedon Mar 30, 2020
Cool!
Basically the idea is to allow the
lossparameter to be aBaseLossobject. But we keep everything private: this should not be documented or anything.Add custom loss support for HistGradientBoosting
NicolasHug commentedon Apr 15, 2020
@flamby we have implemented support for this and this should be out in the next version. Please keep in mind however that this is part of a private API so no backward compatibility is guaranteed:
Sandy4321 commentedon Jun 27, 2021
is it possible to add custom loss function to classification tree as well?
https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html?highlight=tree#sklearn.tree.DecisionTreeClassifier
Sandy4321 commentedon Jun 27, 2021
people really need it
https://stackoverflow.com/questions/54267745/implementing-custom-loss-function-in-scikit-learn
It's tricky, but you can do it...
Open up your classifier. Let's use an RFC for example: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html
click [source]
See how it's inheriting from ForestClassifier? Right there in the class definition. Click that word to jump to it's parent definition.
See how this new object is inheriting from ClassifierMixin? Click that.
See how the bottom of that ClassifierMixin class says this?
from .metrics import accuracy_score
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
That's your model being trained on accuracy. You need to inject at this point if you want to train your model to be a "recall model" or a "precision model" or whatever model. This accuracy metric is baked into SKlearn. Some day, a better man than I will make this a parameter which models accept, however in the mean time, you gotta go into your sklearn installation, and tweak this accuracy_score to be whatever you want.
NicolasHug commentedon Jun 28, 2021
Hi @Sandy4321 , a few things:
Please avoid posting the same post in 3 different places in the repo. You also posted in #16908 (comment) and in #17659 (comment). If you have a question or something cool to show, please open a new issue or a new discussion
You mean regular DecisionTreeClassifier here. They don't use loss functions, they just have a splitting criterion. It's not possible to define a custom splitting criterion for those trees: #17436
This has absolutely nothing to do with the loss of gradient boosting estimators, which is what this issue was about.
Also, the instructions you copy pasted from stack overflow are completely wrong (the question of OP itself is underspecified). The fact that accuracy score is used in ClassifierMixins is just because by default models will be scored with accuracy, or evaluated with that, but never trained with accuracy.
Also you don't need to go deep in the skleran code to change the accuracy scorer. Everywhere where it's relevant, sklearn tools like GridSearchCV will accept a "scoring" parameter which will allow to replace accuracy_score with an arbitrary callable.