本文整理汇总了Python中xgboost.XGBModel方法的典型用法代码示例。如果您正苦于以下问题:Python xgboost.XGBModel方法的具体用法?Python xgboost.XGBModel怎么用?Python xgboost.XGBModel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xgboost
的用法示例。
在下文中一共展示了xgboost.XGBModel方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: optimize_hyperparam
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def optimize_hyperparam(self, X, y, test_size=.2, n_eval=100):
X_trn, X_val, y_trn, y_val = train_test_split(X, y, test_size=test_size, shuffle=self.shuffle)
def objective(hyperparams):
model = XGBModel(n_estimators=self.n_est, **self.params, **hyperparams)
model.fit(X=X_trn, y=y_trn,
eval_set=[(X_val, y_val)],
eval_metric=self.metric,
early_stopping_rounds=self.n_stop,
verbose=False)
score = model.evals_result()['validation_0'][self.metric][model.best_iteration] * self.loss_sign
return {'loss': score, 'status': STATUS_OK, 'model': model}
trials = Trials()
best = hyperopt.fmin(fn=objective, space=self.space, trials=trials,
algo=tpe.suggest, max_evals=n_eval, verbose=1,
rstate=self.random_state)
hyperparams = space_eval(self.space, best)
return hyperparams, trials
示例2: load_pkl
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def load_pkl(name):
"""Load xgboost model from pickle and perform conversion from version
0.90 if necessary.
:return:
XGBoost model
"""
import pickle
import xgboost
with open(name, 'rb') as f:
try:
model = pickle.load(f)
return model
except xgboost.core.XGBoostError as e:
if "Check failed: header == serialisation_header_" in str(e):
# pylint: disable=unused-import
import xgboost_prev
import tempfile
class Unpickler(pickle.Unpickler):
def find_class(self, module, name):
if module.startswith("xgboost"):
return pickle.Unpickler.find_class(
self, module.replace(
"xgboost", "xgboost_prev"),
name)
return pickle.Unpickler.find_class(self, module, name)
f.seek(0)
model = Unpickler(f).load()
temp_file = tempfile.NamedTemporaryFile(
prefix='xgboost_migration', suffix='.model')
model.save_model(temp_file.name)
migrated_model = xgboost.XGBModel()
migrated_model.load_model(temp_file.name)
return migrated_model
raise
示例3: fit
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def fit(self, X, y):
self.model = XGBModel(n_estimators=self.n_best, **self.params)
self.model.fit(X=X[self.features], y=y, eval_metric='mae', verbose=False)
return self
示例4: plot_importance
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def plot_importance(self, ax=None, height=0.2,
xlim=None, title='Feature importance',
xlabel='F score', ylabel='Features',
grid=True, **kwargs):
"""Plot importance based on fitted trees.
Parameters
----------
ax : matplotlib Axes, default None
Target axes instance. If None, new figure and axes will be created.
height : float, default 0.2
Bar height, passed to ax.barh()
xlim : tuple, default None
Tuple passed to axes.xlim()
title : str, default "Feature importance"
Axes title. To disable, pass None.
xlabel : str, default "F score"
X axis title label. To disable, pass None.
ylabel : str, default "Features"
Y axis title label. To disable, pass None.
kwargs :
Other keywords passed to ax.barh()
Returns
-------
ax : matplotlib Axes
"""
import xgboost as xgb
if not isinstance(self._df.estimator, xgb.XGBModel):
raise ValueError('estimator must be XGBRegressor or XGBClassifier')
# print(type(self._df.estimator.booster), self._df.estimator.booster)
return xgb.plot_importance(self._df.estimator,
ax=ax, height=height, xlim=xlim, title=title,
xlabel=xlabel, ylabel=ylabel, grid=True, **kwargs)
示例5: to_graphviz
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def to_graphviz(self, num_trees=0, rankdir='UT',
yes_color='#0000FF', no_color='#FF0000', **kwargs):
"""Convert specified tree to graphviz instance. IPython can automatically plot the
returned graphiz instance. Otherwise, you shoud call .render() method
of the returned graphiz instance.
Parameters
----------
num_trees : int, default 0
Specify the ordinal number of target tree
rankdir : str, default "UT"
Passed to graphiz via graph_attr
yes_color : str, default '#0000FF'
Edge color when meets the node condigion.
no_color : str, default '#FF0000'
Edge color when doesn't meet the node condigion.
kwargs :
Other keywords passed to graphviz graph_attr
Returns
-------
ax : matplotlib Axes
"""
import xgboost as xgb
if not isinstance(self._df.estimator, xgb.XGBModel):
raise ValueError('estimator must be XGBRegressor or XGBClassifier')
return xgb.to_graphviz(self._df.estimator,
num_trees=num_trees, rankdir=rankdir,
yes_color=yes_color, no_color=no_color, **kwargs)
示例6: plot_tree
# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import XGBModel [as 别名]
def plot_tree(self, num_trees=0, rankdir='UT', ax=None, **kwargs):
"""Plot specified tree.
Parameters
----------
booster : Booster, XGBModel
Booster or XGBModel instance
num_trees : int, default 0
Specify the ordinal number of target tree
rankdir : str, default "UT"
Passed to graphiz via graph_attr
ax : matplotlib Axes, default None
Target axes instance. If None, new figure and axes will be created.
kwargs :
Other keywords passed to to_graphviz
Returns
-------
ax : matplotlib Axes
"""
import xgboost as xgb
if not isinstance(self._df.estimator, xgb.XGBModel):
raise ValueError('estimator must be XGBRegressor or XGBClassifier')
return xgb.plot_tree(self._df.estimator,
num_trees=num_trees, rankdir=rankdir, **kwargs)