当前位置: 首页>>代码示例>>Python>>正文


Python xgboost.Booster方法代码示例

本文整理汇总了Python中xgboost.Booster方法的典型用法代码示例。如果您正苦于以下问题:Python xgboost.Booster方法的具体用法?Python xgboost.Booster怎么用?Python xgboost.Booster使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在xgboost的用法示例。


在下文中一共展示了xgboost.Booster方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_loaded_booster

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def get_loaded_booster(model_dir):
    model_files = (data_file for data_file in os.listdir(model_dir)
                   if os.path.isfile(os.path.join(model_dir, data_file)))
    model_file = next(model_files)
    try:
        booster = pkl.load(open(os.path.join(model_dir, model_file), 'rb'))
        format = PKL_FORMAT
    except Exception as exp_pkl:
        try:
            booster = xgb.Booster()
            booster.load_model(os.path.join(model_dir, model_file))
            format = XGB_FORMAT
        except Exception as exp_xgb:
            raise RuntimeError("Model at {} cannot be loaded:\n{}\n{}".format(model_dir, str(exp_pkl), str(exp_xgb)))
    booster.set_param('nthread', 1)
    return booster, format 
开发者ID:aws,项目名称:sagemaker-xgboost-container,代码行数:18,代码来源:serve_utils.py

示例2: predict

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def predict(model, data, session=None, run_kwargs=None, run=True):
    from xgboost import Booster

    data = check_data(data)
    if not isinstance(model, Booster):
        raise TypeError('model has to be a xgboost.Booster, got {0} instead'.format(type(model)))

    num_class = model.attr('num_class')
    if isinstance(data, TENSOR_TYPE):
        output_types = [OutputType.tensor]
    elif num_class is not None:
        output_types = [OutputType.dataframe]
    else:
        output_types = [OutputType.series]

    op = XGBPredict(data=data, model=model, gpu=data.op.gpu, output_types=output_types)
    result = op()
    if run:
        result.execute(session=session, **(run_kwargs or dict()))
    return result 
开发者ID:mars-project,项目名称:mars,代码行数:22,代码来源:predict.py

示例3: read_model_from_oss

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def read_model_from_oss(kw):
    """
    helper function to read a model from oss
    :param kw: OSS parameter
    :return: XGBoost booster model
    """
    auth = oss2.Auth(kw['access_id'], kw['access_key'])
    bucket = kw['access_bucket']
    bkt = oss2.Bucket(auth=auth, endpoint=kw['endpoint'], bucket_name=bucket)
    oss_path = kw["path"]

    temp_model_fname = os.path.join(tempfile.mkdtemp(), 'local_model')
    try:
        bkt.get_object_to_file(key=oss_path, filename=temp_model_fname)
        logger.info("success to load model from oss %s", oss_path)
    except Exception as e:
        logging.error("fail to load model: " + e)
        raise Exception("fail to load model from oss %s", oss_path)

    bst = xgb.Booster({'nthread': 2})  # init model

    bst.load_model(temp_model_fname)

    return bst 
开发者ID:kubeflow,项目名称:xgboost-operator,代码行数:26,代码来源:utils.py

示例4: __init__

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def __init__(self, spec, model):
        super(_XgboostModelArtifactWrapper, self).__init__(spec)

        try:
            import xgboost as xgb
        except ImportError:
            raise MissingDependencyException(
                "xgboost package is required to use XgboostModelArtifact"
            )

        if not isinstance(model, xgb.core.Booster):
            raise InvalidArgument(
                "Expect `model` argument to be a `xgboost.core.Booster` instance"
            )

        self._model = model 
开发者ID:bentoml,项目名称:BentoML,代码行数:18,代码来源:xgboost_model_artifact.py

示例5: _patch_model_io

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def _patch_model_io():
        if PatchXGBoostModelIO.__patched:
            return

        if 'xgboost' not in sys.modules:
            return
        PatchXGBoostModelIO.__patched = True
        try:
            import xgboost as xgb
            bst = xgb.Booster
            bst.save_model = _patched_call(bst.save_model, PatchXGBoostModelIO._save)
            bst.load_model = _patched_call(bst.load_model, PatchXGBoostModelIO._load)
        except ImportError:
            pass
        except Exception:
            pass 
开发者ID:allegroai,项目名称:trains,代码行数:18,代码来源:xgboost_bind.py

示例6: load_model

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def load_model(model_uri):
    """
    Load an XGBoost model from a local file or a run.

    :param model_uri: The location, in URI format, of the MLflow model. For example:

                      - ``/Users/me/path/to/local/model``
                      - ``relative/path/to/local/model``
                      - ``s3://my_bucket/path/to/model``
                      - ``runs:/<mlflow_run_id>/run-relative/path/to/model``

                      For more information about supported URI schemes, see
                      `Referencing Artifacts <https://www.mlflow.org/docs/latest/tracking.html#
                      artifact-locations>`_.

    :return: An XGBoost model (an instance of `xgboost.Booster`_)
    """
    local_model_path = _download_artifact_from_uri(artifact_uri=model_uri)
    flavor_conf = _get_flavor_configuration(model_path=local_model_path, flavor_name=FLAVOR_NAME)
    xgb_model_file_path = os.path.join(local_model_path, flavor_conf.get("data", "model.xgb"))
    return _load_model(path=xgb_model_file_path) 
开发者ID:mlflow,项目名称:mlflow,代码行数:23,代码来源:xgboost.py

