What is the proper usage of scale_pos_weight in xgboost for imbalanced datasets? I have a very imbalanced dataset. I'm trying to follow the tuning advice and use scale_pos_weight but not sure how should I tune it. 
I can see that RegLossObj.GetGradient does:
if (info.labels[i] == 1.0f) w *= param_.scale_pos_weight

so a gradient of a positive sample would be more influential. However, according to the xgboost paper, the gradient statistic is always used locally = within the instances of a specific node in a specific tree:


*

*within the context of a node, to evaluate the loss reduction of a candidate split

*within the context of a leaf node, to optimize the weight given to that node


So there's no way of knowing in advance what would be a good scale_pos_weight - it is a very different number for a node that ends up with 1:100 ratio between positive and negative instances, and for a node with a 1:2 ratio.
Any hints? 
 A: Generally, scale_pos_weight is the ratio of number of negative class to the positive class.
Suppose, the dataset has 90 observations of negative class and 10 observations of positive class, then ideal value of scale_pos_weight should be 9.
See the doc:
http://xgboost.readthedocs.io/en/latest/parameter.html 
A: I understand your question and frustration, but I am not sure this is something that could be computed analytically, rather you'd have to determine a good setting empirically for your data, as you do for most hyper parameters, using cross validation as @user2149631 suggested. I've had some success using SelectFPR with Xgboost and the sklearn API to lower the FPR for XGBoost via feature selection instead, then further tuning the scale_pos_weight between 0 and 1.0. O.9 seems to work well but as with anything, YMMV depending on your data. You can also weight each data point individually when sending it to XGboost if you look through their docs. You have to use their API not the sklearn wrapper. That way you can weight one set of data points much higher than the other, and it will impact the boosting algorithm it uses.
A: All the documentation says that is should be:
scale_pos_weight = count(negative examples)/count(Positive examples)

In practice, that works pretty well, but if your dataset is extremely unbalanced I'd recommend using something more conservative like: 
scale_pos_weight = sqrt(count(negative examples)/count(Positive examples)) 

This is useful to limit the effect of a multiplication of positive examples by a very high weight. 
A: I also stumbled upon this dilemma and still looking for the best solution. However, I would suggest you using methods such as Grid Search (GridSearchCV in sklearn) for best parameter tuning for your classifier. However, if your dataset is highly imbalanced, its worthwhile to consider sampling methods (especially random oversampling and SMOTE oversampling methods) and model ensemble on data samples with different ratios of positive and negative class examples. Here is one nice and useful (almost comprehensive) tutorial about handling imbalanced datasets. 
https://www.analyticsvidhya.com/blog/2017/03/imbalanced-classification-problem/
