Python xgboost.Booster() Examples

The following are 30 code examples of xgboost.Booster(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xgboost , or try the search function .
Example #1
Source File: predict.py    From mars with Apache License 2.0 6 votes vote down vote up
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 
Example #2
Source File: xgboost_model_artifact.py    From BentoML with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: utils.py    From xgboost-operator with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: xgboost_bind.py    From trains with Apache License 2.0 6 votes vote down vote up
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 
Example #5
Source File: xgboost.py    From mlflow with Apache License 2.0 6 votes vote down vote up
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) 
Example #6
Source File: tdlib.py    From Crypto_trading_robot with MIT License 6 votes vote down vote up
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 
Example #7
Source File: serve_utils.py    From sagemaker-xgboost-container with Apache License 2.0 6 votes vote down vote up
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 
Example #8
Source File: xgboost.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _load_model(path):
    import xgboost as xgb
    model = xgb.Booster()
    model.load_model(os.path.abspath(path))
    return model 
Example #9
Source File: tests_helper.py    From onnxmltools with MIT License 5 votes vote down vote up
def convert_model(model, name, input_types):
    """
    Runs the appropriate conversion method.

    :param model: model
    :return: *onnx* model
    """
    from sklearn.base import BaseEstimator
    if model.__class__.__name__.startswith("LGBM"):
        from onnxmltools.convert import convert_lightgbm
        model, prefix = convert_lightgbm(model, name, input_types), "LightGbm"
    elif model.__class__.__name__.startswith("XGB"):
        from onnxmltools.convert import convert_xgboost
        model, prefix = convert_xgboost(model, name, input_types), "XGB"
    elif model.__class__.__name__ == 'Booster':
        import lightgbm
        if isinstance(model, lightgbm.Booster):
            from onnxmltools.convert import convert_lightgbm
            model, prefix = convert_lightgbm(model, name, input_types), "LightGbm"
        else:
            raise RuntimeError("Unable to convert model of type '{0}'.".format(type(model)))
    elif model.__class__.__name__.startswith("CatBoost"):
        from onnxmltools.convert import convert_catboost
        model, prefix = convert_catboost(model, name, input_types), "CatBoost"
    elif isinstance(model, BaseEstimator):
        from onnxmltools.convert import convert_sklearn
        model, prefix = convert_sklearn(model, name, input_types), "Sklearn"
    else:
        from onnxmltools.convert import convert_coreml
        model, prefix = convert_coreml(model, name, input_types), "Cml"
    if model is None:
        raise RuntimeError("Unable to convert model of type '{0}'.".format(type(model)))
    return model, prefix 
Example #10
Source File: convert.py    From onnxmltools with MIT License 5 votes vote down vote up
def convert(model, name=None, initial_types=None, doc_string='', target_opset=None,
            targeted_onnx=onnx.__version__, custom_conversion_functions=None,
            custom_shape_calculators=None):
    '''
    This function produces an equivalent ONNX model of the given xgboost model.

    :param model: A xgboost model
    :param initial_types: a python list. Each element is a tuple of a variable name and a type defined in data_types.py
    :param name: The name of the graph (type: GraphProto) in the produced ONNX model (type: ModelProto)
    :param doc_string: A string attached onto the produced ONNX model
    :param target_opset: number, for example, 7 for ONNX 1.2, and 8 for ONNX 1.3.
    :param targeted_onnx: A string (for example, '1.1.2' and '1.2') used to specify the targeted ONNX version of the
        produced model. If ONNXMLTools cannot find a compatible ONNX python package, an error may be thrown.
    :param custom_conversion_functions: a dictionary for specifying the user customized conversion function
    :param custom_shape_calculators: a dictionary for specifying the user customized shape calculator
    :return: An ONNX model (type: ModelProto) which is equivalent to the input xgboost model
    '''
    if initial_types is None:
        raise ValueError('Initial types are required. See usage of convert(...) in \
                           onnxmltools.convert.xgboost.convert for details')
    if name is None:
        name = str(uuid4().hex)

    if isinstance(model, xgboost.Booster):
        model = WrappedBooster(model)
    target_opset = target_opset if target_opset else get_maximum_opset_supported()
    topology = parse_xgboost(model, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
    topology.compile()
    onnx_model = convert_topology(topology, name, doc_string, target_opset, targeted_onnx)
    return onnx_model 
Example #11
Source File: ReRanker.py    From EARL with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.rerun = False
	self.pred_change = {}
        print "ReRanker initializing"
        try:
            self.model = xgb.Booster({'nthread': 4})
            self.model.load_model('../models/db_predia_reranker.model')            
        except Exception,e:
            print e
            sys.exit(1) 
Example #12
Source File: models.py    From open-solution-home-credit with MIT License 5 votes vote down vote up
def load(self, filepath):
        self.estimator = xgb.Booster(params=self.model_config)
        self.estimator.load_model(filepath)
        return self 
Example #13
Source File: component.py    From pipelines with Apache License 2.0 5 votes vote down vote up
def xgboost_predict(
    data_path: InputPath('CSV'),  # Also supports LibSVM
    model_path: InputPath('XGBoostModel'),
    predictions_path: OutputPath('Text'),
    label_column: int = None,
):
    '''Make predictions using a trained XGBoost model.

    Args:
        data_path: Path for the feature data in CSV format.
        model_path: Path for the trained model in binary XGBoost format.
        predictions_path: Output path for the predictions.
        label_column: Column containing the label data.

    Annotations:
        author: Alexey Volkov <alexey.volkov@ark-kun.com>
    '''
    from pathlib import Path

    import numpy
    import pandas
    import xgboost

    df = pandas.read_csv(
        data_path,
    )

    if label_column is not None:
        df = df.drop(columns=[df.columns[label_column]])

    testing_data = xgboost.DMatrix(
        data=df,
    )

    model = xgboost.Booster(model_file=model_path)

    predictions = model.predict(testing_data)

    Path(predictions_path).parent.mkdir(parents=True, exist_ok=True)
    numpy.savetxt(predictions_path, predictions) 
Example #14
Source File: models.py    From steppy-toolkit with MIT License 5 votes vote down vote up
def load(self, filepath):
        self.estimator = xgb.Booster(params=self.booster_parameters)
        self.estimator.load_model(filepath)
        return self 
Example #15
Source File: component.py    From pipelines with Apache License 2.0 5 votes vote down vote up
def xgboost_predict(
    data_path: InputPath('ApacheParquet'),
    model_path: InputPath('XGBoostModel'),
    predictions_path: OutputPath('Text'),
    label_column_name: str = None,
):
    '''Make predictions using a trained XGBoost model.

    Args:
        data_path: Path for the feature data in Apache Parquet format.
        model_path: Path for the trained model in binary XGBoost format.
        predictions_path: Output path for the predictions.
        label_column_name: Optional. Name of the column containing the label data that is excluded during the prediction.

    Annotations:
        author: Alexey Volkov <alexey.volkov@ark-kun.com>
    '''
    from pathlib import Path

    import numpy
    import pandas
    import xgboost

    # Loading data
    df = pandas.read_parquet(data_path)
    if label_column_name:
        df = df.drop(columns=[label_column_name])

    evaluation_data = xgboost.DMatrix(
        data=df,
    )

    # Training
    model = xgboost.Booster(model_file=model_path)

    predictions = model.predict(evaluation_data)

    Path(predictions_path).parent.mkdir(parents=True, exist_ok=True)
    numpy.savetxt(predictions_path, predictions) 
Example #16
Source File: train_xgboost.py    From jh-kaggle-util with Apache License 2.0 5 votes vote down vote up
def load_model(cls,path,name):
        root = jhkaggle.jhkaggle_config['PATH']
        model_path = os.path.join(root,path)
        meta_filename = os.path.join(model_path,"meta.json")
        with open(meta_filename, 'r') as fp:
            meta = json.load(fp)

        result = TrainXGBoost(meta['data_source'],None,False)
        result.model = xgb.Booster({'nthread':-1}) #init model
        result.model.load_model(os.path.join(model_path,name+".bin"))
        return result 
Example #17
Source File: binary.py    From uai-sdk with Apache License 2.0 5 votes vote down vote up
def load_model(self):
        model_file = self.model_name
        bst = xgb.Booster({'nthread':1})
        bst.load_model(model_file)

        self.bst = bst 
Example #18
Source File: xgboost.py    From search-MjoLniR with MIT License 5 votes vote down vote up
def loadBoosterFromLocalFile(path: str) -> 'XGBoostBooster':
        booster = xgb.Booster.load_model(path)
        # TODO: Not having the training parameters or the evaluation metrics
        # almost makes this a different thing...
        return XGBoostBooster(booster) 
Example #19
Source File: test_checkpointing.py    From sagemaker-xgboost-container with Apache License 2.0 5 votes vote down vote up
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) 
Example #20
Source File: spark_tools.py    From go-ml-transpiler with Apache License 2.0 5 votes vote down vote up
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 
Example #21
Source File: test_predict.py    From mars with Apache License 2.0 5 votes vote down vote up
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) 
Example #22
Source File: test_predict.py    From mars with Apache License 2.0 5 votes vote down vote up
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) 
Example #23
Source File: test_train.py    From mars with Apache License 2.0 5 votes vote down vote up
def testLocalTrainTensor(self):
        dtrain = MarsDMatrix(self.X, self.y)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster) 