示例7: predict_label

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def predict_label(self, X, robot):

        bst = xgb.Booster({'nthread': 4})  # init model
        bst.load_model('models/{}'.format(robot.model_name))  # load the model

        try: # may not work if there are Nones
            dtest = xgb.DMatrix(X)
            pred = bst.predict(dtest)
            label = np.argmax(pred, axis=1)[0]   # giving a prediction:
            # 0 : nothing
            # 1 : long
            # 2: short
            label_probability = pred[0][int(label)]
        except ValueError:   # in case it fails
            label, label_probability = 0, 1
            print('Note: Issue when predicting the value')

        return label, label_probability


    ### Data preparation 
开发者ID:illi4,项目名称:Crypto_trading_robot,代码行数:23,代码来源:tdlib.py

示例8: load

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def load(self, filepath):
        self.estimator = xgb.Booster(params=self.booster_parameters)
        self.estimator.load_model(filepath)
        return self 
开发者ID:minerva-ml,项目名称:steppy-toolkit,代码行数:6,代码来源:models.py

示例9: test_train_zero_or_negative_rounds

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def test_train_zero_or_negative_rounds(tmpdir, caplog):

    X_train = np.random.random(size=(100, 5))
    y_train = np.random.random(size=(100, 1))
    dtrain = xgb.DMatrix(X_train, label=y_train)

    X_test = np.random.random(size=(100, 5))
    y_test = np.random.random(size=(100, 1))
    dtest = xgb.DMatrix(X_test, label=y_test)

    params = {"objective": "binary:logistic"}

    train_args = dict(
        params=params,
        dtrain=dtrain,
        num_boost_round=0,
        evals=[(dtrain, 'train'), (dtest, 'test')]
    )
    checkpoint_dir = os.path.join(tmpdir, "test_checkpoints")

    bst = checkpointing.train(train_args, checkpoint_dir)
    assert isinstance(bst, xgb.Booster)
    assert not os.listdir(checkpoint_dir)

    train_args["num_boost_round"] = -1
    bst = checkpointing.train(train_args, checkpoint_dir)
    assert isinstance(bst, xgb.Booster)
    assert not os.listdir(checkpoint_dir) 
开发者ID:aws,项目名称:sagemaker-xgboost-container,代码行数:30,代码来源:test_checkpointing.py

示例10: load_spark_model

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def load_spark_model(model_path, metadata_path):

    import xgboost as xgb
    import json
    import numpy as np

    if not isinstance(model_path, str) or not isinstance(model_path, str):
        raise ValueError("model and metadata paths must be str, not {0} and {1}".format(type(model_path), type(metadata_path)))

    with open(metadata_path) as f:
        metadata = json.loads(f.read().strip())

    xgb_class = metadata.get("class")
    if xgb_class == "ml.dmlc.xgboost4j.scala.spark.XGBoostClassificationModel":
        clf = xgb.XGBClassifier()
        setattr(clf, "base_score", metadata["paramMap"]["baseScore"])
    elif xgb_class == "ml.dmlc.xgboost4j.scala.spark.XGBoostRegressionModel":
        clf = xgb.XGBRegressor()
    else:
        raise ValueError("Unsupported model.")

    setattr(clf, "objective", metadata["paramMap"]["objective"])
    setattr(clf, "missing",
            np.nan if metadata["paramMap"]["missing"] in ["NaN", "nan", "null", "None"] else metadata["paramMap"][
                "missing"])
    setattr(clf, "booster", metadata["paramMap"].get("booster", "gbtree"))
    setattr(clf, "n_estimators", metadata["paramMap"].get("numRound", 1))

    booster = xgb.Booster()
    booster.load_model(model_path)

    clf._Booster = booster
    return clf 
开发者ID:znly,项目名称:go-ml-transpiler,代码行数:35,代码来源:spark_tools.py

示例11: testLocalPredictTensor

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def testLocalPredictTensor(self):
        dtrain = MarsDMatrix(self.X, self.y)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster)

        prediction = predict(booster, self.X)
        self.assertIsInstance(prediction.to_numpy(), np.ndarray)

        prediction = predict(booster, dtrain)
        self.assertIsInstance(prediction.fetch(), np.ndarray)

        with self.assertRaises(TypeError):
            predict(None, self.X) 
开发者ID:mars-project,项目名称:mars,代码行数:15,代码来源:test_predict.py

示例12: testLocalPredictDataFrame

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def testLocalPredictDataFrame(self):
        dtrain = MarsDMatrix(self.X_df, self.y_series)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster)

        prediction = predict(booster, self.X_df)
        self.assertIsInstance(prediction.to_pandas(), pd.Series) 
开发者ID:mars-project,项目名称:mars,代码行数:9,代码来源:test_predict.py

示例13: testLocalTrainTensor

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def testLocalTrainTensor(self):
        dtrain = MarsDMatrix(self.X, self.y)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster) 
开发者ID:mars-project,项目名称:mars,代码行数:6,代码来源:test_train.py

示例14: testLocalTrainDataFrame

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def testLocalTrainDataFrame(self):
        dtrain = MarsDMatrix(self.X_df, self.y_series)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster) 
开发者ID:mars-project,项目名称:mars,代码行数:6,代码来源:test_train.py

示例15: __init__

# 需要导入模块: import xgboost [as 别名]
# 或者: from xgboost import Booster [as 别名]
def __init__(self, booster: xgb.Booster) -> None:
        self.booster = booster 
开发者ID:wikimedia,项目名称:search-MjoLniR,代码行数:4,代码来源:xgboost.py


注:本文中的xgboost.Booster方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。