XGBoost: A Scalable Tree Boosting System

repository·master·Indexed 12 days ago

https://github.com/dmlc/xgboost

A highly efficient, distributed gradient boosting library designed for scalable machine learning tasks. It supports Python, R, C/C++, Java, and Scala, with integrations for Spark, Kubernetes, and Dask. Features include a C API for custom data iterators, support for external memory loading, and compatibility with hyperparameter optimization tools like Optuna and FLAML.

Tokens
111.4K
Snippets
315
Records
519
Agent score
96%

What's inside XGBoost

  1. Overview of XGBoost4J for Scala and Java

    master

    XGBoost4J is the JVM-based package for XGBoost, designed to integrate XGBoost's optimizations and performance into the JVM ecosystem. It allows developers to:

    • Train XGBoost models using Scala and Java with support for customization.
    • Execute distributed XGBoost training natively on JVM-based big data frameworks, specifically Apache Flink and Apache Spark.
  2. Overview of XGBoost

    master

    XGBoost (eXtreme Gradient Boosting) is an optimized distributed gradient boosting library designed for efficiency, flexibility, and portability. It implements machine learning algorithms under the Gradient Boosting framework, specifically providing parallel tree boosting (GBDT, GBM) to solve data science problems quickly and accurately.

    Key features include:

    • Scalability: Can solve problems involving billions of examples.
    • Distributed Support: The same code runs across major distributed environments including Kubernetes, Hadoop, SGE, Dask, Spark, and PySpark.
    • Integration: Integrated with tools like Optuna for hyperparameter optimization.
  3. Overview of XGBoost4J-Spark integration

    master

    XGBoost4J-Spark integrates XGBoost with Apache Spark by fitting XGBoost into the Spark MLLIB framework. This allows developers to combine XGBoost's high-performance gradient boosting algorithms with Spark's distributed data processing capabilities for:

    • Feature Engineering: Performing feature extraction, transformation, dimensionality reduction, and selection at scale.
    • Pipelines: Constructing, evaluating, and tuning machine learning pipelines using Spark's ML infrastructure.
    • Persistence: Persisting and loading machine learning models and entire ML Pipelines.
  4. List of available XGBoost tutorials

    master

    The following topics are covered in the official XGBoost tutorials. Use these as starting points for specific implementation tasks:

    Core Modeling & Features

    • model: General model training.
    • saving_model: How to persist models to disk.
    • slicing_model: Techniques for model slicing.
    • learning_to_rank: Implementation of ranking objectives.
    • dart: Using DART (Dropouts meet Multiple Additive Regression Trees).
    • monotonic: Enforcing monotonic constraints on features.
    • feature_interaction_constraint: Restricting feature interactions.
    • aft_survival_analysis: Accelerated Failure Time survival analysis.
    • categorical: Handling categorical features.
    • multioutput: Multi-output regression/classification.
    • rf: Random Forest implementation within XGBoost.
    • intercept: Using intercept terms.

    Distributed & Scalable Computing

    • kubernetes: Running XGBoost on Kubernetes.
    • Distributed XGBoost with XGBoost4J-Spark: Using XGBoost with Spark via the Java/Scala API.
    • Distributed XGBoost with XGBoost4J-Spark-GPU: Using XGBoost with Spark and GPU acceleration.
    • dask: Scaling XGBoost with Dask.
    • spark_estimator: Using the Spark estimator interface.
    • ray: Scaling XGBoost with Ray.
    • external_memory: Training with datasets larger than RAM using external memory.

    Advanced API & Customization

    • c_api_tutorial: Using the XGBoost C API.
    • input_format: Understanding supported data input formats.
    • param_tuning: Hyperparameter tuning strategies.
    • custom_metric_obj: Implementing custom evaluation metrics.
    • advanced_custom_obj: Implementing advanced custom objective functions.
    • privacy_preserving: Privacy-preserving machine learning techniques.
  5. Overview of Learning to Rank in XGBoost

    master

    Learning-to-rank (LTR) in XGBoost aims to train models that arrange query results into an ordered list. XGBoost implements LTR using the LambdaMART algorithm, which is an adaptation of the LambdaRank framework to gradient boosting trees.

    Key characteristics include:

    • Pairwise Model: It compares the relevance of pairs of samples within a query group to calculate proxy gradients.
    • Objective Functions: Supports objectives like rank:ndcg (default) and rank:map.
    • Data Structure: Training samples must be grouped by a query index (qid). Samples within a query group must be sorted by their qid in non-decreasing order.
  6. Overview of Distributed XGBoost with Dask

    master

    XGBoost provides a Dask interface to run training and prediction across a distributed cluster. A Dask cluster consists of a scheduler, workers, and a client (the user entry point).

    When using the XGBoost Dask interface, you must pass the client object as an argument to most functions. If client is set to None, XGBoost will attempt to use the default client returned by Dask.

    Key Requirements:

    • Data (X and y) must be Dask DataFrames or Dask Arrays.
    • Cluster construction should be guarded by if __name__ == "__main__": to avoid errors in distributed environments.
    from xgboost import dask as dxgb
    import dask.array as da
    import dask.distributed
    
    if __name__ == "__main__":
        cluster = dask.distributed.LocalCluster()
        client = dask.distributed.Client(cluster)
    
        # X and y must be Dask dataframes or arrays
        num_obs = 1e5
        num_features = 20
        X = da.random.random(size=(num_obs, num_features), chunks=(1000, num_features))
        y = da.random.random(size=(num_obs, 1), chunks=(1000, 1))
    
        dtrain = dxgb.DaskDMatrix(client, X, y)
    
        output = dxgb.train(
            client,
            {"verbosity": 2, "tree_method": "hist", "objective": "reg:squarederror"},
            dtrain,
            num_boost_round=4,
            evals=[(dtrain, "train")],
        )
  7. How to pass parameters to xgb.train in R

    master

    In the XGBoost R package, parameters for the core library (such as max_depth, regularization terms, or device) are passed to the xgb.train function as an R list object.

    To provide a more idiomatic experience with IDE autocompletion and in-package documentation, it is recommended to use the xgb.params constructor function to build this list before passing it to xgb.train.

    # Conceptual usage pattern
    params <- xgb.params(
      max_depth = 6,
      eta = 0.1,
      # ... other parameters
    )
    
    xgb.train(
      params = params,
      # ... other arguments
    )
  8. How XGBoost Distributed Runtime Works

    master

    The XGBoost runtime is a Go plugin within the Kubeflow Trainer controller. It manages the lifecycle of a distributed training job through two primary mechanisms:

    1. Environment Injection

    It implements the EnforceMLPolicyPlugin interface to inject DMLC_* environment variables and expose the required container port (29500) for the Rabit tracker.

    2. Tracker Discovery

    Workers discover the RabitTracker on the rank-0 pod using a Kubernetes headless service. The DMLC_TRACKER_URI is automatically constructed using the pattern: <trainjob-name>-node-0-0.<trainjob-name>

    Example Pod Mapping for a job named myjob with 4 nodes:

    • myjob-node-0-0: DMLC_TASK_ID=0 (Acts as both Tracker and Worker)
    • myjob-node-0-1: DMLC_TASK_ID=1 (Worker)
    • myjob-node-0-2: DMLC_TASK_ID=2 (Worker)
    • myjob-node-0-3: DMLC_TASK_ID=3 (Worker)
  9. How optimal partitioning works for categorical data

    master
    Optimal partitioning is a technique used to find the best way to split categorical predictors at each node. Instead of enumerating all possible $2^{k-1} - 1$ binary partitions for $k$ categories, the algorithm sorts categories based on their gradient histogram (or projected scores in the case of vector leaves) and then enumerates contiguous partitions. This significantly reduces the computational complexity of finding the optimal split for discrete values.
  10. Distributed Learning-to-Rank in XGBoost

    master

    XGBoost supports distributed learning-to-rank via Dask, Spark, and PySpark.

    Key Considerations for Distributed Training:

    • Data Partitioning: To maintain accuracy, it is ideal to divide data partitions by query group so no single query group is split across workers. This prevents the loss of 'effective pairs' and normalizers like IDCG that occur when a group is split into smaller subgroups.
    • Sorting: The (Py)Spark and Dask interfaces can sort data by query ID to ensure correct aggregation of sample gradients.
    • Warning: Position-debiasing is not yet supported for existing distributed interfaces.
  11. Use `base_margin` for boosting from existing models

    master

    The base_margin is a metadata field for DMatrix (or a parameter in the sklearn fit method) that specifies the global bias for the boosted model.

    If base_margin is supplied, it overrides the base_score training parameter. This is useful when you want to train an XGBoost model to continue boosting from the predictions of another model.

  12. Understand the consequences of force push

    master

    A git push --force is required when you perform operations that alter the commit history, such as rebase or squash.

    Safety Rule: It is safe to force push to your own fork as long as the commits you are altering are exclusively your own. Avoid force pushing to shared branches where others may have based their work on your previous commit history.