本文整理汇总了Python中xgboost.train方法的典型用法代码示例。如果您正苦于以下问题:Python xgboost.train方法的具体用法?Python xgboost.train怎么用?Python xgboost.train使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xgboost
的用法示例。
在下文中一共展示了xgboost.train方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def fit(self):
"""
Gets data and preprocess by prepare_data() function
Trains with the selected parameters from grid search and saves the model
"""
data = self.get_input()
df_train, df_test = self.prepare_data(data)
xtr, ytr = df_train.drop(['Value'], axis=1), df_train['Value'].values
xgbtrain = xgb.DMatrix(xtr, ytr)
reg_cv = self.grid_search(xtr, ytr)
param = reg_cv.best_params_
bst = xgb.train(dtrain=xgbtrain, params=param)
# save model to file
mlflow.sklearn.save_model(bst, "model")
return df_test
示例2: fit
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def fit(self, X, y, X_valid, y_valid):
logger.info('XGBoost, train data shape {}'.format(X.shape))
logger.info('XGBoost, validation data shape {}'.format(X_valid.shape))
logger.info('XGBoost, train labels shape {}'.format(y.shape))
logger.info('XGBoost, validation labels shape {}'.format(y_valid.shape))
train = xgb.DMatrix(data=X,
label=y,
**self.dmatrix_parameters)
valid = xgb.DMatrix(data=X_valid,
label=y_valid,
**self.dmatrix_parameters)
self.estimator = xgb.train(params=self.booster_parameters,
dtrain=train,
evals=[(train, 'train'), (valid, 'valid')],
**self.training_parameters)
return self
示例3: train
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def train(params, dtrain, evals=(), **kwargs):
"""
Train XGBoost model in Mars manner.
Parameters
----------
Parameters are the same as `xgboost.train`.
Returns
-------
results: Booster
"""
evals_result = kwargs.pop('evals_result', dict())
session = kwargs.pop('session', None)
run_kwargs = kwargs.pop('run_kwargs', dict())
op = XGBTrain(params=params, dtrain=dtrain, evals=evals, kwargs=kwargs)
t = op()
ret = t.execute(session=session, **run_kwargs).fetch(session=session)
evals_result.update(ret['history'])
bst = pickle.loads(ret['booster'])
num_class = params.get('num_class')
if num_class:
bst.set_attr(num_class=str(num_class))
return bst
示例4: do_run
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def do_run(self, train, predict, window):
LabelColumnName = 'label'
data_file = "data_file_xgboost_" + str(window) + ".pkl"
if os.path.exists(data_file):
input = open(data_file, 'rb')
data_feature = pickle.load(input)
input.close()
else:
data_feature = get_all_stocks_feature_data(self.paras, window, LabelColumnName)
output = open(data_file, 'wb')
pickle.dump(data_feature, output)
output.close()
model = None
train_feature = {}
if train: model = self.train_data(data_feature, window, LabelColumnName)
if predict: self.predict_data(model, data_feature, window, LabelColumnName)
示例5: train_lgb
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def train_lgb(train_features, train_y, valid_features, valid_y, *,
lr, num_boost_round):
train_data = lgb.Dataset(train_features, train_y)
valid_data = lgb.Dataset(valid_features, valid_y, reference=train_data)
params = {
'objective': 'binary',
'metric': 'binary_logloss',
'learning_rate': lr,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'feature_fraction': 0.9,
'min_data_in_leaf': 20,
'num_leaves': 41,
'scale_pos_weight': 1.2,
'lambda_l2': 1,
}
print(params)
return lgb.train(
params=params,
train_set=train_data,
num_boost_round=num_boost_round,
early_stopping_rounds=20,
valid_sets=[valid_data],
verbose_eval=10,
)
示例6: train_xgb
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def train_xgb(train_features, train_y, valid_features, valid_y, *,
eta, num_boost_round):
train_data = xgb.DMatrix(train_features, label=train_y)
valid_data = xgb.DMatrix(valid_features, label=valid_y)
params = {
'eta': eta,
'objective': 'binary:logistic',
'gamma': 0.01,
'max_depth': 8,
}
print(params)
eval_list = [(valid_data, 'eval')]
return xgb.train(
params, train_data, num_boost_round, eval_list,
early_stopping_rounds=20,
verbose_eval=10,
)
示例7: test_train_model
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def test_train_model():
"""
test xgboost train in a single machine
:return: trained model
"""
rank = 1
world_size = 10
place = "/tmp/data"
dmatrix = read_train_data(rank, world_size, place)
param_xgboost_default = {'max_depth': 2, 'eta': 1, 'silent': 1,
'objective': 'multi:softprob', 'num_class': 3}
booster = xgb.train(param_xgboost_default, dtrain=dmatrix)
assert booster is not None
return booster
示例8: test_model_predict
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def test_model_predict(booster):
"""
test xgboost train in the single node
:return: true if pass the test
"""
rank = 1
world_size = 10
place = "/tmp/data"
dmatrix, y_test = read_predict_data(rank, world_size, place)
preds = booster.predict(dmatrix)
best_preds = np.asarray([np.argmax(line) for line in preds])
score = precision_score(y_test, best_preds, average='macro')
assert score > 0.99
logging.info("Predict accuracy: %f", score)
return True
示例9: fit
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def fit(self, X, y, x_val=None, y_val=None):
dtrain = xgb.DMatrix(X, label=y)
if x_val is not None:
dtest = xgb.DMatrix(x_val, label=y_val)
watchlist = [(dtrain, 'train'), (dtest, 'validation')]
self.clf = xgb.train(params=self.params,
dtrain=dtrain,
num_boost_round=self.num_round,
early_stopping_rounds=self.early_stopping_rounds,
evals=watchlist,
verbose_eval=self.verbose)
else:
self.clf = xgb.train(params=self.params,
dtrain=dtrain,
num_boost_round=self.num_round,
early_stopping_rounds=self.early_stopping_rounds)
return
示例10: setUpClass
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def setUpClass(self):
if not _HAS_XGBOOST:
return
if not _HAS_SKLEARN:
return
# Load data and train model
scikit_data = load_boston()
self.X = scikit_data.data.astype("f").astype("d")
self.dtrain = xgboost.DMatrix(
scikit_data.data,
label=scikit_data.target,
feature_names=scikit_data.feature_names,
)
self.feature_names = scikit_data.feature_names
self.output_name = "target"
示例11: _train_convert_evaluate_assert
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def _train_convert_evaluate_assert(self, bt_params={}, **params):
"""
Set up the unit test by loading the dataset and training a model.
"""
# Train a model
xgb_model = xgboost.train(bt_params, self.dtrain, **params)
# Convert the model
spec = xgb_converter.convert(
xgb_model, self.feature_names, self.output_name, force_32bit_float=False
)
if _is_macos() and _macos_version() >= (10, 13):
# Get predictions
df = pd.DataFrame(self.X, columns=self.feature_names)
df["prediction"] = xgb_model.predict(self.dtrain)
# Evaluate it
metrics = evaluate_regressor(spec, df, target="target", verbose=False)
self._check_metrics(metrics, bt_params)
示例12: setUpClass
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def setUpClass(self):
"""
Set up the unit test by loading the dataset and training a model.
"""
if not _HAS_XGBOOST:
return
if not _HAS_SKLEARN:
return
scikit_data = load_boston()
dtrain = xgboost.DMatrix(
scikit_data.data,
label=scikit_data.target,
feature_names=scikit_data.feature_names,
)
xgb_model = xgboost.train({}, dtrain, 1)
# Save the data and the model
self.scikit_data = scikit_data
self.xgb_model = xgb_model
self.feature_names = self.scikit_data.feature_names
示例13: evaluate
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def evaluate(features):
dtrain = xgb.DMatrix(tr_x[features], label=tr_y)
dvalid = xgb.DMatrix(va_x[features], label=va_y)
params = {'objective': 'binary:logistic', 'silent': 1, 'random_state': 71}
num_round = 10 # 実際にはもっと多いround数が必要
early_stopping_rounds = 3
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
model = xgb.train(params, dtrain, num_round,
evals=watchlist, early_stopping_rounds=early_stopping_rounds,
verbose_eval=0)
va_pred = model.predict(dvalid)
score = log_loss(va_y, va_pred)
return score
# ---------------------------------
# Greedy Forward Selection
# ----------------------------------
示例14: train
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def train(self, tr_x, tr_y, va_x=None, va_y=None):
# データのセット
validation = va_x is not None
dtrain = xgb.DMatrix(tr_x, label=tr_y)
if validation:
dvalid = xgb.DMatrix(va_x, label=va_y)
# ハイパーパラメータの設定
params = dict(self.params)
num_round = params.pop('num_round')
# 学習
if validation:
early_stopping_rounds = params.pop('early_stopping_rounds')
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
self.model = xgb.train(params, dtrain, num_round, evals=watchlist,
early_stopping_rounds=early_stopping_rounds)
else:
watchlist = [(dtrain, 'train')]
self.model = xgb.train(params, dtrain, num_round, evals=watchlist)
示例15: train_model
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import train [as 别名]
def train_model(self, x_train, y_train, x_val, y_val):
print("Will train XGB for {} rounds, RandomSeed: {}".format(self.rounds, self.params['seed']))
xg_train = xgb.DMatrix(x_train, label=y_train)
if y_val is None:
watchlist = [(xg_train, 'train')]
model = xgb.train(self.params, xg_train, self.rounds, watchlist)
else:
early_stop = self.rounds if self.early_stop == 0 else self.early_stop
xg_val = xgb.DMatrix(x_val, label=y_val)
watchlist = [(xg_train, 'train'), (xg_val, 'eval')]
model = xgb.train(self.params, xg_train, self.rounds, watchlist, early_stopping_rounds=early_stop)
self.steps = model.best_iteration
return model