Skip to content

[MRG] Make argument sample_weight optional in multinomials - #13024

Closed
david-cortes wants to merge 1 commit into
scikit-learn:masterfrom
david-cortes:master
Closed

david-cortes wants to merge 1 commit into
scikit-learn:masterfrom
david-cortes:master

Conversation

@david-cortes

Copy link
Copy Markdown
Contributor

Functions for multinomial logistic regression (_multinomial_loss, _multinomial_loss_grad, _multinomial_grad_hess) have an argument sample_weight which, according to documentation, should be optional (like it is for logistic functions), but is actually compulsory.

This pull request sets the weights to 1 when they are missing, making the argument optional.

Fixes #13020

@david-cortes david-cortes reopened this Jan 21, 2019

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private, for internal use. If their caller always passes a sample_weight, we should not include this redundant validation. It makes the code harder to understand.

@david-cortes

Copy link
Copy Markdown
Contributor Author

These are private, for internal use. If their caller always passes a sample_weight, we should not include this redundant validation. It makes the code harder to understand.

Then how about removing the check in _multinomial_loss, but still including it in _multinomial_loss_grad and _multinomial_grad_hess ?

@jnothman

Copy link
Copy Markdown
Member

For what reason?

@david-cortes

Copy link
Copy Markdown
Contributor Author

For what reason?

_multinomial_loss is not called directly anywhere, but is rather called by _multinomial_loss_grad , which in turn can be called by _multinomial_grad_hess (when that function is used).

In a regular situation, when someone uses these functions outside of scikit-learn’s linear_model module (_multinomial_loss would not be required directly for fitting logistic regression models), it would avoid an unnecessary check, but since _multinomial_loss_grad might be used without _multinomial_grad_hess, a parameter check would have to occur in both in order for sample_weight to be optional.

@jnothman

jnothman commented Jan 22, 2019 via email

Copy link
Copy Markdown
Member

@david-cortes

Copy link
Copy Markdown
Contributor Author

But they're only designed for internal use, not for "someone" to use.

I realize they start with underscores, but they are IMHO very handy functions that I think also have a lot of usability externally. For me, they are very useful when working on topics like optimizers or active learning, and it’s quite annoying to be initializing the weights every time.

@adrinjalali

Copy link
Copy Markdown
Member

If you use them often, you can either have wrappers around them or get the signature you need with partials. Also, keeping all arguments compulsory in private APIs helps us not to forget to pass any of those parameters as the input to the function and avoid some of the bugs which would happen otherwise.

@david-cortes

Copy link
Copy Markdown
Contributor Author

The binary versions (_logistic_loss_and_grad, _logistic_loss, _logistic_grad_hess) are currently doing extra unneeded checks on sample_weight actually.

How about adding wrappers for logistic and linear functions to sklearn.utils then?

@glemaitre

Copy link
Copy Markdown
Member

We could modify the checking for the binary case, making sure this is done in the class (i.e., LogisticRegressionCV and LogisticRegression) and not in the private functions. We should be more consistent in this regard.

@jnothman

jnothman commented Jan 28, 2019 via email

Copy link
Copy Markdown
Member

@agramfort

Copy link
Copy Markdown
Member

closing in favor of #13281

@agramfort agramfort closed this Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants