### Install from Binary Wheel Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/python_packaging.rst.txt Installing from a pre-built binary wheel is fast as it contains the compiled shared library and does not require compilation during installation. ```console pip install xgboost-2.0.0-py3-none-linux_x86_64.whl ``` -------------------------------- ### Using a Custom GitHub Actions Setup Source: https://xgboost.readthedocs.io/en/latest/contrib/ci.html Example of how to use a custom composite action for environment setup in a GitHub Actions workflow. Specify the environment name and configuration file. ```yaml - uses: dmlc/xgboost-devops/actions/miniforge-setup@main with: environment-name: cpp_test environment-file: ops/conda_env/cpp_test.yml ``` -------------------------------- ### Install XGBoost as Editable Installation Source: https://xgboost.readthedocs.io/en/latest/build.html Set up XGBoost for rapid development by creating an editable installation. This links the installed package directly to your source code, so changes are immediately reflected. First, build the shared library, then install the Python package using the -e flag. ```bash # Build shared library libxgboost.so cmake -B build -S . -GNinja cd build && ninja # Install as editable installation cd ../python-package pip install -e . ``` -------------------------------- ### Install Ray Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/ray.rst.txt Install the Ray framework using pip. This is the first step for enabling distributed computations. ```bash pip install ray ``` -------------------------------- ### Install XGBoost-Ray Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/ray.rst.txt Install the XGBoost-Ray library, which includes Ray as a dependency if not already installed. ```bash pip install xgboost_ray ``` -------------------------------- ### Install from Binary Wheel Source: https://xgboost.readthedocs.io/en/latest/contrib/python_packaging.html Install XGBoost using a pre-compiled binary wheel. This process is quick as the shared library (libxgboost.so) is already included in the wheel and does not require compilation during installation. ```bash pip install xgboost-2.0.0-py3-none-linux_x86_64.whl # Completes quickly ``` -------------------------------- ### Sphinx Build Output Example Source: https://xgboost.readthedocs.io/en/latest/contrib/docs.html This is an example of the console output when the 'make html' command successfully builds the documentation. ```bash $ make html sphinx-build -b html -d _build/doctrees . _build/html Running Sphinx v6.2.1 ... The HTML pages are in _build/html. Build finished. The HTML pages are in _build/html. ``` -------------------------------- ### Install XGBoost with GPU support in R (Linux/Windows) Source: https://xgboost.readthedocs.io/en/latest/_sources/install.rst.txt Install XGBoost with GPU support by downloading the pre-built binary package and then installing dependencies and the package itself using R commands. ```bash # Install dependencies R -q -e "install.packages(c('data.table', 'jsonlite'))" # Install XGBoost R CMD INSTALL ./xgboost_r_gpu_linux.tar.gz ``` -------------------------------- ### Install Python Package Reusing Pre-built Shared Library Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Navigate to the python-package directory and run 'pip install .' to install the Python package. This command will automatically reuse the pre-compiled lib/libxgboost.so if it exists, speeding up the installation. ```console $ cd python-package/ $ pip install . # Will re-use lib/libxgboost.so ``` -------------------------------- ### Install XGBoost R GPU nightly build Source: https://xgboost.readthedocs.io/en/latest/_sources/install.rst.txt Install an experimental R build with GPU support from nightly builds. Download the appropriate tar.gz file and then install dependencies and the package. ```bash # Install dependencies R -q -e "install.packages(c('data.table', 'jsonlite', 'remotes'))" # Install XGBoost R CMD INSTALL ./xgboost_r_gpu_linux.tar.gz ``` -------------------------------- ### Initial Margin File Example Source: https://xgboost.readthedocs.io/en/latest/tutorials/input_format.html Example of an initial margin file for XGBoost. Each line provides an initial margin prediction for an instance. ```text -0.4 1.0 3.4 ``` -------------------------------- ### Install Python Package Using System libxgboost.so Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Ensure libxgboost.so is in the system library path. Then, navigate to the python-package directory and install using 'pip install . --config-settings use_system_libxgboost=True'. This is useful for package managers. ```bash cd python-package pip install . --config-settings use_system_libxgboost=True ``` -------------------------------- ### Install XGBoost4J with Maven (Skip Tests) Source: https://xgboost.readthedocs.io/en/latest/build.html Install the XGBoost4J package using Maven, skipping tests for a faster installation. Ensure JAVA_HOME is set and Maven 3+ is available. ```bash mvn -DskipTests install ``` -------------------------------- ### Install and Enable Pre-commit Hooks Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/coding_guide.rst.txt Install the pre-commit package and enable the hooks for your local repository. This sets up automatic checks for formatting and linting. ```bash python -m pip install pre-commit pre-commit install ``` -------------------------------- ### Build and Install Python Package as Editable Installation Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt First, build the shared library using CMake and Ninja. Then, navigate to the python-package directory and run 'pip install -e .' to install the package in editable mode, linking directly to your source code. ```bash # Build shared library libxgboost.so cmake -B build -S . -GNinja cd build && ninja # Install as editable installation cd ../python-package pip install -e . ``` -------------------------------- ### Example: Run CPU Build Script Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/ci.rst.txt Execute a CPU build script within a Docker container. This example uses a specific image URI for CPU builds. ```bash python3 ops/docker_run.py \ --image-uri 492475357299.dkr.ecr.us-west-2.amazonaws.com/xgb-ci.cpu:main \ -- bash ops/pipeline/build-cpu-impl.sh cpu ``` -------------------------------- ### Install R Package from Source Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Install the R package after obtaining the source code. Ensure you are in the R-package directory. ```bash cd R-package R CMD INSTALL . ``` -------------------------------- ### Install from Source Distribution Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/python_packaging.rst.txt Installing from an sdist will compile the C++ code using CMake and a C++ compiler. Use the -v flag to observe the build progress. ```console pip install -v xgboost-2.0.0.tar.gz ``` -------------------------------- ### Install Documentation Dependencies Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/docs.rst.txt Install the required packages for building the documentation using a requirements.txt file. This command should be run within the activated Conda environment. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Kubeflow Python SDK Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/kubernetes.rst.txt Install the Kubeflow Python SDK using pip. This is optional and used for programmatic submission of training jobs. ```bash pip install kubeflow ``` -------------------------------- ### SparkXGBClassifier Example Source: https://xgboost.readthedocs.io/en/latest/python/python_api.html Demonstrates how to create a PySpark DataFrame and initialize SparkXGBClassifier for training. This example requires SparkSession and Vectors to be available. ```python from xgboost.spark import SparkXGBClassifier from pyspark.ml.linalg import Vectors df_train = spark.createDataFrame([ (Vectors.dense(1.0, 2.0, 3.0), 0, False, 1.0), (Vectors.sparse(3, {1: 1.0, 2: 5.5}), 1, False, 2.0), (Vectors.dense(4.0, 5.0, 6.0), 0, True, 1.0), ``` -------------------------------- ### Install from Source Distribution Source: https://xgboost.readthedocs.io/en/latest/contrib/python_packaging.html Install XGBoost using a source distribution file. The '-v' flag shows verbose output, detailing the compilation process of the bundled C++ code into libxgboost.so. ```bash pip install -v xgboost-2.0.0.tar.gz # Add -v to show build progress ``` -------------------------------- ### Multiclass Classification Setup Source: https://xgboost.readthedocs.io/en/latest/r_docs/R-package/docs/reference/xgb.cb.gblinear.history.html Prepares data and parameters for a multiclass classification problem using the gblinear booster. This setup is a prerequisite for training or cross-validation. ```R #### Multiclass classification: dtrain <- xgb.DMatrix(scale(x), label = as.numeric(iris$Species) - 1, nthread = nthread) param <- xgb.params( booster = "gblinear", objective = "multi:softprob", num_class = 3, reg_lambda = 0.0003, reg_alpha = 0.0003, nthread = nthread ) # For the default linear updater 'shotgun' it sometimes is helpful ``` -------------------------------- ### Install XGBoost using pip Source: https://xgboost.readthedocs.io/en/latest/_sources/install.rst.txt Install the latest stable release of XGBoost from PyPI. Pip 21.3+ is required. Consider using --user or virtualenv for permission issues. ```bash pip install xgboost ``` -------------------------------- ### Install XGBoost4J to Local Maven Repository (Skip Tests) Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Install the XGBoost4J artifacts to your local Maven repository, skipping tests. This is a faster alternative if tests are not required. ```bash mvn -DskipTests install ``` -------------------------------- ### Create and Train Learner Instance Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1Learner.html Demonstrates the basic workflow of creating a Learner instance, configuring it, and performing training iterations. ```cpp std::unique_ptr learner{Learner::Create(cache_mats)}; learner->Configure(configs); for (int iter = 0; iter < max_iter; ++iter) { learner->UpdateOneIter(iter, train_mat); LOG(INFO) << learner->EvalOneIter(iter, data_sets, data_names); } ``` -------------------------------- ### Build Documentation with Make Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Run 'make ' in the 'xgboost/doc' directory to build documentation in a specified format. Replace '' with the desired output format (e.g., html, pdf). Use 'make help' for a list of supported formats. ```bash make ``` ```bash make help ``` -------------------------------- ### Get XGBoost Build Information Source: https://xgboost.readthedocs.io/en/latest/python/python_api.html Retrieve build information for the installed XGBoost version using `xgboost.build_info()`. ```python xgboost.build_info() ``` -------------------------------- ### Get Data Iterator Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1common_1_1IterSpan.html Returns the starting iterator of the IterSpan. This can be used to access the underlying data sequence. ```cpp template XGBOOST_DEVICE It | data () const noexcept ``` -------------------------------- ### Install Python Package Building Shared Object Automatically Source: https://xgboost.readthedocs.io/en/latest/build.html Install the Python package directly. If the shared object is not present, the setup script will automatically run the CMake build command. Use the verbose flag (-v) for detailed build progress. ```bash cd python-package/ pip install -v . # Builds the shared object automatically. ``` -------------------------------- ### Install Python Package with Custom CMake Settings Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Use the '--config-settings' option with 'pip install -v .' to pass custom CMake flags. For example, to enable CUDA and NCCL, use '--config-settings use_cuda=True --config-settings use_nccl=True'. Requires Pip 22.1 or later. ```bash pip install -v . --config-settings use_cuda=True --config-settings use_nccl=True ``` -------------------------------- ### Train and Predict with XGBoost in Julia Source: https://xgboost.readthedocs.io/en/latest/_sources/get_started.rst.txt A Julia example for reading data using libsvm format, training an XGBoost model, and performing predictions. Make sure the XGBoost package is installed. ```julia using XGBoost # read data train_X, train_Y = readlibsvm("demo/data/agaricus.txt.train", (6513, 126)) test_X, test_Y = readlibsvm("demo/data/agaricus.txt.test", (1611, 126)) # fit model num_round = 2 bst = xgboost(train_X, num_round, label=train_Y, eta=1, max_depth=2) # predict pred = predict(bst, test_X) ``` -------------------------------- ### SparkXGBRanker Initialization and Training Example Source: https://xgboost.readthedocs.io/en/latest/python/python_api.html Demonstrates how to initialize SparkXGBRanker, create training and testing DataFrames with dense and sparse vectors, train a model, and transform test data for predictions. Ensure SparkSession is available. ```python from xgboost.spark import SparkXGBRanker from pyspark.ml.linalg import Vectors ranker = SparkXGBRanker(qid_col="qid") df_train = spark.createDataFrame( [ (Vectors.dense(1.0, 2.0, 3.0), 0, 0), (Vectors.dense(4.0, 5.0, 6.0), 1, 0), (Vectors.dense(9.0, 4.0, 8.0), 2, 0), (Vectors.sparse(3, {1: 1.0, 2: 5.5}), 0, 1), (Vectors.sparse(3, {1: 6.0, 2: 7.5}), 1, 1), (Vectors.sparse(3, {1: 8.0, 2: 9.5}), 2, 1), ], ["features", "label", "qid"], ) df_test = spark.createDataFrame( [ (Vectors.dense(1.5, 2.0, 3.0), 0), (Vectors.dense(4.5, 5.0, 6.0), 0), (Vectors.dense(9.0, 4.5, 8.0), 0), (Vectors.sparse(3, {1: 1.0, 2: 6.0}), 1), (Vectors.sparse(3, {1: 6.0, 2: 7.0}), 1), (Vectors.sparse(3, {1: 8.0, 2: 10.5}), 1), ], ["features", "qid"], ) model = ranker.fit(df_train) model.transform(df_test).show() ``` -------------------------------- ### Command-line Argument Parsing for Demo Source: https://xgboost.readthedocs.io/en/latest/_downloads/5d9ee6d18c01834ac20e56ec291518b4/learning_to_rank.ipynb Sets up command-line argument parsing for the demonstration script, requiring paths for data and cache directories. This is used to launch the ranking and click data demos. ```python parser = argparse.ArgumentParser( description="Demonstration of learning to rank using XGBoost." ) parser.add_argument( "--data", type=str, help="Root directory of the MSLR-WEB10K data.", required=True, ) parser.add_argument( "--cache", type=str, help="Directory for caching processed data.", required=True, ) args = parser.parse_args() ``` -------------------------------- ### start() Source: https://xgboost.readthedocs.io/en/latest/jvm_docs/javadocs/index-all.html Starts the tracker or communicator. ```APIDOC ## start() ### Description Starts the tracker or communicator. ### Method N/A (Java Method/Interface Method) ``` -------------------------------- ### XGBoost Learning to Rank Argument Parser Setup Source: https://xgboost.readthedocs.io/en/latest/python/examples/learning_to_rank.html Sets up an argument parser for a learning to rank demonstration script. It defines required arguments for data and cache directories, essential for running the demo. ```python if __name__ == "__main__": parser = argparse.ArgumentParser( description="Demonstration of learning to rank using XGBoost." ) parser.add_argument( "--data", type=str, help="Root directory of the MSLR-WEB10K data.", required=True, ) parser.add_argument( "--cache", type=str, help="Directory for caching processed data.", required=True, ) args = parser.parse_args() ranking_demo(args) click_data_demo(args) ``` -------------------------------- ### Example: Run GPU Test Script Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/ci.rst.txt Execute a Python wheel test script with GPU support enabled. Ensure the --use-gpus flag is set. ```bash python3 ops/docker_run.py --image-uri 492475357299.dkr.ecr.us-west-2.amazonaws.com/xgb-ci.gpu:main --use-gpus -- bash ops/pipeline/test-python-wheel.sh gpu ``` -------------------------------- ### Build HTML Documentation Locally Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/coding_guide.rst.txt Command to build the HTML documentation locally. This command should be run from the 'doc' directory. ```bash make html ``` -------------------------------- ### Verify XGBoost Installation Source: https://xgboost.readthedocs.io/en/latest/_sources/python/python_intro.rst.txt Run this Python code to verify your XGBoost installation. ```python import xgboost as xgb ``` -------------------------------- ### Setup Miniforge environment using custom action Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/ci.rst.txt Uses a custom GitHub Action to set up a Conda environment for C++ testing. Specify the environment name and its definition file. ```yaml - uses: dmlc/xgboost-devops/actions/miniforge-setup@main with: environment-name: cpp_test environment-file: ops/conda_env/cpp_test.yml ``` -------------------------------- ### Install GPU XGBoost with Conda Source: https://xgboost.readthedocs.io/en/latest/_sources/install.rst.txt Explicitly install the GPU-enabled variant of XGBoost using Conda. ```bash conda install -c conda-forge py-xgboost=*=cuda* ``` -------------------------------- ### Basic xgb.train Example Source: https://xgboost.readthedocs.io/en/latest/r_docs/R-package/docs/reference/xgb.train.html Demonstrates a simple use case of xgb.train with predefined parameters and evaluation sets. Ensure data is loaded and converted to xgb.DMatrix format. ```R data(agaricus.train, package = "xgboost") data(agaricus.test, package = "xgboost") ## Keep the number of threads to 1 for examples nthread <- 1 data.table::setDTthreads(nthread) dtrain <- with( agaricus.train, xgb.DMatrix(data, label = label, nthread = nthread) ) dtest <- with( agaricus.test, xgb.DMatrix(data, label = label, nthread = nthread) ) evals <- list(train = dtrain, eval = dtest) ## A simple xgb.train example: param <- xgb.params( max_depth = 2, nthread = nthread, objective = "binary:logistic", eval_metric = "auc" ) bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals, verbose = 0) ``` -------------------------------- ### Install CPU-only XGBoost with Conda Source: https://xgboost.readthedocs.io/en/latest/_sources/install.rst.txt Explicitly install the CPU-only variant of XGBoost using Conda. ```bash conda install -c conda-forge py-xgboost=*=cpu* ``` -------------------------------- ### Set up and Run Cross-Validation Source: https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html Initializes a CrossValidator with the pipeline, evaluator, parameter grid, and number of folds, then fits it to the training data. ```scala val cv = new CrossValidator() .setEstimator(pipeline) .setEvaluator(evaluator) .setEstimatorParamMaps(paramGrid) .setNumFolds(3) val cvModel = cv.fit(training) ``` -------------------------------- ### Main entry point for training with external memory Source: https://xgboost.readthedocs.io/en/latest/_downloads/4dd89726ef8a241fabfe76f949a5d070/external_memory.ipynb This function serves as the entry point for training, generating random data in batches and then calling the appropriate training functions based on the specified tree method and device. ```python def main(work_dir: str, cli_args: argparse.Namespace) -> None: """Entry point for training.""" # generate some random data for demo files = make_batches( n_samples_per_batch=1024, n_features=17, n_batches=31, work_dir=work_dir ) it = Iterator(cli_args.device, files) hist_train(it) approx_train(it) ``` -------------------------------- ### Install libomp on MacOS Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt On MacOS, install the 'libomp' dependency using Homebrew before building XGBoost. ```bash brew install libomp ``` -------------------------------- ### XGTrackerRun Source: https://xgboost.readthedocs.io/en/latest/dev/c__api_8h.html Starts the tracker process. The tracker runs in the background, and this function returns once the tracker has been successfully started. ```APIDOC ## XGTrackerRun ### Description Starts the tracker process. The tracker runs in the background, and this function returns once the tracker has been successfully started. ### Signature `int XGTrackerRun(TrackerHandle handle, char const *config)` ### Parameters * **handle** (TrackerHandle) - Handle to the tracker instance. * **config** (char const *) - Configuration string for running the tracker. ``` -------------------------------- ### Get Tensor Strides Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1linalg_1_1TensorView.html Retrieves the strides of the tensor. Can be used to get all strides or the stride of a specific dimension. ```cpp LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Stride() const ``` ```cpp LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Stride(size_t i) const ``` -------------------------------- ### Install Python Package with Automatic Shared Object Build Source: https://xgboost.readthedocs.io/en/latest/_sources/build.rst.txt Navigate to the python-package directory and run 'pip install -v .' to install the Python package. If the shared object is not present, this command will automatically trigger the CMake build process. ```console $ cd python-package/ $ pip install -v . # Builds the shared object automatically. ``` -------------------------------- ### Apply Training Job Manifest (Bash) Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/kubernetes.rst.txt Apply the Kubernetes manifest file to create the training job. ```bash kubectl apply -f xgboost-cpu-trainjob.yaml ``` -------------------------------- ### Install Pytest for Python Unit Tests Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/unit_tests.rst.txt Installs the pytest package, a requirement for running Python unit tests. ```bash pip3 install pytest ``` -------------------------------- ### Get Constant Host Pointer (Alias) Source: https://xgboost.readthedocs.io/en/latest/dev/host__device__vector_8h_source.html Alias for getting a const pointer to the host data. This is equivalent to ConstHostPointer. ```cpp const T* HostPointer() const { return ConstHostPointer(); } ``` -------------------------------- ### Get Constant Device Pointer (Overload) Source: https://xgboost.readthedocs.io/en/latest/dev/host__device__vector_8h_source.html Overload for getting a const pointer to the device data. This is an alias for ConstDevicePointer. ```cpp const T* DevicePointer() const { return ConstDevicePointer(); } ``` -------------------------------- ### Get Constant Device Span (Overload) Source: https://xgboost.readthedocs.io/en/latest/dev/host__device__vector_8h_source.html Overload for getting a constant Span to the device data. This is an alias for ConstDeviceSpan. ```cpp common::Span DeviceSpan() const { return ConstDeviceSpan(); } ``` -------------------------------- ### explainParams() Source: https://xgboost.readthedocs.io/en/latest/python/python_api.html Returns the documentation of all params with their optionally default values and user-supplied values. ```APIDOC ## explainParams() ### Description Returns the documentation of all params with their optionally default values and user-supplied values. ### Return type str ``` -------------------------------- ### Get Tensor Shape Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1linalg_1_1TensorView.html Retrieves the shape of the tensor. Can be used to get the full shape or the shape of a specific dimension. ```cpp LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Shape() const ``` ```cpp LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Shape(size_t i) const ``` -------------------------------- ### explainParams() Source: https://xgboost.readthedocs.io/en/latest/python/python_api.html Returns the documentation of all params with their optionally default values and user-supplied values. ```APIDOC ## explainParams() ### Description Returns the documentation of all params with their optionally default values and user-supplied values. ### Return Type str ``` -------------------------------- ### XGBoost Native Interface Walkthrough Source: https://xgboost.readthedocs.io/en/latest/_downloads/b5f61f45a56c997359ac4a89209d9336/basic_walkthrough.ipynb This snippet demonstrates the complete workflow of using the native XGBoost interface. It includes data loading, DMatrix creation, parameter configuration, model training with evaluation, prediction, and model persistence. ```python import os import pickle import numpy as np from sklearn.datasets import load_svmlight_file import xgboost as xgb # Make sure the demo knows where to load the data. CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) XGBOOST_ROOT_DIR = os.path.dirname(os.path.dirname(CURRENT_DIR)) DEMO_DIR = os.path.join(XGBOOST_ROOT_DIR, "demo") # X is a scipy csr matrix, XGBoost supports many other input types, X, y = load_svmlight_file(os.path.join(DEMO_DIR, "data", "agaricus.txt.train")) dtrain = xgb.DMatrix(X, y) # validation set X_test, y_test = load_svmlight_file(os.path.join(DEMO_DIR, "data", "agaricus.txt.test")) dtest = xgb.DMatrix(X_test, y_test) # specify parameters via map, definition are same as c++ version param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} # specify validations set to watch performance watchlist = [(dtest, "eval"), (dtrain, "train")] # number of boosting rounds num_round = 2 bst = xgb.train(param, dtrain, num_boost_round=num_round, evals=watchlist) # run prediction preds = bst.predict(dtest) labels = dtest.get_label() print( "error=%f" % ( sum(1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i]) / float(len(preds)) ) ) bst.save_model("model-0.json") # dump model bst.dump_model("dump.raw.txt") # dump model with feature map bst.dump_model("dump.nice.txt", os.path.join(DEMO_DIR, "data/featmap.txt")) # save dmatrix into binary buffer dtest.save_binary("dtest.dmatrix") # save model bst.save_model("model-1.json") # load model and data in bst2 = xgb.Booster(model_file="model-1.json") dtest2 = xgb.DMatrix("dtest.dmatrix") preds2 = bst2.predict(dtest2) # assert they are the same assert np.sum(np.abs(preds2 - preds)) == 0 # alternatively, you can pickle the booster pks = pickle.dumps(bst2) # load model and data in bst3 = pickle.loads(pks) preds3 = bst3.predict(dtest2) # assert they are the same assert np.sum(np.abs(preds3 - preds)) == 0 ``` -------------------------------- ### Get Batches (SparsePage) Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1DMatrix.html Gets batches of data specifically as `SparsePage` from the DMatrix. This is an optimized version for sparse data. ```APIDOC ## GetBatches() [2/6] ```cpp template<> BatchSet xgboost::DMatrix::GetBatches() ``` ### Description Gets batches of SparsePage from the DMatrix. ``` -------------------------------- ### Create and Populate a JSON Object Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1Json.html Demonstrates how to create a JSON object and assign values to its keys, including strings and arrays. Note the limitation regarding UTF-8 support. ```cpp // Create a JSON object. Json object { Object() }; // Assign key "key" with a JSON string "Value"; object["key"] = String("Value"); // Assign key "arr" with a empty JSON Array; object["arr"] = Array(); ``` -------------------------------- ### Install XGBoost from CRAN Source: https://xgboost.readthedocs.io/en/latest/r_docs/R-package/docs/index.html Install the stable, pre-compiled version of the XGBoost R package from CRAN. This is the recommended method for most users. ```r install.packages('xgboost') ``` -------------------------------- ### Get Constant Host Span (Alias) Source: https://xgboost.readthedocs.io/en/latest/dev/host__device__vector_8h_source.html Alias for getting a constant Span to the host data. This is equivalent to HostSpan() const. ```cpp common::Span ConstHostSpan() const { return HostSpan(); } ``` -------------------------------- ### Build XGBoost with GPU Support and Package Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/ci.rst.txt Build XGBoost with GPU support and package it as a Python wheel. Requires setting the DOCKER_REGISTRY environment variable. ```bash export DOCKER_REGISTRY=492475357299.dkr.ecr.us-west-2.amazonaws.com python3 ops/docker_run.py \ --image-uri ${DOCKER_REGISTRY}/xgb-ci.gpu_build_rockylinux8:main \ -- ops/pipeline/build-cuda-impl.sh ``` -------------------------------- ### Get Batches (SortedCSCPage) Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1DMatrix.html Gets batches of data as `SortedCSCPage` from the DMatrix, requiring a context object. This is suitable for column-sparse data. ```APIDOC ## GetBatches() [3/6] ```cpp template<> BatchSet xgboost::DMatrix::GetBatches(Context const * _ctx_) ``` ### Description Gets batches of SortedCSCPage from the DMatrix, using the provided context. ### Parameters - **_ctx_** (Context const *) - The context object. ``` -------------------------------- ### Span begin() Method Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1common_1_1Span.html Returns a constexpr, device-compatible iterator to the beginning of the span. Marked as noexcept. ```cpp constexpr XGBOOST_DEVICE iterator | begin () const __span_noexcept ``` -------------------------------- ### Example Prediction Results Table Source: https://xgboost.readthedocs.io/en/latest/_sources/jvm/xgboost4j_spark_gpu_tutorial.rst.txt This is an example of the output table generated after performing predictions on a test dataset using a trained XGBoostClassificationModel. ```text +------------+-----------+------------------+-------------------+"class"+"rawPrediction"+"probability"+"prediction"+ +------------+-----------+------------------+-------------------+"class"+"rawPrediction"+"probability"+"prediction"+ |sepal length|sepal width| petal length| petal width| 0|[3.16666603088378...|[0.98853939771652...| 0.0| | 4.6| 3.1| 1.5| 0.2| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 4.8| 3.1| 1.6| 0.2| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 4.8| 3.4| 1.6| 0.2| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 4.8| 3.4|1.9000000000000001| 0.2| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 4.9| 2.4| 3.3| 1.0| 1|[-2.1498908996582...|[0.00596602633595...| 1.0| | 4.9| 2.5| 4.5| 1.7| 2|[-2.1498908996582...|[0.00596602633595...| 1.0| | 5.0| 3.5| 1.3|0.30000000000000004| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 5.1| 2.5| 3.0| 1.1| 1|[3.16666603088378...|[0.98853939771652...| 0.0| | 5.1| 3.3| 1.7| 0.5| 0|[3.25857257843017...|[0.98969423770904...| 0.0| | 5.1| 3.5| 1.4| 0.2| 0|[3.25857257843017...|[0.98969423770904...| 0.0| +------------+-----------+------------------+-------------------+-----+--------------------+--------------------+----------+ ``` -------------------------------- ### Main Execution Block for Dask Learning to Rank Demo Source: https://xgboost.readthedocs.io/en/latest/_sources/python/dask-examples/dask_learning_to_rank.rst.txt Parses command-line arguments for data path, cache directory, device, and split behavior, then initializes and runs the appropriate learning to rank demo. This is the entry point for the script. ```python if __name__ == "__main__": parser = argparse.ArgumentParser( description="Demonstration of learning to rank using XGBoost." ) parser.add_argument( "--data", type=str, help="Root directory of the MSLR-WEB10K data.", required=True, ) parser.add_argument( "--cache", type=str, help="Directory for caching processed data.", required=True, ) parser.add_argument("--device", choices=["cpu", "cuda"], default="cpu") parser.add_argument( "--no-split", action="store_true", help="Flag to indicate query groups should not be split.", ) args = parser.parse_args() with gen_client(args.device) as client: if args.no_split: ranking_wo_split_demo(client, args) else: ranking_demo(client, args) ``` -------------------------------- ### Create Learner Instance Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1Learner.html Static method to create a new instance of the Learner. Requires a vector of DMatrix objects for caching. ```cpp static Learner * Create(const std::vector< std::shared_ptr< DMatrix > > &cache_data) ``` -------------------------------- ### Install XGBoost via Conda Source: https://xgboost.readthedocs.io/en/latest/tutorials/c_api_tutorial.html Commands to clone the XGBoost repository, activate a Conda environment, build, and install XGBoost within that environment. ```bash # clone the XGBoost repository & its submodules git clone --recursive https://github.com/dmlc/xgboost cd xgboost # Activate the Conda environment, into which we'll install XGBoost conda activate [env_name] # Build the compiled version of XGBoost inside the build folder cmake -B build -S . -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX # install XGBoost in your conda environment (usually under [your home directory]/miniconda3) cmake --build build --target install ``` -------------------------------- ### Verify XGBoost Runtime Installation Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/kubernetes.rst.txt Check if the XGBoost runtime is available on your Kubernetes cluster after installation. This command lists all cluster training runtimes. ```bash kubectl get clustertrainingruntime ``` -------------------------------- ### Print xgb.Booster Object Source: https://xgboost.readthedocs.io/en/latest/r_docs/R-package/docs/reference/print.xgb.Booster.html Demonstrates how to train an xgb.Booster model and then print its details. This includes setting up training data, training the model with specified parameters, adding a custom attribute, and finally printing the booster object to view its summary. ```r data(agaricus.train, package = "xgboost") train <- agaricus.train bst <- xgb.train( data = xgb.DMatrix(train$data, label = train$label, nthread = 1), nrounds = 2, params = xgb.params( max_depth = 2, nthread = 2, objective = "binary:logistic" ) ) attr(bst, "myattr") <- "memo" print(bst) ``` -------------------------------- ### Creating and Saving an xgb.DMatrix Source: https://xgboost.readthedocs.io/en/latest/r_docs/R-package/docs/reference/xgb.DMatrix.html Demonstrates how to create an xgb.DMatrix from training data, save it to a file, and then load it back. This is useful for managing large datasets or for use in reproducible workflows. Ensure the number of threads is managed for consistent example execution. ```R data(agaricus.train, package = "xgboost") ## Keep the number of threads to 1 for examples nthread <- 1 data.table::setDTthreads(nthread) dtrain <- with( agaricus.train, xgb.DMatrix(data, label = label, nthread = nthread) ) fname <- file.path(tempdir(), "xgb.DMatrix.data") xgb.DMatrix.save(dtrain, fname) dtrain <- xgb.DMatrix(fname, nthread = 1) ``` -------------------------------- ### Sphinx Build Output Example Source: https://xgboost.readthedocs.io/en/latest/_sources/contrib/docs.rst.txt This console output shows a successful Sphinx build process, indicating that the HTML pages have been generated and are located in the '_build/html' directory. ```console $ make html sphinx-build -b html -d _build/doctrees . _build/html Running Sphinx v6.2.1 ... The HTML pages are in _build/html. Build finished. The HTML pages are in _build/html. ``` -------------------------------- ### Basic External Memory DMatrix Creation and Training Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/external_memory.rst.txt Demonstrates the creation of an external memory DMatrix and subsequent model training. Ensure necessary imports and data iterators are available. ```python Xy_valid = xgboost.ExtMemQuantileDMatrix(it_valid, max_bin=n_bins, ref=Xy_train) booster = xgboost.train( { "tree_method": "hist", "max_bin": n_bins, "device": device, }, Xy_train, num_boost_round=n_rounds, evals=[(Xy_train, "Train"), (Xy_valid, "Valid")] ) ``` -------------------------------- ### Get Batches (Generic) Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1DMatrix.html Gets batches of data from the DMatrix. This generic version can be used with different page types specified by the template parameter `T`. ```APIDOC ## GetBatches() [1/6] ```cpp template BatchSet xgboost::DMatrix::GetBatches() ``` ### Description Gets batches. Use range based for loop over BatchSet to access individual batches. ### Returns A BatchSet containing batches of type T. ``` -------------------------------- ### Evaluation Log Example Source: https://xgboost.readthedocs.io/en/latest/_sources/tutorials/advanced_custom_obj.rst.txt Example of an evaluation log produced during XGBoost training with a custom objective. Shows the decreasing loss over boosting rounds. ```none [0] Train-dirichlet_ll:-40.25009 [1] Train-dirichlet_ll:-47.69122 [2] Train-dirichlet_ll:-52.64620 [3] Train-dirichlet_ll:-56.36977 [4] Train-dirichlet_ll:-59.33048 [5] Train-dirichlet_ll:-61.93359 [6] Train-dirichlet_ll:-64.17280 [7] Train-dirichlet_ll:-66.29709 [8] Train-dirichlet_ll:-68.21001 [9] Train-dirichlet_ll:-70.03442 ``` -------------------------------- ### Configure() Source: https://xgboost.readthedocs.io/en/latest/dev/classxgboost_1_1TreeUpdater.html Initializes the tree updater with the provided arguments. This method is essential for setting up the updater's behavior based on specific configurations. ```APIDOC ## Configure() ### Description Initializes the updater with given arguments. ### Method virtual void Configure(const Args &args) ### Parameters #### Arguments - **args** (Args) - Required - Arguments to the objective function. ```