Skip to content

[Feature Request] Custom loss functions for GradientBoostingRegressor/Classifier and HistGradientBoostingRegressor/Classifier  #17659

Closed

Description

@zachmayer

Describe the workflow you want to enable

In Xgboost and LightGBM, you can specify a custom objective function (as well as gradients for that objective): https://xgboost.readthedocs.io/en/latest/tutorials/custom_metric_obj.html

It would be pretty cool if Sklearn supported that too

Describe your proposed solution

Use Xgboost / LightGBM instead of sklearn, but who wants to do that 😁

Describe alternatives you've considered, if relevant

Xgboost / LightGBM

Additional context

If you have a problem-specific objective function, it can be really useful to boost directly on that function.

Activity

  1. changed the title [-]Custom loss functions for GradientBoostingRegressor/Classifier and HistGradientBoostingRegressor/Classifier [/-] [+][Feature Request] Custom loss functions for GradientBoostingRegressor/Classifier and HistGradientBoostingRegressor/Classifier [/+] on Jun 22, 2020
  2. thomasjpfan commented on Jun 22, 2020

    @thomasjpfan
    Member

    We opened using custom loss functions in for HistGradientBoosting in #16908

    Support for this feature is summarized here: #15841 (comment)

  3. zachmayer commented on Jun 22, 2020

    @zachmayer
    ContributorAuthor

    Oh awesome! I'm sorry I missed that, that's very cool!

  4. Sandy4321 commented on Jun 27, 2021

    @Sandy4321

    people really need it
    https://stackoverflow.com/questions/54267745/implementing-custom-loss-function-in-scikit-learn

    It's tricky, but you can do it...

    1. Open up your classifier. Let's use an RFC for example: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html

    2. click [source]

    3. See how it's inheriting from ForestClassifier? Right there in the class definition. Click that word to jump to it's parent definition.

    4. See how this new object is inheriting from ClassifierMixin? Click that.

    5. 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.

  5. NicolasHug commented on Jun 28, 2021

    @NicolasHug
    Member

    @Sandy4321 please see #15841 (comment). The instructions copied pasted from SO above are incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions