Update demo for ranking. (#5154)

This commit is contained in:
Jiaming Yuan 2019-12-24 13:39:07 +08:00 committed by GitHub
parent 0202e04a8e
commit 73b1bd2789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 8 deletions

View File

@ -1,6 +1,6 @@
Learning to rank Learning to rank
==== ====
XGBoost supports accomplishing ranking tasks. In ranking scenario, data are often grouped and we need the [group information file](../../doc/tutorials/input_format.rst#group-input-format) to specify ranking tasks. The model used in XGBoost for ranking is the LambdaRank, this function is not yet completed. Currently, we provide pairwise rank. XGBoost supports accomplishing ranking tasks. In ranking scenario, data are often grouped and we need the [group information file](../../doc/tutorials/input_format.rst#group-input-format) to specify ranking tasks. The model used in XGBoost for ranking is the LambdaRank. See [parameters](../../doc/parameter.rst) for supported metrics.
### Parameters ### Parameters
The configuration setting is similar to the regression and binary classification setting, except user need to specify the objectives: The configuration setting is similar to the regression and binary classification setting, except user need to specify the objectives:

View File

@ -34,8 +34,8 @@ test_dmatrix = DMatrix(x_test)
train_dmatrix.set_group(group_train) train_dmatrix.set_group(group_train)
valid_dmatrix.set_group(group_valid) valid_dmatrix.set_group(group_valid)
params = {'objective': 'rank:pairwise', 'eta': 0.1, 'gamma': 1.0, params = {'objective': 'rank:ndcg', 'eta': 0.1, 'gamma': 1.0,
'min_child_weight': 0.1, 'max_depth': 6} 'min_child_weight': 0.1, 'max_depth': 6}
xgb_model = xgb.train(params, train_dmatrix, num_boost_round=4, xgb_model = xgb.train(params, train_dmatrix, num_boost_round=4,
evals=[(valid_dmatrix, 'validation')]) evals=[(valid_dmatrix, 'validation')])
pred = xgb_model.predict(test_dmatrix) pred = xgb_model.predict(test_dmatrix)

View File

@ -2,7 +2,6 @@
import xgboost as xgb import xgboost as xgb
from sklearn.datasets import load_svmlight_file from sklearn.datasets import load_svmlight_file
# This script demonstrate how to do ranking with XGBRanker # This script demonstrate how to do ranking with XGBRanker
x_train, y_train = load_svmlight_file("mq2008.train") x_train, y_train = load_svmlight_file("mq2008.train")
x_valid, y_valid = load_svmlight_file("mq2008.vali") x_valid, y_valid = load_svmlight_file("mq2008.vali")
@ -26,10 +25,10 @@ with open("mq2008.test.group", "r") as f:
for line in data: for line in data:
group_test.append(int(line.split("\n")[0])) group_test.append(int(line.split("\n")[0]))
params = {'objective': 'rank:pairwise', 'learning_rate': 0.1, params = {'objective': 'rank:ndcg', 'learning_rate': 0.1,
'gamma': 1.0, 'min_child_weight': 0.1, 'gamma': 1.0, 'min_child_weight': 0.1,
'max_depth': 6, 'n_estimators': 4} 'max_depth': 6, 'n_estimators': 4}
model = xgb.sklearn.XGBRanker(**params) model = xgb.sklearn.XGBRanker(**params)
model.fit(x_train, y_train, group_train, model.fit(x_train, y_train, group_train, verbose=True,
eval_set=[(x_valid, y_valid)], eval_group=[group_valid]) eval_set=[(x_valid, y_valid)], eval_group=[group_valid])
pred = model.predict(x_test) pred = model.predict(x_test)