[Breaking] Require format to be specified in input URI. (#9077)
Previously, we use `libsvm` as default when format is not specified. However, the dmlc data parser is not particularly robust against errors, and the most common type of error is undefined format. Along with which, we will recommend users to use other data loader instead. We will continue the maintenance of the parsers as it's currently used for many internal tests including federated learning.
This commit is contained in:
@@ -7,15 +7,19 @@ import os
|
||||
import xgboost as xgb
|
||||
|
||||
CURRENT_DIR = os.path.dirname(__file__)
|
||||
dtrain = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.train'))
|
||||
dtest = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.test'))
|
||||
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
|
||||
dtrain = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
|
||||
)
|
||||
dtest = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.test?format=libsvm")
|
||||
)
|
||||
watchlist = [(dtest, "eval"), (dtrain, "train")]
|
||||
###
|
||||
# advanced: start from a initial base prediction
|
||||
#
|
||||
print('start running example to start from a initial prediction')
|
||||
print("start running example to start from a initial prediction")
|
||||
# specify parameters via map, definition are same as c++ version
|
||||
param = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'}
|
||||
param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
|
||||
# train xgboost for 1 round
|
||||
bst = xgb.train(param, dtrain, 1, watchlist)
|
||||
# Note: we need the margin value instead of transformed prediction in
|
||||
@@ -27,5 +31,5 @@ ptest = bst.predict(dtest, output_margin=True)
|
||||
dtrain.set_base_margin(ptrain)
|
||||
dtest.set_base_margin(ptest)
|
||||
|
||||
print('this is result of running from initial prediction')
|
||||
print("this is result of running from initial prediction")
|
||||
bst = xgb.train(param, dtrain, 1, watchlist)
|
||||
|
||||
@@ -10,27 +10,45 @@ import xgboost as xgb
|
||||
|
||||
# load data in do training
|
||||
CURRENT_DIR = os.path.dirname(__file__)
|
||||
dtrain = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.train'))
|
||||
param = {'max_depth':2, 'eta':1, 'objective':'binary:logistic'}
|
||||
dtrain = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
|
||||
)
|
||||
param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
|
||||
num_round = 2
|
||||
|
||||
print('running cross validation')
|
||||
print("running cross validation")
|
||||
# do cross validation, this will print result out as
|
||||
# [iteration] metric_name:mean_value+std_value
|
||||
# std_value is standard deviation of the metric
|
||||
xgb.cv(param, dtrain, num_round, nfold=5,
|
||||
metrics={'error'}, seed=0,
|
||||
callbacks=[xgb.callback.EvaluationMonitor(show_stdv=True)])
|
||||
xgb.cv(
|
||||
param,
|
||||
dtrain,
|
||||
num_round,
|
||||
nfold=5,
|
||||
metrics={"error"},
|
||||
seed=0,
|
||||
callbacks=[xgb.callback.EvaluationMonitor(show_stdv=True)],
|
||||
)
|
||||
|
||||
print('running cross validation, disable standard deviation display')
|
||||
print("running cross validation, disable standard deviation display")
|
||||
# do cross validation, this will print result out as
|
||||
# [iteration] metric_name:mean_value
|
||||
res = xgb.cv(param, dtrain, num_boost_round=10, nfold=5,
|
||||
metrics={'error'}, seed=0,
|
||||
callbacks=[xgb.callback.EvaluationMonitor(show_stdv=False),
|
||||
xgb.callback.EarlyStopping(3)])
|
||||
res = xgb.cv(
|
||||
param,
|
||||
dtrain,
|
||||
num_boost_round=10,
|
||||
nfold=5,
|
||||
metrics={"error"},
|
||||
seed=0,
|
||||
callbacks=[
|
||||
xgb.callback.EvaluationMonitor(show_stdv=False),
|
||||
xgb.callback.EarlyStopping(3),
|
||||
],
|
||||
)
|
||||
print(res)
|
||||
print('running cross validation, with preprocessing function')
|
||||
print("running cross validation, with preprocessing function")
|
||||
|
||||
|
||||
# define the preprocessing function
|
||||
# used to return the preprocessed training, test data, and parameter
|
||||
# we can use this to do weight rescale, etc.
|
||||
@@ -38,32 +56,36 @@ print('running cross validation, with preprocessing function')
|
||||
def fpreproc(dtrain, dtest, param):
|
||||
label = dtrain.get_label()
|
||||
ratio = float(np.sum(label == 0)) / np.sum(label == 1)
|
||||
param['scale_pos_weight'] = ratio
|
||||
param["scale_pos_weight"] = ratio
|
||||
return (dtrain, dtest, param)
|
||||
|
||||
|
||||
# do cross validation, for each fold
|
||||
# the dtrain, dtest, param will be passed into fpreproc
|
||||
# then the return value of fpreproc will be used to generate
|
||||
# results of that fold
|
||||
xgb.cv(param, dtrain, num_round, nfold=5,
|
||||
metrics={'auc'}, seed=0, fpreproc=fpreproc)
|
||||
xgb.cv(param, dtrain, num_round, nfold=5, metrics={"auc"}, seed=0, fpreproc=fpreproc)
|
||||
|
||||
###
|
||||
# you can also do cross validation with customized loss function
|
||||
# See custom_objective.py
|
||||
##
|
||||
print('running cross validation, with customized loss function')
|
||||
print("running cross validation, with customized loss function")
|
||||
|
||||
|
||||
def logregobj(preds, dtrain):
|
||||
labels = dtrain.get_label()
|
||||
preds = 1.0 / (1.0 + np.exp(-preds))
|
||||
grad = preds - labels
|
||||
hess = preds * (1.0 - preds)
|
||||
return grad, hess
|
||||
|
||||
|
||||
def evalerror(preds, dtrain):
|
||||
labels = dtrain.get_label()
|
||||
return 'error', float(sum(labels != (preds > 0.0))) / len(labels)
|
||||
return "error", float(sum(labels != (preds > 0.0))) / len(labels)
|
||||
|
||||
param = {'max_depth':2, 'eta':1}
|
||||
|
||||
param = {"max_depth": 2, "eta": 1}
|
||||
# train with customized objective
|
||||
xgb.cv(param, dtrain, num_round, nfold=5, seed=0,
|
||||
obj=logregobj, feval=evalerror)
|
||||
xgb.cv(param, dtrain, num_round, nfold=5, seed=0, obj=logregobj, feval=evalerror)
|
||||
|
||||
@@ -7,28 +7,37 @@ import os
|
||||
import xgboost as xgb
|
||||
|
||||
CURRENT_DIR = os.path.dirname(__file__)
|
||||
dtrain = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.train'))
|
||||
dtest = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.test'))
|
||||
dtrain = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
|
||||
)
|
||||
dtest = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.test?format=libsvm")
|
||||
)
|
||||
|
||||
param = [('max_depth', 2), ('objective', 'binary:logistic'), ('eval_metric', 'logloss'), ('eval_metric', 'error')]
|
||||
param = [
|
||||
("max_depth", 2),
|
||||
("objective", "binary:logistic"),
|
||||
("eval_metric", "logloss"),
|
||||
("eval_metric", "error"),
|
||||
]
|
||||
|
||||
num_round = 2
|
||||
watchlist = [(dtest,'eval'), (dtrain,'train')]
|
||||
watchlist = [(dtest, "eval"), (dtrain, "train")]
|
||||
|
||||
evals_result = {}
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist, evals_result=evals_result)
|
||||
|
||||
print('Access logloss metric directly from evals_result:')
|
||||
print(evals_result['eval']['logloss'])
|
||||
print("Access logloss metric directly from evals_result:")
|
||||
print(evals_result["eval"]["logloss"])
|
||||
|
||||
print('')
|
||||
print('Access metrics through a loop:')
|
||||
print("")
|
||||
print("Access metrics through a loop:")
|
||||
for e_name, e_mtrs in evals_result.items():
|
||||
print('- {}'.format(e_name))
|
||||
print("- {}".format(e_name))
|
||||
for e_mtr_name, e_mtr_vals in e_mtrs.items():
|
||||
print(' - {}'.format(e_mtr_name))
|
||||
print(' - {}'.format(e_mtr_vals))
|
||||
print(" - {}".format(e_mtr_name))
|
||||
print(" - {}".format(e_mtr_vals))
|
||||
|
||||
print('')
|
||||
print('Access complete dictionary:')
|
||||
print("")
|
||||
print("Access complete dictionary:")
|
||||
print(evals_result)
|
||||
|
||||
@@ -11,14 +11,22 @@ import xgboost as xgb
|
||||
# basically, we are using linear model, instead of tree for our boosters
|
||||
##
|
||||
CURRENT_DIR = os.path.dirname(__file__)
|
||||
dtrain = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.train'))
|
||||
dtest = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.test'))
|
||||
dtrain = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
|
||||
)
|
||||
dtest = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.test?format=libsvm")
|
||||
)
|
||||
# change booster to gblinear, so that we are fitting a linear model
|
||||
# alpha is the L1 regularizer
|
||||
# lambda is the L2 regularizer
|
||||
# you can also set lambda_bias which is L2 regularizer on the bias term
|
||||
param = {'objective':'binary:logistic', 'booster':'gblinear',
|
||||
'alpha': 0.0001, 'lambda': 1}
|
||||
param = {
|
||||
"objective": "binary:logistic",
|
||||
"booster": "gblinear",
|
||||
"alpha": 0.0001,
|
||||
"lambda": 1,
|
||||
}
|
||||
|
||||
# normally, you do not need to set eta (step_size)
|
||||
# XGBoost uses a parallel coordinate descent algorithm (shotgun),
|
||||
@@ -29,9 +37,15 @@ param = {'objective':'binary:logistic', 'booster':'gblinear',
|
||||
##
|
||||
# the rest of settings are the same
|
||||
##
|
||||
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
|
||||
watchlist = [(dtest, "eval"), (dtrain, "train")]
|
||||
num_round = 4
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist)
|
||||
preds = bst.predict(dtest)
|
||||
labels = dtest.get_label()
|
||||
print('error=%f' % (sum(1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i]) / float(len(preds))))
|
||||
print(
|
||||
"error=%f"
|
||||
% (
|
||||
sum(1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i])
|
||||
/ float(len(preds))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -16,8 +16,8 @@ test = os.path.join(CURRENT_DIR, "../data/agaricus.txt.test")
|
||||
|
||||
def native_interface():
|
||||
# load data in do training
|
||||
dtrain = xgb.DMatrix(train)
|
||||
dtest = xgb.DMatrix(test)
|
||||
dtrain = xgb.DMatrix(train + "?format=libsvm")
|
||||
dtest = xgb.DMatrix(test + "?format=libsvm")
|
||||
param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
|
||||
watchlist = [(dtest, "eval"), (dtrain, "train")]
|
||||
num_round = 3
|
||||
|
||||
@@ -8,14 +8,18 @@ import xgboost as xgb
|
||||
|
||||
# load data in do training
|
||||
CURRENT_DIR = os.path.dirname(__file__)
|
||||
dtrain = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.train'))
|
||||
dtest = xgb.DMatrix(os.path.join(CURRENT_DIR, '../data/agaricus.txt.test'))
|
||||
param = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'}
|
||||
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
|
||||
dtrain = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
|
||||
)
|
||||
dtest = xgb.DMatrix(
|
||||
os.path.join(CURRENT_DIR, "../data/agaricus.txt.test?format=libsvm")
|
||||
)
|
||||
param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
|
||||
watchlist = [(dtest, "eval"), (dtrain, "train")]
|
||||
num_round = 3
|
||||
bst = xgb.train(param, dtrain, num_round, watchlist)
|
||||
|
||||
print('start testing predict the leaf indices')
|
||||
print("start testing predict the leaf indices")
|
||||
# predict using first 2 tree
|
||||
leafindex = bst.predict(
|
||||
dtest, iteration_range=(0, 2), pred_leaf=True, strict_shape=True
|
||||
|
||||
Reference in New Issue
Block a user