當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。