As far as I know, to train learning to rank models, you need to have three things in the dataset:

  • label or relevance
  • group or query id
  • feature vector

For example, the Microsoft Learning to Rank dataset uses this format (label, group id, and features).

1 qid:10 1:0.031310 2:0.666667 ...
0 qid:10 1:0.078682 2:0.166667 ...

I am trying out XGBoost that utilizes GBMs to do pairwise ranking. They have an example for a ranking task that uses the C++ program to learn on the Microsoft dataset like above.

However, I am using their Python wrapper and cannot seem to find where I can input the group id (qid above). I can train the model using just the features and relevance score, but I am missing something.

Here is a sample script.

gbm = XGBRegressor(objective="rank:pairwise")

X =  np.random.normal(0, 1, 1000).reshape(100, 10)
y = np.random.randint(0, 5, 100)

gbm.fit(X, y) ### --- no group id needed???

print gbm.predict(X)

# should be in reverse order of relevance score
print y[gbm.predict_proba(X)[:, 1].argsort()][::-1]
  • $\begingroup$ I also come across this problem, but what kind of set_group should I pass to the function? when I construct a numpy or list I get error like this: d:\build\xgboost\xgboost-git\dmlc-core\include\dmlc\./logging.h:235: [12:03:09] D:\Build\xgboost\xgboost-git\src\c_api\c_api.cc:342: Check failed: (src.info.group_ptr.size()) == (0) slice does not support group structure @amyrit $\endgroup$ Commented Feb 4, 2017 at 6:56

2 Answers 2


According to the XGBoost documentation, XGboost expects:

  • the examples of a same group to be consecutive examples,
  • a list with the size of each group (which you can set with set_group method of DMatrix in Python).
  • 1
    $\begingroup$ Thanks, looks like API for model buildling (sklearn.py) is a little incomplete for the Python package. $\endgroup$
    – tokestermw
    Commented Feb 19, 2016 at 16:26
  • $\begingroup$ Link to documentation is dead $\endgroup$
    – user45254
    Commented Nov 2, 2020 at 21:28
  • 1
    $\begingroup$ @user45254 thanks for pointing out. I've not used XGBoost for a long while, but I think xgboost.readthedocs.io/en/latest/tutorials/input_format.html is the right link now, can you confirm? $\endgroup$
    – amyrit
    Commented Nov 3, 2020 at 10:18
  • 1
    $\begingroup$ @amyrit yes that looks correct! $\endgroup$
    – user45254
    Commented Nov 3, 2020 at 14:34
  • $\begingroup$ Thanks, I updated the answer to reflect the change. $\endgroup$
    – amyrit
    Commented Nov 3, 2020 at 14:37

set_group is very important to ranking, because only the scores in one group are comparable. You can sort data according to their scores in their own group.

For easy ranking, you can use my xgboostExtension.


Not the answer you're looking for? Browse other questions tagged or ask your own question.