Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the mechanism of using param ‘scale_pos_weight’? #1299

Closed
pr0x2b opened this issue Apr 3, 2018 · 5 comments
Closed

What is the mechanism of using param ‘scale_pos_weight’? #1299

pr0x2b opened this issue Apr 3, 2018 · 5 comments

Comments

@pr0x2b
Copy link

pr0x2b commented Apr 3, 2018

It seems that XGBoost and LightGBM both have scale_pos_weight argument but calculation is done completely different. I couldn't find any authentic answer regarding how to calculate it in LightGBM so requesting it here.

I'm using following method to calculate scale_pos_weight. I am not sure if it's correct.

Number of positive: 143540 , number of negative: 59856460
Number of data: 60000000, number of used features: 11

scale_pos_weight = 100 - ( [number of positive samples / total samples] * 100 )
scale_pos_weight = 100 - ( [ 143540 / 60000000 ] * 100 ) 
scale_pos_weight = 99.76
@bbennett36
Copy link

bbennett36 commented Apr 5, 2018

scale_pos_weight is always just negatives / positives I thought?

https://stats.stackexchange.com/questions/243207/what-is-the-proper-usage-of-scale-pos-weight-in-xgboost-for-imbalanced-datasets

This answer is for XGBoost but it should be the same for both implmentations. If you're still unsure, I think 'is_unbalance' essentially does the same thing but calculates 'scale_pos_weight' by itself.

@pr0x2b
Copy link
Author

pr0x2b commented Apr 5, 2018

Thanks @bbennett36
Actually, that formula is also mentioned in XGBoost documentation but LightGBM documentation lacks details on this parameter. That's why I wanted to confirm if formula stays the same for XGBoost and LightGBM or different.
I took reference for the formula from this post

@samratp-zz
Copy link

@pranavpandya84
negatives / positives looks more accurate to me.

From the document we can see

scale_pos_weight, default=1.0, type=double
– weight of positive class in binary classification task

With the default value of '1', it implies that the positive class has a weight equal to the negative class. So, in your case as the positive class is less than the negative class the number should have been less than '1' and not more than '1'.

This is just my understanding and I may not be correct...

@Laurae2
Copy link
Contributor

Laurae2 commented Apr 6, 2018

For both xgboost and LightGBM, scale_pos_weight, if assuming perfectly balanced positive/negative samples, means that:

number of positive samples = number of negative samples

which also means the following when using weights through scale_pos_weight:

number of positive samples * sample_pos_weight = number of negative samples

Therefore, its value, if asking for balance, is the following:

sample_pos_weight = number of negative samples / number of positive samples

More simple explanation: https://sites.google.com/view/lauraepp/parameters and type "scale" in the search box, then click on "Positive Binary Scaling".

image

Related C++ code:

  • xgboost proof: w += y * ((param_.scale_pos_weight * w) - w); where y is the label (0 negative or 1 positive in src/objective/regression_obj.cc)

  • LightGBM proof: label_weights_[1] *= scale_pos_weight_; where the 2nd index (1) is for positive labels (in src/objective/binary_objective.hpp)

@Laurae2 Laurae2 closed this as completed Apr 6, 2018
@pr0x2b
Copy link
Author

pr0x2b commented Apr 6, 2018

Thanks a lot. Perfect!

@lock lock bot locked as resolved and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants