# XGBoost — Scalable Gradient Boosting for Machine Learning > XGBoost is an optimized distributed gradient boosting library for supervised learning tasks. It provides highly efficient implementations of gradient boosted trees for classification, regression, and ranking across CPU and GPU with bindings for Python, R, Java, and more. ## Install Save as a script file and run: # XGBoost — Scalable Gradient Boosting for Machine Learning ## Quick Use ```bash pip install xgboost python -c " import xgboost as xgb from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model = xgb.XGBClassifier(n_estimators=100, use_label_encoder=False, eval_metric='mlogloss') model.fit(X_train, y_train) print(f'Accuracy: {model.score(X_test, y_test):.2f}') " ``` ## Introduction XGBoost (eXtreme Gradient Boosting) is one of the most successful machine learning algorithms for structured and tabular data. Originally developed by Tianqi Chen, it has won numerous Kaggle competitions and remains a go-to choice for classification, regression, and ranking problems in both research and industry. ## What XGBoost Does - Trains gradient boosted decision tree ensembles for classification, regression, and ranking - Handles missing values natively without requiring imputation - Supports distributed training across multiple machines via Dask, Spark, and Ray integrations - Provides GPU-accelerated training with the hist and approx tree methods - Includes built-in cross-validation, early stopping, and feature importance analysis ## Architecture Overview XGBoost builds an ensemble of decision trees sequentially, where each new tree corrects the residual errors of the previous ensemble. It uses a second-order Taylor expansion of the loss function to find optimal splits efficiently. The core is written in C++ with a column-block data structure that enables parallel and cache-aware split finding. External memory mode allows training on datasets larger than RAM. ## Self-Hosting & Configuration - Install via pip: `pip install xgboost` or conda: `conda install -c conda-forge xgboost` - GPU training requires `xgboost[cuda12]` and a compatible NVIDIA driver - Key hyperparameters: `max_depth`, `learning_rate`, `n_estimators`, `subsample`, `colsample_bytree` - Distributed training supported via Dask (`xgb.dask.DaskXGBClassifier`) or Spark (`xgb.spark.SparkXGBClassifier`) - Models save and load with `model.save_model()` and `xgb.Booster.load_model()` in JSON or binary format ## Key Features - Regularized learning (L1 and L2) to prevent overfitting built into the objective - Histogram-based approximate split finding for fast training on large datasets - Native handling of sparse data and missing values - Monotonic constraints to enforce domain knowledge on feature relationships - Scikit-learn compatible API alongside a native Booster interface ## Comparison with Similar Tools - **LightGBM** — uses leaf-wise growth for faster training on large data but may overfit small datasets more easily - **CatBoost** — excels with categorical features out of the box but is slower to train in many benchmarks - **Random Forest** — simpler ensemble method but generally less accurate than boosted trees on tabular data - **Neural Networks** — better for unstructured data (images, text) but XGBoost often wins on tabular benchmarks - **scikit-learn GBM** — simpler API but lacks XGBoost's distributed training and GPU acceleration ## FAQ **Q: When should I use XGBoost over deep learning?** A: XGBoost typically outperforms deep learning on structured/tabular datasets with fewer than millions of rows. For images, text, or very large unstructured data, deep learning is usually better. **Q: How do I tune XGBoost hyperparameters?** A: Start with `learning_rate=0.1`, `max_depth=6`, `n_estimators=1000` with early stopping. Use Optuna or grid search to refine `subsample`, `colsample_bytree`, and regularization terms. **Q: Can XGBoost handle categorical features directly?** A: Yes, since version 1.6, XGBoost supports `enable_categorical=True` for native categorical handling without one-hot encoding. **Q: Does XGBoost scale to large datasets?** A: Yes, via distributed backends (Dask, Spark, Ray), GPU acceleration, and external memory mode for out-of-core training. ## Sources - https://github.com/dmlc/xgboost - https://xgboost.readthedocs.io --- Source: https://tokrepo.com/en/workflows/d13939ab-3d9c-11f1-9bc6-00163e2b0d79 Author: Script Depot