From e0a0343ae671138a0e119c7f954c7ce798554547 Mon Sep 17 00:00:00 2001 From: antinucleon Date: Fri, 16 May 2014 17:48:03 -0600 Subject: [PATCH 1/4] speedtest --- demo/kaggle-higgs/speedtest.py | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 demo/kaggle-higgs/speedtest.py diff --git a/demo/kaggle-higgs/speedtest.py b/demo/kaggle-higgs/speedtest.py new file mode 100755 index 000000000..1b07d4619 --- /dev/null +++ b/demo/kaggle-higgs/speedtest.py @@ -0,0 +1,63 @@ +#!/usr/local/bin/python +# this is the example script to use xgboost to train +import sys +import numpy as np +# add path of xgboost python module +sys.path.append('../../python/') +import xgboost as xgb +from sklearn.ensemble import GradientBoostingClassifier +import time +test_size = 550000 + +# path to where the data lies +dpath = 'data' + +# load in training data, directly use numpy +dtrain = np.loadtxt( dpath+'/training.csv', delimiter=',', skiprows=1, converters={32: lambda x:int(x=='s') } ) +print 'finish loading from csv ' + +label = dtrain[:,32] +data = dtrain[:,1:31] +# rescale weight to make it same as test set +weight = dtrain[:,31] * float(test_size) / len(label) + +sum_wpos = sum( weight[i] for i in xrange(len(label)) if label[i] == 1.0 ) +sum_wneg = sum( weight[i] for i in xrange(len(label)) if label[i] == 0.0 ) + +# print weight statistics +print 'weight statistics: wpos=%g, wneg=%g, ratio=%g' % ( sum_wpos, sum_wneg, sum_wneg/sum_wpos ) + +# construct xgboost.DMatrix from numpy array, treat -999.0 as missing value +xgmat = xgb.DMatrix( data, label=label, missing = -999.0, weight=weight ) + +# setup parameters for xgboost +param = {} +# use logistic regression loss +param['loss_type'] = 1 +# scale weight of positive examples +param['scale_pos_weight'] = sum_wneg/sum_wpos +param['bst:eta'] = 0.1 +param['bst:max_depth'] = 6 +param['eval_metric'] = 'auc' +param['silent'] = 1 +param['nthread'] = 4 + +# you can directly throw param in, though we want to watch multiple metrics here +plst = param.items()+[('eval_metric', 'ams@0.15')] + +watchlist = [ (xgmat,'train') ] +# boost 135 tres +num_round = 135 +print 'loading data end, start to boost trees' +print "training GBM from sklearn" +tmp = time.time() +gbm = GradientBoostingClassifier(n_estimators=135, max_depth=6, verbose=2) +gbm.fit(data, label) +print "GBM costs: %s seconds" % str(time.time() - tmp) +#raw_input() +print "training xgboost" +tmp = time.time() +bst = xgb.train( plst, xgmat, num_round, watchlist ); +print "XGBoost costs: %s seconds" % str(time.time() - tmp) + +print 'finish training' From ae70b9b152c659d2d79ba9497e523430cf0b0f83 Mon Sep 17 00:00:00 2001 From: antinucleon Date: Fri, 16 May 2014 18:05:17 -0600 Subject: [PATCH 2/4] new speed test --- demo/kaggle-higgs/speedtest.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/demo/kaggle-higgs/speedtest.py b/demo/kaggle-higgs/speedtest.py index 1b07d4619..d17c2680b 100755 --- a/demo/kaggle-higgs/speedtest.py +++ b/demo/kaggle-higgs/speedtest.py @@ -42,22 +42,25 @@ param['eval_metric'] = 'auc' param['silent'] = 1 param['nthread'] = 4 -# you can directly throw param in, though we want to watch multiple metrics here plst = param.items()+[('eval_metric', 'ams@0.15')] watchlist = [ (xgmat,'train') ] -# boost 135 tres -num_round = 135 +# boost 10 tres +num_round = 10 print 'loading data end, start to boost trees' print "training GBM from sklearn" tmp = time.time() -gbm = GradientBoostingClassifier(n_estimators=135, max_depth=6, verbose=2) +gbm = GradientBoostingClassifier(n_estimators=num_round, max_depth=6, verbose=2) gbm.fit(data, label) print "GBM costs: %s seconds" % str(time.time() - tmp) #raw_input() print "training xgboost" -tmp = time.time() -bst = xgb.train( plst, xgmat, num_round, watchlist ); -print "XGBoost costs: %s seconds" % str(time.time() - tmp) +threads = [1, 2, 4, 16] +for i in threads: + param['nthread'] = i + tmp = time.time() + plst = param.items()+[('eval_metric', 'ams@0.15')] + bst = xgb.train( plst, xgmat, num_round, watchlist ); + print "XGBoost with %d thread costs: %s seconds" % (i, str(time.time() - tmp)) print 'finish training' From c28a1be34c0c6ed32fb1c7db042fced9a13979e7 Mon Sep 17 00:00:00 2001 From: tqchen Date: Fri, 16 May 2014 18:19:57 -0700 Subject: [PATCH 3/4] minor changes --- demo/kaggle-higgs/README.md | 2 +- demo/kaggle-higgs/run.sh | 4 ++-- demo/kaggle-higgs/speedtest.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/demo/kaggle-higgs/README.md b/demo/kaggle-higgs/README.md index b2ec971bd..2736b1326 100644 --- a/demo/kaggle-higgs/README.md +++ b/demo/kaggle-higgs/README.md @@ -7,4 +7,4 @@ run ./run.sh you need to compile xgboost python model in ../../python - +speedtest.py compares xgboost's speed on this dataset with sklearn diff --git a/demo/kaggle-higgs/run.sh b/demo/kaggle-higgs/run.sh index e6b5d91fa..c69426c25 100755 --- a/demo/kaggle-higgs/run.sh +++ b/demo/kaggle-higgs/run.sh @@ -1,4 +1,4 @@ #!/bin/bash -./higgs-numpy.py -./higgs-pred.py \ No newline at end of file +python higgs-numpy.py +python higgs-pred.py \ No newline at end of file diff --git a/demo/kaggle-higgs/speedtest.py b/demo/kaggle-higgs/speedtest.py index d17c2680b..212389c01 100755 --- a/demo/kaggle-higgs/speedtest.py +++ b/demo/kaggle-higgs/speedtest.py @@ -1,4 +1,4 @@ -#!/usr/local/bin/python +#!/usr/bin/python # this is the example script to use xgboost to train import sys import numpy as np @@ -52,7 +52,7 @@ print "training GBM from sklearn" tmp = time.time() gbm = GradientBoostingClassifier(n_estimators=num_round, max_depth=6, verbose=2) gbm.fit(data, label) -print "GBM costs: %s seconds" % str(time.time() - tmp) +print "sklearn.GBM costs: %s seconds" % str(time.time() - tmp) #raw_input() print "training xgboost" threads = [1, 2, 4, 16] From b3c3ecd9c969aa81eee704a11e8928b9c9727430 Mon Sep 17 00:00:00 2001 From: tqchen Date: Fri, 16 May 2014 18:25:01 -0700 Subject: [PATCH 4/4] chng few things --- demo/kaggle-higgs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/kaggle-higgs/README.md b/demo/kaggle-higgs/README.md index 2736b1326..ffd140d0a 100644 --- a/demo/kaggle-higgs/README.md +++ b/demo/kaggle-higgs/README.md @@ -7,4 +7,4 @@ run ./run.sh you need to compile xgboost python model in ../../python -speedtest.py compares xgboost's speed on this dataset with sklearn +speedtest.py compares xgboost's speed on this dataset with sklearn.GBM