Example #24
Source File: test_train.py    From mars with Apache License 2.0 5 votes vote down vote up
def testLocalTrainDataFrame(self):
        dtrain = MarsDMatrix(self.X_df, self.y_series)
        booster = train({}, dtrain, num_boost_round=2)
        self.assertIsInstance(booster, Booster) 
Example #25
Source File: xgboost.py    From search-MjoLniR with MIT License 5 votes vote down vote up
def __init__(self, booster: xgb.Booster) -> None:
        self.booster = booster 
Example #26
Source File: xgboost_model_artifact.py    From BentoML with Apache License 2.0 5 votes vote down vote up
def load(self, path):
        try:
            import xgboost as xgb
        except ImportError:
            raise MissingDependencyException(
                "xgboost package is required to use XgboostModelArtifact"
            )
        bst = xgb.Booster()
        bst.load_model(self._model_file_path(path))

        return self.pack(bst) 
Example #27
Source File: xgboost.py    From search-MjoLniR with MIT License 5 votes vote down vote up
def __init__(
        self,
        booster: xgb.Booster,
        evals_result: EvalsResult,
        params: Mapping[str, Any]
    ) -> None:
        super().__init__(booster)
        self.evals_result = evals_result
        self.params = params 
Example #28
Source File: xgboost.py    From mljar-supervised with MIT License 5 votes vote down vote up
def load(self, model_file_path):
        logger.debug("XgbLearner load model from %s" % model_file_path)
        self.model = xgb.Booster()  # init model
        self.model.load_model(model_file_path) 
Example #29
Source File: model.py    From ebonite with Apache License 2.0 5 votes vote down vote up
def dump(self, model: xgboost.Booster) -> FilesContextManager:
        with tempfile.TemporaryDirectory(prefix='ebonite_xgboost_dump') as f:
            path = os.path.join(f, self.model_path)
            model.save_model(path)
            yield Blobs({self.model_path: LocalFileBlob(path)}) 
Example #30
Source File: model.py    From ebonite with Apache License 2.0 5 votes vote down vote up
def load(self, path):
        model = xgboost.Booster()
        model.load_model(os.path.join(path, self.model_path))
        return model