Compare commits
1 Commits
master-roc
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
81d88acf39 |
@ -17,7 +17,7 @@ AllowShortEnumsOnASingleLine: true
|
|||||||
AllowShortBlocksOnASingleLine: Never
|
AllowShortBlocksOnASingleLine: Never
|
||||||
AllowShortCaseLabelsOnASingleLine: false
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
AllowShortFunctionsOnASingleLine: All
|
AllowShortFunctionsOnASingleLine: All
|
||||||
AllowShortLambdasOnASingleLine: Inline
|
AllowShortLambdasOnASingleLine: All
|
||||||
AllowShortIfStatementsOnASingleLine: WithoutElse
|
AllowShortIfStatementsOnASingleLine: WithoutElse
|
||||||
AllowShortLoopsOnASingleLine: true
|
AllowShortLoopsOnASingleLine: true
|
||||||
AlwaysBreakAfterDefinitionReturnType: None
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
|||||||
8
.github/dependabot.yml
vendored
8
.github/dependabot.yml
vendored
@ -8,7 +8,7 @@ updates:
|
|||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages"
|
directory: "/jvm-packages"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "monthly"
|
interval: "daily"
|
||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages/xgboost4j"
|
directory: "/jvm-packages/xgboost4j"
|
||||||
schedule:
|
schedule:
|
||||||
@ -16,11 +16,11 @@ updates:
|
|||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages/xgboost4j-gpu"
|
directory: "/jvm-packages/xgboost4j-gpu"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "monthly"
|
interval: "daily"
|
||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages/xgboost4j-example"
|
directory: "/jvm-packages/xgboost4j-example"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "monthly"
|
interval: "daily"
|
||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages/xgboost4j-spark"
|
directory: "/jvm-packages/xgboost4j-spark"
|
||||||
schedule:
|
schedule:
|
||||||
@ -28,4 +28,4 @@ updates:
|
|||||||
- package-ecosystem: "maven"
|
- package-ecosystem: "maven"
|
||||||
directory: "/jvm-packages/xgboost4j-spark-gpu"
|
directory: "/jvm-packages/xgboost4j-spark-gpu"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "monthly"
|
interval: "daily"
|
||||||
|
|||||||
43
.github/workflows/i386.yml
vendored
43
.github/workflows/i386.yml
vendored
@ -1,43 +0,0 @@
|
|||||||
name: XGBoost-i386-test
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read # to fetch code (actions/checkout)
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-32bit:
|
|
||||||
name: Build 32-bit
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
services:
|
|
||||||
registry:
|
|
||||||
image: registry:2
|
|
||||||
ports:
|
|
||||||
- 5000:5000
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2.5.0
|
|
||||||
with:
|
|
||||||
submodules: 'true'
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
with:
|
|
||||||
driver-opts: network=host
|
|
||||||
- name: Build and push container
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: tests/ci_build/Dockerfile.i386
|
|
||||||
push: true
|
|
||||||
tags: localhost:5000/xgboost/build-32bit:latest
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
- name: Build XGBoost
|
|
||||||
run: |
|
|
||||||
docker run --rm -v $PWD:/workspace -w /workspace \
|
|
||||||
-e CXXFLAGS='-Wno-error=overloaded-virtual -Wno-error=maybe-uninitialized -Wno-error=redundant-move' \
|
|
||||||
localhost:5000/xgboost/build-32bit:latest \
|
|
||||||
tests/ci_build/build_via_cmake.sh
|
|
||||||
58
.github/workflows/jvm_tests.yml
vendored
58
.github/workflows/jvm_tests.yml
vendored
@ -5,10 +5,6 @@ on: [push, pull_request]
|
|||||||
permissions:
|
permissions:
|
||||||
contents: read # to fetch code (actions/checkout)
|
contents: read # to fetch code (actions/checkout)
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-with-jvm:
|
test-with-jvm:
|
||||||
name: Test JVM on OS ${{ matrix.os }}
|
name: Test JVM on OS ${{ matrix.os }}
|
||||||
@ -19,36 +15,31 @@ jobs:
|
|||||||
os: [windows-latest, ubuntu-latest, macos-11]
|
os: [windows-latest, ubuntu-latest, macos-11]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
|
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
||||||
with:
|
with:
|
||||||
submodules: 'true'
|
submodules: 'true'
|
||||||
|
|
||||||
- uses: mamba-org/setup-micromamba@422500192359a097648154e8db4e39bdb6c6eed7 # v1.8.1
|
- uses: actions/setup-python@7f80679172b057fc5e90d70d197929d454754a5a # v4.3.0
|
||||||
with:
|
with:
|
||||||
micromamba-version: '1.5.6-0'
|
python-version: '3.8'
|
||||||
environment-name: jvm_tests
|
architecture: 'x64'
|
||||||
create-args: >-
|
|
||||||
python=3.10
|
- uses: actions/setup-java@d202f5dbf7256730fb690ec59f6381650114feb2 # v3.6.0
|
||||||
awscli
|
with:
|
||||||
cache-downloads: true
|
java-version: 1.8
|
||||||
cache-environment: true
|
|
||||||
init-shell: bash powershell
|
- name: Install Python packages
|
||||||
|
run: |
|
||||||
|
python -m pip install wheel setuptools
|
||||||
|
python -m pip install awscli
|
||||||
|
|
||||||
- name: Cache Maven packages
|
- name: Cache Maven packages
|
||||||
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
|
uses: actions/cache@6998d139ddd3e68c71e9e398d8e40b71a2f39812 # v3.2.5
|
||||||
with:
|
with:
|
||||||
path: ~/.m2
|
path: ~/.m2
|
||||||
key: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}
|
key: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}
|
||||||
restore-keys: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}
|
restore-keys: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}
|
||||||
|
|
||||||
- name: Build xgboost4j.dll
|
|
||||||
run: |
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake .. -G"Visual Studio 17 2022" -A x64 -DJVM_BINDINGS=ON
|
|
||||||
cmake --build . --config Release
|
|
||||||
if: matrix.os == 'windows-latest'
|
|
||||||
|
|
||||||
- name: Test XGBoost4J (Core)
|
- name: Test XGBoost4J (Core)
|
||||||
run: |
|
run: |
|
||||||
cd jvm-packages
|
cd jvm-packages
|
||||||
@ -56,19 +47,18 @@ jobs:
|
|||||||
|
|
||||||
- name: Extract branch name
|
- name: Extract branch name
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
|
||||||
echo "branch=${GITHUB_REF#refs/heads/}" >> "$GITHUB_OUTPUT"
|
|
||||||
id: extract_branch
|
id: extract_branch
|
||||||
if: |
|
if: |
|
||||||
(github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
|
(github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
|
||||||
(matrix.os == 'windows-latest' || matrix.os == 'macos-11')
|
matrix.os == 'windows-latest'
|
||||||
|
|
||||||
- name: Publish artifact xgboost4j.dll to S3
|
- name: Publish artifact xgboost4j.dll to S3
|
||||||
run: |
|
run: |
|
||||||
cd lib/
|
cd lib/
|
||||||
Rename-Item -Path xgboost4j.dll -NewName xgboost4j_${{ github.sha }}.dll
|
Rename-Item -Path xgboost4j.dll -NewName xgboost4j_${{ github.sha }}.dll
|
||||||
dir
|
dir
|
||||||
python -m awscli s3 cp xgboost4j_${{ github.sha }}.dll s3://xgboost-nightly-builds/${{ steps.extract_branch.outputs.branch }}/libxgboost4j/ --acl public-read --region us-west-2
|
python -m awscli s3 cp xgboost4j_${{ github.sha }}.dll s3://xgboost-nightly-builds/${{ steps.extract_branch.outputs.branch }}/ --acl public-read
|
||||||
if: |
|
if: |
|
||||||
(github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
|
(github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
|
||||||
matrix.os == 'windows-latest'
|
matrix.os == 'windows-latest'
|
||||||
@ -76,20 +66,6 @@ jobs:
|
|||||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_IAM_S3_UPLOADER }}
|
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_IAM_S3_UPLOADER }}
|
||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_IAM_S3_UPLOADER }}
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_IAM_S3_UPLOADER }}
|
||||||
|
|
||||||
- name: Publish artifact libxgboost4j.dylib to S3
|
|
||||||
shell: bash -l {0}
|
|
||||||
run: |
|
|
||||||
cd lib/
|
|
||||||
mv -v libxgboost4j.dylib libxgboost4j_${{ github.sha }}.dylib
|
|
||||||
ls
|
|
||||||
python -m awscli s3 cp libxgboost4j_${{ github.sha }}.dylib s3://xgboost-nightly-builds/${{ steps.extract_branch.outputs.branch }}/libxgboost4j/ --acl public-read --region us-west-2
|
|
||||||
if: |
|
|
||||||
(github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
|
|
||||||
matrix.os == 'macos-11'
|
|
||||||
env:
|
|
||||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_IAM_S3_UPLOADER }}
|
|
||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_IAM_S3_UPLOADER }}
|
|
||||||
|
|
||||||
|
|
||||||
- name: Test XGBoost4J (Core, Spark, Examples)
|
- name: Test XGBoost4J (Core, Spark, Examples)
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
65
.github/workflows/main.yml
vendored
65
.github/workflows/main.yml
vendored
@ -9,10 +9,6 @@ on: [push, pull_request]
|
|||||||
permissions:
|
permissions:
|
||||||
contents: read # to fetch code (actions/checkout)
|
contents: read # to fetch code (actions/checkout)
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
|
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
|
||||||
jobs:
|
jobs:
|
||||||
gtest-cpu:
|
gtest-cpu:
|
||||||
@ -33,7 +29,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -DGOOGLE_TEST=ON -DUSE_OPENMP=ON -DUSE_DMLC_GTEST=ON -GNinja -DBUILD_DEPRECATED_CLI=ON
|
cmake .. -DGOOGLE_TEST=ON -DUSE_OPENMP=ON -DUSE_DMLC_GTEST=ON -DPLUGIN_DENSE_PARSER=ON -GNinja
|
||||||
ninja -v
|
ninja -v
|
||||||
- name: Run gtest binary
|
- name: Run gtest binary
|
||||||
run: |
|
run: |
|
||||||
@ -60,52 +56,13 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -GNinja -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON -DUSE_OPENMP=OFF -DBUILD_DEPRECATED_CLI=ON
|
cmake .. -GNinja -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON -DUSE_OPENMP=OFF
|
||||||
ninja -v
|
ninja -v
|
||||||
- name: Run gtest binary
|
- name: Run gtest binary
|
||||||
run: |
|
run: |
|
||||||
cd build
|
cd build
|
||||||
ctest --extra-verbose
|
ctest --extra-verbose
|
||||||
|
|
||||||
gtest-cpu-sycl:
|
|
||||||
name: Test Google C++ unittest (CPU SYCL)
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest]
|
|
||||||
python-version: ["3.8"]
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
|
||||||
with:
|
|
||||||
submodules: 'true'
|
|
||||||
- uses: mamba-org/provision-with-micromamba@f347426e5745fe3dfc13ec5baf20496990d0281f # v14
|
|
||||||
with:
|
|
||||||
cache-downloads: true
|
|
||||||
cache-env: true
|
|
||||||
environment-name: linux_sycl_test
|
|
||||||
environment-file: tests/ci_build/conda_env/linux_sycl_test.yml
|
|
||||||
|
|
||||||
- name: Display Conda env
|
|
||||||
run: |
|
|
||||||
conda info
|
|
||||||
conda list
|
|
||||||
- name: Build and install XGBoost
|
|
||||||
shell: bash -l {0}
|
|
||||||
run: |
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake .. -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON -DPLUGIN_SYCL=ON -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX
|
|
||||||
make -j$(nproc)
|
|
||||||
- name: Run gtest binary for SYCL
|
|
||||||
run: |
|
|
||||||
cd build
|
|
||||||
./testxgboost --gtest_filter=Sycl*
|
|
||||||
- name: Run gtest binary for non SYCL
|
|
||||||
run: |
|
|
||||||
cd build
|
|
||||||
./testxgboost --gtest_filter=-Sycl*
|
|
||||||
|
|
||||||
c-api-demo:
|
c-api-demo:
|
||||||
name: Test installing XGBoost lib + building the C API demo
|
name: Test installing XGBoost lib + building the C API demo
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
@ -178,14 +135,24 @@ jobs:
|
|||||||
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
||||||
with:
|
with:
|
||||||
submodules: 'true'
|
submodules: 'true'
|
||||||
- uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
|
- uses: actions/setup-python@7f80679172b057fc5e90d70d197929d454754a5a # v4.3.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.8"
|
python-version: "3.8"
|
||||||
architecture: 'x64'
|
architecture: 'x64'
|
||||||
- name: Install Python packages
|
- name: Install Python packages
|
||||||
run: |
|
run: |
|
||||||
python -m pip install wheel setuptools cmakelint cpplint pylint
|
python -m pip install wheel setuptools cpplint pylint
|
||||||
- name: Run lint
|
- name: Run lint
|
||||||
run: |
|
run: |
|
||||||
python3 tests/ci_build/lint_cpp.py
|
python3 dmlc-core/scripts/lint.py xgboost cpp R-package/src
|
||||||
sh ./tests/ci_build/lint_cmake.sh
|
|
||||||
|
python3 dmlc-core/scripts/lint.py --exclude_path \
|
||||||
|
python-package/xgboost/dmlc-core \
|
||||||
|
python-package/xgboost/include \
|
||||||
|
python-package/xgboost/lib \
|
||||||
|
python-package/xgboost/rabit \
|
||||||
|
python-package/xgboost/src \
|
||||||
|
--pylint-rc python-package/.pylintrc \
|
||||||
|
xgboost \
|
||||||
|
cpp \
|
||||||
|
include src python-package
|
||||||
|
|||||||
92
.github/workflows/python_tests.yml
vendored
92
.github/workflows/python_tests.yml
vendored
@ -9,10 +9,6 @@ defaults:
|
|||||||
run:
|
run:
|
||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
python-mypy-lint:
|
python-mypy-lint:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@ -147,7 +143,7 @@ jobs:
|
|||||||
# Set prefix, to use OpenMP library from Conda env
|
# Set prefix, to use OpenMP library from Conda env
|
||||||
# See https://github.com/dmlc/xgboost/issues/7039#issuecomment-1025038228
|
# See https://github.com/dmlc/xgboost/issues/7039#issuecomment-1025038228
|
||||||
# to learn why we don't use libomp from Homebrew.
|
# to learn why we don't use libomp from Homebrew.
|
||||||
cmake .. -GNinja -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DBUILD_DEPRECATED_CLI=ON
|
cmake .. -GNinja -DCMAKE_PREFIX_PATH=$CONDA_PREFIX
|
||||||
ninja
|
ninja
|
||||||
|
|
||||||
- name: Install Python package
|
- name: Install Python package
|
||||||
@ -194,7 +190,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
mkdir build_msvc
|
mkdir build_msvc
|
||||||
cd build_msvc
|
cd build_msvc
|
||||||
cmake .. -G"Visual Studio 17 2022" -DCMAKE_CONFIGURATION_TYPES="Release" -A x64 -DBUILD_DEPRECATED_CLI=ON
|
cmake .. -G"Visual Studio 17 2022" -DCMAKE_CONFIGURATION_TYPES="Release" -A x64 -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON
|
||||||
cmake --build . --config Release --parallel $(nproc)
|
cmake --build . --config Release --parallel $(nproc)
|
||||||
|
|
||||||
- name: Install Python package
|
- name: Install Python package
|
||||||
@ -238,7 +234,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -GNinja -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DBUILD_DEPRECATED_CLI=ON
|
cmake .. -GNinja -DCMAKE_PREFIX_PATH=$CONDA_PREFIX
|
||||||
ninja
|
ninja
|
||||||
|
|
||||||
- name: Install Python package
|
- name: Install Python package
|
||||||
@ -259,85 +255,3 @@ jobs:
|
|||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
run: |
|
run: |
|
||||||
pytest -s -v -rxXs --durations=0 ./tests/test_distributed/test_with_spark
|
pytest -s -v -rxXs --durations=0 ./tests/test_distributed/test_with_spark
|
||||||
|
|
||||||
python-sycl-tests-on-ubuntu:
|
|
||||||
name: Test XGBoost Python package with SYCL on ${{ matrix.config.os }}
|
|
||||||
runs-on: ${{ matrix.config.os }}
|
|
||||||
timeout-minutes: 90
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
config:
|
|
||||||
- {os: ubuntu-latest, python-version: "3.8"}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
submodules: 'true'
|
|
||||||
|
|
||||||
- uses: mamba-org/provision-with-micromamba@f347426e5745fe3dfc13ec5baf20496990d0281f # v14
|
|
||||||
with:
|
|
||||||
cache-downloads: true
|
|
||||||
cache-env: true
|
|
||||||
environment-name: linux_sycl_test
|
|
||||||
environment-file: tests/ci_build/conda_env/linux_sycl_test.yml
|
|
||||||
|
|
||||||
- name: Display Conda env
|
|
||||||
run: |
|
|
||||||
conda info
|
|
||||||
conda list
|
|
||||||
- name: Build XGBoost on Ubuntu
|
|
||||||
run: |
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake .. -DPLUGIN_SYCL=ON -DCMAKE_PREFIX_PATH=$CONDA_PREFIX
|
|
||||||
make -j$(nproc)
|
|
||||||
- name: Install Python package
|
|
||||||
run: |
|
|
||||||
cd python-package
|
|
||||||
python --version
|
|
||||||
pip install -v .
|
|
||||||
- name: Test Python package
|
|
||||||
run: |
|
|
||||||
pytest -s -v -rxXs --durations=0 ./tests/python-sycl/
|
|
||||||
|
|
||||||
|
|
||||||
python-system-installation-on-ubuntu:
|
|
||||||
name: Test XGBoost Python package System Installation on ${{ matrix.os }}
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest]
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
submodules: 'true'
|
|
||||||
|
|
||||||
- name: Set up Python 3.8
|
|
||||||
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
|
|
||||||
with:
|
|
||||||
python-version: 3.8
|
|
||||||
|
|
||||||
- name: Install ninja
|
|
||||||
run: |
|
|
||||||
sudo apt-get update && sudo apt-get install -y ninja-build
|
|
||||||
|
|
||||||
- name: Build XGBoost on Ubuntu
|
|
||||||
run: |
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake .. -GNinja
|
|
||||||
ninja
|
|
||||||
|
|
||||||
- name: Copy lib to system lib
|
|
||||||
run: |
|
|
||||||
cp lib/* "$(python -c 'import sys; print(sys.base_prefix)')/lib"
|
|
||||||
|
|
||||||
- name: Install XGBoost in Virtual Environment
|
|
||||||
run: |
|
|
||||||
cd python-package
|
|
||||||
pip install virtualenv
|
|
||||||
virtualenv venv
|
|
||||||
source venv/bin/activate && \
|
|
||||||
pip install -v . --config-settings use_system_libxgboost=True && \
|
|
||||||
python -c 'import xgboost'
|
|
||||||
|
|||||||
8
.github/workflows/python_wheels.yml
vendored
8
.github/workflows/python_wheels.yml
vendored
@ -5,10 +5,6 @@ on: [push, pull_request]
|
|||||||
permissions:
|
permissions:
|
||||||
contents: read # to fetch code (actions/checkout)
|
contents: read # to fetch code (actions/checkout)
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
python-wheels:
|
python-wheels:
|
||||||
name: Build wheel for ${{ matrix.platform_id }}
|
name: Build wheel for ${{ matrix.platform_id }}
|
||||||
@ -21,11 +17,11 @@ jobs:
|
|||||||
- os: macos-latest
|
- os: macos-latest
|
||||||
platform_id: macosx_arm64
|
platform_id: macosx_arm64
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3.0.0
|
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
||||||
with:
|
with:
|
||||||
submodules: 'true'
|
submodules: 'true'
|
||||||
- name: Setup Python
|
- name: Setup Python
|
||||||
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
|
uses: actions/setup-python@7f80679172b057fc5e90d70d197929d454754a5a # v4.3.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.8"
|
python-version: "3.8"
|
||||||
- name: Build wheels
|
- name: Build wheels
|
||||||
|
|||||||
4
.github/workflows/r_nold.yml
vendored
4
.github/workflows/r_nold.yml
vendored
@ -10,10 +10,6 @@ on:
|
|||||||
permissions:
|
permissions:
|
||||||
contents: read # to fetch code (actions/checkout)
|
contents: read # to fetch code (actions/checkout)
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-R-noLD:
|
test-R-noLD:
|
||||||
if: github.event.comment.body == '/gha run r-nold-test' && contains('OWNER,MEMBER,COLLABORATOR', github.event.comment.author_association)
|
if: github.event.comment.body == '/gha run r-nold-test' && contains('OWNER,MEMBER,COLLABORATOR', github.event.comment.author_association)
|
||||||
|
|||||||
33
.github/workflows/r_tests.yml
vendored
33
.github/workflows/r_tests.yml
vendored
@ -8,10 +8,6 @@ env:
|
|||||||
permissions:
|
permissions:
|
||||||
contents: read # to fetch code (actions/checkout)
|
contents: read # to fetch code (actions/checkout)
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
lintr:
|
lintr:
|
||||||
runs-on: ${{ matrix.config.os }}
|
runs-on: ${{ matrix.config.os }}
|
||||||
@ -29,7 +25,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
submodules: 'true'
|
submodules: 'true'
|
||||||
|
|
||||||
- uses: r-lib/actions/setup-r@e40ad904310fc92e96951c1b0d64f3de6cbe9e14 # v2.6.5
|
- uses: r-lib/actions/setup-r@50d1eae9b8da0bb3f8582c59a5b82225fa2fe7f2 # v2.3.1
|
||||||
with:
|
with:
|
||||||
r-version: ${{ matrix.config.r }}
|
r-version: ${{ matrix.config.r }}
|
||||||
|
|
||||||
@ -50,7 +46,7 @@ jobs:
|
|||||||
MAKEFLAGS="-j$(nproc)" R CMD INSTALL R-package/
|
MAKEFLAGS="-j$(nproc)" R CMD INSTALL R-package/
|
||||||
Rscript tests/ci_build/lint_r.R $(pwd)
|
Rscript tests/ci_build/lint_r.R $(pwd)
|
||||||
|
|
||||||
test-Rpkg:
|
test-R-on-Windows:
|
||||||
runs-on: ${{ matrix.config.os }}
|
runs-on: ${{ matrix.config.os }}
|
||||||
name: Test R on OS ${{ matrix.config.os }}, R ${{ matrix.config.r }}, Compiler ${{ matrix.config.compiler }}, Build ${{ matrix.config.build }}
|
name: Test R on OS ${{ matrix.config.os }}, R ${{ matrix.config.r }}, Compiler ${{ matrix.config.compiler }}, Build ${{ matrix.config.build }}
|
||||||
strategy:
|
strategy:
|
||||||
@ -58,22 +54,17 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
config:
|
config:
|
||||||
- {os: windows-latest, r: 'release', compiler: 'mingw', build: 'autotools'}
|
- {os: windows-latest, r: 'release', compiler: 'mingw', build: 'autotools'}
|
||||||
- {os: ubuntu-latest, r: 'release', compiler: 'none', build: 'cmake'}
|
- {os: windows-latest, r: '4.2.0', compiler: 'msvc', build: 'cmake'}
|
||||||
env:
|
env:
|
||||||
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
|
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
|
||||||
RSPM: ${{ matrix.config.rspm }}
|
RSPM: ${{ matrix.config.rspm }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install system dependencies
|
|
||||||
run: |
|
|
||||||
sudo apt update
|
|
||||||
sudo apt install libcurl4-openssl-dev libssl-dev libssh2-1-dev libgit2-dev libglpk-dev libxml2-dev libharfbuzz-dev libfribidi-dev
|
|
||||||
if: matrix.config.os == 'ubuntu-latest'
|
|
||||||
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
- uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
|
||||||
with:
|
with:
|
||||||
submodules: 'true'
|
submodules: 'true'
|
||||||
|
|
||||||
- uses: r-lib/actions/setup-r@e40ad904310fc92e96951c1b0d64f3de6cbe9e14 # v2.6.5
|
- uses: r-lib/actions/setup-r@50d1eae9b8da0bb3f8582c59a5b82225fa2fe7f2 # v2.3.1
|
||||||
with:
|
with:
|
||||||
r-version: ${{ matrix.config.r }}
|
r-version: ${{ matrix.config.r }}
|
||||||
|
|
||||||
@ -84,7 +75,7 @@ jobs:
|
|||||||
key: ${{ runner.os }}-r-${{ matrix.config.r }}-6-${{ hashFiles('R-package/DESCRIPTION') }}
|
key: ${{ runner.os }}-r-${{ matrix.config.r }}-6-${{ hashFiles('R-package/DESCRIPTION') }}
|
||||||
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-6-${{ hashFiles('R-package/DESCRIPTION') }}
|
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-6-${{ hashFiles('R-package/DESCRIPTION') }}
|
||||||
|
|
||||||
- uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
|
- uses: actions/setup-python@7f80679172b057fc5e90d70d197929d454754a5a # v4.3.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.8"
|
python-version: "3.8"
|
||||||
architecture: 'x64'
|
architecture: 'x64'
|
||||||
@ -99,18 +90,12 @@ jobs:
|
|||||||
- name: Test R
|
- name: Test R
|
||||||
run: |
|
run: |
|
||||||
python tests/ci_build/test_r_package.py --compiler='${{ matrix.config.compiler }}' --build-tool="${{ matrix.config.build }}" --task=check
|
python tests/ci_build/test_r_package.py --compiler='${{ matrix.config.compiler }}' --build-tool="${{ matrix.config.build }}" --task=check
|
||||||
if: matrix.config.compiler != 'none'
|
|
||||||
|
|
||||||
- name: Test R
|
|
||||||
run: |
|
|
||||||
python tests/ci_build/test_r_package.py --build-tool="${{ matrix.config.build }}" --task=check
|
|
||||||
if: matrix.config.compiler == 'none'
|
|
||||||
|
|
||||||
test-R-on-Debian:
|
test-R-on-Debian:
|
||||||
name: Test R package on Debian
|
name: Test R package on Debian
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container:
|
||||||
image: rhub/debian-gcc-release
|
image: rhub/debian-gcc-devel
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
@ -130,12 +115,12 @@ jobs:
|
|||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
run: |
|
run: |
|
||||||
Rscript -e "source('./R-package/tests/helper_scripts/install_deps.R')"
|
/tmp/R-devel/bin/Rscript -e "source('./R-package/tests/helper_scripts/install_deps.R')"
|
||||||
|
|
||||||
- name: Test R
|
- name: Test R
|
||||||
shell: bash -l {0}
|
shell: bash -l {0}
|
||||||
run: |
|
run: |
|
||||||
python3 tests/ci_build/test_r_package.py --r=/usr/bin/R --build-tool=autotools --task=check
|
python3 tests/ci_build/test_r_package.py --r=/tmp/R-devel/bin/R --build-tool=autotools --task=check
|
||||||
|
|
||||||
- uses: dorny/paths-filter@v2
|
- uses: dorny/paths-filter@v2
|
||||||
id: changes
|
id: changes
|
||||||
@ -147,4 +132,4 @@ jobs:
|
|||||||
- name: Run document check
|
- name: Run document check
|
||||||
if: steps.changes.outputs.r_package == 'true'
|
if: steps.changes.outputs.r_package == 'true'
|
||||||
run: |
|
run: |
|
||||||
python3 tests/ci_build/test_r_package.py --r=/usr/bin/R --task=doc
|
python3 tests/ci_build/test_r_package.py --r=/tmp/R-devel/bin/R --task=doc
|
||||||
|
|||||||
8
.github/workflows/scorecards.yml
vendored
8
.github/workflows/scorecards.yml
vendored
@ -22,12 +22,12 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout code"
|
- name: "Checkout code"
|
||||||
uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3.0.0
|
uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # tag=v3.0.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: "Run analysis"
|
- name: "Run analysis"
|
||||||
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
|
uses: ossf/scorecard-action@99c53751e09b9529366343771cc321ec74e9bd3d # tag=v2.0.6
|
||||||
with:
|
with:
|
||||||
results_file: results.sarif
|
results_file: results.sarif
|
||||||
results_format: sarif
|
results_format: sarif
|
||||||
@ -41,7 +41,7 @@ jobs:
|
|||||||
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
||||||
# format to the repository Actions tab.
|
# format to the repository Actions tab.
|
||||||
- name: "Upload artifact"
|
- name: "Upload artifact"
|
||||||
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
|
uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535 # tag=v3.0.0
|
||||||
with:
|
with:
|
||||||
name: SARIF file
|
name: SARIF file
|
||||||
path: results.sarif
|
path: results.sarif
|
||||||
@ -49,6 +49,6 @@ jobs:
|
|||||||
|
|
||||||
# Upload the results to GitHub's code scanning dashboard.
|
# Upload the results to GitHub's code scanning dashboard.
|
||||||
- name: "Upload to code-scanning"
|
- name: "Upload to code-scanning"
|
||||||
uses: github/codeql-action/upload-sarif@83a02f7883b12e0e4e1a146174f5e2292a01e601 # v2.16.4
|
uses: github/codeql-action/upload-sarif@5f532563584d71fdef14ee64d17bafb34f751ce5 # tag=v1.0.26
|
||||||
with:
|
with:
|
||||||
sarif_file: results.sarif
|
sarif_file: results.sarif
|
||||||
|
|||||||
4
.github/workflows/update_rapids.yml
vendored
4
.github/workflows/update_rapids.yml
vendored
@ -3,7 +3,7 @@ name: update-rapids
|
|||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "0 20 * * 1" # Run once weekly
|
- cron: "0 20 * * *" # Run once daily
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
@ -32,7 +32,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
bash tests/buildkite/update-rapids.sh
|
bash tests/buildkite/update-rapids.sh
|
||||||
- name: Create Pull Request
|
- name: Create Pull Request
|
||||||
uses: peter-evans/create-pull-request@v6
|
uses: peter-evans/create-pull-request@v5
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/master'
|
||||||
with:
|
with:
|
||||||
add-paths: |
|
add-paths: |
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@ -48,7 +48,6 @@ Debug
|
|||||||
*.Rproj
|
*.Rproj
|
||||||
./xgboost.mpi
|
./xgboost.mpi
|
||||||
./xgboost.mock
|
./xgboost.mock
|
||||||
*.bak
|
|
||||||
#.Rbuildignore
|
#.Rbuildignore
|
||||||
R-package.Rproj
|
R-package.Rproj
|
||||||
*.cache*
|
*.cache*
|
||||||
@ -146,8 +145,6 @@ __MACOSX/
|
|||||||
model*.json
|
model*.json
|
||||||
|
|
||||||
# R tests
|
# R tests
|
||||||
*.htm
|
|
||||||
*.html
|
|
||||||
*.libsvm
|
*.libsvm
|
||||||
*.rds
|
*.rds
|
||||||
Rplots.pdf
|
Rplots.pdf
|
||||||
|
|||||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -5,6 +5,3 @@
|
|||||||
[submodule "gputreeshap"]
|
[submodule "gputreeshap"]
|
||||||
path = gputreeshap
|
path = gputreeshap
|
||||||
url = https://github.com/rapidsai/gputreeshap.git
|
url = https://github.com/rapidsai/gputreeshap.git
|
||||||
[submodule "rocgputreeshap"]
|
|
||||||
path = rocgputreeshap
|
|
||||||
url = https://github.com/ROCmSoftwarePlatform/rocgputreeshap
|
|
||||||
|
|||||||
@ -32,3 +32,4 @@ formats:
|
|||||||
python:
|
python:
|
||||||
install:
|
install:
|
||||||
- requirements: doc/requirements.txt
|
- requirements: doc/requirements.txt
|
||||||
|
system_packages: true
|
||||||
|
|||||||
1
CITATION
1
CITATION
@ -15,3 +15,4 @@
|
|||||||
address = {New York, NY, USA},
|
address = {New York, NY, USA},
|
||||||
keywords = {large-scale machine learning},
|
keywords = {large-scale machine learning},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
421
CMakeLists.txt
421
CMakeLists.txt
@ -1,60 +1,36 @@
|
|||||||
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
|
||||||
|
project(xgboost LANGUAGES CXX C VERSION 2.0.0)
|
||||||
if(PLUGIN_SYCL)
|
|
||||||
set(CMAKE_CXX_COMPILER "g++")
|
|
||||||
set(CMAKE_C_COMPILER "gcc")
|
|
||||||
string(REPLACE " -isystem ${CONDA_PREFIX}/include" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
project(xgboost LANGUAGES CXX C VERSION 2.1.0)
|
|
||||||
include(cmake/Utils.cmake)
|
include(cmake/Utils.cmake)
|
||||||
list(APPEND CMAKE_MODULE_PATH "${xgboost_SOURCE_DIR}/cmake/modules")
|
list(APPEND CMAKE_MODULE_PATH "${xgboost_SOURCE_DIR}/cmake/modules")
|
||||||
|
cmake_policy(SET CMP0022 NEW)
|
||||||
# These policies are already set from 3.18 but we still need to set the policy
|
cmake_policy(SET CMP0079 NEW)
|
||||||
# default variables here for lower minimum versions in the submodules
|
cmake_policy(SET CMP0076 NEW)
|
||||||
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
|
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
|
||||||
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
|
cmake_policy(SET CMP0063 NEW)
|
||||||
set(CMAKE_POLICY_DEFAULT_CMP0076 NEW)
|
|
||||||
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
|
if ((${CMAKE_VERSION} VERSION_GREATER 3.13) OR (${CMAKE_VERSION} VERSION_EQUAL 3.13))
|
||||||
set(CMAKE_POLICY_DEFAULT_CMP0079 NEW)
|
cmake_policy(SET CMP0077 NEW)
|
||||||
|
endif ((${CMAKE_VERSION} VERSION_GREATER 3.13) OR (${CMAKE_VERSION} VERSION_EQUAL 3.13))
|
||||||
|
|
||||||
message(STATUS "CMake version ${CMAKE_VERSION}")
|
message(STATUS "CMake version ${CMAKE_VERSION}")
|
||||||
|
|
||||||
# Check compiler versions
|
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
|
||||||
# Use recent compilers to ensure that std::filesystem is available
|
message(FATAL_ERROR "GCC version must be at least 5.0!")
|
||||||
if(MSVC)
|
|
||||||
if(MSVC_VERSION LESS 1920)
|
|
||||||
message(FATAL_ERROR "Need Visual Studio 2019 or newer to build XGBoost")
|
|
||||||
endif()
|
|
||||||
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
||||||
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.1")
|
|
||||||
message(FATAL_ERROR "Need GCC 8.1 or newer to build XGBoost")
|
|
||||||
endif()
|
|
||||||
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
|
|
||||||
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.0")
|
|
||||||
message(FATAL_ERROR "Need Xcode 11.0 (AppleClang 11.0) or newer to build XGBoost")
|
|
||||||
endif()
|
|
||||||
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
|
||||||
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0")
|
|
||||||
message(FATAL_ERROR "Need Clang 9.0 or newer to build XGBoost")
|
|
||||||
endif()
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(${xgboost_SOURCE_DIR}/cmake/PrefetchIntrinsics.cmake)
|
include(${xgboost_SOURCE_DIR}/cmake/FindPrefetchIntrinsics.cmake)
|
||||||
find_prefetch_intrinsics()
|
find_prefetch_intrinsics()
|
||||||
include(${xgboost_SOURCE_DIR}/cmake/Version.cmake)
|
include(${xgboost_SOURCE_DIR}/cmake/Version.cmake)
|
||||||
write_version()
|
write_version()
|
||||||
set_default_configuration_release()
|
set_default_configuration_release()
|
||||||
|
|
||||||
#-- Options
|
#-- Options
|
||||||
include(CMakeDependentOption)
|
|
||||||
|
|
||||||
## User options
|
## User options
|
||||||
option(BUILD_C_DOC "Build documentation for C APIs using Doxygen." OFF)
|
option(BUILD_C_DOC "Build documentation for C APIs using Doxygen." OFF)
|
||||||
option(USE_OPENMP "Build with OpenMP support." ON)
|
option(USE_OPENMP "Build with OpenMP support." ON)
|
||||||
option(BUILD_STATIC_LIB "Build static library" OFF)
|
option(BUILD_STATIC_LIB "Build static library" OFF)
|
||||||
option(BUILD_DEPRECATED_CLI "Build the deprecated command line interface" OFF)
|
|
||||||
option(FORCE_SHARED_CRT "Build with dynamic CRT on Windows (/MD)" OFF)
|
option(FORCE_SHARED_CRT "Build with dynamic CRT on Windows (/MD)" OFF)
|
||||||
|
option(RABIT_BUILD_MPI "Build MPI" OFF)
|
||||||
## Bindings
|
## Bindings
|
||||||
option(JVM_BINDINGS "Build JVM bindings" OFF)
|
option(JVM_BINDINGS "Build JVM bindings" OFF)
|
||||||
option(R_LIB "Build shared library for R package" OFF)
|
option(R_LIB "Build shared library for R package" OFF)
|
||||||
@ -66,7 +42,7 @@ option(ENABLE_ALL_WARNINGS "Enable all compiler warnings. Only effective for GCC
|
|||||||
option(LOG_CAPI_INVOCATION "Log all C API invocations for debugging" OFF)
|
option(LOG_CAPI_INVOCATION "Log all C API invocations for debugging" OFF)
|
||||||
option(GOOGLE_TEST "Build google tests" OFF)
|
option(GOOGLE_TEST "Build google tests" OFF)
|
||||||
option(USE_DMLC_GTEST "Use google tests bundled with dmlc-core submodule" OFF)
|
option(USE_DMLC_GTEST "Use google tests bundled with dmlc-core submodule" OFF)
|
||||||
option(USE_DEVICE_DEBUG "Generate CUDA/HIP device debug info." OFF)
|
option(USE_DEVICE_DEBUG "Generate CUDA device debug info." OFF)
|
||||||
option(USE_NVTX "Build with cuda profiling annotations. Developers only." OFF)
|
option(USE_NVTX "Build with cuda profiling annotations. Developers only." OFF)
|
||||||
set(NVTX_HEADER_DIR "" CACHE PATH "Path to the stand-alone nvtx header")
|
set(NVTX_HEADER_DIR "" CACHE PATH "Path to the stand-alone nvtx header")
|
||||||
option(RABIT_MOCK "Build rabit with mock" OFF)
|
option(RABIT_MOCK "Build rabit with mock" OFF)
|
||||||
@ -74,37 +50,14 @@ option(HIDE_CXX_SYMBOLS "Build shared library and hide all C++ symbols" OFF)
|
|||||||
option(KEEP_BUILD_ARTIFACTS_IN_BINARY_DIR "Output build artifacts in CMake binary dir" OFF)
|
option(KEEP_BUILD_ARTIFACTS_IN_BINARY_DIR "Output build artifacts in CMake binary dir" OFF)
|
||||||
## CUDA
|
## CUDA
|
||||||
option(USE_CUDA "Build with GPU acceleration" OFF)
|
option(USE_CUDA "Build with GPU acceleration" OFF)
|
||||||
option(USE_PER_THREAD_DEFAULT_STREAM "Build with per-thread default stream" ON)
|
|
||||||
option(USE_NCCL "Build with NCCL to enable distributed GPU support." OFF)
|
option(USE_NCCL "Build with NCCL to enable distributed GPU support." OFF)
|
||||||
# This is specifically designed for PyPI binary release and should be disabled for most of the cases.
|
|
||||||
option(USE_DLOPEN_NCCL "Whether to load nccl dynamically." OFF)
|
|
||||||
option(BUILD_WITH_SHARED_NCCL "Build with shared NCCL library." OFF)
|
option(BUILD_WITH_SHARED_NCCL "Build with shared NCCL library." OFF)
|
||||||
|
set(GPU_COMPUTE_VER "" CACHE STRING
|
||||||
if(USE_CUDA)
|
|
||||||
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES AND NOT DEFINED ENV{CUDAARCHS})
|
|
||||||
set(GPU_COMPUTE_VER "" CACHE STRING
|
|
||||||
"Semicolon separated list of compute versions to be built against, e.g. '35;61'")
|
"Semicolon separated list of compute versions to be built against, e.g. '35;61'")
|
||||||
else()
|
## Copied From dmlc
|
||||||
# Clear any cached values from previous runs
|
option(USE_HDFS "Build with HDFS support" OFF)
|
||||||
unset(GPU_COMPUTE_VER)
|
option(USE_AZURE "Build with AZURE support" OFF)
|
||||||
unset(GPU_COMPUTE_VER CACHE)
|
option(USE_S3 "Build with S3 support" OFF)
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# CUDA device LTO was introduced in CMake v3.25 and requires host LTO to also be enabled but can still
|
|
||||||
# be explicitly disabled allowing for LTO on host only, host and device, or neither, but device-only LTO
|
|
||||||
# is not a supproted configuration
|
|
||||||
cmake_dependent_option(USE_CUDA_LTO
|
|
||||||
"Enable link-time optimization for CUDA device code"
|
|
||||||
"${CMAKE_INTERPROCEDURAL_OPTIMIZATION}"
|
|
||||||
"CMAKE_VERSION VERSION_GREATER_EQUAL 3.25;USE_CUDA;CMAKE_INTERPROCEDURAL_OPTIMIZATION"
|
|
||||||
OFF)
|
|
||||||
## HIP
|
|
||||||
option(USE_HIP "Build with GPU acceleration" OFF)
|
|
||||||
option(USE_RCCL "Build with RCCL to enable distributed GPU support." OFF)
|
|
||||||
# This is specifically designed for PyPI binary release and should be disabled for most of the cases.
|
|
||||||
option(USE_DLOPEN_RCCL "Whether to load nccl dynamically." OFF)
|
|
||||||
option(BUILD_WITH_SHARED_RCCL "Build with shared RCCL library." OFF)
|
|
||||||
## Sanitizers
|
## Sanitizers
|
||||||
option(USE_SANITIZER "Use santizer flags" OFF)
|
option(USE_SANITIZER "Use santizer flags" OFF)
|
||||||
option(SANITIZER_PATH "Path to sanitizes.")
|
option(SANITIZER_PATH "Path to sanitizes.")
|
||||||
@ -112,159 +65,95 @@ set(ENABLED_SANITIZERS "address" "leak" CACHE STRING
|
|||||||
"Semicolon separated list of sanitizer names. E.g 'address;leak'. Supported sanitizers are
|
"Semicolon separated list of sanitizer names. E.g 'address;leak'. Supported sanitizers are
|
||||||
address, leak, undefined and thread.")
|
address, leak, undefined and thread.")
|
||||||
## Plugins
|
## Plugins
|
||||||
|
option(PLUGIN_DENSE_PARSER "Build dense parser plugin" OFF)
|
||||||
option(PLUGIN_RMM "Build with RAPIDS Memory Manager (RMM)" OFF)
|
option(PLUGIN_RMM "Build with RAPIDS Memory Manager (RMM)" OFF)
|
||||||
option(PLUGIN_FEDERATED "Build with Federated Learning" OFF)
|
option(PLUGIN_FEDERATED "Build with Federated Learning" OFF)
|
||||||
## TODO: 1. Add check if DPC++ compiler is used for building
|
## TODO: 1. Add check if DPC++ compiler is used for building
|
||||||
option(PLUGIN_SYCL "SYCL plugin" OFF)
|
option(PLUGIN_UPDATER_ONEAPI "DPC++ updater" OFF)
|
||||||
option(ADD_PKGCONFIG "Add xgboost.pc into system." ON)
|
option(ADD_PKGCONFIG "Add xgboost.pc into system." ON)
|
||||||
|
|
||||||
#-- Checks for building XGBoost
|
#-- Checks for building XGBoost
|
||||||
if(USE_DEBUG_OUTPUT AND (NOT (CMAKE_BUILD_TYPE MATCHES Debug)))
|
if (USE_DEBUG_OUTPUT AND (NOT (CMAKE_BUILD_TYPE MATCHES Debug)))
|
||||||
message(SEND_ERROR "Do not enable `USE_DEBUG_OUTPUT' with release build.")
|
message(SEND_ERROR "Do not enable `USE_DEBUG_OUTPUT' with release build.")
|
||||||
endif()
|
endif (USE_DEBUG_OUTPUT AND (NOT (CMAKE_BUILD_TYPE MATCHES Debug)))
|
||||||
if(USE_NCCL AND NOT (USE_CUDA))
|
if (USE_NCCL AND NOT (USE_CUDA))
|
||||||
message(SEND_ERROR "`USE_NCCL` must be enabled with `USE_CUDA` flag.")
|
message(SEND_ERROR "`USE_NCCL` must be enabled with `USE_CUDA` flag.")
|
||||||
endif()
|
endif (USE_NCCL AND NOT (USE_CUDA))
|
||||||
if(USE_DEVICE_DEBUG AND NOT (USE_CUDA))
|
if (USE_DEVICE_DEBUG AND NOT (USE_CUDA))
|
||||||
message(SEND_ERROR "`USE_DEVICE_DEBUG` must be enabled with `USE_CUDA` flag.")
|
message(SEND_ERROR "`USE_DEVICE_DEBUG` must be enabled with `USE_CUDA` flag.")
|
||||||
endif()
|
endif (USE_DEVICE_DEBUG AND NOT (USE_CUDA))
|
||||||
if(BUILD_WITH_SHARED_NCCL AND (NOT USE_NCCL))
|
if (BUILD_WITH_SHARED_NCCL AND (NOT USE_NCCL))
|
||||||
message(SEND_ERROR "Build XGBoost with -DUSE_NCCL=ON to enable BUILD_WITH_SHARED_NCCL.")
|
message(SEND_ERROR "Build XGBoost with -DUSE_NCCL=ON to enable BUILD_WITH_SHARED_NCCL.")
|
||||||
endif()
|
endif (BUILD_WITH_SHARED_NCCL AND (NOT USE_NCCL))
|
||||||
if(USE_DLOPEN_NCCL AND (NOT USE_NCCL))
|
if (JVM_BINDINGS AND R_LIB)
|
||||||
message(SEND_ERROR "Build XGBoost with -DUSE_NCCL=ON to enable USE_DLOPEN_NCCL.")
|
|
||||||
endif()
|
|
||||||
if(USE_DLOPEN_NCCL AND (NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux")))
|
|
||||||
message(SEND_ERROR "`USE_DLOPEN_NCCL` supports only Linux at the moment.")
|
|
||||||
endif()
|
|
||||||
if(USE_RCCL AND NOT (USE_HIP))
|
|
||||||
message(SEND_ERROR "`USE_RCCL` must be enabled with `USE_HIP` flag.")
|
|
||||||
endif()
|
|
||||||
if(BUILD_WITH_SHARED_RCCL AND (NOT USE_RCCL))
|
|
||||||
message(SEND_ERROR "Build XGBoost with -DUSE_RCCL=ON to enable BUILD_WITH_SHARED_RCCL.")
|
|
||||||
endif()
|
|
||||||
if(USE_DLOPEN_RCCL AND (NOT USE_RCCL))
|
|
||||||
message(SEND_ERROR "Build XGBoost with -DUSE_RCCL=ON to enable USE_DLOPEN_RCCL.")
|
|
||||||
endif()
|
|
||||||
if(USE_DLOPEN_RCCL AND (NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux")))
|
|
||||||
message(SEND_ERROR "`USE_DLOPEN_RCCL` supports only Linux at the moment.")
|
|
||||||
endif()
|
|
||||||
if(JVM_BINDINGS AND R_LIB)
|
|
||||||
message(SEND_ERROR "`R_LIB' is not compatible with `JVM_BINDINGS' as they both have customized configurations.")
|
message(SEND_ERROR "`R_LIB' is not compatible with `JVM_BINDINGS' as they both have customized configurations.")
|
||||||
endif()
|
endif (JVM_BINDINGS AND R_LIB)
|
||||||
if(R_LIB AND GOOGLE_TEST)
|
if (R_LIB AND GOOGLE_TEST)
|
||||||
message(
|
message(WARNING "Some C++ unittests will fail with `R_LIB` enabled,
|
||||||
WARNING
|
as R package redirects some functions to R runtime implementation.")
|
||||||
"Some C++ tests will fail with `R_LIB` enabled, as R package redirects some functions to R runtime implementation."
|
endif (R_LIB AND GOOGLE_TEST)
|
||||||
)
|
if (USE_AVX)
|
||||||
endif()
|
message(SEND_ERROR "The option 'USE_AVX' is deprecated as experimental AVX features have been removed from XGBoost.")
|
||||||
if(PLUGIN_RMM AND NOT (USE_CUDA))
|
endif (USE_AVX)
|
||||||
|
if (PLUGIN_LZ4)
|
||||||
|
message(SEND_ERROR "The option 'PLUGIN_LZ4' is removed from XGBoost.")
|
||||||
|
endif (PLUGIN_LZ4)
|
||||||
|
if (PLUGIN_RMM AND NOT (USE_CUDA))
|
||||||
message(SEND_ERROR "`PLUGIN_RMM` must be enabled with `USE_CUDA` flag.")
|
message(SEND_ERROR "`PLUGIN_RMM` must be enabled with `USE_CUDA` flag.")
|
||||||
endif()
|
endif (PLUGIN_RMM AND NOT (USE_CUDA))
|
||||||
if(PLUGIN_RMM AND NOT ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")))
|
if (PLUGIN_RMM AND NOT ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")))
|
||||||
message(SEND_ERROR "`PLUGIN_RMM` must be used with GCC or Clang compiler.")
|
message(SEND_ERROR "`PLUGIN_RMM` must be used with GCC or Clang compiler.")
|
||||||
endif()
|
endif (PLUGIN_RMM AND NOT ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")))
|
||||||
if(PLUGIN_RMM AND NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux"))
|
if (PLUGIN_RMM AND NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux"))
|
||||||
message(SEND_ERROR "`PLUGIN_RMM` must be used with Linux.")
|
message(SEND_ERROR "`PLUGIN_RMM` must be used with Linux.")
|
||||||
endif()
|
endif (PLUGIN_RMM AND NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux"))
|
||||||
if(ENABLE_ALL_WARNINGS)
|
if (ENABLE_ALL_WARNINGS)
|
||||||
if((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
|
if ((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
|
||||||
message(SEND_ERROR "ENABLE_ALL_WARNINGS is only available for Clang and GCC.")
|
message(SEND_ERROR "ENABLE_ALL_WARNINGS is only available for Clang and GCC.")
|
||||||
endif()
|
endif ((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
|
||||||
endif()
|
endif (ENABLE_ALL_WARNINGS)
|
||||||
if(BUILD_STATIC_LIB AND (R_LIB OR JVM_BINDINGS))
|
if (BUILD_STATIC_LIB AND (R_LIB OR JVM_BINDINGS))
|
||||||
message(SEND_ERROR "Cannot build a static library libxgboost.a when R or JVM packages are enabled.")
|
message(SEND_ERROR "Cannot build a static library libxgboost.a when R or JVM packages are enabled.")
|
||||||
endif()
|
endif (BUILD_STATIC_LIB AND (R_LIB OR JVM_BINDINGS))
|
||||||
if(PLUGIN_FEDERATED)
|
if (PLUGIN_FEDERATED)
|
||||||
if(CMAKE_CROSSCOMPILING)
|
if (CMAKE_CROSSCOMPILING)
|
||||||
message(SEND_ERROR "Cannot cross compile with federated learning support")
|
message(SEND_ERROR "Cannot cross compile with federated learning support")
|
||||||
endif()
|
endif ()
|
||||||
if(BUILD_STATIC_LIB)
|
if (BUILD_STATIC_LIB)
|
||||||
message(SEND_ERROR "Cannot build static lib with federated learning support")
|
message(SEND_ERROR "Cannot build static lib with federated learning support")
|
||||||
endif()
|
endif ()
|
||||||
if(R_LIB OR JVM_BINDINGS)
|
if (R_LIB OR JVM_BINDINGS)
|
||||||
message(SEND_ERROR "Cannot enable federated learning support when R or JVM packages are enabled.")
|
message(SEND_ERROR "Cannot enable federated learning support when R or JVM packages are enabled.")
|
||||||
endif()
|
endif ()
|
||||||
if(WIN32)
|
if (WIN32)
|
||||||
message(SEND_ERROR "Federated learning not supported for Windows platform")
|
message(SEND_ERROR "Federated learning not supported for Windows platform")
|
||||||
endif()
|
endif ()
|
||||||
endif()
|
endif ()
|
||||||
|
|
||||||
#-- Removed options
|
|
||||||
if(USE_AVX)
|
|
||||||
message(SEND_ERROR "The option `USE_AVX` is deprecated as experimental AVX features have been removed from XGBoost.")
|
|
||||||
endif()
|
|
||||||
if(PLUGIN_LZ4)
|
|
||||||
message(SEND_ERROR "The option `PLUGIN_LZ4` is removed from XGBoost.")
|
|
||||||
endif()
|
|
||||||
if(RABIT_BUILD_MPI)
|
|
||||||
message(SEND_ERROR "The option `RABIT_BUILD_MPI` has been removed from XGBoost.")
|
|
||||||
endif()
|
|
||||||
if(USE_S3)
|
|
||||||
message(SEND_ERROR "The option `USE_S3` has been removed from XGBoost")
|
|
||||||
endif()
|
|
||||||
if(USE_AZURE)
|
|
||||||
message(SEND_ERROR "The option `USE_AZURE` has been removed from XGBoost")
|
|
||||||
endif()
|
|
||||||
if(USE_HDFS)
|
|
||||||
message(SEND_ERROR "The option `USE_HDFS` has been removed from XGBoost")
|
|
||||||
endif()
|
|
||||||
if(PLUGIN_DENSE_PARSER)
|
|
||||||
message(SEND_ERROR "The option `PLUGIN_DENSE_PARSER` has been removed from XGBoost.")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
#-- Sanitizer
|
#-- Sanitizer
|
||||||
if(USE_SANITIZER)
|
if (USE_SANITIZER)
|
||||||
include(cmake/Sanitizer.cmake)
|
include(cmake/Sanitizer.cmake)
|
||||||
enable_sanitizers("${ENABLED_SANITIZERS}")
|
enable_sanitizers("${ENABLED_SANITIZERS}")
|
||||||
endif()
|
endif (USE_SANITIZER)
|
||||||
|
|
||||||
if(USE_CUDA)
|
if (USE_CUDA)
|
||||||
set(USE_OPENMP ON CACHE BOOL "CUDA requires OpenMP" FORCE)
|
set(USE_OPENMP ON CACHE BOOL "CUDA requires OpenMP" FORCE)
|
||||||
# `export CXX=' is ignored by CMake CUDA.
|
# `export CXX=' is ignored by CMake CUDA.
|
||||||
if(NOT DEFINED CMAKE_CUDA_HOST_COMPILER AND NOT DEFINED ENV{CUDAHOSTCXX})
|
set(CMAKE_CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
|
||||||
set(CMAKE_CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER} CACHE FILEPATH
|
|
||||||
"The compiler executable to use when compiling host code for CUDA or HIP language files.")
|
|
||||||
mark_as_advanced(CMAKE_CUDA_HOST_COMPILER)
|
|
||||||
message(STATUS "Configured CUDA host compiler: ${CMAKE_CUDA_HOST_COMPILER}")
|
message(STATUS "Configured CUDA host compiler: ${CMAKE_CUDA_HOST_COMPILER}")
|
||||||
endif()
|
|
||||||
|
|
||||||
if(NOT DEFINED CMAKE_CUDA_RUNTIME_LIBRARY)
|
|
||||||
set(CMAKE_CUDA_RUNTIME_LIBRARY Static)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
enable_language(CUDA)
|
enable_language(CUDA)
|
||||||
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_LESS 11.0)
|
if (${CMAKE_CUDA_COMPILER_VERSION} VERSION_LESS 11.0)
|
||||||
message(FATAL_ERROR "CUDA version must be at least 11.0!")
|
message(FATAL_ERROR "CUDA version must be at least 11.0!")
|
||||||
endif()
|
endif()
|
||||||
if(DEFINED GPU_COMPUTE_VER)
|
set(GEN_CODE "")
|
||||||
compute_cmake_cuda_archs("${GPU_COMPUTE_VER}")
|
format_gencode_flags("${GPU_COMPUTE_VER}" GEN_CODE)
|
||||||
endif()
|
|
||||||
add_subdirectory(${PROJECT_SOURCE_DIR}/gputreeshap)
|
add_subdirectory(${PROJECT_SOURCE_DIR}/gputreeshap)
|
||||||
|
|
||||||
find_package(CUDAToolkit REQUIRED)
|
find_package(CUDAToolkit REQUIRED)
|
||||||
endif()
|
endif (USE_CUDA)
|
||||||
|
|
||||||
if (USE_HIP)
|
if (FORCE_COLORED_OUTPUT AND (CMAKE_GENERATOR STREQUAL "Ninja") AND
|
||||||
set(USE_OPENMP ON CACHE BOOL "HIP requires OpenMP" FORCE)
|
|
||||||
# `export CXX=' is ignored by CMake HIP.
|
|
||||||
set(CMAKE_HIP_HOST_COMPILER ${CMAKE_CXX_COMPILER})
|
|
||||||
message(STATUS "Configured HIP host compiler: ${CMAKE_HIP_HOST_COMPILER}")
|
|
||||||
|
|
||||||
enable_language(HIP)
|
|
||||||
find_package(hip REQUIRED)
|
|
||||||
find_package(rocthrust REQUIRED)
|
|
||||||
find_package(hipcub REQUIRED)
|
|
||||||
|
|
||||||
set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -I${HIP_INCLUDE_DIRS}")
|
|
||||||
set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -Wunused-result -w")
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__HIP_PLATFORM_AMD__")
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${HIP_INCLUDE_DIRS}")
|
|
||||||
#set(CMAKE_HIP_SEPARABLE_COMPILATION ON)
|
|
||||||
add_subdirectory(${PROJECT_SOURCE_DIR}/rocgputreeshap)
|
|
||||||
endif (USE_HIP)
|
|
||||||
|
|
||||||
if(FORCE_COLORED_OUTPUT AND (CMAKE_GENERATOR STREQUAL "Ninja") AND
|
|
||||||
((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
|
((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
|
||||||
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")))
|
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")))
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
|
||||||
@ -272,10 +161,10 @@ endif()
|
|||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
if(USE_OPENMP)
|
if (USE_OPENMP)
|
||||||
if(APPLE)
|
if (APPLE)
|
||||||
find_package(OpenMP)
|
find_package(OpenMP)
|
||||||
if(NOT OpenMP_FOUND)
|
if (NOT OpenMP_FOUND)
|
||||||
# Try again with extra path info; required for libomp 15+ from Homebrew
|
# Try again with extra path info; required for libomp 15+ from Homebrew
|
||||||
execute_process(COMMAND brew --prefix libomp
|
execute_process(COMMAND brew --prefix libomp
|
||||||
OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX
|
OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX
|
||||||
@ -288,94 +177,68 @@ if(USE_OPENMP)
|
|||||||
set(OpenMP_CXX_LIB_NAMES omp)
|
set(OpenMP_CXX_LIB_NAMES omp)
|
||||||
set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib)
|
set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib)
|
||||||
find_package(OpenMP REQUIRED)
|
find_package(OpenMP REQUIRED)
|
||||||
endif()
|
endif ()
|
||||||
else()
|
else ()
|
||||||
find_package(OpenMP REQUIRED)
|
find_package(OpenMP REQUIRED)
|
||||||
endif()
|
endif ()
|
||||||
endif()
|
endif (USE_OPENMP)
|
||||||
#Add for IBM i
|
#Add for IBM i
|
||||||
if(${CMAKE_SYSTEM_NAME} MATCHES "OS400")
|
if (${CMAKE_SYSTEM_NAME} MATCHES "OS400")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||||
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -X64 qc <TARGET> <OBJECTS>")
|
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -X64 qc <TARGET> <OBJECTS>")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(USE_NCCL)
|
if (USE_NCCL)
|
||||||
find_package(Nccl REQUIRED)
|
find_package(Nccl REQUIRED)
|
||||||
endif()
|
endif (USE_NCCL)
|
||||||
|
|
||||||
if (USE_RCCL)
|
|
||||||
find_package(rccl REQUIRED)
|
|
||||||
endif (USE_RCCL)
|
|
||||||
|
|
||||||
# dmlc-core
|
# dmlc-core
|
||||||
msvc_use_static_runtime()
|
msvc_use_static_runtime()
|
||||||
if(FORCE_SHARED_CRT)
|
if (FORCE_SHARED_CRT)
|
||||||
set(DMLC_FORCE_SHARED_CRT ON)
|
set(DMLC_FORCE_SHARED_CRT ON)
|
||||||
endif()
|
endif ()
|
||||||
add_subdirectory(${xgboost_SOURCE_DIR}/dmlc-core)
|
add_subdirectory(${xgboost_SOURCE_DIR}/dmlc-core)
|
||||||
|
|
||||||
if(MSVC)
|
if (MSVC)
|
||||||
if(TARGET dmlc_unit_tests)
|
if (TARGET dmlc_unit_tests)
|
||||||
target_compile_options(
|
target_compile_options(dmlc_unit_tests PRIVATE
|
||||||
dmlc_unit_tests PRIVATE
|
-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE)
|
||||||
-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE
|
endif (TARGET dmlc_unit_tests)
|
||||||
)
|
endif (MSVC)
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# rabit
|
# rabit
|
||||||
add_subdirectory(rabit)
|
add_subdirectory(rabit)
|
||||||
|
if (RABIT_BUILD_MPI)
|
||||||
|
find_package(MPI REQUIRED)
|
||||||
|
endif (RABIT_BUILD_MPI)
|
||||||
|
|
||||||
# core xgboost
|
# core xgboost
|
||||||
add_subdirectory(${xgboost_SOURCE_DIR}/src)
|
add_subdirectory(${xgboost_SOURCE_DIR}/src)
|
||||||
target_link_libraries(objxgboost PUBLIC dmlc)
|
target_link_libraries(objxgboost PUBLIC dmlc)
|
||||||
|
|
||||||
# Link -lstdc++fs for GCC 8.x
|
|
||||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0")
|
|
||||||
target_link_libraries(objxgboost PUBLIC stdc++fs)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Exports some R specific definitions and objects
|
# Exports some R specific definitions and objects
|
||||||
if(R_LIB)
|
if (R_LIB)
|
||||||
add_subdirectory(${xgboost_SOURCE_DIR}/R-package)
|
add_subdirectory(${xgboost_SOURCE_DIR}/R-package)
|
||||||
endif()
|
endif (R_LIB)
|
||||||
|
|
||||||
# This creates its own shared library `xgboost4j'.
|
# This creates its own shared library `xgboost4j'.
|
||||||
if(JVM_BINDINGS)
|
if (JVM_BINDINGS)
|
||||||
add_subdirectory(${xgboost_SOURCE_DIR}/jvm-packages)
|
add_subdirectory(${xgboost_SOURCE_DIR}/jvm-packages)
|
||||||
endif()
|
endif (JVM_BINDINGS)
|
||||||
|
|
||||||
# Plugin
|
# Plugin
|
||||||
add_subdirectory(${xgboost_SOURCE_DIR}/plugin)
|
add_subdirectory(${xgboost_SOURCE_DIR}/plugin)
|
||||||
|
|
||||||
if(PLUGIN_RMM)
|
if (PLUGIN_RMM)
|
||||||
find_package(rmm REQUIRED)
|
find_package(rmm REQUIRED)
|
||||||
|
endif (PLUGIN_RMM)
|
||||||
# Patch the rmm targets so they reference the static cudart
|
|
||||||
# Remove this patch once RMM stops specifying cudart requirement
|
|
||||||
# (since RMM is a header-only library, it should not specify cudart in its CMake config)
|
|
||||||
get_target_property(rmm_link_libs rmm::rmm INTERFACE_LINK_LIBRARIES)
|
|
||||||
list(REMOVE_ITEM rmm_link_libs CUDA::cudart)
|
|
||||||
list(APPEND rmm_link_libs CUDA::cudart_static)
|
|
||||||
set_target_properties(rmm::rmm PROPERTIES INTERFACE_LINK_LIBRARIES "${rmm_link_libs}")
|
|
||||||
get_target_property(rmm_link_libs rmm::rmm INTERFACE_LINK_LIBRARIES)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(PLUGIN_SYCL)
|
|
||||||
set(CMAKE_CXX_LINK_EXECUTABLE
|
|
||||||
"icpx <FLAGS> <CMAKE_CXX_LINK_FLAGS> -qopenmp <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
|
|
||||||
set(CMAKE_CXX_CREATE_SHARED_LIBRARY
|
|
||||||
"icpx <CMAKE_SHARED_LIBRARY_CXX_FLAGS> -qopenmp <LANGUAGE_COMPILE_FLAGS> \
|
|
||||||
<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG>,<TARGET_SONAME> \
|
|
||||||
-o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
#-- library
|
#-- library
|
||||||
if(BUILD_STATIC_LIB)
|
if (BUILD_STATIC_LIB)
|
||||||
add_library(xgboost STATIC)
|
add_library(xgboost STATIC)
|
||||||
else()
|
else (BUILD_STATIC_LIB)
|
||||||
add_library(xgboost SHARED)
|
add_library(xgboost SHARED)
|
||||||
endif()
|
endif (BUILD_STATIC_LIB)
|
||||||
target_link_libraries(xgboost PRIVATE objxgboost)
|
target_link_libraries(xgboost PRIVATE objxgboost)
|
||||||
target_include_directories(xgboost
|
target_include_directories(xgboost
|
||||||
INTERFACE
|
INTERFACE
|
||||||
@ -384,70 +247,58 @@ target_include_directories(xgboost
|
|||||||
#-- End shared library
|
#-- End shared library
|
||||||
|
|
||||||
#-- CLI for xgboost
|
#-- CLI for xgboost
|
||||||
if(BUILD_DEPRECATED_CLI)
|
add_executable(runxgboost ${xgboost_SOURCE_DIR}/src/cli_main.cc)
|
||||||
add_executable(runxgboost ${xgboost_SOURCE_DIR}/src/cli_main.cc)
|
target_link_libraries(runxgboost PRIVATE objxgboost)
|
||||||
target_link_libraries(runxgboost PRIVATE objxgboost)
|
target_include_directories(runxgboost
|
||||||
target_include_directories(runxgboost
|
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${xgboost_SOURCE_DIR}/include
|
${xgboost_SOURCE_DIR}/include
|
||||||
${xgboost_SOURCE_DIR}/dmlc-core/include
|
${xgboost_SOURCE_DIR}/dmlc-core/include
|
||||||
${xgboost_SOURCE_DIR}/rabit/include
|
${xgboost_SOURCE_DIR}/rabit/include
|
||||||
)
|
)
|
||||||
set_target_properties(runxgboost PROPERTIES OUTPUT_NAME xgboost)
|
set_target_properties(runxgboost PROPERTIES OUTPUT_NAME xgboost)
|
||||||
xgboost_target_properties(runxgboost)
|
|
||||||
xgboost_target_link_libraries(runxgboost)
|
|
||||||
xgboost_target_defs(runxgboost)
|
|
||||||
|
|
||||||
if(KEEP_BUILD_ARTIFACTS_IN_BINARY_DIR)
|
|
||||||
set_output_directory(runxgboost ${xgboost_BINARY_DIR})
|
|
||||||
else()
|
|
||||||
set_output_directory(runxgboost ${xgboost_SOURCE_DIR})
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
#-- End CLI for xgboost
|
#-- End CLI for xgboost
|
||||||
|
|
||||||
# Common setup for all targets
|
# Common setup for all targets
|
||||||
foreach(target xgboost objxgboost dmlc)
|
foreach(target xgboost objxgboost dmlc runxgboost)
|
||||||
xgboost_target_properties(${target})
|
xgboost_target_properties(${target})
|
||||||
xgboost_target_link_libraries(${target})
|
xgboost_target_link_libraries(${target})
|
||||||
xgboost_target_defs(${target})
|
xgboost_target_defs(${target})
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
if(JVM_BINDINGS)
|
if (JVM_BINDINGS)
|
||||||
xgboost_target_properties(xgboost4j)
|
xgboost_target_properties(xgboost4j)
|
||||||
xgboost_target_link_libraries(xgboost4j)
|
xgboost_target_link_libraries(xgboost4j)
|
||||||
xgboost_target_defs(xgboost4j)
|
xgboost_target_defs(xgboost4j)
|
||||||
endif()
|
endif (JVM_BINDINGS)
|
||||||
|
|
||||||
if(KEEP_BUILD_ARTIFACTS_IN_BINARY_DIR)
|
if (KEEP_BUILD_ARTIFACTS_IN_BINARY_DIR)
|
||||||
|
set_output_directory(runxgboost ${xgboost_BINARY_DIR})
|
||||||
set_output_directory(xgboost ${xgboost_BINARY_DIR}/lib)
|
set_output_directory(xgboost ${xgboost_BINARY_DIR}/lib)
|
||||||
else()
|
else ()
|
||||||
|
set_output_directory(runxgboost ${xgboost_SOURCE_DIR})
|
||||||
set_output_directory(xgboost ${xgboost_SOURCE_DIR}/lib)
|
set_output_directory(xgboost ${xgboost_SOURCE_DIR}/lib)
|
||||||
endif()
|
endif ()
|
||||||
|
|
||||||
# Ensure these two targets do not build simultaneously, as they produce outputs with conflicting names
|
# Ensure these two targets do not build simultaneously, as they produce outputs with conflicting names
|
||||||
if(BUILD_DEPRECATED_CLI)
|
add_dependencies(xgboost runxgboost)
|
||||||
add_dependencies(xgboost runxgboost)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
#-- Installing XGBoost
|
#-- Installing XGBoost
|
||||||
if(R_LIB)
|
if (R_LIB)
|
||||||
include(cmake/RPackageInstallTargetSetup.cmake)
|
include(cmake/RPackageInstallTargetSetup.cmake)
|
||||||
set_target_properties(xgboost PROPERTIES PREFIX "")
|
set_target_properties(xgboost PROPERTIES PREFIX "")
|
||||||
if(APPLE)
|
if (APPLE)
|
||||||
set_target_properties(xgboost PROPERTIES SUFFIX ".so")
|
set_target_properties(xgboost PROPERTIES SUFFIX ".so")
|
||||||
endif()
|
endif (APPLE)
|
||||||
setup_rpackage_install_target(xgboost "${CMAKE_CURRENT_BINARY_DIR}/R-package-install")
|
setup_rpackage_install_target(xgboost "${CMAKE_CURRENT_BINARY_DIR}/R-package-install")
|
||||||
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/dummy_inst")
|
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/dummy_inst")
|
||||||
endif()
|
endif (R_LIB)
|
||||||
if(MINGW)
|
if (MINGW)
|
||||||
set_target_properties(xgboost PROPERTIES PREFIX "")
|
set_target_properties(xgboost PROPERTIES PREFIX "")
|
||||||
endif()
|
endif (MINGW)
|
||||||
|
|
||||||
if(BUILD_C_DOC)
|
if (BUILD_C_DOC)
|
||||||
include(cmake/Doc.cmake)
|
include(cmake/Doc.cmake)
|
||||||
run_doxygen()
|
run_doxygen()
|
||||||
endif()
|
endif (BUILD_C_DOC)
|
||||||
|
|
||||||
include(CPack)
|
include(CPack)
|
||||||
|
|
||||||
@ -463,19 +314,11 @@ install(DIRECTORY ${xgboost_SOURCE_DIR}/include/xgboost
|
|||||||
# > in any export set.
|
# > in any export set.
|
||||||
#
|
#
|
||||||
# https://github.com/dmlc/xgboost/issues/6085
|
# https://github.com/dmlc/xgboost/issues/6085
|
||||||
if(BUILD_STATIC_LIB)
|
if (BUILD_STATIC_LIB)
|
||||||
if(BUILD_DEPRECATED_CLI)
|
|
||||||
set(INSTALL_TARGETS xgboost runxgboost objxgboost dmlc)
|
set(INSTALL_TARGETS xgboost runxgboost objxgboost dmlc)
|
||||||
else()
|
else (BUILD_STATIC_LIB)
|
||||||
set(INSTALL_TARGETS xgboost objxgboost dmlc)
|
|
||||||
endif()
|
|
||||||
else()
|
|
||||||
if(BUILD_DEPRECATED_CLI)
|
|
||||||
set(INSTALL_TARGETS xgboost runxgboost)
|
set(INSTALL_TARGETS xgboost runxgboost)
|
||||||
else()
|
endif (BUILD_STATIC_LIB)
|
||||||
set(INSTALL_TARGETS xgboost)
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
install(TARGETS ${INSTALL_TARGETS}
|
install(TARGETS ${INSTALL_TARGETS}
|
||||||
EXPORT XGBoostTargets
|
EXPORT XGBoostTargets
|
||||||
@ -504,7 +347,7 @@ install(
|
|||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/xgboost)
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/xgboost)
|
||||||
|
|
||||||
#-- Test
|
#-- Test
|
||||||
if(GOOGLE_TEST)
|
if (GOOGLE_TEST)
|
||||||
enable_testing()
|
enable_testing()
|
||||||
# Unittests.
|
# Unittests.
|
||||||
add_executable(testxgboost)
|
add_executable(testxgboost)
|
||||||
@ -524,7 +367,6 @@ if(GOOGLE_TEST)
|
|||||||
${xgboost_SOURCE_DIR}/tests/cli/machine.conf.in
|
${xgboost_SOURCE_DIR}/tests/cli/machine.conf.in
|
||||||
${xgboost_BINARY_DIR}/tests/cli/machine.conf
|
${xgboost_BINARY_DIR}/tests/cli/machine.conf
|
||||||
@ONLY)
|
@ONLY)
|
||||||
if(BUILD_DEPRECATED_CLI)
|
|
||||||
add_test(
|
add_test(
|
||||||
NAME TestXGBoostCLI
|
NAME TestXGBoostCLI
|
||||||
COMMAND runxgboost ${xgboost_BINARY_DIR}/tests/cli/machine.conf
|
COMMAND runxgboost ${xgboost_BINARY_DIR}/tests/cli/machine.conf
|
||||||
@ -532,8 +374,7 @@ if(GOOGLE_TEST)
|
|||||||
set_tests_properties(TestXGBoostCLI
|
set_tests_properties(TestXGBoostCLI
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
PASS_REGULAR_EXPRESSION ".*test-rmse:0.087.*")
|
PASS_REGULAR_EXPRESSION ".*test-rmse:0.087.*")
|
||||||
endif()
|
endif (GOOGLE_TEST)
|
||||||
endif()
|
|
||||||
|
|
||||||
# For MSVC: Call msvc_use_static_runtime() once again to completely
|
# For MSVC: Call msvc_use_static_runtime() once again to completely
|
||||||
# replace /MD with /MT. See https://github.com/dmlc/xgboost/issues/4462
|
# replace /MD with /MT. See https://github.com/dmlc/xgboost/issues/4462
|
||||||
@ -541,10 +382,10 @@ endif()
|
|||||||
msvc_use_static_runtime()
|
msvc_use_static_runtime()
|
||||||
|
|
||||||
# Add xgboost.pc
|
# Add xgboost.pc
|
||||||
if(ADD_PKGCONFIG)
|
if (ADD_PKGCONFIG)
|
||||||
configure_file(${xgboost_SOURCE_DIR}/cmake/xgboost.pc.in ${xgboost_BINARY_DIR}/xgboost.pc @ONLY)
|
configure_file(${xgboost_SOURCE_DIR}/cmake/xgboost.pc.in ${xgboost_BINARY_DIR}/xgboost.pc @ONLY)
|
||||||
|
|
||||||
install(
|
install(
|
||||||
FILES ${xgboost_BINARY_DIR}/xgboost.pc
|
FILES ${xgboost_BINARY_DIR}/xgboost.pc
|
||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
||||||
endif()
|
endif (ADD_PKGCONFIG)
|
||||||
|
|||||||
@ -10,8 +10,8 @@ The Project Management Committee(PMC) consists group of active committers that m
|
|||||||
- Tianqi is a Ph.D. student working on large-scale machine learning. He is the creator of the project.
|
- Tianqi is a Ph.D. student working on large-scale machine learning. He is the creator of the project.
|
||||||
* [Michael Benesty](https://github.com/pommedeterresautee)
|
* [Michael Benesty](https://github.com/pommedeterresautee)
|
||||||
- Michael is a lawyer and data scientist in France. He is the creator of XGBoost interactive analysis module in R.
|
- Michael is a lawyer and data scientist in France. He is the creator of XGBoost interactive analysis module in R.
|
||||||
* [Yuan Tang](https://github.com/terrytangyuan), Red Hat
|
* [Yuan Tang](https://github.com/terrytangyuan), Akuity
|
||||||
- Yuan is a principal software engineer at Red Hat. He contributed mostly in R and Python packages.
|
- Yuan is a founding engineer at Akuity. He contributed mostly in R and Python packages.
|
||||||
* [Nan Zhu](https://github.com/CodingCat), Uber
|
* [Nan Zhu](https://github.com/CodingCat), Uber
|
||||||
- Nan is a software engineer in Uber. He contributed mostly in JVM packages.
|
- Nan is a software engineer in Uber. He contributed mostly in JVM packages.
|
||||||
* [Jiaming Yuan](https://github.com/trivialfis)
|
* [Jiaming Yuan](https://github.com/trivialfis)
|
||||||
|
|||||||
220
NEWS.md
220
NEWS.md
@ -3,224 +3,6 @@ XGBoost Change Log
|
|||||||
|
|
||||||
This file records the changes in xgboost library in reverse chronological order.
|
This file records the changes in xgboost library in reverse chronological order.
|
||||||
|
|
||||||
## 2.0.0 (2023 Aug 16)
|
|
||||||
|
|
||||||
We are excited to announce the release of XGBoost 2.0. This note will begin by covering some overall changes and then highlight specific updates to the package.
|
|
||||||
|
|
||||||
### Initial work on multi-target trees with vector-leaf outputs
|
|
||||||
We have been working on vector-leaf tree models for multi-target regression, multi-label classification, and multi-class classification in version 2.0. Previously, XGBoost would build a separate model for each target. However, with this new feature that's still being developed, XGBoost can build one tree for all targets. The feature has multiple benefits and trade-offs compared to the existing approach. It can help prevent overfitting, produce smaller models, and build trees that consider the correlation between targets. In addition, users can combine vector leaf and scalar leaf trees during a training session using a callback. Please note that the feature is still a working in progress, and many parts are not yet available. See #9043 for the current status. Related PRs: (#8538, #8697, #8902, #8884, #8895, #8898, #8612, #8652, #8698, #8908, #8928, #8968, #8616, #8922, #8890, #8872, #8889, #9509) Please note that, only the `hist` (default) tree method on CPU can be used for building vector leaf trees at the moment.
|
|
||||||
|
|
||||||
### New `device` parameter.
|
|
||||||
|
|
||||||
A new `device` parameter is set to replace the existing `gpu_id`, `gpu_hist`, `gpu_predictor`, `cpu_predictor`, `gpu_coord_descent`, and the PySpark specific parameter `use_gpu`. Onward, users need only the `device` parameter to select which device to run along with the ordinal of the device. For more information, please see our document page (https://xgboost.readthedocs.io/en/stable/parameter.html#general-parameters) . For example, with `device="cuda", tree_method="hist"`, XGBoost will run the `hist` tree method on GPU. (#9363, #8528, #8604, #9354, #9274, #9243, #8896, #9129, #9362, #9402, #9385, #9398, #9390, #9386, #9412, #9507, #9536). The old behavior of ``gpu_hist`` is preserved but deprecated. In addition, the `predictor` parameter is removed.
|
|
||||||
|
|
||||||
|
|
||||||
### `hist` is now the default tree method
|
|
||||||
Starting from 2.0, the `hist` tree method will be the default. In previous versions, XGBoost chooses `approx` or `exact` depending on the input data and training environment. The new default can help XGBoost train models more efficiently and consistently. (#9320, #9353)
|
|
||||||
|
|
||||||
### GPU-based approx tree method
|
|
||||||
There's initial support for using the `approx` tree method on GPU. The performance of the `approx` is not yet well optimized but is feature complete except for the JVM packages. It can be accessed through the use of the parameter combination `device="cuda", tree_method="approx"`. (#9414, #9399, #9478). Please note that the Scala-based Spark interface is not yet supported.
|
|
||||||
|
|
||||||
### Optimize and bound the size of the histogram on CPU, to control memory footprint
|
|
||||||
|
|
||||||
XGBoost has a new parameter `max_cached_hist_node` for users to limit the CPU cache size for histograms. It can help prevent XGBoost from caching histograms too aggressively. Without the cache, performance is likely to decrease. However, the size of the cache grows exponentially with the depth of the tree. The limit can be crucial when growing deep trees. In most cases, users need not configure this parameter as it does not affect the model's accuracy. (#9455, #9441, #9440, #9427, #9400).
|
|
||||||
|
|
||||||
Along with the cache limit, XGBoost also reduces the memory usage of the `hist` and `approx` tree method on distributed systems by cutting the size of the cache by half. (#9433)
|
|
||||||
|
|
||||||
### Improved external memory support
|
|
||||||
There is some exciting development around external memory support in XGBoost. It's still an experimental feature, but the performance has been significantly improved with the default `hist` tree method. We replaced the old file IO logic with memory map. In addition to performance, we have reduced CPU memory usage and added extensive documentation. Beginning from 2.0.0, we encourage users to try it with the `hist` tree method when the memory saving by `QuantileDMatrix` is not sufficient. (#9361, #9317, #9282, #9315, #8457)
|
|
||||||
|
|
||||||
### Learning to rank
|
|
||||||
We created a brand-new implementation for the learning-to-rank task. With the latest version, XGBoost gained a set of new features for ranking task including:
|
|
||||||
|
|
||||||
- A new parameter `lambdarank_pair_method` for choosing the pair construction strategy.
|
|
||||||
- A new parameter `lambdarank_num_pair_per_sample` for controlling the number of samples for each group.
|
|
||||||
- An experimental implementation of unbiased learning-to-rank, which can be accessed using the `lambdarank_unbiased` parameter.
|
|
||||||
- Support for custom gain function with `NDCG` using the `ndcg_exp_gain` parameter.
|
|
||||||
- Deterministic GPU computation for all objectives and metrics.
|
|
||||||
- `NDCG` is now the default objective function.
|
|
||||||
- Improved performance of metrics using caches.
|
|
||||||
- Support scikit-learn utilities for `XGBRanker`.
|
|
||||||
- Extensive documentation on how learning-to-rank works with XGBoost.
|
|
||||||
|
|
||||||
For more information, please see the [tutorial](https://xgboost.readthedocs.io/en/latest/tutorials/learning_to_rank.html). Related PRs: (#8771, #8692, #8783, #8789, #8790, #8859, #8887, #8893, #8906, #8931, #9075, #9015, #9381, #9336, #8822, #9222, #8984, #8785, #8786, #8768)
|
|
||||||
|
|
||||||
### Automatically estimated intercept
|
|
||||||
|
|
||||||
In the previous version, `base_score` was a constant that could be set as a training parameter. In the new version, XGBoost can automatically estimate this parameter based on input labels for optimal accuracy. (#8539, #8498, #8272, #8793, #8607)
|
|
||||||
|
|
||||||
### Quantile regression
|
|
||||||
The XGBoost algorithm now supports quantile regression, which involves minimizing the quantile loss (also called "pinball loss"). Furthermore, XGBoost allows for training with multiple target quantiles simultaneously with one tree per quantile. (#8775, #8761, #8760, #8758, #8750)
|
|
||||||
|
|
||||||
### L1 and Quantile regression now supports learning rate
|
|
||||||
Both objectives use adaptive trees due to the lack of proper Hessian values. In the new version, XGBoost can scale the leaf value with the learning rate accordingly. (#8866)
|
|
||||||
|
|
||||||
### Export cut value
|
|
||||||
|
|
||||||
Using the Python or the C package, users can export the quantile values (not to be confused with quantile regression) used for the `hist` tree method. (#9356)
|
|
||||||
|
|
||||||
### column-based split and federated learning
|
|
||||||
We made progress on column-based split for federated learning. In 2.0, both `approx`, `hist`, and `hist` with vector leaf can work with column-based data split, along with support for vertical federated learning. Work on GPU support is still on-going, stay tuned. (#8576, #8468, #8442, #8847, #8811, #8985, #8623, #8568, #8828, #8932, #9081, #9102, #9103, #9124, #9120, #9367, #9370, #9343, #9171, #9346, #9270, #9244, #8494, #8434, #8742, #8804, #8710, #8676, #9020, #9002, #9058, #9037, #9018, #9295, #9006, #9300, #8765, #9365, #9060)
|
|
||||||
|
|
||||||
### PySpark
|
|
||||||
After the initial introduction of the PySpark interface, it has gained some new features and optimizations in 2.0.
|
|
||||||
|
|
||||||
- GPU-based prediction. (#9292, #9542)
|
|
||||||
- Optimization for data initialization by avoiding the stack operation. (#9088)
|
|
||||||
- Support predict feature contribution. (#8633)
|
|
||||||
- Python typing support. (#9156, #9172, #9079, #8375)
|
|
||||||
- `use_gpu` is deprecated. The `device` parameter is preferred.
|
|
||||||
- Update eval_metric validation to support list of strings (#8826)
|
|
||||||
- Improved logs for training (#9449)
|
|
||||||
- Maintenance, including refactoring and document updates (#8324, #8465, #8605, #9202, #9460, #9302, #8385, #8630, #8525, #8496)
|
|
||||||
- Fix for GPU setup. (#9495)
|
|
||||||
|
|
||||||
### Other General New Features
|
|
||||||
Here's a list of new features that don't have their own section and yet are general to all language bindings.
|
|
||||||
|
|
||||||
- Use array interface for CSC matrix. This helps XGBoost to use a consistent number of threads and align the interface of the CSC matrix with other interfaces. In addition, memory usage is likely to decrease with CSC input thanks to on-the-fly type conversion. (#8672)
|
|
||||||
- CUDA compute 90 is now part of the default build.. (#9397)
|
|
||||||
|
|
||||||
### Other General Optimization
|
|
||||||
These optimizations are general to all language bindings. For language-specific optimization, please visit the corresponding sections.
|
|
||||||
|
|
||||||
- Performance for input with `array_interface` on CPU (like `numpy`) is significantly improved. (#9090)
|
|
||||||
- Some optimization with CUDA for data initialization. (#9199, #9209, #9144)
|
|
||||||
- Use the latest thrust policy to prevent synchronizing GPU devices. (#9212)
|
|
||||||
- XGBoost now uses a per-thread CUDA stream, which prevents synchronization with other streams. (#9416, #9396, #9413)
|
|
||||||
|
|
||||||
### Notable breaking change
|
|
||||||
|
|
||||||
Other than the aforementioned change with the `device` parameter, here's a list of breaking changes affecting all packages.
|
|
||||||
|
|
||||||
- Users must specify the format for text input (#9077). However, we suggest using third-party data structures such as `numpy.ndarray` instead of relying on text inputs. See https://github.com/dmlc/xgboost/issues/9472 for more info.
|
|
||||||
|
|
||||||
### Notable bug fixes
|
|
||||||
|
|
||||||
Some noteworthy bug fixes that are not related to specific language bindings are listed in this section.
|
|
||||||
|
|
||||||
- Some language environments use a different thread to perform garbage collection, which breaks the thread-local cache used in XGBoost. XGBoost 2.0 implements a new thread-safe cache using a light weight lock to replace the thread-local cache. (#8851)
|
|
||||||
- Fix model IO by clearing the prediction cache. (#8904)
|
|
||||||
- `inf` is checked during data construction. (#8911)
|
|
||||||
- Preserve order of saved updaters configuration. Usually, this is not an issue unless the `updater` parameter is used instead of the `tree_method` parameter (#9355)
|
|
||||||
- Fix GPU memory allocation issue with categorical splits. (#9529)
|
|
||||||
- Handle escape sequence like `\t\n` in feature names for JSON model dump. (#9474)
|
|
||||||
- Normalize file path for model IO and text input. This handles short paths on Windows and paths that contain `~` on Unix (#9463). In addition, all path inputs are required to be encoded in UTF-8 (#9448, #9443)
|
|
||||||
- Fix integer overflow on H100. (#9380)
|
|
||||||
- Fix weighted sketching on GPU with categorical features. (#9341)
|
|
||||||
- Fix metric serialization. The bug might cause some of the metrics to be dropped during evaluation. (#9405)
|
|
||||||
- Fixes compilation errors on MSVC x86 targets (#8823)
|
|
||||||
- Pick up the dmlc-core fix for the CSV parser. (#8897)
|
|
||||||
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
Aside from documents for new features, we have many smaller updates to improve user experience, from troubleshooting guides to typo fixes.
|
|
||||||
|
|
||||||
- Explain CPU/GPU interop. (#8450)
|
|
||||||
- Guide to troubleshoot NCCL errors. (#8943, #9206)
|
|
||||||
- Add a note for rabit port selection. (#8879)
|
|
||||||
- How to build the docs using conda (#9276)
|
|
||||||
- Explain how to obtain reproducible results on distributed systems. (#8903)
|
|
||||||
|
|
||||||
* Fixes and small updates to document and demonstration scripts. (#8626, #8436, #8995, #8907, #8923, #8926, #9358, #9232, #9201, #9469, #9462, #9458, #8543, #8597, #8401, #8784, #9213, #9098, #9008, #9223, #9333, #9434, #9435, #9415, #8773, #8752, #9291, #9549)
|
|
||||||
|
|
||||||
### Python package
|
|
||||||
* New Features and Improvements
|
|
||||||
- Support primitive types of pyarrow-backed pandas dataframe. (#8653)
|
|
||||||
- Warning messages emitted by XGBoost are now emitted using Python warnings. (#9387)
|
|
||||||
- User can now format the value printed near the bars on the `plot_importance` plot (#8540)
|
|
||||||
- XGBoost has improved half-type support (float16) with pandas, cupy, and cuDF. With GPU input, the handling is through CUDA `__half` type, and no data copy is made. (#8487, #9207, #8481)
|
|
||||||
- Support `Series` and Python primitive types in `inplace_predict` and `QuantileDMatrix` (#8547, #8542)
|
|
||||||
- Support all pandas' nullable integer types. (#8480)
|
|
||||||
- Custom metric with the scikit-learn interface now supports `sample_weight`. (#8706)
|
|
||||||
- Enable Installation of Python Package with System lib in a Virtual Environment (#9349)
|
|
||||||
- Raise if expected workers are not alive in `xgboost.dask.train` (#9421)
|
|
||||||
|
|
||||||
* Optimization
|
|
||||||
- Cache transformed data in `QuantileDMatrix` for efficiency. (#8666, #9445)
|
|
||||||
- Take datatable as row-major input. (#8472)
|
|
||||||
- Remove unnecessary conversions between data structures (#8546)
|
|
||||||
|
|
||||||
* Adopt modern Python packaging conventions (PEP 517, PEP 518, PEP 621)
|
|
||||||
- XGBoost adopted the modern Python packaging conventions. The old setup script `setup.py` is now replaced with the new configuration file `pyproject.toml`. Along with this, XGBoost now supports Python 3.11. (#9021, #9112, #9114, #9115) Consult the latest documentation for the updated instructions to build and install XGBoost.
|
|
||||||
|
|
||||||
* Fixes
|
|
||||||
- `DataIter` now accepts only keyword arguments. (#9431)
|
|
||||||
- Fix empty DMatrix with categorical features. (#8739)
|
|
||||||
- Convert ``DaskXGBClassifier.classes_`` to an array (#8452)
|
|
||||||
- Define `best_iteration` only if early stopping is used to be consistent with documented behavior. (#9403)
|
|
||||||
- Make feature validation immutable. (#9388)
|
|
||||||
|
|
||||||
* Breaking changes
|
|
||||||
- Discussed in the new `device` parameter section, the `predictor` parameter is now removed. (#9129)
|
|
||||||
- Remove support for single-string feature info. Feature type and names should be a sequence of strings (#9401)
|
|
||||||
- Remove parameters in the `save_model` call for the scikit-learn interface. (#8963)
|
|
||||||
- Remove the `ntree_limit` in the python package. This has been deprecated in previous versions. (#8345)
|
|
||||||
|
|
||||||
* Maintenance including formatting and refactoring along with type hints.
|
|
||||||
- More consistent use of `black` and `isort` for code formatting (#8420, #8748, #8867)
|
|
||||||
- Improved type support. Most of the type changes happen in the PySpark module; here, we list the remaining changes. (#8444, #8617, #9197, #9005)
|
|
||||||
- Set `enable_categorical` to True in predict. (#8592)
|
|
||||||
- Some refactoring and updates for tests (#8395, #8372, #8557, #8379, #8702, #9459, #9316, #8446, #8695, #8409, #8993, #9480)
|
|
||||||
|
|
||||||
* Documentation
|
|
||||||
- Add introduction and notes for the sklearn interface. (#8948)
|
|
||||||
- Demo for using dask for hyper-parameter optimization. (#8891)
|
|
||||||
- Document all supported Python input types. (#8643)
|
|
||||||
- Other documentation updates (#8944, #9304)
|
|
||||||
|
|
||||||
### R package
|
|
||||||
- Use the new data consumption interface for CSR and CSC. This provides better control for the number of threads and improves performance. (#8455, #8673)
|
|
||||||
- Accept multiple evaluation metrics during training. (#8657)
|
|
||||||
- Fix integer inputs with `NA`. (#9522)
|
|
||||||
- Some refactoring for the R package (#8545, #8430, #8614, #8624, #8613, #9457, #8689, #8563, #9461, #8647, #8564, #8565, #8736, #8610, #8609, #8599, #8704, #9456, #9450, #9476, #9477, #9481). Special thanks to @jameslamb.
|
|
||||||
- Document updates (#8886, #9323, #9437, #8998)
|
|
||||||
|
|
||||||
### JVM packages
|
|
||||||
Following are changes specific to various JVM-based packages.
|
|
||||||
|
|
||||||
- Stop using Rabit in prediction (#9054)
|
|
||||||
- Set feature_names and feature_types in jvm-packages. This is to prepare support for categorical features (#9364)
|
|
||||||
- Scala 2.13 support. (#9099)
|
|
||||||
- Change training stage from `ResultStage` to `ShuffleMapStage` (#9423)
|
|
||||||
- Automatically set the max/min direction for the best score during early stopping. (#9404)
|
|
||||||
* Revised support for `flink` (#9046)
|
|
||||||
|
|
||||||
* Breaking changes
|
|
||||||
- Scala-based tracker is removed. (#9078, #9045)
|
|
||||||
- Change `DeviceQuantileDmatrix` into `QuantileDMatrix` (#8461)
|
|
||||||
|
|
||||||
* Maintenance (#9253, #9166, #9395, #9389, #9224, #9233, #9351, #9479)
|
|
||||||
|
|
||||||
* CI bot PRs
|
|
||||||
We employed GitHub dependent bot to help us keep the dependencies up-to-date for JVM packages. With the help from the bot, we have cleared up all the dependencies that are lagging behind (#8501, #8507).
|
|
||||||
|
|
||||||
Here's a list of dependency update PRs including those made by dependent bots (#8456, #8560, #8571, #8561, #8562, #8600, #8594, #8524, #8509, #8548, #8549, #8533, #8521, #8534, #8532, #8516, #8503, #8531, #8530, #8518, #8512, #8515, #8517, #8506, #8504, #8502, #8629, #8815, #8813, #8814, #8877, #8876, #8875, #8874, #8873, #9049, #9070, #9073, #9039, #9083, #8917, #8952, #8980, #8973, #8962, #9252, #9208, #9131, #9136, #9219, #9160, #9158, #9163, #9184, #9192, #9265, #9268, #8882, #8837, #8662, #8661, #8390, #9056, #8508, #8925, #8920, #9149, #9230, #9097, #8648, #9203, #8593).
|
|
||||||
|
|
||||||
### Maintenance
|
|
||||||
Maintenance work includes refactoring, fixing small issues that don't affect end users. (#9256, #8627, #8756, #8735, #8966, #8864, #8747, #8892, #9057, #8921, #8949, #8941, #8942, #9108, #9125, #9155, #9153, #9176, #9447, #9444, #9436, #9438, #9430, #9200, #9210, #9055, #9014, #9004, #8999, #9154, #9148, #9283, #9246, #8888, #8900, #8871, #8861, #8858, #8791, #8807, #8751, #8703, #8696, #8693, #8677, #8686, #8665, #8660, #8386, #8371, #8410, #8578, #8574, #8483, #8443, #8454, #8733)
|
|
||||||
|
|
||||||
### CI
|
|
||||||
- Build pip wheel with RMM support (#9383)
|
|
||||||
- Other CI updates including updating dependencies and work on the CI infrastructure. (#9464, #9428, #8767, #9394, #9278, #9214, #9234, #9205, #9034, #9104, #8878, #9294, #8625, #8806, #8741, #8707, #8381, #8382, #8388, #8402, #8397, #8445, #8602, #8628, #8583, #8460, #9544)
|
|
||||||
|
|
||||||
## 1.7.6 (2023 Jun 16)
|
|
||||||
|
|
||||||
This is a patch release for bug fixes. The CRAN package for the R binding is kept at 1.7.5.
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
* Fix distributed training with mixed dense and sparse partitions. (#9272)
|
|
||||||
* Fix monotone constraints on CPU with large trees. (#9122)
|
|
||||||
* [spark] Make the spark model have the same UID as its estimator (#9022)
|
|
||||||
* Optimize prediction with `QuantileDMatrix`. (#9096)
|
|
||||||
|
|
||||||
### Document
|
|
||||||
* Improve doxygen (#8959)
|
|
||||||
* Update the cuDF pip index URL. (#9106)
|
|
||||||
|
|
||||||
### Maintenance
|
|
||||||
* Fix tests with pandas 2.0. (#9014)
|
|
||||||
|
|
||||||
## 1.7.5 (2023 Mar 30)
|
## 1.7.5 (2023 Mar 30)
|
||||||
This is a patch release for bug fixes.
|
This is a patch release for bug fixes.
|
||||||
|
|
||||||
@ -2101,7 +1883,7 @@ This release marks a major milestone for the XGBoost project.
|
|||||||
## v0.90 (2019.05.18)
|
## v0.90 (2019.05.18)
|
||||||
|
|
||||||
### XGBoost Python package drops Python 2.x (#4379, #4381)
|
### XGBoost Python package drops Python 2.x (#4379, #4381)
|
||||||
Python 2.x is reaching its end-of-life at the end of this year. [Many scientific Python packages are now moving to drop Python 2.x](https://python3statement.github.io/).
|
Python 2.x is reaching its end-of-life at the end of this year. [Many scientific Python packages are now moving to drop Python 2.x](https://python3statement.org/).
|
||||||
|
|
||||||
### XGBoost4J-Spark now requires Spark 2.4.x (#4377)
|
### XGBoost4J-Spark now requires Spark 2.4.x (#4377)
|
||||||
* Spark 2.3 is reaching its end-of-life soon. See discussion at #4389.
|
* Spark 2.3 is reaching its end-of-life soon. See discussion at #4389.
|
||||||
|
|||||||
@ -4,5 +4,3 @@
|
|||||||
^.*\.Rproj$
|
^.*\.Rproj$
|
||||||
^\.Rproj\.user$
|
^\.Rproj\.user$
|
||||||
README.md
|
README.md
|
||||||
^doc$
|
|
||||||
^Meta$
|
|
||||||
|
|||||||
@ -1,62 +1,41 @@
|
|||||||
find_package(LibR REQUIRED)
|
find_package(LibR REQUIRED)
|
||||||
message(STATUS "LIBR_CORE_LIBRARY " ${LIBR_CORE_LIBRARY})
|
message(STATUS "LIBR_CORE_LIBRARY " ${LIBR_CORE_LIBRARY})
|
||||||
|
|
||||||
file(
|
file(GLOB_RECURSE R_SOURCES
|
||||||
GLOB_RECURSE R_SOURCES
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/*.cc
|
${CMAKE_CURRENT_LIST_DIR}/src/*.cc
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/*.c
|
${CMAKE_CURRENT_LIST_DIR}/src/*.c)
|
||||||
)
|
|
||||||
|
|
||||||
# Use object library to expose symbols
|
# Use object library to expose symbols
|
||||||
add_library(xgboost-r OBJECT ${R_SOURCES})
|
add_library(xgboost-r OBJECT ${R_SOURCES})
|
||||||
|
if (ENABLE_ALL_WARNINGS)
|
||||||
if(ENABLE_ALL_WARNINGS)
|
|
||||||
target_compile_options(xgboost-r PRIVATE -Wall -Wextra)
|
target_compile_options(xgboost-r PRIVATE -Wall -Wextra)
|
||||||
endif()
|
endif (ENABLE_ALL_WARNINGS)
|
||||||
|
target_compile_definitions(xgboost-r
|
||||||
if(MSVC)
|
PUBLIC
|
||||||
# https://github.com/microsoft/LightGBM/pull/6061
|
|
||||||
# MSVC doesn't work with anonymous types in structs. (R complex)
|
|
||||||
#
|
|
||||||
# syntax error: missing ';' before identifier 'private_data_c'
|
|
||||||
#
|
|
||||||
target_compile_definitions(xgboost-r PRIVATE -DR_LEGACY_RCOMPLEX)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_compile_definitions(
|
|
||||||
xgboost-r PUBLIC
|
|
||||||
-DXGBOOST_STRICT_R_MODE=1
|
-DXGBOOST_STRICT_R_MODE=1
|
||||||
|
-DXGBOOST_CUSTOMIZE_GLOBAL_PRNG=1
|
||||||
-DDMLC_LOG_BEFORE_THROW=0
|
-DDMLC_LOG_BEFORE_THROW=0
|
||||||
-DDMLC_DISABLE_STDIN=1
|
-DDMLC_DISABLE_STDIN=1
|
||||||
-DDMLC_LOG_CUSTOMIZE=1
|
-DDMLC_LOG_CUSTOMIZE=1
|
||||||
-DRABIT_STRICT_CXX98_
|
-DRABIT_STRICT_CXX98_)
|
||||||
)
|
target_include_directories(xgboost-r
|
||||||
|
PRIVATE
|
||||||
target_include_directories(
|
|
||||||
xgboost-r PRIVATE
|
|
||||||
${LIBR_INCLUDE_DIRS}
|
${LIBR_INCLUDE_DIRS}
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${PROJECT_SOURCE_DIR}/dmlc-core/include
|
${PROJECT_SOURCE_DIR}/dmlc-core/include
|
||||||
${PROJECT_SOURCE_DIR}/rabit/include
|
${PROJECT_SOURCE_DIR}/rabit/include)
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(xgboost-r PUBLIC ${LIBR_CORE_LIBRARY})
|
target_link_libraries(xgboost-r PUBLIC ${LIBR_CORE_LIBRARY})
|
||||||
|
if (USE_OPENMP)
|
||||||
if(USE_OPENMP)
|
|
||||||
find_package(OpenMP REQUIRED)
|
find_package(OpenMP REQUIRED)
|
||||||
target_link_libraries(xgboost-r PUBLIC OpenMP::OpenMP_CXX OpenMP::OpenMP_C)
|
target_link_libraries(xgboost-r PUBLIC OpenMP::OpenMP_CXX OpenMP::OpenMP_C)
|
||||||
endif()
|
endif (USE_OPENMP)
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
xgboost-r PROPERTIES
|
xgboost-r PROPERTIES
|
||||||
CXX_STANDARD 17
|
CXX_STANDARD 17
|
||||||
CXX_STANDARD_REQUIRED ON
|
CXX_STANDARD_REQUIRED ON
|
||||||
POSITION_INDEPENDENT_CODE ON
|
POSITION_INDEPENDENT_CODE ON)
|
||||||
)
|
|
||||||
|
|
||||||
# Get compilation and link flags of xgboost-r and propagate to objxgboost
|
# Get compilation and link flags of xgboost-r and propagate to objxgboost
|
||||||
target_link_libraries(objxgboost PUBLIC xgboost-r)
|
target_link_libraries(objxgboost PUBLIC xgboost-r)
|
||||||
|
|
||||||
# Add all objects of xgboost-r to objxgboost
|
# Add all objects of xgboost-r to objxgboost
|
||||||
target_sources(objxgboost INTERFACE $<TARGET_OBJECTS:xgboost-r>)
|
target_sources(objxgboost INTERFACE $<TARGET_OBJECTS:xgboost-r>)
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
Package: xgboost
|
Package: xgboost
|
||||||
Type: Package
|
Type: Package
|
||||||
Title: Extreme Gradient Boosting
|
Title: Extreme Gradient Boosting
|
||||||
Version: 2.1.0.0
|
Version: 2.0.0.1
|
||||||
Date: 2023-08-19
|
Date: 2022-10-18
|
||||||
Authors@R: c(
|
Authors@R: c(
|
||||||
person("Tianqi", "Chen", role = c("aut"),
|
person("Tianqi", "Chen", role = c("aut"),
|
||||||
email = "tianqi.tchen@gmail.com"),
|
email = "tianqi.tchen@gmail.com"),
|
||||||
@ -56,16 +56,14 @@ Suggests:
|
|||||||
testthat,
|
testthat,
|
||||||
igraph (>= 1.0.1),
|
igraph (>= 1.0.1),
|
||||||
float,
|
float,
|
||||||
titanic,
|
titanic
|
||||||
RhpcBLASctl
|
|
||||||
Depends:
|
Depends:
|
||||||
R (>= 4.3.0)
|
R (>= 3.3.0)
|
||||||
Imports:
|
Imports:
|
||||||
Matrix (>= 1.1-0),
|
Matrix (>= 1.1-0),
|
||||||
methods,
|
methods,
|
||||||
data.table (>= 1.9.6),
|
data.table (>= 1.9.6),
|
||||||
jsonlite (>= 1.0)
|
jsonlite (>= 1.0),
|
||||||
Roxygen: list(markdown = TRUE)
|
RoxygenNote: 7.2.3
|
||||||
RoxygenNote: 7.3.1
|
|
||||||
Encoding: UTF-8
|
Encoding: UTF-8
|
||||||
SystemRequirements: GNU make, C++17
|
SystemRequirements: GNU make, C++17
|
||||||
|
|||||||
@ -1,61 +1,46 @@
|
|||||||
# Generated by roxygen2: do not edit by hand
|
# Generated by roxygen2: do not edit by hand
|
||||||
|
|
||||||
S3method("[",xgb.Booster)
|
|
||||||
S3method("[",xgb.DMatrix)
|
S3method("[",xgb.DMatrix)
|
||||||
S3method("dimnames<-",xgb.DMatrix)
|
S3method("dimnames<-",xgb.DMatrix)
|
||||||
S3method(coef,xgb.Booster)
|
|
||||||
S3method(dim,xgb.DMatrix)
|
S3method(dim,xgb.DMatrix)
|
||||||
S3method(dimnames,xgb.DMatrix)
|
S3method(dimnames,xgb.DMatrix)
|
||||||
S3method(getinfo,xgb.Booster)
|
|
||||||
S3method(getinfo,xgb.DMatrix)
|
S3method(getinfo,xgb.DMatrix)
|
||||||
S3method(length,xgb.Booster)
|
|
||||||
S3method(predict,xgb.Booster)
|
S3method(predict,xgb.Booster)
|
||||||
|
S3method(predict,xgb.Booster.handle)
|
||||||
S3method(print,xgb.Booster)
|
S3method(print,xgb.Booster)
|
||||||
S3method(print,xgb.DMatrix)
|
S3method(print,xgb.DMatrix)
|
||||||
S3method(print,xgb.cv.synchronous)
|
S3method(print,xgb.cv.synchronous)
|
||||||
S3method(setinfo,xgb.Booster)
|
|
||||||
S3method(setinfo,xgb.DMatrix)
|
S3method(setinfo,xgb.DMatrix)
|
||||||
S3method(variable.names,xgb.Booster)
|
S3method(slice,xgb.DMatrix)
|
||||||
export("xgb.attr<-")
|
export("xgb.attr<-")
|
||||||
export("xgb.attributes<-")
|
export("xgb.attributes<-")
|
||||||
export("xgb.config<-")
|
export("xgb.config<-")
|
||||||
export("xgb.parameters<-")
|
export("xgb.parameters<-")
|
||||||
|
export(cb.cv.predict)
|
||||||
|
export(cb.early.stop)
|
||||||
|
export(cb.evaluation.log)
|
||||||
|
export(cb.gblinear.history)
|
||||||
|
export(cb.print.evaluation)
|
||||||
|
export(cb.reset.parameters)
|
||||||
|
export(cb.save.model)
|
||||||
export(getinfo)
|
export(getinfo)
|
||||||
export(setinfo)
|
export(setinfo)
|
||||||
export(xgb.Callback)
|
export(slice)
|
||||||
|
export(xgb.Booster.complete)
|
||||||
export(xgb.DMatrix)
|
export(xgb.DMatrix)
|
||||||
export(xgb.DMatrix.hasinfo)
|
|
||||||
export(xgb.DMatrix.save)
|
export(xgb.DMatrix.save)
|
||||||
export(xgb.DataBatch)
|
|
||||||
export(xgb.DataIter)
|
|
||||||
export(xgb.ExternalDMatrix)
|
|
||||||
export(xgb.QuantileDMatrix)
|
|
||||||
export(xgb.QuantileDMatrix.from_iterator)
|
|
||||||
export(xgb.attr)
|
export(xgb.attr)
|
||||||
export(xgb.attributes)
|
export(xgb.attributes)
|
||||||
export(xgb.cb.cv.predict)
|
|
||||||
export(xgb.cb.early.stop)
|
|
||||||
export(xgb.cb.evaluation.log)
|
|
||||||
export(xgb.cb.gblinear.history)
|
|
||||||
export(xgb.cb.print.evaluation)
|
|
||||||
export(xgb.cb.reset.parameters)
|
|
||||||
export(xgb.cb.save.model)
|
|
||||||
export(xgb.config)
|
export(xgb.config)
|
||||||
export(xgb.copy.Booster)
|
|
||||||
export(xgb.create.features)
|
export(xgb.create.features)
|
||||||
export(xgb.cv)
|
export(xgb.cv)
|
||||||
export(xgb.dump)
|
export(xgb.dump)
|
||||||
export(xgb.gblinear.history)
|
export(xgb.gblinear.history)
|
||||||
export(xgb.get.DMatrix.data)
|
|
||||||
export(xgb.get.DMatrix.num.non.missing)
|
|
||||||
export(xgb.get.DMatrix.qcut)
|
|
||||||
export(xgb.get.config)
|
export(xgb.get.config)
|
||||||
export(xgb.get.num.boosted.rounds)
|
|
||||||
export(xgb.ggplot.deepness)
|
export(xgb.ggplot.deepness)
|
||||||
export(xgb.ggplot.importance)
|
export(xgb.ggplot.importance)
|
||||||
export(xgb.ggplot.shap.summary)
|
export(xgb.ggplot.shap.summary)
|
||||||
export(xgb.importance)
|
export(xgb.importance)
|
||||||
export(xgb.is.same.Booster)
|
|
||||||
export(xgb.load)
|
export(xgb.load)
|
||||||
export(xgb.load.raw)
|
export(xgb.load.raw)
|
||||||
export(xgb.model.dt.tree)
|
export(xgb.model.dt.tree)
|
||||||
@ -67,16 +52,19 @@ export(xgb.plot.shap.summary)
|
|||||||
export(xgb.plot.tree)
|
export(xgb.plot.tree)
|
||||||
export(xgb.save)
|
export(xgb.save)
|
||||||
export(xgb.save.raw)
|
export(xgb.save.raw)
|
||||||
|
export(xgb.serialize)
|
||||||
export(xgb.set.config)
|
export(xgb.set.config)
|
||||||
export(xgb.slice.Booster)
|
|
||||||
export(xgb.slice.DMatrix)
|
|
||||||
export(xgb.train)
|
export(xgb.train)
|
||||||
|
export(xgb.unserialize)
|
||||||
export(xgboost)
|
export(xgboost)
|
||||||
import(methods)
|
import(methods)
|
||||||
importClassesFrom(Matrix,CsparseMatrix)
|
|
||||||
importClassesFrom(Matrix,dgCMatrix)
|
importClassesFrom(Matrix,dgCMatrix)
|
||||||
importClassesFrom(Matrix,dgRMatrix)
|
importClassesFrom(Matrix,dgeMatrix)
|
||||||
|
importFrom(Matrix,colSums)
|
||||||
importFrom(Matrix,sparse.model.matrix)
|
importFrom(Matrix,sparse.model.matrix)
|
||||||
|
importFrom(Matrix,sparseMatrix)
|
||||||
|
importFrom(Matrix,sparseVector)
|
||||||
|
importFrom(Matrix,t)
|
||||||
importFrom(data.table,":=")
|
importFrom(data.table,":=")
|
||||||
importFrom(data.table,as.data.table)
|
importFrom(data.table,as.data.table)
|
||||||
importFrom(data.table,data.table)
|
importFrom(data.table,data.table)
|
||||||
@ -94,12 +82,8 @@ importFrom(graphics,points)
|
|||||||
importFrom(graphics,title)
|
importFrom(graphics,title)
|
||||||
importFrom(jsonlite,fromJSON)
|
importFrom(jsonlite,fromJSON)
|
||||||
importFrom(jsonlite,toJSON)
|
importFrom(jsonlite,toJSON)
|
||||||
importFrom(methods,new)
|
|
||||||
importFrom(stats,coef)
|
|
||||||
importFrom(stats,median)
|
importFrom(stats,median)
|
||||||
importFrom(stats,predict)
|
importFrom(stats,predict)
|
||||||
importFrom(stats,sd)
|
|
||||||
importFrom(stats,variable.names)
|
|
||||||
importFrom(utils,head)
|
importFrom(utils,head)
|
||||||
importFrom(utils,object.size)
|
importFrom(utils,object.size)
|
||||||
importFrom(utils,str)
|
importFrom(utils,str)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -26,11 +26,6 @@ NVL <- function(x, val) {
|
|||||||
'multi:softprob', 'rank:pairwise', 'rank:ndcg', 'rank:map'))
|
'multi:softprob', 'rank:pairwise', 'rank:ndcg', 'rank:map'))
|
||||||
}
|
}
|
||||||
|
|
||||||
.RANKING_OBJECTIVES <- function() {
|
|
||||||
return(c('binary:logistic', 'binary:logitraw', 'binary:hinge', 'multi:softmax',
|
|
||||||
'multi:softprob'))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Low-level functions for boosting --------------------------------------------
|
# Low-level functions for boosting --------------------------------------------
|
||||||
@ -98,14 +93,6 @@ check.booster.params <- function(params, ...) {
|
|||||||
interaction_constraints <- sapply(params[['interaction_constraints']], function(x) paste0('[', paste(x, collapse = ','), ']'))
|
interaction_constraints <- sapply(params[['interaction_constraints']], function(x) paste0('[', paste(x, collapse = ','), ']'))
|
||||||
params[['interaction_constraints']] <- paste0('[', paste(interaction_constraints, collapse = ','), ']')
|
params[['interaction_constraints']] <- paste0('[', paste(interaction_constraints, collapse = ','), ']')
|
||||||
}
|
}
|
||||||
|
|
||||||
# for evaluation metrics, should generate multiple entries per metric
|
|
||||||
if (NROW(params[['eval_metric']]) > 1) {
|
|
||||||
eval_metrics <- as.list(params[["eval_metric"]])
|
|
||||||
names(eval_metrics) <- rep("eval_metric", length(eval_metrics))
|
|
||||||
params_without_ev_metrics <- within(params, rm("eval_metric"))
|
|
||||||
params <- c(params_without_ev_metrics, eval_metrics)
|
|
||||||
}
|
|
||||||
return(params)
|
return(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,49 +134,27 @@ check.custom.eval <- function(env = parent.frame()) {
|
|||||||
if (!is.null(env$feval) &&
|
if (!is.null(env$feval) &&
|
||||||
is.null(env$maximize) && (
|
is.null(env$maximize) && (
|
||||||
!is.null(env$early_stopping_rounds) ||
|
!is.null(env$early_stopping_rounds) ||
|
||||||
has.callbacks(env$callbacks, "early_stop")))
|
has.callbacks(env$callbacks, 'cb.early.stop')))
|
||||||
stop("Please set 'maximize' to indicate whether the evaluation metric needs to be maximized or not")
|
stop("Please set 'maximize' to indicate whether the evaluation metric needs to be maximized or not")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Update a booster handle for an iteration with dtrain data
|
# Update a booster handle for an iteration with dtrain data
|
||||||
xgb.iter.update <- function(bst, dtrain, iter, obj) {
|
xgb.iter.update <- function(booster_handle, dtrain, iter, obj = NULL) {
|
||||||
|
if (!identical(class(booster_handle), "xgb.Booster.handle")) {
|
||||||
|
stop("booster_handle must be of xgb.Booster.handle class")
|
||||||
|
}
|
||||||
if (!inherits(dtrain, "xgb.DMatrix")) {
|
if (!inherits(dtrain, "xgb.DMatrix")) {
|
||||||
stop("dtrain must be of xgb.DMatrix class")
|
stop("dtrain must be of xgb.DMatrix class")
|
||||||
}
|
}
|
||||||
handle <- xgb.get.handle(bst)
|
|
||||||
|
|
||||||
if (is.null(obj)) {
|
if (is.null(obj)) {
|
||||||
.Call(XGBoosterUpdateOneIter_R, handle, as.integer(iter), dtrain)
|
.Call(XGBoosterUpdateOneIter_R, booster_handle, as.integer(iter), dtrain)
|
||||||
} else {
|
} else {
|
||||||
pred <- predict(
|
pred <- predict(booster_handle, dtrain, outputmargin = TRUE, training = TRUE,
|
||||||
bst,
|
ntreelimit = 0)
|
||||||
dtrain,
|
|
||||||
outputmargin = TRUE,
|
|
||||||
training = TRUE,
|
|
||||||
reshape = TRUE
|
|
||||||
)
|
|
||||||
gpair <- obj(pred, dtrain)
|
gpair <- obj(pred, dtrain)
|
||||||
n_samples <- dim(dtrain)[1]
|
.Call(XGBoosterBoostOneIter_R, booster_handle, dtrain, gpair$grad, gpair$hess)
|
||||||
grad <- gpair$grad
|
|
||||||
hess <- gpair$hess
|
|
||||||
|
|
||||||
if ((is.matrix(grad) && dim(grad)[1] != n_samples) ||
|
|
||||||
(is.vector(grad) && length(grad) != n_samples) ||
|
|
||||||
(is.vector(grad) != is.vector(hess))) {
|
|
||||||
warning(paste(
|
|
||||||
"Since 2.1.0, the shape of the gradient and hessian is required to be ",
|
|
||||||
"(n_samples, n_targets) or (n_samples, n_classes). Will reshape assuming ",
|
|
||||||
"column-major order.",
|
|
||||||
sep = ""
|
|
||||||
))
|
|
||||||
grad <- matrix(grad, nrow = n_samples)
|
|
||||||
hess <- matrix(hess, nrow = n_samples)
|
|
||||||
}
|
|
||||||
|
|
||||||
.Call(
|
|
||||||
XGBoosterTrainOneIter_R, handle, dtrain, iter, grad, hess
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return(TRUE)
|
return(TRUE)
|
||||||
}
|
}
|
||||||
@ -198,22 +163,23 @@ xgb.iter.update <- function(bst, dtrain, iter, obj) {
|
|||||||
# Evaluate one iteration.
|
# Evaluate one iteration.
|
||||||
# Returns a named vector of evaluation metrics
|
# Returns a named vector of evaluation metrics
|
||||||
# with the names in a 'datasetname-metricname' format.
|
# with the names in a 'datasetname-metricname' format.
|
||||||
xgb.iter.eval <- function(bst, evals, iter, feval) {
|
xgb.iter.eval <- function(booster_handle, watchlist, iter, feval = NULL) {
|
||||||
handle <- xgb.get.handle(bst)
|
if (!identical(class(booster_handle), "xgb.Booster.handle"))
|
||||||
|
stop("class of booster_handle must be xgb.Booster.handle")
|
||||||
|
|
||||||
if (length(evals) == 0)
|
if (length(watchlist) == 0)
|
||||||
return(NULL)
|
return(NULL)
|
||||||
|
|
||||||
evnames <- names(evals)
|
evnames <- names(watchlist)
|
||||||
if (is.null(feval)) {
|
if (is.null(feval)) {
|
||||||
msg <- .Call(XGBoosterEvalOneIter_R, handle, as.integer(iter), evals, as.list(evnames))
|
msg <- .Call(XGBoosterEvalOneIter_R, booster_handle, as.integer(iter), watchlist, as.list(evnames))
|
||||||
mat <- matrix(strsplit(msg, '\\s+|:')[[1]][-1], nrow = 2)
|
mat <- matrix(strsplit(msg, '\\s+|:')[[1]][-1], nrow = 2)
|
||||||
res <- structure(as.numeric(mat[2, ]), names = mat[1, ])
|
res <- structure(as.numeric(mat[2, ]), names = mat[1, ])
|
||||||
} else {
|
} else {
|
||||||
res <- sapply(seq_along(evals), function(j) {
|
res <- sapply(seq_along(watchlist), function(j) {
|
||||||
w <- evals[[j]]
|
w <- watchlist[[j]]
|
||||||
## predict using all trees
|
## predict using all trees
|
||||||
preds <- predict(bst, w, outputmargin = TRUE, iterationrange = "all")
|
preds <- predict(booster_handle, w, outputmargin = TRUE, iterationrange = c(1, 1))
|
||||||
eval_res <- feval(preds, w)
|
eval_res <- feval(preds, w)
|
||||||
out <- eval_res$value
|
out <- eval_res$value
|
||||||
names(out) <- paste0(evnames[j], "-", eval_res$metric)
|
names(out) <- paste0(evnames[j], "-", eval_res$metric)
|
||||||
@ -240,45 +206,35 @@ convert.labels <- function(labels, objective_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Generates random (stratified if needed) CV folds
|
# Generates random (stratified if needed) CV folds
|
||||||
generate.cv.folds <- function(nfold, nrows, stratified, label, group, params) {
|
generate.cv.folds <- function(nfold, nrows, stratified, label, params) {
|
||||||
if (NROW(group)) {
|
|
||||||
if (stratified) {
|
|
||||||
warning(
|
|
||||||
paste0(
|
|
||||||
"Stratified splitting is not supported when using 'group' attribute.",
|
|
||||||
" Will use unstratified splitting."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return(generate.group.folds(nfold, group))
|
|
||||||
}
|
|
||||||
objective <- params$objective
|
|
||||||
if (!is.character(objective)) {
|
|
||||||
warning("Will use unstratified splitting (custom objective used)")
|
|
||||||
stratified <- FALSE
|
|
||||||
}
|
|
||||||
# cannot stratify if label is NULL
|
|
||||||
if (stratified && is.null(label)) {
|
|
||||||
warning("Will use unstratified splitting (no 'labels' available)")
|
|
||||||
stratified <- FALSE
|
|
||||||
}
|
|
||||||
|
|
||||||
# cannot do it for rank
|
# cannot do it for rank
|
||||||
|
objective <- params$objective
|
||||||
if (is.character(objective) && strtrim(objective, 5) == 'rank:') {
|
if (is.character(objective) && strtrim(objective, 5) == 'rank:') {
|
||||||
stop("\n\tAutomatic generation of CV-folds is not implemented for ranking without 'group' field!\n",
|
stop("\n\tAutomatic generation of CV-folds is not implemented for ranking!\n",
|
||||||
"\tConsider providing pre-computed CV-folds through the 'folds=' parameter.\n")
|
"\tConsider providing pre-computed CV-folds through the 'folds=' parameter.\n")
|
||||||
}
|
}
|
||||||
# shuffle
|
# shuffle
|
||||||
rnd_idx <- sample.int(nrows)
|
rnd_idx <- sample.int(nrows)
|
||||||
if (stratified && length(label) == length(rnd_idx)) {
|
if (stratified &&
|
||||||
|
length(label) == length(rnd_idx)) {
|
||||||
y <- label[rnd_idx]
|
y <- label[rnd_idx]
|
||||||
|
# WARNING: some heuristic logic is employed to identify classification setting!
|
||||||
# - For classification, need to convert y labels to factor before making the folds,
|
# - For classification, need to convert y labels to factor before making the folds,
|
||||||
# and then do stratification by factor levels.
|
# and then do stratification by factor levels.
|
||||||
# - For regression, leave y numeric and do stratification by quantiles.
|
# - For regression, leave y numeric and do stratification by quantiles.
|
||||||
if (is.character(objective)) {
|
if (is.character(objective)) {
|
||||||
y <- convert.labels(y, objective)
|
y <- convert.labels(y, params$objective)
|
||||||
|
} else {
|
||||||
|
# If no 'objective' given in params, it means that user either wants to
|
||||||
|
# use the default 'reg:squarederror' objective or has provided a custom
|
||||||
|
# obj function. Here, assume classification setting when y has 5 or less
|
||||||
|
# unique values:
|
||||||
|
if (length(unique(y)) <= 5) {
|
||||||
|
y <- factor(y)
|
||||||
}
|
}
|
||||||
folds <- xgb.createFolds(y = y, k = nfold)
|
}
|
||||||
|
folds <- xgb.createFolds(y, nfold)
|
||||||
} else {
|
} else {
|
||||||
# make simple non-stratified folds
|
# make simple non-stratified folds
|
||||||
kstep <- length(rnd_idx) %/% nfold
|
kstep <- length(rnd_idx) %/% nfold
|
||||||
@ -292,33 +248,10 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, group, params) {
|
|||||||
return(folds)
|
return(folds)
|
||||||
}
|
}
|
||||||
|
|
||||||
generate.group.folds <- function(nfold, group) {
|
|
||||||
ngroups <- length(group) - 1
|
|
||||||
if (ngroups < nfold) {
|
|
||||||
stop("DMatrix has fewer groups than folds.")
|
|
||||||
}
|
|
||||||
seq_groups <- seq_len(ngroups)
|
|
||||||
indices <- lapply(seq_groups, function(gr) seq(group[gr] + 1, group[gr + 1]))
|
|
||||||
assignments <- base::split(seq_groups, as.integer(seq_groups %% nfold))
|
|
||||||
assignments <- unname(assignments)
|
|
||||||
|
|
||||||
out <- vector("list", nfold)
|
|
||||||
randomized_groups <- sample(ngroups)
|
|
||||||
for (idx in seq_len(nfold)) {
|
|
||||||
groups_idx_test <- randomized_groups[assignments[[idx]]]
|
|
||||||
groups_test <- indices[groups_idx_test]
|
|
||||||
idx_test <- unlist(groups_test)
|
|
||||||
attributes(idx_test)$group_test <- lengths(groups_test)
|
|
||||||
attributes(idx_test)$group_train <- lengths(indices[-groups_idx_test])
|
|
||||||
out[[idx]] <- idx_test
|
|
||||||
}
|
|
||||||
return(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
# Creates CV folds stratified by the values of y.
|
# Creates CV folds stratified by the values of y.
|
||||||
# It was borrowed from caret::createFolds and simplified
|
# It was borrowed from caret::createFolds and simplified
|
||||||
# by always returning an unnamed list of fold indices.
|
# by always returning an unnamed list of fold indices.
|
||||||
xgb.createFolds <- function(y, k) {
|
xgb.createFolds <- function(y, k = 10) {
|
||||||
if (is.numeric(y)) {
|
if (is.numeric(y)) {
|
||||||
## Group the numeric data based on their magnitudes
|
## Group the numeric data based on their magnitudes
|
||||||
## and sample within those groups.
|
## and sample within those groups.
|
||||||
@ -387,45 +320,16 @@ xgb.createFolds <- function(y, k) {
|
|||||||
#' @name xgboost-deprecated
|
#' @name xgboost-deprecated
|
||||||
NULL
|
NULL
|
||||||
|
|
||||||
#' @title Model Serialization and Compatibility
|
#' Do not use \code{\link[base]{saveRDS}} or \code{\link[base]{save}} for long-term archival of
|
||||||
#' @description
|
#' models. Instead, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}}.
|
||||||
#'
|
#'
|
||||||
#' When it comes to serializing XGBoost models, it's possible to use R serializers such as
|
#' It is a common practice to use the built-in \code{\link[base]{saveRDS}} function (or
|
||||||
#' \link{save} or \link{saveRDS} to serialize an XGBoost R model, but XGBoost also provides
|
#' \code{\link[base]{save}}) to persist R objects to the disk. While it is possible to persist
|
||||||
#' its own serializers with better compatibility guarantees, which allow loading
|
#' \code{xgb.Booster} objects using \code{\link[base]{saveRDS}}, it is not advisable to do so if
|
||||||
#' said models in other language bindings of XGBoost.
|
#' the model is to be accessed in the future. If you train a model with the current version of
|
||||||
#'
|
#' XGBoost and persist it with \code{\link[base]{saveRDS}}, the model is not guaranteed to be
|
||||||
#' Note that an `xgb.Booster` object, outside of its core components, might also keep:\itemize{
|
#' accessible in later releases of XGBoost. To ensure that your model can be accessed in future
|
||||||
#' \item Additional model configuration (accessible through \link{xgb.config}),
|
#' releases of XGBoost, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}} instead.
|
||||||
#' which includes model fitting parameters like `max_depth` and runtime parameters like `nthread`.
|
|
||||||
#' These are not necessarily useful for prediction/importance/plotting.
|
|
||||||
#' \item Additional R-specific attributes - e.g. results of callbacks, such as evaluation logs,
|
|
||||||
#' which are kept as a `data.table` object, accessible through `attributes(model)$evaluation_log`
|
|
||||||
#' if present.
|
|
||||||
#' }
|
|
||||||
#'
|
|
||||||
#' The first one (configurations) does not have the same compatibility guarantees as
|
|
||||||
#' the model itself, including attributes that are set and accessed through \link{xgb.attributes} - that is, such configuration
|
|
||||||
#' might be lost after loading the booster in a different XGBoost version, regardless of the
|
|
||||||
#' serializer that was used. These are saved when using \link{saveRDS}, but will be discarded
|
|
||||||
#' if loaded into an incompatible XGBoost version. They are not saved when using XGBoost's
|
|
||||||
#' serializers from its public interface including \link{xgb.save} and \link{xgb.save.raw}.
|
|
||||||
#'
|
|
||||||
#' The second ones (R attributes) are not part of the standard XGBoost model structure, and thus are
|
|
||||||
#' not saved when using XGBoost's own serializers. These attributes are only used for informational
|
|
||||||
#' purposes, such as keeping track of evaluation metrics as the model was fit, or saving the R
|
|
||||||
#' call that produced the model, but are otherwise not used for prediction / importance / plotting / etc.
|
|
||||||
#' These R attributes are only preserved when using R's serializers.
|
|
||||||
#'
|
|
||||||
#' Note that XGBoost models in R starting from version `2.1.0` and onwards, and XGBoost models
|
|
||||||
#' before version `2.1.0`; have a very different R object structure and are incompatible with
|
|
||||||
#' each other. Hence, models that were saved with R serializers live `saveRDS` or `save` before
|
|
||||||
#' version `2.1.0` will not work with latter `xgboost` versions and vice versa. Be aware that
|
|
||||||
#' the structure of R model objects could in theory change again in the future, so XGBoost's serializers
|
|
||||||
#' should be preferred for long-term storage.
|
|
||||||
#'
|
|
||||||
#' Furthermore, note that using the package `qs` for serialization will require version 0.26 or
|
|
||||||
#' higher of said package, and will have the same compatibility restrictions as R serializers.
|
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#' Use \code{\link{xgb.save}} to save the XGBoost model as a stand-alone file. You may opt into
|
#' Use \code{\link{xgb.save}} to save the XGBoost model as a stand-alone file. You may opt into
|
||||||
@ -438,29 +342,26 @@ NULL
|
|||||||
#' The \code{\link{xgb.save.raw}} function is useful if you'd like to persist the XGBoost model
|
#' The \code{\link{xgb.save.raw}} function is useful if you'd like to persist the XGBoost model
|
||||||
#' as part of another R object.
|
#' as part of another R object.
|
||||||
#'
|
#'
|
||||||
#' Use \link{saveRDS} if you require the R-specific attributes that a booster might have, such
|
#' Note: Do not use \code{\link{xgb.serialize}} to store models long-term. It persists not only the
|
||||||
#' as evaluation logs, but note that future compatibility of such objects is outside XGBoost's
|
#' model but also internal configurations and parameters, and its format is not stable across
|
||||||
#' control as it relies on R's serialization format (see e.g. the details section in
|
#' multiple XGBoost versions. Use \code{\link{xgb.serialize}} only for checkpointing.
|
||||||
#' \link{serialize} and \link{save} from base R).
|
|
||||||
#'
|
#'
|
||||||
#' For more details and explanation about model persistence and archival, consult the page
|
#' For more details and explanation about model persistence and archival, consult the page
|
||||||
#' \url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}.
|
#' \url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' bst <- xgb.train(data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2,
|
||||||
#' max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
|
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
#' objective = "binary:logistic")
|
|
||||||
#'
|
#'
|
||||||
#' # Save as a stand-alone file; load it with xgb.load()
|
#' # Save as a stand-alone file; load it with xgb.load()
|
||||||
#' fname <- file.path(tempdir(), "xgb_model.ubj")
|
#' xgb.save(bst, 'xgb.model')
|
||||||
#' xgb.save(bst, fname)
|
#' bst2 <- xgb.load('xgb.model')
|
||||||
#' bst2 <- xgb.load(fname)
|
|
||||||
#'
|
#'
|
||||||
#' # Save as a stand-alone file (JSON); load it with xgb.load()
|
#' # Save as a stand-alone file (JSON); load it with xgb.load()
|
||||||
#' fname <- file.path(tempdir(), "xgb_model.json")
|
#' xgb.save(bst, 'xgb.model.json')
|
||||||
#' xgb.save(bst, fname)
|
#' bst2 <- xgb.load('xgb.model.json')
|
||||||
#' bst2 <- xgb.load(fname)
|
#' if (file.exists('xgb.model.json')) file.remove('xgb.model.json')
|
||||||
#'
|
#'
|
||||||
#' # Save as a raw byte vector; load it with xgb.load.raw()
|
#' # Save as a raw byte vector; load it with xgb.load.raw()
|
||||||
#' xgb_bytes <- xgb.save.raw(bst)
|
#' xgb_bytes <- xgb.save.raw(bst)
|
||||||
@ -471,12 +372,12 @@ NULL
|
|||||||
#' # Persist the R object. Here, saveRDS() is okay, since it doesn't persist
|
#' # Persist the R object. Here, saveRDS() is okay, since it doesn't persist
|
||||||
#' # xgb.Booster directly. What's being persisted is the future-proof byte representation
|
#' # xgb.Booster directly. What's being persisted is the future-proof byte representation
|
||||||
#' # as given by xgb.save.raw().
|
#' # as given by xgb.save.raw().
|
||||||
#' fname <- file.path(tempdir(), "my_object.Rds")
|
#' saveRDS(obj, 'my_object.rds')
|
||||||
#' saveRDS(obj, fname)
|
|
||||||
#' # Read back the R object
|
#' # Read back the R object
|
||||||
#' obj2 <- readRDS(fname)
|
#' obj2 <- readRDS('my_object.rds')
|
||||||
#' # Re-construct xgb.Booster object from the bytes
|
#' # Re-construct xgb.Booster object from the bytes
|
||||||
#' bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
|
#' bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
|
||||||
|
#' if (file.exists('my_object.rds')) file.remove('my_object.rds')
|
||||||
#'
|
#'
|
||||||
#' @name a-compatibility-note-for-saveRDS-save
|
#' @name a-compatibility-note-for-saveRDS-save
|
||||||
NULL
|
NULL
|
||||||
@ -492,8 +393,7 @@ depr_par_lut <- matrix(c(
|
|||||||
'plot.height', 'plot_height',
|
'plot.height', 'plot_height',
|
||||||
'plot.width', 'plot_width',
|
'plot.width', 'plot_width',
|
||||||
'n_first_tree', 'trees',
|
'n_first_tree', 'trees',
|
||||||
'dummy', 'DUMMY',
|
'dummy', 'DUMMY'
|
||||||
'watchlist', 'evals'
|
|
||||||
), ncol = 2, byrow = TRUE)
|
), ncol = 2, byrow = TRUE)
|
||||||
colnames(depr_par_lut) <- c('old', 'new')
|
colnames(depr_par_lut) <- c('old', 'new')
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -6,12 +6,11 @@
|
|||||||
#' @param fname the name of the file to write.
|
#' @param fname the name of the file to write.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
#' fname <- file.path(tempdir(), "xgb.DMatrix.data")
|
#' xgb.DMatrix.save(dtrain, 'xgb.DMatrix.data')
|
||||||
#' xgb.DMatrix.save(dtrain, fname)
|
#' dtrain <- xgb.DMatrix('xgb.DMatrix.data')
|
||||||
#' dtrain <- xgb.DMatrix(fname)
|
#' if (file.exists('xgb.DMatrix.data')) file.remove('xgb.DMatrix.data')
|
||||||
#' @export
|
#' @export
|
||||||
xgb.DMatrix.save <- function(dmatrix, fname) {
|
xgb.DMatrix.save <- function(dmatrix, fname) {
|
||||||
if (typeof(fname) != "character")
|
if (typeof(fname) != "character")
|
||||||
|
|||||||
@ -4,14 +4,7 @@
|
|||||||
#' values of one or more global-scope parameters. Use \code{xgb.get.config} to fetch the current
|
#' values of one or more global-scope parameters. Use \code{xgb.get.config} to fetch the current
|
||||||
#' values of all global-scope parameters (listed in
|
#' values of all global-scope parameters (listed in
|
||||||
#' \url{https://xgboost.readthedocs.io/en/stable/parameter.html}).
|
#' \url{https://xgboost.readthedocs.io/en/stable/parameter.html}).
|
||||||
#' @details
|
|
||||||
#' Note that serialization-related functions might use a globally-configured number of threads,
|
|
||||||
#' which is managed by the system's OpenMP (OMP) configuration instead. Typically, XGBoost methods
|
|
||||||
#' accept an `nthreads` parameter, but some methods like `readRDS` might get executed before such
|
|
||||||
#' parameter can be supplied.
|
|
||||||
#'
|
#'
|
||||||
#' The number of OMP threads can in turn be configured for example through an environment variable
|
|
||||||
#' `OMP_NUM_THREADS` (needs to be set before R is started), or through `RhpcBLASctl::omp_set_num_threads`.
|
|
||||||
#' @rdname xgbConfig
|
#' @rdname xgbConfig
|
||||||
#' @title Set and get global configuration
|
#' @title Set and get global configuration
|
||||||
#' @name xgb.set.config, xgb.get.config
|
#' @name xgb.set.config, xgb.get.config
|
||||||
|
|||||||
@ -51,7 +51,7 @@
|
|||||||
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
#' dtest <- with(agaricus.test, xgb.DMatrix(data, label = label, nthread = 2))
|
#' dtest <- with(agaricus.test, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
#'
|
#'
|
||||||
#' param <- list(max_depth=2, eta=1, objective='binary:logistic')
|
#' param <- list(max_depth=2, eta=1, silent=1, objective='binary:logistic')
|
||||||
#' nrounds = 4
|
#' nrounds = 4
|
||||||
#'
|
#'
|
||||||
#' bst = xgb.train(params = param, data = dtrain, nrounds = nrounds, nthread = 2)
|
#' bst = xgb.train(params = param, data = dtrain, nrounds = nrounds, nthread = 2)
|
||||||
@ -71,6 +71,7 @@
|
|||||||
#' new.dtest <- xgb.DMatrix(
|
#' new.dtest <- xgb.DMatrix(
|
||||||
#' data = new.features.test, label = agaricus.test$label, nthread = 2
|
#' data = new.features.test, label = agaricus.test$label, nthread = 2
|
||||||
#' )
|
#' )
|
||||||
|
#' watchlist <- list(train = new.dtrain)
|
||||||
#' bst <- xgb.train(params = param, data = new.dtrain, nrounds = nrounds, nthread = 2)
|
#' bst <- xgb.train(params = param, data = new.dtrain, nrounds = nrounds, nthread = 2)
|
||||||
#'
|
#'
|
||||||
#' # Model accuracy with new features
|
#' # Model accuracy with new features
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#' Cross Validation
|
#' Cross Validation
|
||||||
#'
|
#'
|
||||||
#' The cross validation function of xgboost.
|
#' The cross validation function of xgboost
|
||||||
#'
|
#'
|
||||||
#' @param params the list of parameters. The complete list of parameters is
|
#' @param params the list of parameters. The complete list of parameters is
|
||||||
#' available in the \href{http://xgboost.readthedocs.io/en/latest/parameter.html}{online documentation}. Below
|
#' available in the \href{http://xgboost.readthedocs.io/en/latest/parameter.html}{online documentation}. Below
|
||||||
@ -19,19 +19,15 @@
|
|||||||
#'
|
#'
|
||||||
#' See \code{\link{xgb.train}} for further details.
|
#' See \code{\link{xgb.train}} for further details.
|
||||||
#' See also demo/ for walkthrough example in R.
|
#' See also demo/ for walkthrough example in R.
|
||||||
#'
|
#' @param data takes an \code{xgb.DMatrix}, \code{matrix}, or \code{dgCMatrix} as the input.
|
||||||
#' Note that, while `params` accepts a `seed` entry and will use such parameter for model training if
|
|
||||||
#' supplied, this seed is not used for creation of train-test splits, which instead rely on R's own RNG
|
|
||||||
#' system - thus, for reproducible results, one needs to call the `set.seed` function beforehand.
|
|
||||||
#' @param data An `xgb.DMatrix` object, with corresponding fields like `label` or bounds as required
|
|
||||||
#' for model training by the objective.
|
|
||||||
#'
|
|
||||||
#' Note that only the basic `xgb.DMatrix` class is supported - variants such as `xgb.QuantileDMatrix`
|
|
||||||
#' or `xgb.ExternalDMatrix` are not supported here.
|
|
||||||
#' @param nrounds the max number of iterations
|
#' @param nrounds the max number of iterations
|
||||||
#' @param nfold the original dataset is randomly partitioned into \code{nfold} equal size subsamples.
|
#' @param nfold the original dataset is randomly partitioned into \code{nfold} equal size subsamples.
|
||||||
|
#' @param label vector of response values. Should be provided only when data is an R-matrix.
|
||||||
|
#' @param missing is only used when input is a dense matrix. By default is set to NA, which means
|
||||||
|
#' that NA values should be considered as 'missing' by the algorithm.
|
||||||
|
#' Sometimes, 0 or other extreme value might be used to represent missing values.
|
||||||
#' @param prediction A logical value indicating whether to return the test fold predictions
|
#' @param prediction A logical value indicating whether to return the test fold predictions
|
||||||
#' from each CV model. This parameter engages the \code{\link{xgb.cb.cv.predict}} callback.
|
#' from each CV model. This parameter engages the \code{\link{cb.cv.predict}} callback.
|
||||||
#' @param showsd \code{boolean}, whether to show standard deviation of cross validation
|
#' @param showsd \code{boolean}, whether to show standard deviation of cross validation
|
||||||
#' @param metrics, list of evaluation metrics to be used in cross validation,
|
#' @param metrics, list of evaluation metrics to be used in cross validation,
|
||||||
#' when it is not specified, the evaluation metric is chosen according to objective function.
|
#' when it is not specified, the evaluation metric is chosen according to objective function.
|
||||||
@ -51,44 +47,27 @@
|
|||||||
#' @param feval customized evaluation function. Returns
|
#' @param feval customized evaluation function. Returns
|
||||||
#' \code{list(metric='metric-name', value='metric-value')} with given
|
#' \code{list(metric='metric-name', value='metric-value')} with given
|
||||||
#' prediction and dtrain.
|
#' prediction and dtrain.
|
||||||
#' @param stratified A \code{boolean} indicating whether sampling of folds should be stratified
|
#' @param stratified a \code{boolean} indicating whether sampling of folds should be stratified
|
||||||
#' by the values of outcome labels. For real-valued labels in regression objectives,
|
#' by the values of outcome labels.
|
||||||
#' stratification will be done by discretizing the labels into up to 5 buckets beforehand.
|
|
||||||
#'
|
|
||||||
#' If passing "auto", will be set to `TRUE` if the objective in `params` is a classification
|
|
||||||
#' objective (from XGBoost's built-in objectives, doesn't apply to custom ones), and to
|
|
||||||
#' `FALSE` otherwise.
|
|
||||||
#'
|
|
||||||
#' This parameter is ignored when `data` has a `group` field - in such case, the splitting
|
|
||||||
#' will be based on whole groups (note that this might make the folds have different sizes).
|
|
||||||
#'
|
|
||||||
#' Value `TRUE` here is \bold{not} supported for custom objectives.
|
|
||||||
#' @param folds \code{list} provides a possibility to use a list of pre-defined CV folds
|
#' @param folds \code{list} provides a possibility to use a list of pre-defined CV folds
|
||||||
#' (each element must be a vector of test fold's indices). When folds are supplied,
|
#' (each element must be a vector of test fold's indices). When folds are supplied,
|
||||||
#' the \code{nfold} and \code{stratified} parameters are ignored.
|
#' the \code{nfold} and \code{stratified} parameters are ignored.
|
||||||
#'
|
|
||||||
#' If `data` has a `group` field and the objective requires this field, each fold (list element)
|
|
||||||
#' must additionally have two attributes (retrievable through \link{attributes}) named `group_test`
|
|
||||||
#' and `group_train`, which should hold the `group` to assign through \link{setinfo.xgb.DMatrix} to
|
|
||||||
#' the resulting DMatrices.
|
|
||||||
#' @param train_folds \code{list} list specifying which indicies to use for training. If \code{NULL}
|
#' @param train_folds \code{list} list specifying which indicies to use for training. If \code{NULL}
|
||||||
#' (the default) all indices not specified in \code{folds} will be used for training.
|
#' (the default) all indices not specified in \code{folds} will be used for training.
|
||||||
#'
|
|
||||||
#' This is not supported when `data` has `group` field.
|
|
||||||
#' @param verbose \code{boolean}, print the statistics during the process
|
#' @param verbose \code{boolean}, print the statistics during the process
|
||||||
#' @param print_every_n Print each n-th iteration evaluation messages when \code{verbose>0}.
|
#' @param print_every_n Print each n-th iteration evaluation messages when \code{verbose>0}.
|
||||||
#' Default is 1 which means all messages are printed. This parameter is passed to the
|
#' Default is 1 which means all messages are printed. This parameter is passed to the
|
||||||
#' \code{\link{xgb.cb.print.evaluation}} callback.
|
#' \code{\link{cb.print.evaluation}} callback.
|
||||||
#' @param early_stopping_rounds If \code{NULL}, the early stopping function is not triggered.
|
#' @param early_stopping_rounds If \code{NULL}, the early stopping function is not triggered.
|
||||||
#' If set to an integer \code{k}, training with a validation set will stop if the performance
|
#' If set to an integer \code{k}, training with a validation set will stop if the performance
|
||||||
#' doesn't improve for \code{k} rounds.
|
#' doesn't improve for \code{k} rounds.
|
||||||
#' Setting this parameter engages the \code{\link{xgb.cb.early.stop}} callback.
|
#' Setting this parameter engages the \code{\link{cb.early.stop}} callback.
|
||||||
#' @param maximize If \code{feval} and \code{early_stopping_rounds} are set,
|
#' @param maximize If \code{feval} and \code{early_stopping_rounds} are set,
|
||||||
#' then this parameter must be set as well.
|
#' then this parameter must be set as well.
|
||||||
#' When it is \code{TRUE}, it means the larger the evaluation score the better.
|
#' When it is \code{TRUE}, it means the larger the evaluation score the better.
|
||||||
#' This parameter is passed to the \code{\link{xgb.cb.early.stop}} callback.
|
#' This parameter is passed to the \code{\link{cb.early.stop}} callback.
|
||||||
#' @param callbacks a list of callback functions to perform various task during boosting.
|
#' @param callbacks a list of callback functions to perform various task during boosting.
|
||||||
#' See \code{\link{xgb.Callback}}. Some of the callbacks are automatically created depending on the
|
#' See \code{\link{callbacks}}. Some of the callbacks are automatically created depending on the
|
||||||
#' parameters' values. User can provide either existing or their own callback methods in order
|
#' parameters' values. User can provide either existing or their own callback methods in order
|
||||||
#' to customize the training process.
|
#' to customize the training process.
|
||||||
#' @param ... other parameters to pass to \code{params}.
|
#' @param ... other parameters to pass to \code{params}.
|
||||||
@ -111,25 +90,26 @@
|
|||||||
#' \itemize{
|
#' \itemize{
|
||||||
#' \item \code{call} a function call.
|
#' \item \code{call} a function call.
|
||||||
#' \item \code{params} parameters that were passed to the xgboost library. Note that it does not
|
#' \item \code{params} parameters that were passed to the xgboost library. Note that it does not
|
||||||
#' capture parameters changed by the \code{\link{xgb.cb.reset.parameters}} callback.
|
#' capture parameters changed by the \code{\link{cb.reset.parameters}} callback.
|
||||||
|
#' \item \code{callbacks} callback functions that were either automatically assigned or
|
||||||
|
#' explicitly passed.
|
||||||
#' \item \code{evaluation_log} evaluation history stored as a \code{data.table} with the
|
#' \item \code{evaluation_log} evaluation history stored as a \code{data.table} with the
|
||||||
#' first column corresponding to iteration number and the rest corresponding to the
|
#' first column corresponding to iteration number and the rest corresponding to the
|
||||||
#' CV-based evaluation means and standard deviations for the training and test CV-sets.
|
#' CV-based evaluation means and standard deviations for the training and test CV-sets.
|
||||||
#' It is created by the \code{\link{xgb.cb.evaluation.log}} callback.
|
#' It is created by the \code{\link{cb.evaluation.log}} callback.
|
||||||
#' \item \code{niter} number of boosting iterations.
|
#' \item \code{niter} number of boosting iterations.
|
||||||
#' \item \code{nfeatures} number of features in training data.
|
#' \item \code{nfeatures} number of features in training data.
|
||||||
#' \item \code{folds} the list of CV folds' indices - either those passed through the \code{folds}
|
#' \item \code{folds} the list of CV folds' indices - either those passed through the \code{folds}
|
||||||
#' parameter or randomly generated.
|
#' parameter or randomly generated.
|
||||||
#' \item \code{best_iteration} iteration number with the best evaluation metric value
|
#' \item \code{best_iteration} iteration number with the best evaluation metric value
|
||||||
#' (only available with early stopping).
|
#' (only available with early stopping).
|
||||||
|
#' \item \code{best_ntreelimit} and the \code{ntreelimit} Deprecated attributes, use \code{best_iteration} instead.
|
||||||
|
#' \item \code{pred} CV prediction values available when \code{prediction} is set.
|
||||||
|
#' It is either vector or matrix (see \code{\link{cb.cv.predict}}).
|
||||||
|
#' \item \code{models} a list of the CV folds' models. It is only available with the explicit
|
||||||
|
#' setting of the \code{cb.cv.predict(save_models = TRUE)} callback.
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' Plus other potential elements that are the result of callbacks, such as a list `cv_predict` with
|
|
||||||
#' a sub-element `pred` when passing `prediction = TRUE`, which is added by the \link{xgb.cb.cv.predict}
|
|
||||||
#' callback (note that one can also pass it manually under `callbacks` with different settings,
|
|
||||||
#' such as saving also the models created during cross validation); or a list `early_stop` which
|
|
||||||
#' will contain elements such as `best_iteration` when using the early stopping callback (\link{xgb.cb.early.stop}).
|
|
||||||
#'
|
|
||||||
#' @examples
|
#' @examples
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
@ -139,17 +119,13 @@
|
|||||||
#' print(cv, verbose=TRUE)
|
#' print(cv, verbose=TRUE)
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.cv <- function(params = list(), data, nrounds, nfold,
|
xgb.cv <- function(params = list(), data, nrounds, nfold, label = NULL, missing = NA,
|
||||||
prediction = FALSE, showsd = TRUE, metrics = list(),
|
prediction = FALSE, showsd = TRUE, metrics = list(),
|
||||||
obj = NULL, feval = NULL, stratified = "auto", folds = NULL, train_folds = NULL,
|
obj = NULL, feval = NULL, stratified = TRUE, folds = NULL, train_folds = NULL,
|
||||||
verbose = TRUE, print_every_n = 1L,
|
verbose = TRUE, print_every_n = 1L,
|
||||||
early_stopping_rounds = NULL, maximize = NULL, callbacks = list(), ...) {
|
early_stopping_rounds = NULL, maximize = NULL, callbacks = list(), ...) {
|
||||||
|
|
||||||
check.deprecation(...)
|
check.deprecation(...)
|
||||||
stopifnot(inherits(data, "xgb.DMatrix"))
|
|
||||||
if (inherits(data, "xgb.DMatrix") && .Call(XGCheckNullPtr_R, data)) {
|
|
||||||
stop("'data' is an invalid 'xgb.DMatrix' object. Must be constructed again.")
|
|
||||||
}
|
|
||||||
|
|
||||||
params <- check.booster.params(params, ...)
|
params <- check.booster.params(params, ...)
|
||||||
# TODO: should we deprecate the redundant 'metrics' parameter?
|
# TODO: should we deprecate the redundant 'metrics' parameter?
|
||||||
@ -159,22 +135,19 @@ xgb.cv <- function(params = list(), data, nrounds, nfold,
|
|||||||
check.custom.obj()
|
check.custom.obj()
|
||||||
check.custom.eval()
|
check.custom.eval()
|
||||||
|
|
||||||
if (stratified == "auto") {
|
#if (is.null(params[['eval_metric']]) && is.null(feval))
|
||||||
if (is.character(params$objective)) {
|
# stop("Either 'eval_metric' or 'feval' must be provided for CV")
|
||||||
stratified <- (
|
|
||||||
(params$objective %in% .CLASSIFICATION_OBJECTIVES())
|
|
||||||
&& !(params$objective %in% .RANKING_OBJECTIVES())
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
stratified <- FALSE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check the labels and groups
|
# Check the labels
|
||||||
cv_label <- getinfo(data, "label")
|
if ((inherits(data, 'xgb.DMatrix') && is.null(getinfo(data, 'label'))) ||
|
||||||
cv_group <- getinfo(data, "group")
|
(!inherits(data, 'xgb.DMatrix') && is.null(label))) {
|
||||||
if (!is.null(train_folds) && NROW(cv_group)) {
|
stop("Labels must be provided for CV either through xgb.DMatrix, or through 'label=' when 'data' is matrix")
|
||||||
stop("'train_folds' is not supported for DMatrix object with 'group' field.")
|
} else if (inherits(data, 'xgb.DMatrix')) {
|
||||||
|
if (!is.null(label))
|
||||||
|
warning("xgb.cv: label will be ignored, since data is of type xgb.DMatrix")
|
||||||
|
cv_label <- getinfo(data, 'label')
|
||||||
|
} else {
|
||||||
|
cv_label <- label
|
||||||
}
|
}
|
||||||
|
|
||||||
# CV folds
|
# CV folds
|
||||||
@ -185,140 +158,98 @@ xgb.cv <- function(params = list(), data, nrounds, nfold,
|
|||||||
} else {
|
} else {
|
||||||
if (nfold <= 1)
|
if (nfold <= 1)
|
||||||
stop("'nfold' must be > 1")
|
stop("'nfold' must be > 1")
|
||||||
folds <- generate.cv.folds(nfold, nrow(data), stratified, cv_label, cv_group, params)
|
folds <- generate.cv.folds(nfold, nrow(data), stratified, cv_label, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Callbacks
|
# Potential TODO: sequential CV
|
||||||
tmp <- .process.callbacks(callbacks, is_cv = TRUE)
|
#if (strategy == 'sequential')
|
||||||
callbacks <- tmp$callbacks
|
# stop('Sequential CV strategy is not yet implemented')
|
||||||
cb_names <- tmp$cb_names
|
|
||||||
rm(tmp)
|
|
||||||
|
|
||||||
# Early stopping callback
|
|
||||||
if (!is.null(early_stopping_rounds) && !("early_stop" %in% cb_names)) {
|
|
||||||
callbacks <- add.callback(
|
|
||||||
callbacks,
|
|
||||||
xgb.cb.early.stop(
|
|
||||||
early_stopping_rounds,
|
|
||||||
maximize = maximize,
|
|
||||||
verbose = verbose
|
|
||||||
),
|
|
||||||
as_first_elt = TRUE
|
|
||||||
)
|
|
||||||
}
|
|
||||||
# verbosity & evaluation printing callback:
|
# verbosity & evaluation printing callback:
|
||||||
params <- c(params, list(silent = 1))
|
params <- c(params, list(silent = 1))
|
||||||
print_every_n <- max(as.integer(print_every_n), 1L)
|
print_every_n <- max(as.integer(print_every_n), 1L)
|
||||||
if (verbose && !("print_evaluation" %in% cb_names)) {
|
if (!has.callbacks(callbacks, 'cb.print.evaluation') && verbose) {
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.print.evaluation(print_every_n, showsd = showsd))
|
callbacks <- add.cb(callbacks, cb.print.evaluation(print_every_n, showsd = showsd))
|
||||||
}
|
}
|
||||||
# evaluation log callback: always is on in CV
|
# evaluation log callback: always is on in CV
|
||||||
if (!("evaluation_log" %in% cb_names)) {
|
evaluation_log <- list()
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.evaluation.log())
|
if (!has.callbacks(callbacks, 'cb.evaluation.log')) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.evaluation.log())
|
||||||
|
}
|
||||||
|
# Early stopping callback
|
||||||
|
stop_condition <- FALSE
|
||||||
|
if (!is.null(early_stopping_rounds) &&
|
||||||
|
!has.callbacks(callbacks, 'cb.early.stop')) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds,
|
||||||
|
maximize = maximize, verbose = verbose))
|
||||||
}
|
}
|
||||||
# CV-predictions callback
|
# CV-predictions callback
|
||||||
if (prediction && !("cv_predict" %in% cb_names)) {
|
if (prediction &&
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.cv.predict(save_models = FALSE))
|
!has.callbacks(callbacks, 'cb.cv.predict')) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.cv.predict(save_models = FALSE))
|
||||||
}
|
}
|
||||||
|
# Sort the callbacks into categories
|
||||||
|
cb <- categorize.callbacks(callbacks)
|
||||||
|
|
||||||
|
|
||||||
# create the booster-folds
|
# create the booster-folds
|
||||||
# train_folds
|
# train_folds
|
||||||
dall <- data
|
dall <- xgb.get.DMatrix(data, label, missing, nthread = params$nthread)
|
||||||
bst_folds <- lapply(seq_along(folds), function(k) {
|
bst_folds <- lapply(seq_along(folds), function(k) {
|
||||||
dtest <- xgb.slice.DMatrix(dall, folds[[k]], allow_groups = TRUE)
|
dtest <- slice(dall, folds[[k]])
|
||||||
# code originally contributed by @RolandASc on stackoverflow
|
# code originally contributed by @RolandASc on stackoverflow
|
||||||
if (is.null(train_folds))
|
if (is.null(train_folds))
|
||||||
dtrain <- xgb.slice.DMatrix(dall, unlist(folds[-k]), allow_groups = TRUE)
|
dtrain <- slice(dall, unlist(folds[-k]))
|
||||||
else
|
else
|
||||||
dtrain <- xgb.slice.DMatrix(dall, train_folds[[k]], allow_groups = TRUE)
|
dtrain <- slice(dall, train_folds[[k]])
|
||||||
if (!is.null(attributes(folds[[k]])$group_test)) {
|
handle <- xgb.Booster.handle(params, list(dtrain, dtest))
|
||||||
setinfo(dtest, "group", attributes(folds[[k]])$group_test)
|
list(dtrain = dtrain, bst = handle, watchlist = list(train = dtrain, test = dtest), index = folds[[k]])
|
||||||
setinfo(dtrain, "group", attributes(folds[[k]])$group_train)
|
|
||||||
}
|
|
||||||
bst <- xgb.Booster(
|
|
||||||
params = params,
|
|
||||||
cachelist = list(dtrain, dtest),
|
|
||||||
modelfile = NULL
|
|
||||||
)
|
|
||||||
bst <- bst$bst
|
|
||||||
list(dtrain = dtrain, bst = bst, evals = list(train = dtrain, test = dtest), index = folds[[k]])
|
|
||||||
})
|
})
|
||||||
|
rm(dall)
|
||||||
|
# a "basket" to collect some results from callbacks
|
||||||
|
basket <- list()
|
||||||
|
|
||||||
# extract parameters that can affect the relationship b/w #trees and #iterations
|
# extract parameters that can affect the relationship b/w #trees and #iterations
|
||||||
num_class <- max(as.numeric(NVL(params[['num_class']], 1)), 1) # nolint
|
num_class <- max(as.numeric(NVL(params[['num_class']], 1)), 1) # nolint
|
||||||
|
num_parallel_tree <- max(as.numeric(NVL(params[['num_parallel_tree']], 1)), 1) # nolint
|
||||||
|
|
||||||
# those are fixed for CV (no training continuation)
|
# those are fixed for CV (no training continuation)
|
||||||
begin_iteration <- 1
|
begin_iteration <- 1
|
||||||
end_iteration <- nrounds
|
end_iteration <- nrounds
|
||||||
|
|
||||||
.execute.cb.before.training(
|
|
||||||
callbacks,
|
|
||||||
bst_folds,
|
|
||||||
dall,
|
|
||||||
NULL,
|
|
||||||
begin_iteration,
|
|
||||||
end_iteration
|
|
||||||
)
|
|
||||||
|
|
||||||
# synchronous CV boosting: run CV folds' models within each iteration
|
# synchronous CV boosting: run CV folds' models within each iteration
|
||||||
for (iteration in begin_iteration:end_iteration) {
|
for (iteration in begin_iteration:end_iteration) {
|
||||||
|
|
||||||
.execute.cb.before.iter(
|
for (f in cb$pre_iter) f()
|
||||||
callbacks,
|
|
||||||
bst_folds,
|
|
||||||
dall,
|
|
||||||
NULL,
|
|
||||||
iteration
|
|
||||||
)
|
|
||||||
|
|
||||||
msg <- lapply(bst_folds, function(fd) {
|
msg <- lapply(bst_folds, function(fd) {
|
||||||
xgb.iter.update(
|
xgb.iter.update(fd$bst, fd$dtrain, iteration - 1, obj)
|
||||||
bst = fd$bst,
|
xgb.iter.eval(fd$bst, fd$watchlist, iteration - 1, feval)
|
||||||
dtrain = fd$dtrain,
|
|
||||||
iter = iteration - 1,
|
|
||||||
obj = obj
|
|
||||||
)
|
|
||||||
xgb.iter.eval(
|
|
||||||
bst = fd$bst,
|
|
||||||
evals = fd$evals,
|
|
||||||
iter = iteration - 1,
|
|
||||||
feval = feval
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
msg <- simplify2array(msg)
|
msg <- simplify2array(msg)
|
||||||
|
bst_evaluation <- rowMeans(msg)
|
||||||
|
bst_evaluation_err <- sqrt(rowMeans(msg^2) - bst_evaluation^2) # nolint
|
||||||
|
|
||||||
should_stop <- .execute.cb.after.iter(
|
for (f in cb$post_iter) f()
|
||||||
callbacks,
|
|
||||||
bst_folds,
|
|
||||||
dall,
|
|
||||||
NULL,
|
|
||||||
iteration,
|
|
||||||
msg
|
|
||||||
)
|
|
||||||
|
|
||||||
if (should_stop) break
|
if (stop_condition) break
|
||||||
}
|
}
|
||||||
cb_outputs <- .execute.cb.after.training(
|
for (f in cb$finalize) f(finalize = TRUE)
|
||||||
callbacks,
|
|
||||||
bst_folds,
|
|
||||||
dall,
|
|
||||||
NULL,
|
|
||||||
iteration,
|
|
||||||
msg
|
|
||||||
)
|
|
||||||
|
|
||||||
# the CV result
|
# the CV result
|
||||||
ret <- list(
|
ret <- list(
|
||||||
call = match.call(),
|
call = match.call(),
|
||||||
params = params,
|
params = params,
|
||||||
niter = iteration,
|
callbacks = callbacks,
|
||||||
nfeatures = ncol(dall),
|
evaluation_log = evaluation_log,
|
||||||
|
niter = end_iteration,
|
||||||
|
nfeatures = ncol(data),
|
||||||
folds = folds
|
folds = folds
|
||||||
)
|
)
|
||||||
ret <- c(ret, cb_outputs)
|
ret <- c(ret, basket)
|
||||||
|
|
||||||
class(ret) <- 'xgb.cv.synchronous'
|
class(ret) <- 'xgb.cv.synchronous'
|
||||||
return(invisible(ret))
|
invisible(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -338,7 +269,7 @@ xgb.cv <- function(params = list(), data, nrounds, nfold,
|
|||||||
#' @examples
|
#' @examples
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' train <- agaricus.train
|
#' train <- agaricus.train
|
||||||
#' cv <- xgb.cv(data = xgb.DMatrix(train$data, label = train$label), nfold = 5, max_depth = 2,
|
#' cv <- xgb.cv(data = train$data, label = train$label, nfold = 5, max_depth = 2,
|
||||||
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
#' print(cv)
|
#' print(cv)
|
||||||
#' print(cv, verbose=TRUE)
|
#' print(cv, verbose=TRUE)
|
||||||
@ -361,16 +292,23 @@ print.xgb.cv.synchronous <- function(x, verbose = FALSE, ...) {
|
|||||||
paste0('"', unlist(x$params), '"'),
|
paste0('"', unlist(x$params), '"'),
|
||||||
sep = ' = ', collapse = ', '), '\n', sep = '')
|
sep = ' = ', collapse = ', '), '\n', sep = '')
|
||||||
}
|
}
|
||||||
|
if (!is.null(x$callbacks) && length(x$callbacks) > 0) {
|
||||||
for (n in c('niter', 'best_iteration')) {
|
cat('callbacks:\n')
|
||||||
if (is.null(x$early_stop[[n]]))
|
lapply(callback.calls(x$callbacks), function(x) {
|
||||||
next
|
cat(' ')
|
||||||
cat(n, ': ', x$early_stop[[n]], '\n', sep = '')
|
print(x)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is.null(x$cv_predict$pred)) {
|
for (n in c('niter', 'best_iteration', 'best_ntreelimit')) {
|
||||||
|
if (is.null(x[[n]]))
|
||||||
|
next
|
||||||
|
cat(n, ': ', x[[n]], '\n', sep = '')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is.null(x$pred)) {
|
||||||
cat('pred:\n')
|
cat('pred:\n')
|
||||||
str(x$cv_predict$pred)
|
str(x$pred)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,9 +316,9 @@ print.xgb.cv.synchronous <- function(x, verbose = FALSE, ...) {
|
|||||||
cat('evaluation_log:\n')
|
cat('evaluation_log:\n')
|
||||||
print(x$evaluation_log, row.names = FALSE, ...)
|
print(x$evaluation_log, row.names = FALSE, ...)
|
||||||
|
|
||||||
if (!is.null(x$early_stop$best_iteration)) {
|
if (!is.null(x$best_iteration)) {
|
||||||
cat('Best iteration:\n')
|
cat('Best iteration:\n')
|
||||||
print(x$evaluation_log[x$early_stop$best_iteration], row.names = FALSE, ...)
|
print(x$evaluation_log[x$best_iteration], row.names = FALSE, ...)
|
||||||
}
|
}
|
||||||
invisible(x)
|
invisible(x)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,10 +13,7 @@
|
|||||||
#' When this option is on, the model dump contains two additional values:
|
#' When this option is on, the model dump contains two additional values:
|
||||||
#' gain is the approximate loss function gain we get in each split;
|
#' gain is the approximate loss function gain we get in each split;
|
||||||
#' cover is the sum of second order gradient in each node.
|
#' cover is the sum of second order gradient in each node.
|
||||||
#' @param dump_format either 'text', 'json', or 'dot' (graphviz) format could be specified.
|
#' @param dump_format either 'text' or 'json' format could be specified.
|
||||||
#'
|
|
||||||
#' Format 'dot' for a single tree can be passed directly to packages that consume this format
|
|
||||||
#' for graph visualization, such as function [DiagrammeR::grViz()]
|
|
||||||
#' @param ... currently not used
|
#' @param ... currently not used
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
@ -24,7 +21,6 @@
|
|||||||
#' as a \code{character} vector. Otherwise it will return \code{TRUE}.
|
#' as a \code{character} vector. Otherwise it will return \code{TRUE}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package='xgboost')
|
#' data(agaricus.test, package='xgboost')
|
||||||
#' train <- agaricus.train
|
#' train <- agaricus.train
|
||||||
@ -41,13 +37,9 @@
|
|||||||
#' # print in JSON format:
|
#' # print in JSON format:
|
||||||
#' cat(xgb.dump(bst, with_stats = TRUE, dump_format='json'))
|
#' cat(xgb.dump(bst, with_stats = TRUE, dump_format='json'))
|
||||||
#'
|
#'
|
||||||
#' # plot first tree leveraging the 'dot' format
|
|
||||||
#' if (requireNamespace('DiagrammeR', quietly = TRUE)) {
|
|
||||||
#' DiagrammeR::grViz(xgb.dump(bst, dump_format = "dot")[[1L]])
|
|
||||||
#' }
|
|
||||||
#' @export
|
#' @export
|
||||||
xgb.dump <- function(model, fname = NULL, fmap = "", with_stats = FALSE,
|
xgb.dump <- function(model, fname = NULL, fmap = "", with_stats = FALSE,
|
||||||
dump_format = c("text", "json", "dot"), ...) {
|
dump_format = c("text", "json"), ...) {
|
||||||
check.deprecation(...)
|
check.deprecation(...)
|
||||||
dump_format <- match.arg(dump_format)
|
dump_format <- match.arg(dump_format)
|
||||||
if (!inherits(model, "xgb.Booster"))
|
if (!inherits(model, "xgb.Booster"))
|
||||||
@ -57,16 +49,9 @@ xgb.dump <- function(model, fname = NULL, fmap = "", with_stats = FALSE,
|
|||||||
if (!(is.null(fmap) || is.character(fmap)))
|
if (!(is.null(fmap) || is.character(fmap)))
|
||||||
stop("fmap: argument must be a character string (when provided)")
|
stop("fmap: argument must be a character string (when provided)")
|
||||||
|
|
||||||
model_dump <- .Call(
|
model <- xgb.Booster.complete(model)
|
||||||
XGBoosterDumpModel_R,
|
model_dump <- .Call(XGBoosterDumpModel_R, model$handle, NVL(fmap, "")[1], as.integer(with_stats),
|
||||||
xgb.get.handle(model),
|
as.character(dump_format))
|
||||||
NVL(fmap, "")[1],
|
|
||||||
as.integer(with_stats),
|
|
||||||
as.character(dump_format)
|
|
||||||
)
|
|
||||||
if (dump_format == "dot") {
|
|
||||||
return(sapply(model_dump, function(x) gsub("^booster\\[\\d+\\]\\n", "\\1", x)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is.null(fname))
|
if (is.null(fname))
|
||||||
model_dump <- gsub('\t', '', model_dump, fixed = TRUE)
|
model_dump <- gsub('\t', '', model_dump, fixed = TRUE)
|
||||||
|
|||||||
@ -127,20 +127,21 @@ xgb.ggplot.shap.summary <- function(data, shap_contrib = NULL, features = NULL,
|
|||||||
p
|
p
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Combine feature values and SHAP values
|
#' Combine and melt feature values and SHAP contributions for sample
|
||||||
|
#' observations.
|
||||||
#'
|
#'
|
||||||
#' Internal function used to combine and melt feature values and SHAP contributions
|
#' Conforms to data format required for ggplot functions.
|
||||||
#' as required for ggplot functions related to SHAP.
|
|
||||||
#'
|
#'
|
||||||
#' @param data_list The result of `xgb.shap.data()`.
|
#' Internal utility function.
|
||||||
#' @param normalize Whether to standardize feature values to mean 0 and
|
|
||||||
#' standard deviation 1. This is useful for comparing multiple features on the same
|
|
||||||
#' plot. Default is \code{FALSE}.
|
|
||||||
#'
|
#'
|
||||||
#' @return A `data.table` containing the observation ID, the feature name, the
|
#' @param data_list List containing 'data' and 'shap_contrib' returned by
|
||||||
|
#' \code{xgb.shap.data()}.
|
||||||
|
#' @param normalize Whether to standardize feature values to have mean 0 and
|
||||||
|
#' standard deviation 1 (useful for comparing multiple features on the same
|
||||||
|
#' plot). Default \code{FALSE}.
|
||||||
|
#'
|
||||||
|
#' @return A data.table containing the observation ID, the feature name, the
|
||||||
#' feature value (normalized if specified), and the SHAP contribution value.
|
#' feature value (normalized if specified), and the SHAP contribution value.
|
||||||
#' @noRd
|
|
||||||
#' @keywords internal
|
|
||||||
prepare.ggplot.shap.data <- function(data_list, normalize = FALSE) {
|
prepare.ggplot.shap.data <- function(data_list, normalize = FALSE) {
|
||||||
data <- data_list[["data"]]
|
data <- data_list[["data"]]
|
||||||
shap_contrib <- data_list[["shap_contrib"]]
|
shap_contrib <- data_list[["shap_contrib"]]
|
||||||
@ -161,16 +162,14 @@ prepare.ggplot.shap.data <- function(data_list, normalize = FALSE) {
|
|||||||
p_data
|
p_data
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Scale feature values
|
#' Scale feature value to have mean 0, standard deviation 1
|
||||||
#'
|
#'
|
||||||
#' Internal function that scales feature values to mean 0 and standard deviation 1.
|
#' This is used to compare multiple features on the same plot.
|
||||||
#' Useful to compare multiple features on the same plot.
|
#' Internal utility function
|
||||||
#'
|
#'
|
||||||
#' @param x Numeric vector.
|
#' @param x Numeric vector
|
||||||
#'
|
#'
|
||||||
#' @return Numeric vector with mean 0 and standard deviation 1.
|
#' @return Numeric vector with mean 0 and sd 1.
|
||||||
#' @noRd
|
|
||||||
#' @keywords internal
|
|
||||||
normalize <- function(x) {
|
normalize <- function(x) {
|
||||||
loc <- mean(x, na.rm = TRUE)
|
loc <- mean(x, na.rm = TRUE)
|
||||||
scale <- stats::sd(x, na.rm = TRUE)
|
scale <- stats::sd(x, na.rm = TRUE)
|
||||||
@ -182,7 +181,7 @@ normalize <- function(x) {
|
|||||||
# ... the plots
|
# ... the plots
|
||||||
# cols number of columns
|
# cols number of columns
|
||||||
# internal utility function
|
# internal utility function
|
||||||
multiplot <- function(..., cols) {
|
multiplot <- function(..., cols = 1) {
|
||||||
plots <- list(...)
|
plots <- list(...)
|
||||||
num_plots <- length(plots)
|
num_plots <- length(plots)
|
||||||
|
|
||||||
|
|||||||
@ -1,132 +1,107 @@
|
|||||||
#' Feature importance
|
#' Importance of features in a model.
|
||||||
#'
|
#'
|
||||||
#' Creates a `data.table` of feature importances.
|
#' Creates a \code{data.table} of feature importances in a model.
|
||||||
#'
|
#'
|
||||||
#' @param feature_names Character vector used to overwrite the feature names
|
#' @param feature_names character vector of feature names. If the model already
|
||||||
#' of the model. The default is `NULL` (use original feature names).
|
#' contains feature names, those would be used when \code{feature_names=NULL} (default value).
|
||||||
#' @param model Object of class `xgb.Booster`.
|
#' Non-null \code{feature_names} could be provided to override those in the model.
|
||||||
#' @param trees An integer vector of tree indices that should be included
|
#' @param model object of class \code{xgb.Booster}.
|
||||||
#' into the importance calculation (only for the "gbtree" booster).
|
#' @param trees (only for the gbtree booster) an integer vector of tree indices that should be included
|
||||||
#' The default (`NULL`) parses all trees.
|
#' into the importance calculation. If set to \code{NULL}, all trees of the model are parsed.
|
||||||
#' It could be useful, e.g., in multiclass classification to get feature importances
|
#' It could be useful, e.g., in multiclass classification to get feature importances
|
||||||
#' for each class separately. *Important*: the tree index in XGBoost models
|
#' for each class separately. IMPORTANT: the tree index in xgboost models
|
||||||
#' is zero-based (e.g., use `trees = 0:4` for the first five trees).
|
#' is zero-based (e.g., use \code{trees = 0:4} for first 5 trees).
|
||||||
#' @param data Deprecated.
|
#' @param data deprecated.
|
||||||
#' @param label Deprecated.
|
#' @param label deprecated.
|
||||||
#' @param target Deprecated.
|
#' @param target deprecated.
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#'
|
#'
|
||||||
#' This function works for both linear and tree models.
|
#' This function works for both linear and tree models.
|
||||||
#'
|
#'
|
||||||
#' For linear models, the importance is the absolute magnitude of linear coefficients.
|
#' For linear models, the importance is the absolute magnitude of linear coefficients.
|
||||||
#' To obtain a meaningful ranking by importance for linear models, the features need to
|
#' For that reason, in order to obtain a meaningful ranking by importance for a linear model,
|
||||||
#' be on the same scale (which is also recommended when using L1 or L2 regularization).
|
#' the features need to be on the same scale (which you also would want to do when using either
|
||||||
|
#' L1 or L2 regularization).
|
||||||
#'
|
#'
|
||||||
#' @return A `data.table` with the following columns:
|
#' @return
|
||||||
#'
|
#'
|
||||||
#' For a tree model:
|
#' For a tree model, a \code{data.table} with the following columns:
|
||||||
#' - `Features`: Names of the features used in the model.
|
#' \itemize{
|
||||||
#' - `Gain`: Fractional contribution of each feature to the model based on
|
#' \item \code{Features} names of the features used in the model;
|
||||||
#' the total gain of this feature's splits. Higher percentage means higher importance.
|
#' \item \code{Gain} represents fractional contribution of each feature to the model based on
|
||||||
#' - `Cover`: Metric of the number of observation related to this feature.
|
#' the total gain of this feature's splits. Higher percentage means a more important
|
||||||
#' - `Frequency`: Percentage of times a feature has been used in trees.
|
#' predictive feature.
|
||||||
|
#' \item \code{Cover} metric of the number of observation related to this feature;
|
||||||
|
#' \item \code{Frequency} percentage representing the relative number of times
|
||||||
|
#' a feature have been used in trees.
|
||||||
|
#' }
|
||||||
#'
|
#'
|
||||||
#' For a linear model:
|
#' A linear model's importance \code{data.table} has the following columns:
|
||||||
#' - `Features`: Names of the features used in the model.
|
#' \itemize{
|
||||||
#' - `Weight`: Linear coefficient of this feature.
|
#' \item \code{Features} names of the features used in the model;
|
||||||
#' - `Class`: Class label (only for multiclass models).
|
#' \item \code{Weight} the linear coefficient of this feature;
|
||||||
|
#' \item \code{Class} (only for multiclass models) class label.
|
||||||
|
#' }
|
||||||
#'
|
#'
|
||||||
#' If `feature_names` is not provided and `model` doesn't have `feature_names`,
|
#' If \code{feature_names} is not provided and \code{model} doesn't have \code{feature_names},
|
||||||
#' the index of the features will be used instead. Because the index is extracted from the model dump
|
#' index of the features will be used instead. Because the index is extracted from the model dump
|
||||||
#' (based on C++ code), it starts at 0 (as in C/C++ or Python) instead of 1 (usual in R).
|
#' (based on C++ code), it starts at 0 (as in C/C++ or Python) instead of 1 (usual in R).
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#'
|
#'
|
||||||
#' # binomial classification using "gbtree":
|
#' # binomial classification using gbtree:
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#'
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2,
|
||||||
#' bst <- xgboost(
|
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
#' data = agaricus.train$data,
|
|
||||||
#' label = agaricus.train$label,
|
|
||||||
#' max_depth = 2,
|
|
||||||
#' eta = 1,
|
|
||||||
#' nthread = 2,
|
|
||||||
#' nrounds = 2,
|
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' xgb.importance(model = bst)
|
#' xgb.importance(model = bst)
|
||||||
#'
|
#'
|
||||||
#' # binomial classification using "gblinear":
|
#' # binomial classification using gblinear:
|
||||||
#' bst <- xgboost(
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, booster = "gblinear",
|
||||||
#' data = agaricus.train$data,
|
#' eta = 0.3, nthread = 1, nrounds = 20, objective = "binary:logistic")
|
||||||
#' label = agaricus.train$label,
|
|
||||||
#' booster = "gblinear",
|
|
||||||
#' eta = 0.3,
|
|
||||||
#' nthread = 1,
|
|
||||||
#' nrounds = 20,objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' xgb.importance(model = bst)
|
#' xgb.importance(model = bst)
|
||||||
#'
|
#'
|
||||||
#' # multiclass classification using "gbtree":
|
#' # multiclass classification using gbtree:
|
||||||
#' nclass <- 3
|
#' nclass <- 3
|
||||||
#' nrounds <- 10
|
#' nrounds <- 10
|
||||||
#' mbst <- xgboost(
|
#' mbst <- xgboost(data = as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1,
|
||||||
#' data = as.matrix(iris[, -5]),
|
#' max_depth = 3, eta = 0.2, nthread = 2, nrounds = nrounds,
|
||||||
#' label = as.numeric(iris$Species) - 1,
|
#' objective = "multi:softprob", num_class = nclass)
|
||||||
#' max_depth = 3,
|
|
||||||
#' eta = 0.2,
|
|
||||||
#' nthread = 2,
|
|
||||||
#' nrounds = nrounds,
|
|
||||||
#' objective = "multi:softprob",
|
|
||||||
#' num_class = nclass
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' # all classes clumped together:
|
#' # all classes clumped together:
|
||||||
#' xgb.importance(model = mbst)
|
#' xgb.importance(model = mbst)
|
||||||
#'
|
|
||||||
#' # inspect importances separately for each class:
|
#' # inspect importances separately for each class:
|
||||||
#' xgb.importance(
|
#' xgb.importance(model = mbst, trees = seq(from=0, by=nclass, length.out=nrounds))
|
||||||
#' model = mbst, trees = seq(from = 0, by = nclass, length.out = nrounds)
|
#' xgb.importance(model = mbst, trees = seq(from=1, by=nclass, length.out=nrounds))
|
||||||
#' )
|
#' xgb.importance(model = mbst, trees = seq(from=2, by=nclass, length.out=nrounds))
|
||||||
#' xgb.importance(
|
|
||||||
#' model = mbst, trees = seq(from = 1, by = nclass, length.out = nrounds)
|
|
||||||
#' )
|
|
||||||
#' xgb.importance(
|
|
||||||
#' model = mbst, trees = seq(from = 2, by = nclass, length.out = nrounds)
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' # multiclass classification using "gblinear":
|
|
||||||
#' mbst <- xgboost(
|
|
||||||
#' data = scale(as.matrix(iris[, -5])),
|
|
||||||
#' label = as.numeric(iris$Species) - 1,
|
|
||||||
#' booster = "gblinear",
|
|
||||||
#' eta = 0.2,
|
|
||||||
#' nthread = 1,
|
|
||||||
#' nrounds = 15,
|
|
||||||
#' objective = "multi:softprob",
|
|
||||||
#' num_class = nclass
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
|
#' # multiclass classification using gblinear:
|
||||||
|
#' mbst <- xgboost(data = scale(as.matrix(iris[, -5])), label = as.numeric(iris$Species) - 1,
|
||||||
|
#' booster = "gblinear", eta = 0.2, nthread = 1, nrounds = 15,
|
||||||
|
#' objective = "multi:softprob", num_class = nclass)
|
||||||
#' xgb.importance(model = mbst)
|
#' xgb.importance(model = mbst)
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.importance <- function(model = NULL, feature_names = getinfo(model, "feature_name"), trees = NULL,
|
xgb.importance <- function(feature_names = NULL, model = NULL, trees = NULL,
|
||||||
data = NULL, label = NULL, target = NULL) {
|
data = NULL, label = NULL, target = NULL) {
|
||||||
|
|
||||||
if (!(is.null(data) && is.null(label) && is.null(target)))
|
if (!(is.null(data) && is.null(label) && is.null(target)))
|
||||||
warning("xgb.importance: parameters 'data', 'label' and 'target' are deprecated")
|
warning("xgb.importance: parameters 'data', 'label' and 'target' are deprecated")
|
||||||
|
|
||||||
|
if (!inherits(model, "xgb.Booster"))
|
||||||
|
stop("model: must be an object of class xgb.Booster")
|
||||||
|
|
||||||
|
if (is.null(feature_names) && !is.null(model$feature_names))
|
||||||
|
feature_names <- model$feature_names
|
||||||
|
|
||||||
if (!(is.null(feature_names) || is.character(feature_names)))
|
if (!(is.null(feature_names) || is.character(feature_names)))
|
||||||
stop("feature_names: Has to be a character vector")
|
stop("feature_names: Has to be a character vector")
|
||||||
|
|
||||||
handle <- xgb.get.handle(model)
|
model <- xgb.Booster.complete(model)
|
||||||
if (xgb.booster_type(model) == "gblinear") {
|
config <- jsonlite::fromJSON(xgb.config(model))
|
||||||
|
if (config$learner$gradient_booster$name == "gblinear") {
|
||||||
args <- list(importance_type = "weight", feature_names = feature_names)
|
args <- list(importance_type = "weight", feature_names = feature_names)
|
||||||
results <- .Call(
|
results <- .Call(
|
||||||
XGBoosterFeatureScore_R, handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null")
|
XGBoosterFeatureScore_R, model$handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null")
|
||||||
)
|
)
|
||||||
names(results) <- c("features", "shape", "weight")
|
names(results) <- c("features", "shape", "weight")
|
||||||
if (length(results$shape) == 2) {
|
if (length(results$shape) == 2) {
|
||||||
@ -147,7 +122,7 @@ xgb.importance <- function(model = NULL, feature_names = getinfo(model, "feature
|
|||||||
for (importance_type in c("weight", "total_gain", "total_cover")) {
|
for (importance_type in c("weight", "total_gain", "total_cover")) {
|
||||||
args <- list(importance_type = importance_type, feature_names = feature_names, tree_idx = trees)
|
args <- list(importance_type = importance_type, feature_names = feature_names, tree_idx = trees)
|
||||||
results <- .Call(
|
results <- .Call(
|
||||||
XGBoosterFeatureScore_R, handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null")
|
XGBoosterFeatureScore_R, model$handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null")
|
||||||
)
|
)
|
||||||
names(results) <- c("features", "shape", importance_type)
|
names(results) <- c("features", "shape", importance_type)
|
||||||
concatenated[
|
concatenated[
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#' The input file is expected to contain a model saved in an xgboost model format
|
#' The input file is expected to contain a model saved in an xgboost model format
|
||||||
#' using either \code{\link{xgb.save}} or \code{\link{xgb.cb.save.model}} in R, or using some
|
#' using either \code{\link{xgb.save}} or \code{\link{cb.save.model}} in R, or using some
|
||||||
#' appropriate methods from other xgboost interfaces. E.g., a model trained in Python and
|
#' appropriate methods from other xgboost interfaces. E.g., a model trained in Python and
|
||||||
#' saved from there in xgboost format, could be loaded from R.
|
#' saved from there in xgboost format, could be loaded from R.
|
||||||
#'
|
#'
|
||||||
@ -17,50 +17,38 @@
|
|||||||
#' An object of \code{xgb.Booster} class.
|
#' An object of \code{xgb.Booster} class.
|
||||||
#'
|
#'
|
||||||
#' @seealso
|
#' @seealso
|
||||||
#' \code{\link{xgb.save}}
|
#' \code{\link{xgb.save}}, \code{\link{xgb.Booster.complete}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package='xgboost')
|
#' data(agaricus.test, package='xgboost')
|
||||||
#'
|
|
||||||
#' ## Keep the number of threads to 1 for examples
|
|
||||||
#' nthread <- 1
|
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
|
||||||
#' train <- agaricus.train
|
#' train <- agaricus.train
|
||||||
#' test <- agaricus.test
|
#' test <- agaricus.test
|
||||||
#' bst <- xgb.train(
|
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
#' data = xgb.DMatrix(train$data, label = train$label),
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
#' max_depth = 2,
|
#' xgb.save(bst, 'xgb.model')
|
||||||
#' eta = 1,
|
#' bst <- xgb.load('xgb.model')
|
||||||
#' nthread = nthread,
|
#' if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||||
#' nrounds = 2,
|
#' pred <- predict(bst, test$data)
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' fname <- file.path(tempdir(), "xgb.ubj")
|
|
||||||
#' xgb.save(bst, fname)
|
|
||||||
#' bst <- xgb.load(fname)
|
|
||||||
#' @export
|
#' @export
|
||||||
xgb.load <- function(modelfile) {
|
xgb.load <- function(modelfile) {
|
||||||
if (is.null(modelfile))
|
if (is.null(modelfile))
|
||||||
stop("xgb.load: modelfile cannot be NULL")
|
stop("xgb.load: modelfile cannot be NULL")
|
||||||
|
|
||||||
bst <- xgb.Booster(
|
handle <- xgb.Booster.handle(modelfile = modelfile)
|
||||||
params = list(),
|
|
||||||
cachelist = list(),
|
|
||||||
modelfile = modelfile
|
|
||||||
)
|
|
||||||
bst <- bst$bst
|
|
||||||
# re-use modelfile if it is raw so we do not need to serialize
|
# re-use modelfile if it is raw so we do not need to serialize
|
||||||
if (typeof(modelfile) == "raw") {
|
if (typeof(modelfile) == "raw") {
|
||||||
warning(
|
warning(
|
||||||
paste(
|
paste(
|
||||||
"The support for loading raw booster with `xgb.load` will be ",
|
"The support for loading raw booster with `xgb.load` will be ",
|
||||||
"discontinued in upcoming release. Use `xgb.load.raw` instead. "
|
"discontinued in upcoming release. Use `xgb.load.raw` or",
|
||||||
|
" `xgb.unserialize` instead. "
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
bst <- xgb.handleToBooster(handle, modelfile)
|
||||||
|
} else {
|
||||||
|
bst <- xgb.handleToBooster(handle, NULL)
|
||||||
}
|
}
|
||||||
|
bst <- xgb.Booster.complete(bst, saveraw = TRUE)
|
||||||
return(bst)
|
return(bst)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,21 @@
|
|||||||
#' User can generate raw memory buffer by calling xgb.save.raw
|
#' User can generate raw memory buffer by calling xgb.save.raw
|
||||||
#'
|
#'
|
||||||
#' @param buffer the buffer returned by xgb.save.raw
|
#' @param buffer the buffer returned by xgb.save.raw
|
||||||
|
#' @param as_booster Return the loaded model as xgb.Booster instead of xgb.Booster.handle.
|
||||||
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.load.raw <- function(buffer) {
|
xgb.load.raw <- function(buffer, as_booster = FALSE) {
|
||||||
cachelist <- list()
|
cachelist <- list()
|
||||||
bst <- .Call(XGBoosterCreate_R, cachelist)
|
handle <- .Call(XGBoosterCreate_R, cachelist)
|
||||||
.Call(XGBoosterLoadModelFromRaw_R, xgb.get.handle(bst), buffer)
|
.Call(XGBoosterLoadModelFromRaw_R, handle, buffer)
|
||||||
return(bst)
|
class(handle) <- "xgb.Booster.handle"
|
||||||
|
|
||||||
|
if (as_booster) {
|
||||||
|
booster <- list(handle = handle, raw = NULL)
|
||||||
|
class(booster) <- "xgb.Booster"
|
||||||
|
booster <- xgb.Booster.complete(booster, saveraw = TRUE)
|
||||||
|
return(booster)
|
||||||
|
} else {
|
||||||
|
return (handle)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,72 +1,67 @@
|
|||||||
#' Parse model text dump
|
#' Parse a boosted tree model text dump
|
||||||
#'
|
#'
|
||||||
#' Parse a boosted tree model text dump into a `data.table` structure.
|
#' Parse a boosted tree model text dump into a \code{data.table} structure.
|
||||||
#'
|
#'
|
||||||
#' @param model Object of class `xgb.Booster`. If it contains feature names (they can be set through
|
#' @param feature_names character vector of feature names. If the model already
|
||||||
#' \link{setinfo}), they will be used in the output from this function.
|
#' contains feature names, those would be used when \code{feature_names=NULL} (default value).
|
||||||
#' @param text Character vector previously generated by the function [xgb.dump()]
|
#' Non-null \code{feature_names} could be provided to override those in the model.
|
||||||
#' (called with parameter `with_stats = TRUE`). `text` takes precedence over `model`.
|
#' @param model object of class \code{xgb.Booster}
|
||||||
#' @param trees An integer vector of tree indices that should be used.
|
#' @param text \code{character} vector previously generated by the \code{xgb.dump}
|
||||||
#' The default (`NULL`) uses all trees.
|
#' function (where parameter \code{with_stats = TRUE} should have been set).
|
||||||
#' Useful, e.g., in multiclass classification to get only
|
#' \code{text} takes precedence over \code{model}.
|
||||||
#' the trees of one class. *Important*: the tree index in XGBoost models
|
#' @param trees an integer vector of tree indices that should be parsed.
|
||||||
#' is zero-based (e.g., use `trees = 0:4` for the first five trees).
|
#' If set to \code{NULL}, all trees of the model are parsed.
|
||||||
#' @param use_int_id A logical flag indicating whether nodes in columns "Yes", "No", and
|
#' It could be useful, e.g., in multiclass classification to get only
|
||||||
#' "Missing" should be represented as integers (when `TRUE`) or as "Tree-Node"
|
#' the trees of one certain class. IMPORTANT: the tree index in xgboost models
|
||||||
#' character strings (when `FALSE`, default).
|
#' is zero-based (e.g., use \code{trees = 0:4} for first 5 trees).
|
||||||
#' @param ... Currently not used.
|
#' @param use_int_id a logical flag indicating whether nodes in columns "Yes", "No", "Missing" should be
|
||||||
|
#' represented as integers (when FALSE) or as "Tree-Node" character strings (when FALSE).
|
||||||
|
#' @param ... currently not used.
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
#' A `data.table` with detailed information about tree nodes. It has the following columns:
|
#' A \code{data.table} with detailed information about model trees' nodes.
|
||||||
#' - `Tree`: integer ID of a tree in a model (zero-based index).
|
|
||||||
#' - `Node`: integer ID of a node in a tree (zero-based index).
|
|
||||||
#' - `ID`: character identifier of a node in a model (only when `use_int_id = FALSE`).
|
|
||||||
#' - `Feature`: for a branch node, a feature ID or name (when available);
|
|
||||||
#' for a leaf node, it simply labels it as `"Leaf"`.
|
|
||||||
#' - `Split`: location of the split for a branch node (split condition is always "less than").
|
|
||||||
#' - `Yes`: ID of the next node when the split condition is met.
|
|
||||||
#' - `No`: ID of the next node when the split condition is not met.
|
|
||||||
#' - `Missing`: ID of the next node when the branch value is missing.
|
|
||||||
#' - `Gain`: either the split gain (change in loss) or the leaf value.
|
|
||||||
#' - `Cover`: metric related to the number of observations either seen by a split
|
|
||||||
#' or collected by a leaf during training.
|
|
||||||
#'
|
#'
|
||||||
#' When `use_int_id = FALSE`, columns "Yes", "No", and "Missing" point to model-wide node identifiers
|
#' The columns of the \code{data.table} are:
|
||||||
#' in the "ID" column. When `use_int_id = TRUE`, those columns point to node identifiers from
|
#'
|
||||||
|
#' \itemize{
|
||||||
|
#' \item \code{Tree}: integer ID of a tree in a model (zero-based index)
|
||||||
|
#' \item \code{Node}: integer ID of a node in a tree (zero-based index)
|
||||||
|
#' \item \code{ID}: character identifier of a node in a model (only when \code{use_int_id=FALSE})
|
||||||
|
#' \item \code{Feature}: for a branch node, it's a feature id or name (when available);
|
||||||
|
#' for a leaf note, it simply labels it as \code{'Leaf'}
|
||||||
|
#' \item \code{Split}: location of the split for a branch node (split condition is always "less than")
|
||||||
|
#' \item \code{Yes}: ID of the next node when the split condition is met
|
||||||
|
#' \item \code{No}: ID of the next node when the split condition is not met
|
||||||
|
#' \item \code{Missing}: ID of the next node when branch value is missing
|
||||||
|
#' \item \code{Quality}: either the split gain (change in loss) or the leaf value
|
||||||
|
#' \item \code{Cover}: metric related to the number of observation either seen by a split
|
||||||
|
#' or collected by a leaf during training.
|
||||||
|
#' }
|
||||||
|
#'
|
||||||
|
#' When \code{use_int_id=FALSE}, columns "Yes", "No", and "Missing" point to model-wide node identifiers
|
||||||
|
#' in the "ID" column. When \code{use_int_id=TRUE}, those columns point to node identifiers from
|
||||||
#' the corresponding trees in the "Node" column.
|
#' the corresponding trees in the "Node" column.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' # Basic use:
|
#' # Basic use:
|
||||||
#'
|
#'
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' ## Keep the number of threads to 1 for examples
|
|
||||||
#' nthread <- 1
|
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
#'
|
||||||
#' bst <- xgboost(
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2,
|
||||||
#' data = agaricus.train$data,
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
#' label = agaricus.train$label,
|
#'
|
||||||
#' max_depth = 2,
|
#' (dt <- xgb.model.dt.tree(colnames(agaricus.train$data), bst))
|
||||||
#' eta = 1,
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' nrounds = 2,
|
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' # This bst model already has feature_names stored with it, so those would be used when
|
#' # This bst model already has feature_names stored with it, so those would be used when
|
||||||
#' # feature_names is not set:
|
#' # feature_names is not set:
|
||||||
#' dt <- xgb.model.dt.tree(bst)
|
#' (dt <- xgb.model.dt.tree(model = bst))
|
||||||
#'
|
#'
|
||||||
#' # How to match feature names of splits that are following a current 'Yes' branch:
|
#' # How to match feature names of splits that are following a current 'Yes' branch:
|
||||||
#' merge(
|
#'
|
||||||
#' dt,
|
#' merge(dt, dt[, .(ID, Y.Feature=Feature)], by.x='Yes', by.y='ID', all.x=TRUE)[order(Tree,Node)]
|
||||||
#' dt[, .(ID, Y.Feature = Feature)], by.x = "Yes", by.y = "ID", all.x = TRUE
|
|
||||||
#' )[
|
|
||||||
#' order(Tree, Node)
|
|
||||||
#' ]
|
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
xgb.model.dt.tree <- function(feature_names = NULL, model = NULL, text = NULL,
|
||||||
trees = NULL, use_int_id = FALSE, ...) {
|
trees = NULL, use_int_id = FALSE, ...) {
|
||||||
check.deprecation(...)
|
check.deprecation(...)
|
||||||
|
|
||||||
@ -76,22 +71,23 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
|||||||
" (or NULL if 'model' was provided).")
|
" (or NULL if 'model' was provided).")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is.null(feature_names) && !is.null(model) && !is.null(model$feature_names))
|
||||||
|
feature_names <- model$feature_names
|
||||||
|
|
||||||
|
if (!(is.null(feature_names) || is.character(feature_names))) {
|
||||||
|
stop("feature_names: must be a character vector")
|
||||||
|
}
|
||||||
|
|
||||||
if (!(is.null(trees) || is.numeric(trees))) {
|
if (!(is.null(trees) || is.numeric(trees))) {
|
||||||
stop("trees: must be a vector of integers.")
|
stop("trees: must be a vector of integers.")
|
||||||
}
|
}
|
||||||
|
|
||||||
feature_names <- NULL
|
|
||||||
if (inherits(model, "xgb.Booster")) {
|
|
||||||
feature_names <- xgb.feature_names(model)
|
|
||||||
}
|
|
||||||
|
|
||||||
from_text <- TRUE
|
|
||||||
if (is.null(text)) {
|
if (is.null(text)) {
|
||||||
text <- xgb.dump(model = model, with_stats = TRUE)
|
text <- xgb.dump(model = model, with_stats = TRUE)
|
||||||
from_text <- FALSE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length(text) < 2 || !any(grepl('leaf=(\\d+)', text))) {
|
if (length(text) < 2 ||
|
||||||
|
sum(grepl('leaf=(\\d+)', text)) < 1) {
|
||||||
stop("Non-tree model detected! This function can only be used with tree models.")
|
stop("Non-tree model detected! This function can only be used with tree models.")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,26 +113,9 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
|||||||
td[, isLeaf := grepl("leaf", t, fixed = TRUE)]
|
td[, isLeaf := grepl("leaf", t, fixed = TRUE)]
|
||||||
|
|
||||||
# parse branch lines
|
# parse branch lines
|
||||||
branch_rx_nonames <- paste0("f(\\d+)<(", anynumber_regex, ")\\] yes=(\\d+),no=(\\d+),missing=(\\d+),",
|
branch_rx <- paste0("f(\\d+)<(", anynumber_regex, ")\\] yes=(\\d+),no=(\\d+),missing=(\\d+),",
|
||||||
"gain=(", anynumber_regex, "),cover=(", anynumber_regex, ")")
|
"gain=(", anynumber_regex, "),cover=(", anynumber_regex, ")")
|
||||||
branch_rx_w_names <- paste0("\\d+:\\[(.+)<(", anynumber_regex, ")\\] yes=(\\d+),no=(\\d+),missing=(\\d+),",
|
branch_cols <- c("Feature", "Split", "Yes", "No", "Missing", "Quality", "Cover")
|
||||||
"gain=(", anynumber_regex, "),cover=(", anynumber_regex, ")")
|
|
||||||
text_has_feature_names <- FALSE
|
|
||||||
if (NROW(feature_names)) {
|
|
||||||
branch_rx <- branch_rx_w_names
|
|
||||||
text_has_feature_names <- TRUE
|
|
||||||
} else {
|
|
||||||
# Note: when passing a text dump, it might or might not have feature names,
|
|
||||||
# but that aspect is unknown from just the text attributes
|
|
||||||
branch_rx <- branch_rx_nonames
|
|
||||||
if (from_text) {
|
|
||||||
if (sum(grepl(branch_rx_w_names, text)) > sum(grepl(branch_rx_nonames, text))) {
|
|
||||||
branch_rx <- branch_rx_w_names
|
|
||||||
text_has_feature_names <- TRUE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
branch_cols <- c("Feature", "Split", "Yes", "No", "Missing", "Gain", "Cover")
|
|
||||||
td[
|
td[
|
||||||
isLeaf == FALSE,
|
isLeaf == FALSE,
|
||||||
(branch_cols) := {
|
(branch_cols) := {
|
||||||
@ -146,7 +125,7 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
|||||||
xtr[, 3:5] <- add.tree.id(xtr[, 3:5], Tree)
|
xtr[, 3:5] <- add.tree.id(xtr[, 3:5], Tree)
|
||||||
if (length(xtr) == 0) {
|
if (length(xtr) == 0) {
|
||||||
as.data.table(
|
as.data.table(
|
||||||
list(Feature = "NA", Split = "NA", Yes = "NA", No = "NA", Missing = "NA", Gain = "NA", Cover = "NA")
|
list(Feature = "NA", Split = "NA", Yes = "NA", No = "NA", Missing = "NA", Quality = "NA", Cover = "NA")
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
as.data.table(xtr)
|
as.data.table(xtr)
|
||||||
@ -158,17 +137,15 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
|||||||
is_stump <- function() {
|
is_stump <- function() {
|
||||||
return(length(td$Feature) == 1 && is.na(td$Feature))
|
return(length(td$Feature) == 1 && is.na(td$Feature))
|
||||||
}
|
}
|
||||||
if (!text_has_feature_names) {
|
|
||||||
if (!is.null(feature_names) && !is_stump()) {
|
if (!is.null(feature_names) && !is_stump()) {
|
||||||
if (length(feature_names) <= max(as.numeric(td$Feature), na.rm = TRUE))
|
if (length(feature_names) <= max(as.numeric(td$Feature), na.rm = TRUE))
|
||||||
stop("feature_names has less elements than there are features used in the model")
|
stop("feature_names has less elements than there are features used in the model")
|
||||||
td[isLeaf == FALSE, Feature := feature_names[as.numeric(Feature) + 1]]
|
td[isLeaf == FALSE, Feature := feature_names[as.numeric(Feature) + 1]]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
# parse leaf lines
|
# parse leaf lines
|
||||||
leaf_rx <- paste0("leaf=(", anynumber_regex, "),cover=(", anynumber_regex, ")")
|
leaf_rx <- paste0("leaf=(", anynumber_regex, "),cover=(", anynumber_regex, ")")
|
||||||
leaf_cols <- c("Feature", "Gain", "Cover")
|
leaf_cols <- c("Feature", "Quality", "Cover")
|
||||||
td[
|
td[
|
||||||
isLeaf == TRUE,
|
isLeaf == TRUE,
|
||||||
(leaf_cols) := {
|
(leaf_cols) := {
|
||||||
@ -183,7 +160,7 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
|
|||||||
]
|
]
|
||||||
|
|
||||||
# convert some columns to numeric
|
# convert some columns to numeric
|
||||||
numeric_cols <- c("Split", "Gain", "Cover")
|
numeric_cols <- c("Split", "Quality", "Cover")
|
||||||
td[, (numeric_cols) := lapply(.SD, as.numeric), .SDcols = numeric_cols]
|
td[, (numeric_cols) := lapply(.SD, as.numeric), .SDcols = numeric_cols]
|
||||||
if (use_int_id) {
|
if (use_int_id) {
|
||||||
int_cols <- c("Yes", "No", "Missing")
|
int_cols <- c("Yes", "No", "Missing")
|
||||||
|
|||||||
@ -1,74 +1,62 @@
|
|||||||
#' Plot model tree depth
|
#' Plot model trees deepness
|
||||||
#'
|
#'
|
||||||
#' Visualizes distributions related to the depth of tree leaves.
|
#' Visualizes distributions related to depth of tree leafs.
|
||||||
#' - `xgb.plot.deepness()` uses base R graphics, while
|
#' \code{xgb.plot.deepness} uses base R graphics, while \code{xgb.ggplot.deepness} uses the ggplot backend.
|
||||||
#' - `xgb.ggplot.deepness()` uses "ggplot2".
|
|
||||||
#'
|
#'
|
||||||
#' @param model Either an `xgb.Booster` model, or the "data.table" returned by [xgb.model.dt.tree()].
|
#' @param model either an \code{xgb.Booster} model generated by the \code{xgb.train} function
|
||||||
#' @param which Which distribution to plot (see details).
|
#' or a data.table result of the \code{xgb.model.dt.tree} function.
|
||||||
#' @param plot Should the plot be shown? Default is `TRUE`.
|
#' @param plot (base R barplot) whether a barplot should be produced.
|
||||||
#' @param ... Other parameters passed to [graphics::barplot()] or [graphics::plot()].
|
#' If FALSE, only a data.table is returned.
|
||||||
|
#' @param which which distribution to plot (see details).
|
||||||
|
#' @param ... other parameters passed to \code{barplot} or \code{plot}.
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#'
|
#'
|
||||||
#' When `which = "2x1"`, two distributions with respect to the leaf depth
|
#' When \code{which="2x1"}, two distributions with respect to the leaf depth
|
||||||
#' are plotted on top of each other:
|
#' are plotted on top of each other:
|
||||||
#' 1. The distribution of the number of leaves in a tree model at a certain depth.
|
#' \itemize{
|
||||||
#' 2. The distribution of the average weighted number of observations ("cover")
|
#' \item the distribution of the number of leafs in a tree model at a certain depth;
|
||||||
#' ending up in leaves at a certain depth.
|
#' \item the distribution of average weighted number of observations ("cover")
|
||||||
|
#' ending up in leafs at certain depth.
|
||||||
|
#' }
|
||||||
|
#' Those could be helpful in determining sensible ranges of the \code{max_depth}
|
||||||
|
#' and \code{min_child_weight} parameters.
|
||||||
#'
|
#'
|
||||||
#' Those could be helpful in determining sensible ranges of the `max_depth`
|
#' When \code{which="max.depth"} or \code{which="med.depth"}, plots of either maximum or median depth
|
||||||
#' and `min_child_weight` parameters.
|
#' per tree with respect to tree number are created. And \code{which="med.weight"} allows to see how
|
||||||
#'
|
|
||||||
#' When `which = "max.depth"` or `which = "med.depth"`, plots of either maximum or
|
|
||||||
#' median depth per tree with respect to the tree number are created.
|
|
||||||
#'
|
|
||||||
#' Finally, `which = "med.weight"` allows to see how
|
|
||||||
#' a tree's median absolute leaf weight changes through the iterations.
|
#' a tree's median absolute leaf weight changes through the iterations.
|
||||||
#'
|
#'
|
||||||
#' These functions have been inspired by the blog post
|
#' This function was inspired by the blog post
|
||||||
#' <https://github.com/aysent/random-forest-leaf-visualization>.
|
#' \url{https://github.com/aysent/random-forest-leaf-visualization}.
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
#' The return value of the two functions is as follows:
|
|
||||||
#' - `xgb.plot.deepness()`: A "data.table" (invisibly).
|
|
||||||
#' Each row corresponds to a terminal leaf in the model. It contains its information
|
|
||||||
#' about depth, cover, and weight (used in calculating predictions).
|
|
||||||
#' If `plot = TRUE`, also a plot is shown.
|
|
||||||
#' - `xgb.ggplot.deepness()`: When `which = "2x1"`, a list of two "ggplot" objects,
|
|
||||||
#' and a single "ggplot" object otherwise.
|
|
||||||
#'
|
#'
|
||||||
#' @seealso [xgb.train()] and [xgb.model.dt.tree()].
|
#' Other than producing plots (when \code{plot=TRUE}), the \code{xgb.plot.deepness} function
|
||||||
|
#' silently returns a processed data.table where each row corresponds to a terminal leaf in a tree model,
|
||||||
|
#' and contains information about leaf's depth, cover, and weight (which is used in calculating predictions).
|
||||||
|
#'
|
||||||
|
#' The \code{xgb.ggplot.deepness} silently returns either a list of two ggplot graphs when \code{which="2x1"}
|
||||||
|
#' or a single ggplot graph for the other \code{which} options.
|
||||||
|
#'
|
||||||
|
#' @seealso
|
||||||
|
#'
|
||||||
|
#' \code{\link{xgb.train}}, \code{\link{xgb.model.dt.tree}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#'
|
#'
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' ## Keep the number of threads to 2 for examples
|
|
||||||
#' nthread <- 2
|
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
#'
|
||||||
#' ## Change max_depth to a higher number to get a more significant result
|
#' # Change max_depth to a higher number to get a more significant result
|
||||||
#' bst <- xgboost(
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 6,
|
||||||
#' data = agaricus.train$data,
|
#' eta = 0.1, nthread = 2, nrounds = 50, objective = "binary:logistic",
|
||||||
#' label = agaricus.train$label,
|
#' subsample = 0.5, min_child_weight = 2)
|
||||||
#' max_depth = 6,
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' nrounds = 50,
|
|
||||||
#' objective = "binary:logistic",
|
|
||||||
#' subsample = 0.5,
|
|
||||||
#' min_child_weight = 2
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' xgb.plot.deepness(bst)
|
#' xgb.plot.deepness(bst)
|
||||||
#' xgb.ggplot.deepness(bst)
|
#' xgb.ggplot.deepness(bst)
|
||||||
#'
|
#'
|
||||||
#' xgb.plot.deepness(
|
#' xgb.plot.deepness(bst, which='max.depth', pch=16, col=rgb(0,0,1,0.3), cex=2)
|
||||||
#' bst, which = "max.depth", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' xgb.plot.deepness(
|
#' xgb.plot.deepness(bst, which='med.weight', pch=16, col=rgb(0,0,1,0.3), cex=2)
|
||||||
#' bst, which = "med.weight", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' @rdname xgb.plot.deepness
|
#' @rdname xgb.plot.deepness
|
||||||
#' @export
|
#' @export
|
||||||
@ -92,7 +80,7 @@ xgb.plot.deepness <- function(model = NULL, which = c("2x1", "max.depth", "med.d
|
|||||||
stop("Model tree columns are not as expected!\n",
|
stop("Model tree columns are not as expected!\n",
|
||||||
" Note that this function works only for tree models.")
|
" Note that this function works only for tree models.")
|
||||||
|
|
||||||
dt_depths <- merge(get.leaf.depth(dt_tree), dt_tree[, .(ID, Cover, Weight = Gain)], by = "ID")
|
dt_depths <- merge(get.leaf.depth(dt_tree), dt_tree[, .(ID, Cover, Weight = Quality)], by = "ID")
|
||||||
setkeyv(dt_depths, c("Tree", "ID"))
|
setkeyv(dt_depths, c("Tree", "ID"))
|
||||||
# count by depth levels, and also calculate average cover at a depth
|
# count by depth levels, and also calculate average cover at a depth
|
||||||
dt_summaries <- dt_depths[, .(.N, Cover = mean(Cover)), Depth]
|
dt_summaries <- dt_depths[, .(.N, Cover = mean(Cover)), Depth]
|
||||||
@ -148,7 +136,7 @@ get.leaf.depth <- function(dt_tree) {
|
|||||||
# list of paths to each leaf in a tree
|
# list of paths to each leaf in a tree
|
||||||
paths <- lapply(paths_tmp$vpath, names)
|
paths <- lapply(paths_tmp$vpath, names)
|
||||||
# combine into a resulting path lengths table for a tree
|
# combine into a resulting path lengths table for a tree
|
||||||
data.table(Depth = lengths(paths), ID = To[Leaf == TRUE])
|
data.table(Depth = sapply(paths, length), ID = To[Leaf == TRUE])
|
||||||
}, by = Tree]
|
}, by = Tree]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +145,6 @@ get.leaf.depth <- function(dt_tree) {
|
|||||||
# They are mainly column names inferred by Data.table...
|
# They are mainly column names inferred by Data.table...
|
||||||
globalVariables(
|
globalVariables(
|
||||||
c(
|
c(
|
||||||
".N", "N", "Depth", "Gain", "Cover", "Tree", "ID", "Yes", "No", "Feature", "Leaf", "Weight"
|
".N", "N", "Depth", "Quality", "Cover", "Tree", "ID", "Yes", "No", "Feature", "Leaf", "Weight"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,75 +1,59 @@
|
|||||||
#' Plot feature importance
|
#' Plot feature importance as a bar graph
|
||||||
#'
|
#'
|
||||||
#' Represents previously calculated feature importance as a bar graph.
|
#' Represents previously calculated feature importance as a bar graph.
|
||||||
#' - `xgb.plot.importance()` uses base R graphics, while
|
#' \code{xgb.plot.importance} uses base R graphics, while \code{xgb.ggplot.importance} uses the ggplot backend.
|
||||||
#' - `xgb.ggplot.importance()` uses "ggplot".
|
|
||||||
#'
|
#'
|
||||||
#' @param importance_matrix A `data.table` as returned by [xgb.importance()].
|
#' @param importance_matrix a \code{data.table} returned by \code{\link{xgb.importance}}.
|
||||||
#' @param top_n Maximal number of top features to include into the plot.
|
#' @param top_n maximal number of top features to include into the plot.
|
||||||
#' @param measure The name of importance measure to plot.
|
#' @param measure the name of importance measure to plot.
|
||||||
#' When `NULL`, 'Gain' would be used for trees and 'Weight' would be used for gblinear.
|
#' When \code{NULL}, 'Gain' would be used for trees and 'Weight' would be used for gblinear.
|
||||||
#' @param rel_to_first Whether importance values should be represented as relative to
|
#' @param rel_to_first whether importance values should be represented as relative to the highest ranked feature.
|
||||||
#' the highest ranked feature, see Details.
|
#' See Details.
|
||||||
#' @param left_margin Adjust the left margin size to fit feature names.
|
#' @param left_margin (base R barplot) allows to adjust the left margin size to fit feature names.
|
||||||
#' When `NULL`, the existing `par("mar")` is used.
|
#' When it is NULL, the existing \code{par('mar')} is used.
|
||||||
#' @param cex Passed as `cex.names` parameter to [graphics::barplot()].
|
#' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{barplot}.
|
||||||
#' @param plot Should the barplot be shown? Default is `TRUE`.
|
#' @param plot (base R barplot) whether a barplot should be produced.
|
||||||
#' @param n_clusters A numeric vector containing the min and the max range
|
#' If FALSE, only a data.table is returned.
|
||||||
|
#' @param n_clusters (ggplot only) a \code{numeric} vector containing the min and the max range
|
||||||
#' of the possible number of clusters of bars.
|
#' of the possible number of clusters of bars.
|
||||||
#' @param ... Other parameters passed to [graphics::barplot()]
|
#' @param ... other parameters passed to \code{barplot} (except horiz, border, cex.names, names.arg, and las).
|
||||||
#' (except `horiz`, `border`, `cex.names`, `names.arg`, and `las`).
|
|
||||||
#' Only used in `xgb.plot.importance()`.
|
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#' The graph represents each feature as a horizontal bar of length proportional to the importance of a feature.
|
#' The graph represents each feature as a horizontal bar of length proportional to the importance of a feature.
|
||||||
#' Features are sorted by decreasing importance.
|
#' Features are shown ranked in a decreasing importance order.
|
||||||
#' It works for both "gblinear" and "gbtree" models.
|
#' It works for importances from both \code{gblinear} and \code{gbtree} models.
|
||||||
#'
|
#'
|
||||||
#' When `rel_to_first = FALSE`, the values would be plotted as in `importance_matrix`.
|
#' When \code{rel_to_first = FALSE}, the values would be plotted as they were in \code{importance_matrix}.
|
||||||
#' For a "gbtree" model, that would mean being normalized to the total of 1
|
#' For gbtree model, that would mean being normalized to the total of 1
|
||||||
#' ("what is feature's importance contribution relative to the whole model?").
|
#' ("what is feature's importance contribution relative to the whole model?").
|
||||||
#' For linear models, `rel_to_first = FALSE` would show actual values of the coefficients.
|
#' For linear models, \code{rel_to_first = FALSE} would show actual values of the coefficients.
|
||||||
#' Setting `rel_to_first = TRUE` allows to see the picture from the perspective of
|
#' Setting \code{rel_to_first = TRUE} allows to see the picture from the perspective of
|
||||||
#' "what is feature's importance contribution relative to the most important feature?"
|
#' "what is feature's importance contribution relative to the most important feature?"
|
||||||
#'
|
#'
|
||||||
#' The "ggplot" backend performs 1-D clustering of the importance values,
|
#' The ggplot-backend method also performs 1-D clustering of the importance values,
|
||||||
#' with bar colors corresponding to different clusters having similar importance values.
|
#' with bar colors corresponding to different clusters that have somewhat similar importance values.
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
#' The return value depends on the function:
|
#' The \code{xgb.plot.importance} function creates a \code{barplot} (when \code{plot=TRUE})
|
||||||
#' - `xgb.plot.importance()`: Invisibly, a "data.table" with `n_top` features sorted
|
#' and silently returns a processed data.table with \code{n_top} features sorted by importance.
|
||||||
#' by importance. If `plot = TRUE`, the values are also plotted as barplot.
|
|
||||||
#' - `xgb.ggplot.importance()`: A customizable "ggplot" object.
|
|
||||||
#' E.g., to change the title, set `+ ggtitle("A GRAPH NAME")`.
|
|
||||||
#'
|
#'
|
||||||
#' @seealso [graphics::barplot()]
|
#' The \code{xgb.ggplot.importance} function returns a ggplot graph which could be customized afterwards.
|
||||||
|
#' E.g., to change the title of the graph, add \code{+ ggtitle("A GRAPH NAME")} to the result.
|
||||||
|
#'
|
||||||
|
#' @seealso
|
||||||
|
#' \code{\link[graphics]{barplot}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' data(agaricus.train)
|
#' data(agaricus.train)
|
||||||
#'
|
#'
|
||||||
#' ## Keep the number of threads to 2 for examples
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 3,
|
||||||
#' nthread <- 2
|
#' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
|
||||||
#' bst <- xgboost(
|
|
||||||
#' data = agaricus.train$data,
|
|
||||||
#' label = agaricus.train$label,
|
|
||||||
#' max_depth = 3,
|
|
||||||
#' eta = 1,
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' nrounds = 2,
|
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' importance_matrix <- xgb.importance(colnames(agaricus.train$data), model = bst)
|
#' importance_matrix <- xgb.importance(colnames(agaricus.train$data), model = bst)
|
||||||
#' xgb.plot.importance(
|
|
||||||
#' importance_matrix, rel_to_first = TRUE, xlab = "Relative importance"
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' gg <- xgb.ggplot.importance(
|
#' xgb.plot.importance(importance_matrix, rel_to_first = TRUE, xlab = "Relative importance")
|
||||||
#' importance_matrix, measure = "Frequency", rel_to_first = TRUE
|
#'
|
||||||
#' )
|
#' (gg <- xgb.ggplot.importance(importance_matrix, measure = "Frequency", rel_to_first = TRUE))
|
||||||
#' gg
|
|
||||||
#' gg + ggplot2::ylab("Frequency")
|
#' gg + ggplot2::ylab("Frequency")
|
||||||
#'
|
#'
|
||||||
#' @rdname xgb.plot.importance
|
#' @rdname xgb.plot.importance
|
||||||
@ -98,13 +82,7 @@ xgb.plot.importance <- function(importance_matrix = NULL, top_n = NULL, measure
|
|||||||
}
|
}
|
||||||
|
|
||||||
# also aggregate, just in case when the values were not yet summed up by feature
|
# also aggregate, just in case when the values were not yet summed up by feature
|
||||||
importance_matrix <- importance_matrix[
|
importance_matrix <- importance_matrix[, Importance := sum(get(measure)), by = Feature]
|
||||||
, lapply(.SD, sum)
|
|
||||||
, .SDcols = setdiff(names(importance_matrix), "Feature")
|
|
||||||
, by = Feature
|
|
||||||
][
|
|
||||||
, Importance := get(measure)
|
|
||||||
]
|
|
||||||
|
|
||||||
# make sure it's ordered
|
# make sure it's ordered
|
||||||
importance_matrix <- importance_matrix[order(-abs(Importance))]
|
importance_matrix <- importance_matrix[order(-abs(Importance))]
|
||||||
|
|||||||
@ -1,10 +1,14 @@
|
|||||||
#' Project all trees on one tree
|
#' Project all trees on one tree and plot it
|
||||||
#'
|
#'
|
||||||
#' Visualization of the ensemble of trees as a single collective unit.
|
#' Visualization of the ensemble of trees as a single collective unit.
|
||||||
#'
|
#'
|
||||||
#' @inheritParams xgb.plot.tree
|
#' @param model produced by the \code{xgb.train} function.
|
||||||
#' @param features_keep Number of features to keep in each position of the multi trees,
|
#' @param feature_names names of each feature as a \code{character} vector.
|
||||||
#' by default 5.
|
#' @param features_keep number of features to keep in each position of the multi trees.
|
||||||
|
#' @param plot_width width in pixels of the graph to produce
|
||||||
|
#' @param plot_height height in pixels of the graph to produce
|
||||||
|
#' @param render a logical flag for whether the graph should be rendered (see Value).
|
||||||
|
#' @param ... currently not used
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#'
|
#'
|
||||||
@ -20,55 +24,49 @@
|
|||||||
#' Moreover, the trees tend to reuse the same features.
|
#' Moreover, the trees tend to reuse the same features.
|
||||||
#'
|
#'
|
||||||
#' The function projects each tree onto one, and keeps for each position the
|
#' The function projects each tree onto one, and keeps for each position the
|
||||||
#' `features_keep` first features (based on the Gain per feature measure).
|
#' \code{features_keep} first features (based on the Gain per feature measure).
|
||||||
#'
|
#'
|
||||||
#' This function is inspired by this blog post:
|
#' This function is inspired by this blog post:
|
||||||
#' <https://wellecks.wordpress.com/2015/02/21/peering-into-the-black-box-visualizing-lambdamart/>
|
#' \url{https://wellecks.wordpress.com/2015/02/21/peering-into-the-black-box-visualizing-lambdamart/}
|
||||||
#'
|
#'
|
||||||
#' @inherit xgb.plot.tree return
|
#' @return
|
||||||
|
#'
|
||||||
|
#' When \code{render = TRUE}:
|
||||||
|
#' returns a rendered graph object which is an \code{htmlwidget} of class \code{grViz}.
|
||||||
|
#' Similar to ggplot objects, it needs to be printed to see it when not running from command line.
|
||||||
|
#'
|
||||||
|
#' When \code{render = FALSE}:
|
||||||
|
#' silently returns a graph object which is of DiagrammeR's class \code{dgr_graph}.
|
||||||
|
#' This could be useful if one wants to modify some of the graph attributes
|
||||||
|
#' before rendering the graph with \code{\link[DiagrammeR]{render_graph}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#'
|
#'
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#'
|
#'
|
||||||
#' ## Keep the number of threads to 2 for examples
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 15,
|
||||||
#' nthread <- 2
|
#' eta = 1, nthread = 2, nrounds = 30, objective = "binary:logistic",
|
||||||
#' data.table::setDTthreads(nthread)
|
#' min_child_weight = 50, verbose = 0)
|
||||||
#'
|
|
||||||
#' bst <- xgboost(
|
|
||||||
#' data = agaricus.train$data,
|
|
||||||
#' label = agaricus.train$label,
|
|
||||||
#' max_depth = 15,
|
|
||||||
#' eta = 1,
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' nrounds = 30,
|
|
||||||
#' objective = "binary:logistic",
|
|
||||||
#' min_child_weight = 50,
|
|
||||||
#' verbose = 0
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' p <- xgb.plot.multi.trees(model = bst, features_keep = 3)
|
#' p <- xgb.plot.multi.trees(model = bst, features_keep = 3)
|
||||||
#' print(p)
|
#' print(p)
|
||||||
#'
|
#'
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' # Below is an example of how to save this plot to a file.
|
#' # Below is an example of how to save this plot to a file.
|
||||||
#' # Note that for export_graph() to work, the {DiagrammeRsvg} and {rsvg} packages
|
#' # Note that for `export_graph` to work, the DiagrammeRsvg and rsvg packages must also be installed.
|
||||||
#' # must also be installed.
|
|
||||||
#'
|
|
||||||
#' library(DiagrammeR)
|
#' library(DiagrammeR)
|
||||||
#'
|
#' gr <- xgb.plot.multi.trees(model=bst, features_keep = 3, render=FALSE)
|
||||||
#' gr <- xgb.plot.multi.trees(model = bst, features_keep = 3, render = FALSE)
|
#' export_graph(gr, 'tree.pdf', width=1500, height=600)
|
||||||
#' export_graph(gr, "tree.pdf", width = 1500, height = 600)
|
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.plot.multi.trees <- function(model, features_keep = 5, plot_width = NULL, plot_height = NULL,
|
xgb.plot.multi.trees <- function(model, feature_names = NULL, features_keep = 5, plot_width = NULL, plot_height = NULL,
|
||||||
render = TRUE, ...) {
|
render = TRUE, ...) {
|
||||||
if (!requireNamespace("DiagrammeR", quietly = TRUE)) {
|
if (!requireNamespace("DiagrammeR", quietly = TRUE)) {
|
||||||
stop("DiagrammeR is required for xgb.plot.multi.trees")
|
stop("DiagrammeR is required for xgb.plot.multi.trees")
|
||||||
}
|
}
|
||||||
check.deprecation(...)
|
check.deprecation(...)
|
||||||
tree.matrix <- xgb.model.dt.tree(model = model)
|
tree.matrix <- xgb.model.dt.tree(feature_names = feature_names, model = model)
|
||||||
|
|
||||||
# first number of the path represents the tree, then the following numbers are related to the path to follow
|
# first number of the path represents the tree, then the following numbers are related to the path to follow
|
||||||
# root init
|
# root init
|
||||||
@ -95,13 +93,13 @@ xgb.plot.multi.trees <- function(model, features_keep = 5, plot_width = NULL, pl
|
|||||||
data.table::set(tree.matrix, j = nm, value = sub("^\\d+-", "", tree.matrix[[nm]]))
|
data.table::set(tree.matrix, j = nm, value = sub("^\\d+-", "", tree.matrix[[nm]]))
|
||||||
|
|
||||||
nodes.dt <- tree.matrix[
|
nodes.dt <- tree.matrix[
|
||||||
, .(Gain = sum(Gain))
|
, .(Quality = sum(Quality))
|
||||||
, by = .(abs.node.position, Feature)
|
, by = .(abs.node.position, Feature)
|
||||||
][, .(Text = paste0(
|
][, .(Text = paste0(
|
||||||
paste0(
|
paste0(
|
||||||
Feature[seq_len(min(length(Feature), features_keep))],
|
Feature[seq_len(min(length(Feature), features_keep))],
|
||||||
" (",
|
" (",
|
||||||
format(Gain[seq_len(min(length(Gain), features_keep))], digits = 5),
|
format(Quality[seq_len(min(length(Quality), features_keep))], digits = 5),
|
||||||
")"
|
")"
|
||||||
),
|
),
|
||||||
collapse = "\n"
|
collapse = "\n"
|
||||||
|
|||||||
@ -1,165 +1,106 @@
|
|||||||
#' SHAP dependence plots
|
#' SHAP contribution dependency plots
|
||||||
#'
|
#'
|
||||||
#' Visualizes SHAP values against feature values to gain an impression of feature effects.
|
#' Visualizing the SHAP feature contribution to prediction dependencies on feature value.
|
||||||
#'
|
#'
|
||||||
#' @param data The data to explain as a `matrix` or `dgCMatrix`.
|
#' @param data data as a \code{matrix} or \code{dgCMatrix}.
|
||||||
#' @param shap_contrib Matrix of SHAP contributions of `data`.
|
#' @param shap_contrib a matrix of SHAP contributions that was computed earlier for the above
|
||||||
#' The default (`NULL`) computes it from `model` and `data`.
|
#' \code{data}. When it is NULL, it is computed internally using \code{model} and \code{data}.
|
||||||
#' @param features Vector of column indices or feature names to plot.
|
#' @param features a vector of either column indices or of feature names to plot. When it is NULL,
|
||||||
#' When `NULL` (default), the `top_n` most important features are selected
|
#' feature importance is calculated, and \code{top_n} high ranked features are taken.
|
||||||
#' by [xgb.importance()].
|
#' @param top_n when \code{features} is NULL, top_n [1, 100] most important features in a model are taken.
|
||||||
#' @param top_n How many of the most important features (<= 100) should be selected?
|
#' @param model an \code{xgb.Booster} model. It has to be provided when either \code{shap_contrib}
|
||||||
#' By default 1 for SHAP dependence and 10 for SHAP summary).
|
#' or \code{features} is missing.
|
||||||
#' Only used when `features = NULL`.
|
#' @param trees passed to \code{\link{xgb.importance}} when \code{features = NULL}.
|
||||||
#' @param model An `xgb.Booster` model. Only required when `shap_contrib = NULL` or
|
#' @param target_class is only relevant for multiclass models. When it is set to a 0-based class index,
|
||||||
#' `features = NULL`.
|
#' only SHAP contributions for that specific class are used.
|
||||||
#' @param trees Passed to [xgb.importance()] when `features = NULL`.
|
#' If it is not set, SHAP importances are averaged over all classes.
|
||||||
#' @param target_class Only relevant for multiclass models. The default (`NULL`)
|
#' @param approxcontrib passed to \code{\link{predict.xgb.Booster}} when \code{shap_contrib = NULL}.
|
||||||
#' averages the SHAP values over all classes. Pass a (0-based) class index
|
#' @param subsample a random fraction of data points to use for plotting. When it is NULL,
|
||||||
#' to show only SHAP values of that class.
|
#' it is set so that up to 100K data points are used.
|
||||||
#' @param approxcontrib Passed to `predict()` when `shap_contrib = NULL`.
|
#' @param n_col a number of columns in a grid of plots.
|
||||||
#' @param subsample Fraction of data points randomly picked for plotting.
|
#' @param col color of the scatterplot markers.
|
||||||
#' The default (`NULL`) will use up to 100k data points.
|
#' @param pch scatterplot marker.
|
||||||
#' @param n_col Number of columns in a grid of plots.
|
#' @param discrete_n_uniq a maximal number of unique values in a feature to consider it as discrete.
|
||||||
#' @param col Color of the scatterplot markers.
|
#' @param discrete_jitter an \code{amount} parameter of jitter added to discrete features' positions.
|
||||||
#' @param pch Scatterplot marker.
|
#' @param ylab a y-axis label in 1D plots.
|
||||||
#' @param discrete_n_uniq Maximal number of unique feature values to consider the
|
#' @param plot_NA whether the contributions of cases with missing values should also be plotted.
|
||||||
#' feature as discrete.
|
#' @param col_NA a color of marker for missing value contributions.
|
||||||
#' @param discrete_jitter Jitter amount added to the values of discrete features.
|
#' @param pch_NA a marker type for NA values.
|
||||||
#' @param ylab The y-axis label in 1D plots.
|
#' @param pos_NA a relative position of the x-location where NA values are shown:
|
||||||
#' @param plot_NA Should contributions of cases with missing values be plotted?
|
#' \code{min(x) + (max(x) - min(x)) * pos_NA}.
|
||||||
#' Default is `TRUE`.
|
#' @param plot_loess whether to plot loess-smoothed curves. The smoothing is only done for features with
|
||||||
#' @param col_NA Color of marker for missing value contributions.
|
#' more than 5 distinct values.
|
||||||
#' @param pch_NA Marker type for `NA` values.
|
#' @param col_loess a color to use for the loess curves.
|
||||||
#' @param pos_NA Relative position of the x-location where `NA` values are shown:
|
#' @param span_loess the \code{span} parameter in \code{\link[stats]{loess}}'s call.
|
||||||
#' `min(x) + (max(x) - min(x)) * pos_NA`.
|
#' @param which whether to do univariate or bivariate plotting. NOTE: only 1D is implemented so far.
|
||||||
#' @param plot_loess Should loess-smoothed curves be plotted? (Default is `TRUE`).
|
#' @param plot whether a plot should be drawn. If FALSE, only a list of matrices is returned.
|
||||||
#' The smoothing is only done for features with more than 5 distinct values.
|
#' @param ... other parameters passed to \code{plot}.
|
||||||
#' @param col_loess Color of loess curves.
|
|
||||||
#' @param span_loess The `span` parameter of [stats::loess()].
|
|
||||||
#' @param which Whether to do univariate or bivariate plotting. Currently, only "1d" is implemented.
|
|
||||||
#' @param plot Should the plot be drawn? (Default is `TRUE`).
|
|
||||||
#' If `FALSE`, only a list of matrices is returned.
|
|
||||||
#' @param ... Other parameters passed to [graphics::plot()].
|
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#'
|
#'
|
||||||
#' These scatterplots represent how SHAP feature contributions depend of feature values.
|
#' These scatterplots represent how SHAP feature contributions depend of feature values.
|
||||||
#' The similarity to partial dependence plots is that they also give an idea for how feature values
|
#' The similarity to partial dependency plots is that they also give an idea for how feature values
|
||||||
#' affect predictions. However, in partial dependence plots, we see marginal dependencies
|
#' affect predictions. However, in partial dependency plots, we usually see marginal dependencies
|
||||||
#' of model prediction on feature value, while SHAP dependence plots display the estimated
|
#' of model prediction on feature value, while SHAP contribution dependency plots display the estimated
|
||||||
#' contributions of a feature to the prediction for each individual case.
|
#' contributions of a feature to model prediction for each individual case.
|
||||||
#'
|
#'
|
||||||
#' When `plot_loess = TRUE`, feature values are rounded to three significant digits and
|
#' When \code{plot_loess = TRUE} is set, feature values are rounded to 3 significant digits and
|
||||||
#' weighted LOESS is computed and plotted, where the weights are the numbers of data points
|
#' weighted LOESS is computed and plotted, where weights are the numbers of data points
|
||||||
#' at each rounded value.
|
#' at each rounded value.
|
||||||
#'
|
#'
|
||||||
#' Note: SHAP contributions are on the scale of the model margin.
|
#' Note: SHAP contributions are shown on the scale of model margin. E.g., for a logistic binomial objective,
|
||||||
#' E.g., for a logistic binomial objective, the margin is on log-odds scale.
|
#' the margin is prediction before a sigmoidal transform into probability-like values.
|
||||||
#' Also, since SHAP stands for "SHapley Additive exPlanation" (model prediction = sum of SHAP
|
#' Also, since SHAP stands for "SHapley Additive exPlanation" (model prediction = sum of SHAP
|
||||||
#' contributions for all features + bias), depending on the objective used, transforming SHAP
|
#' contributions for all features + bias), depending on the objective used, transforming SHAP
|
||||||
#' contributions for a feature from the marginal to the prediction space is not necessarily
|
#' contributions for a feature from the marginal to the prediction space is not necessarily
|
||||||
#' a meaningful thing to do.
|
#' a meaningful thing to do.
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
#' In addition to producing plots (when `plot = TRUE`), it silently returns a list of two matrices:
|
#'
|
||||||
#' - `data`: Feature value matrix.
|
#' In addition to producing plots (when \code{plot=TRUE}), it silently returns a list of two matrices:
|
||||||
#' - `shap_contrib`: Corresponding SHAP value matrix.
|
#' \itemize{
|
||||||
|
#' \item \code{data} the values of selected features;
|
||||||
|
#' \item \code{shap_contrib} the contributions of selected features.
|
||||||
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @references
|
#' @references
|
||||||
#' 1. Scott M. Lundberg, Su-In Lee, "A Unified Approach to Interpreting Model Predictions",
|
#'
|
||||||
#' NIPS Proceedings 2017, <https://arxiv.org/abs/1705.07874>
|
#' Scott M. Lundberg, Su-In Lee, "A Unified Approach to Interpreting Model Predictions", NIPS Proceedings 2017, \url{https://arxiv.org/abs/1705.07874}
|
||||||
#' 2. Scott M. Lundberg, Su-In Lee, "Consistent feature attribution for tree ensembles",
|
#'
|
||||||
#' <https://arxiv.org/abs/1706.06060>
|
#' Scott M. Lundberg, Su-In Lee, "Consistent feature attribution for tree ensembles", \url{https://arxiv.org/abs/1706.06060}
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#'
|
#'
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package = "xgboost")
|
#' data(agaricus.test, package='xgboost')
|
||||||
#'
|
#'
|
||||||
#' ## Keep the number of threads to 1 for examples
|
#' bst <- xgboost(agaricus.train$data, agaricus.train$label, nrounds = 50,
|
||||||
#' nthread <- 1
|
#' eta = 0.1, max_depth = 3, subsample = .5,
|
||||||
#' data.table::setDTthreads(nthread)
|
#' method = "hist", objective = "binary:logistic", nthread = 2, verbose = 0)
|
||||||
#' nrounds <- 20
|
|
||||||
#'
|
|
||||||
#' bst <- xgboost(
|
|
||||||
#' agaricus.train$data,
|
|
||||||
#' agaricus.train$label,
|
|
||||||
#' nrounds = nrounds,
|
|
||||||
#' eta = 0.1,
|
|
||||||
#' max_depth = 3,
|
|
||||||
#' subsample = 0.5,
|
|
||||||
#' objective = "binary:logistic",
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' verbose = 0
|
|
||||||
#' )
|
|
||||||
#'
|
#'
|
||||||
#' xgb.plot.shap(agaricus.test$data, model = bst, features = "odor=none")
|
#' xgb.plot.shap(agaricus.test$data, model = bst, features = "odor=none")
|
||||||
#'
|
|
||||||
#' contr <- predict(bst, agaricus.test$data, predcontrib = TRUE)
|
#' contr <- predict(bst, agaricus.test$data, predcontrib = TRUE)
|
||||||
#' xgb.plot.shap(agaricus.test$data, contr, model = bst, top_n = 12, n_col = 3)
|
#' xgb.plot.shap(agaricus.test$data, contr, model = bst, top_n = 12, n_col = 3)
|
||||||
|
#' xgb.ggplot.shap.summary(agaricus.test$data, contr, model = bst, top_n = 12) # Summary plot
|
||||||
#'
|
#'
|
||||||
#' # Summary plot
|
#' # multiclass example - plots for each class separately:
|
||||||
#' xgb.ggplot.shap.summary(agaricus.test$data, contr, model = bst, top_n = 12)
|
|
||||||
#'
|
|
||||||
#' # Multiclass example - plots for each class separately:
|
|
||||||
#' nclass <- 3
|
#' nclass <- 3
|
||||||
|
#' nrounds <- 20
|
||||||
#' x <- as.matrix(iris[, -5])
|
#' x <- as.matrix(iris[, -5])
|
||||||
#' set.seed(123)
|
#' set.seed(123)
|
||||||
#' is.na(x[sample(nrow(x) * 4, 30)]) <- TRUE # introduce some missing values
|
#' is.na(x[sample(nrow(x) * 4, 30)]) <- TRUE # introduce some missing values
|
||||||
#'
|
#' mbst <- xgboost(data = x, label = as.numeric(iris$Species) - 1, nrounds = nrounds,
|
||||||
#' mbst <- xgboost(
|
#' max_depth = 2, eta = 0.3, subsample = .5, nthread = 2,
|
||||||
#' data = x,
|
#' objective = "multi:softprob", num_class = nclass, verbose = 0)
|
||||||
#' label = as.numeric(iris$Species) - 1,
|
#' trees0 <- seq(from=0, by=nclass, length.out=nrounds)
|
||||||
#' nrounds = nrounds,
|
|
||||||
#' max_depth = 2,
|
|
||||||
#' eta = 0.3,
|
|
||||||
#' subsample = 0.5,
|
|
||||||
#' nthread = nthread,
|
|
||||||
#' objective = "multi:softprob",
|
|
||||||
#' num_class = nclass,
|
|
||||||
#' verbose = 0
|
|
||||||
#' )
|
|
||||||
#' trees0 <- seq(from = 0, by = nclass, length.out = nrounds)
|
|
||||||
#' col <- rgb(0, 0, 1, 0.5)
|
#' col <- rgb(0, 0, 1, 0.5)
|
||||||
#' xgb.plot.shap(
|
#' xgb.plot.shap(x, model = mbst, trees = trees0, target_class = 0, top_n = 4,
|
||||||
#' x,
|
#' n_col = 2, col = col, pch = 16, pch_NA = 17)
|
||||||
#' model = mbst,
|
#' xgb.plot.shap(x, model = mbst, trees = trees0 + 1, target_class = 1, top_n = 4,
|
||||||
#' trees = trees0,
|
#' n_col = 2, col = col, pch = 16, pch_NA = 17)
|
||||||
#' target_class = 0,
|
#' xgb.plot.shap(x, model = mbst, trees = trees0 + 2, target_class = 2, top_n = 4,
|
||||||
#' top_n = 4,
|
#' n_col = 2, col = col, pch = 16, pch_NA = 17)
|
||||||
#' n_col = 2,
|
#' xgb.ggplot.shap.summary(x, model = mbst, target_class = 0, top_n = 4) # Summary plot
|
||||||
#' col = col,
|
|
||||||
#' pch = 16,
|
|
||||||
#' pch_NA = 17
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' xgb.plot.shap(
|
|
||||||
#' x,
|
|
||||||
#' model = mbst,
|
|
||||||
#' trees = trees0 + 1,
|
|
||||||
#' target_class = 1,
|
|
||||||
#' top_n = 4,
|
|
||||||
#' n_col = 2,
|
|
||||||
#' col = col,
|
|
||||||
#' pch = 16,
|
|
||||||
#' pch_NA = 17
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' xgb.plot.shap(
|
|
||||||
#' x,
|
|
||||||
#' model = mbst,
|
|
||||||
#' trees = trees0 + 2,
|
|
||||||
#' target_class = 2,
|
|
||||||
#' top_n = 4,
|
|
||||||
#' n_col = 2,
|
|
||||||
#' col = col,
|
|
||||||
#' pch = 16,
|
|
||||||
#' pch_NA = 17
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' # Summary plot
|
|
||||||
#' xgb.ggplot.shap.summary(x, model = mbst, target_class = 0, top_n = 4)
|
|
||||||
#'
|
#'
|
||||||
#' @rdname xgb.plot.shap
|
#' @rdname xgb.plot.shap
|
||||||
#' @export
|
#' @export
|
||||||
@ -242,48 +183,41 @@ xgb.plot.shap <- function(data, shap_contrib = NULL, features = NULL, top_n = 1,
|
|||||||
invisible(list(data = data, shap_contrib = shap_contrib))
|
invisible(list(data = data, shap_contrib = shap_contrib))
|
||||||
}
|
}
|
||||||
|
|
||||||
#' SHAP summary plot
|
#' SHAP contribution dependency summary plot
|
||||||
#'
|
#'
|
||||||
#' Visualizes SHAP contributions of different features.
|
#' Compare SHAP contributions of different features.
|
||||||
#'
|
#'
|
||||||
#' A point plot (each point representing one observation from `data`) is
|
#' A point plot (each point representing one sample from \code{data}) is
|
||||||
#' produced for each feature, with the points plotted on the SHAP value axis.
|
#' produced for each feature, with the points plotted on the SHAP value axis.
|
||||||
#' Each point (observation) is coloured based on its feature value.
|
#' Each point (observation) is coloured based on its feature value. The plot
|
||||||
#'
|
#' hence allows us to see which features have a negative / positive contribution
|
||||||
#' The plot allows to see which features have a negative / positive contribution
|
|
||||||
#' on the model prediction, and whether the contribution is different for larger
|
#' on the model prediction, and whether the contribution is different for larger
|
||||||
#' or smaller values of the feature. Inspired by the summary plot of
|
#' or smaller values of the feature. We effectively try to replicate the
|
||||||
#' <https://github.com/shap/shap>.
|
#' \code{summary_plot} function from https://github.com/slundberg/shap.
|
||||||
#'
|
#'
|
||||||
#' @inheritParams xgb.plot.shap
|
#' @inheritParams xgb.plot.shap
|
||||||
#'
|
#'
|
||||||
#' @return A `ggplot2` object.
|
#' @return A \code{ggplot2} object.
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples # See \code{\link{xgb.plot.shap}}.
|
||||||
#' # See examples in xgb.plot.shap()
|
#' @seealso \code{\link{xgb.plot.shap}}, \code{\link{xgb.ggplot.shap.summary}},
|
||||||
#'
|
#' \url{https://github.com/slundberg/shap}
|
||||||
#' @seealso [xgb.plot.shap()], [xgb.ggplot.shap.summary()],
|
|
||||||
#' and the Python library <https://github.com/shap/shap>.
|
|
||||||
xgb.plot.shap.summary <- function(data, shap_contrib = NULL, features = NULL, top_n = 10, model = NULL,
|
xgb.plot.shap.summary <- function(data, shap_contrib = NULL, features = NULL, top_n = 10, model = NULL,
|
||||||
trees = NULL, target_class = NULL, approxcontrib = FALSE, subsample = NULL) {
|
trees = NULL, target_class = NULL, approxcontrib = FALSE, subsample = NULL) {
|
||||||
# Only ggplot implementation is available.
|
# Only ggplot implementation is available.
|
||||||
xgb.ggplot.shap.summary(data, shap_contrib, features, top_n, model, trees, target_class, approxcontrib, subsample)
|
xgb.ggplot.shap.summary(data, shap_contrib, features, top_n, model, trees, target_class, approxcontrib, subsample)
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Prepare data for SHAP plots
|
#' Prepare data for SHAP plots. To be used in xgb.plot.shap, xgb.plot.shap.summary, etc.
|
||||||
#'
|
#' Internal utility function.
|
||||||
#' Internal function used in [xgb.plot.shap()], [xgb.plot.shap.summary()], etc.
|
|
||||||
#'
|
#'
|
||||||
#' @inheritParams xgb.plot.shap
|
#' @inheritParams xgb.plot.shap
|
||||||
#' @param max_observations Maximum number of observations to consider.
|
|
||||||
#' @keywords internal
|
#' @keywords internal
|
||||||
#' @noRd
|
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return A list containing: 'data', a matrix containing sample observations
|
||||||
#' A list containing:
|
#' and their feature values; 'shap_contrib', a matrix containing the SHAP contribution
|
||||||
#' - `data`: The matrix of feature values.
|
#' values for these observations.
|
||||||
#' - `shap_contrib`: The matrix with corresponding SHAP values.
|
|
||||||
xgb.shap.data <- function(data, shap_contrib = NULL, features = NULL, top_n = 1, model = NULL,
|
xgb.shap.data <- function(data, shap_contrib = NULL, features = NULL, top_n = 1, model = NULL,
|
||||||
trees = NULL, target_class = NULL, approxcontrib = FALSE,
|
trees = NULL, target_class = NULL, approxcontrib = FALSE,
|
||||||
subsample = NULL, max_observations = 100000) {
|
subsample = NULL, max_observations = 100000) {
|
||||||
@ -303,11 +237,7 @@ xgb.shap.data <- function(data, shap_contrib = NULL, features = NULL, top_n = 1,
|
|||||||
if (is.character(features) && is.null(colnames(data)))
|
if (is.character(features) && is.null(colnames(data)))
|
||||||
stop("either provide `data` with column names or provide `features` as column indices")
|
stop("either provide `data` with column names or provide `features` as column indices")
|
||||||
|
|
||||||
model_feature_names <- NULL
|
if (is.null(model$feature_names) && model$nfeatures != ncol(data))
|
||||||
if (is.null(features) && !is.null(model)) {
|
|
||||||
model_feature_names <- xgb.feature_names(model)
|
|
||||||
}
|
|
||||||
if (is.null(model_feature_names) && xgb.num_feature(model) != ncol(data))
|
|
||||||
stop("if model has no feature_names, columns in `data` must match features in model")
|
stop("if model has no feature_names, columns in `data` must match features in model")
|
||||||
|
|
||||||
if (!is.null(subsample)) {
|
if (!is.null(subsample)) {
|
||||||
@ -336,7 +266,7 @@ xgb.shap.data <- function(data, shap_contrib = NULL, features = NULL, top_n = 1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is.null(features)) {
|
if (is.null(features)) {
|
||||||
if (!is.null(model_feature_names)) {
|
if (!is.null(model$feature_names)) {
|
||||||
imp <- xgb.importance(model = model, trees = trees)
|
imp <- xgb.importance(model = model, trees = trees)
|
||||||
} else {
|
} else {
|
||||||
imp <- xgb.importance(model = model, trees = trees, feature_names = colnames(data))
|
imp <- xgb.importance(model = model, trees = trees, feature_names = colnames(data))
|
||||||
|
|||||||
@ -1,109 +1,74 @@
|
|||||||
#' Plot boosted trees
|
#' Plot a boosted tree model
|
||||||
#'
|
#'
|
||||||
#' Read a tree model text dump and plot the model.
|
#' Read a tree model text dump and plot the model.
|
||||||
#'
|
#'
|
||||||
#' @param model Object of class `xgb.Booster`. If it contains feature names (they can be set through
|
#' @param feature_names names of each feature as a \code{character} vector.
|
||||||
#' \link{setinfo}), they will be used in the output from this function.
|
#' @param model produced by the \code{xgb.train} function.
|
||||||
#' @param trees An integer vector of tree indices that should be used.
|
#' @param trees an integer vector of tree indices that should be visualized.
|
||||||
#' The default (`NULL`) uses all trees.
|
#' If set to \code{NULL}, all trees of the model are included.
|
||||||
#' Useful, e.g., in multiclass classification to get only
|
#' IMPORTANT: the tree index in xgboost model is zero-based
|
||||||
#' the trees of one class. *Important*: the tree index in XGBoost models
|
#' (e.g., use \code{trees = 0:2} for the first 3 trees in a model).
|
||||||
#' is zero-based (e.g., use `trees = 0:2` for the first three trees).
|
#' @param plot_width the width of the diagram in pixels.
|
||||||
#' @param plot_width,plot_height Width and height of the graph in pixels.
|
#' @param plot_height the height of the diagram in pixels.
|
||||||
#' The values are passed to [DiagrammeR::render_graph()].
|
#' @param render a logical flag for whether the graph should be rendered (see Value).
|
||||||
#' @param render Should the graph be rendered or not? The default is `TRUE`.
|
|
||||||
#' @param show_node_id a logical flag for whether to show node id's in the graph.
|
#' @param show_node_id a logical flag for whether to show node id's in the graph.
|
||||||
#' @param style Style to use for the plot. Options are:\itemize{
|
|
||||||
#' \item `"xgboost"`: will use the plot style defined in the core XGBoost library,
|
|
||||||
#' which is shared between different interfaces through the 'dot' format. This
|
|
||||||
#' style was not available before version 2.1.0 in R. It always plots the trees
|
|
||||||
#' vertically (from top to bottom).
|
|
||||||
#' \item `"R"`: will use the style defined from XGBoost's R interface, which predates
|
|
||||||
#' the introducition of the standardized style from the core library. It might plot
|
|
||||||
#' the trees horizontally (from left to right).
|
|
||||||
#' }
|
|
||||||
#'
|
|
||||||
#' Note that `style="xgboost"` is only supported when all of the following conditions are met:\itemize{
|
|
||||||
#' \item Only a single tree is being plotted.
|
|
||||||
#' \item Node IDs are not added to the graph.
|
|
||||||
#' \item The graph is being returned as `htmlwidget` (`render=TRUE`).
|
|
||||||
#' }
|
|
||||||
#' @param ... currently not used.
|
#' @param ... currently not used.
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#'
|
#'
|
||||||
#' When using `style="xgboost"`, the content of each node is visualized as follows:
|
#' The content of each node is organised that way:
|
||||||
#' - For non-terminal nodes, it will display the split condition (number or name if
|
|
||||||
#' available, and the condition that would decide to which node to go next).
|
|
||||||
#' - Those nodes will be connected to their children by arrows that indicate whether the
|
|
||||||
#' branch corresponds to the condition being met or not being met.
|
|
||||||
#' - Terminal (leaf) nodes contain the margin to add when ending there.
|
|
||||||
#'
|
#'
|
||||||
#' When using `style="R"`, the content of each node is visualized like this:
|
#' \itemize{
|
||||||
#' - *Feature name*.
|
#' \item Feature name.
|
||||||
#' - *Cover:* The sum of second order gradients of training data.
|
#' \item \code{Cover}: The sum of second order gradient of training data classified to the leaf.
|
||||||
#' For the squared loss, this simply corresponds to the number of instances in the node.
|
#' If it is square loss, this simply corresponds to the number of instances seen by a split
|
||||||
#' The deeper in the tree, the lower the value.
|
#' or collected by a leaf during training.
|
||||||
#' - *Gain* (for split nodes): Information gain metric of a split
|
#' The deeper in the tree a node is, the lower this metric will be.
|
||||||
|
#' \item \code{Gain} (for split nodes): the information gain metric of a split
|
||||||
#' (corresponds to the importance of the node in the model).
|
#' (corresponds to the importance of the node in the model).
|
||||||
#' - *Value* (for leaves): Margin value that the leaf may contribute to the prediction.
|
#' \item \code{Value} (for leafs): the margin value that the leaf may contribute to prediction.
|
||||||
#'
|
#' }
|
||||||
#' The tree root nodes also indicate the tree index (0-based).
|
#' The tree root nodes also indicate the Tree index (0-based).
|
||||||
#'
|
#'
|
||||||
#' The "Yes" branches are marked by the "< split_value" label.
|
#' The "Yes" branches are marked by the "< split_value" label.
|
||||||
#' The branches also used for missing values are marked as bold
|
#' The branches that also used for missing values are marked as bold
|
||||||
#' (as in "carrying extra capacity").
|
#' (as in "carrying extra capacity").
|
||||||
#'
|
#'
|
||||||
#' This function uses [GraphViz](https://www.graphviz.org/) as DiagrammeR backend.
|
#' This function uses \href{https://www.graphviz.org/}{GraphViz} as a backend of DiagrammeR.
|
||||||
#'
|
#'
|
||||||
#' @return
|
#' @return
|
||||||
#' The value depends on the `render` parameter:
|
#'
|
||||||
#' - If `render = TRUE` (default): Rendered graph object which is an htmlwidget of
|
#' When \code{render = TRUE}:
|
||||||
#' class `grViz`. Similar to "ggplot" objects, it needs to be printed when not
|
#' returns a rendered graph object which is an \code{htmlwidget} of class \code{grViz}.
|
||||||
#' running from the command line.
|
#' Similar to ggplot objects, it needs to be printed to see it when not running from command line.
|
||||||
#' - If `render = FALSE`: Graph object which is of DiagrammeR's class `dgr_graph`.
|
#'
|
||||||
|
#' When \code{render = FALSE}:
|
||||||
|
#' silently returns a graph object which is of DiagrammeR's class \code{dgr_graph}.
|
||||||
#' This could be useful if one wants to modify some of the graph attributes
|
#' This could be useful if one wants to modify some of the graph attributes
|
||||||
#' before rendering the graph with [DiagrammeR::render_graph()].
|
#' before rendering the graph with \code{\link[DiagrammeR]{render_graph}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' data(agaricus.train, package = "xgboost")
|
#' data(agaricus.train, package='xgboost')
|
||||||
#'
|
|
||||||
#' bst <- xgboost(
|
|
||||||
#' data = agaricus.train$data,
|
|
||||||
#' label = agaricus.train$label,
|
|
||||||
#' max_depth = 3,
|
|
||||||
#' eta = 1,
|
|
||||||
#' nthread = 2,
|
|
||||||
#' nrounds = 2,
|
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#'
|
|
||||||
#' # plot the first tree, using the style from xgboost's core library
|
|
||||||
#' # (this plot should look identical to the ones generated from other
|
|
||||||
#' # interfaces like the python package for xgboost)
|
|
||||||
#' xgb.plot.tree(model = bst, trees = 1, style = "xgboost")
|
|
||||||
#'
|
#'
|
||||||
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 3,
|
||||||
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
#' # plot all the trees
|
#' # plot all the trees
|
||||||
#' xgb.plot.tree(model = bst, trees = NULL)
|
#' xgb.plot.tree(model = bst)
|
||||||
#'
|
|
||||||
#' # plot only the first tree and display the node ID:
|
#' # plot only the first tree and display the node ID:
|
||||||
#' xgb.plot.tree(model = bst, trees = 0, show_node_id = TRUE)
|
#' xgb.plot.tree(model = bst, trees = 0, show_node_id = TRUE)
|
||||||
#'
|
#'
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' # Below is an example of how to save this plot to a file.
|
#' # Below is an example of how to save this plot to a file.
|
||||||
#' # Note that for export_graph() to work, the {DiagrammeRsvg}
|
#' # Note that for `export_graph` to work, the DiagrammeRsvg and rsvg packages must also be installed.
|
||||||
#' # and {rsvg} packages must also be installed.
|
|
||||||
#'
|
|
||||||
#' library(DiagrammeR)
|
#' library(DiagrammeR)
|
||||||
#'
|
#' gr <- xgb.plot.tree(model=bst, trees=0:1, render=FALSE)
|
||||||
#' gr <- xgb.plot.tree(model = bst, trees = 0:1, render = FALSE)
|
#' export_graph(gr, 'tree.pdf', width=1500, height=1900)
|
||||||
#' export_graph(gr, "tree.pdf", width = 1500, height = 1900)
|
#' export_graph(gr, 'tree.png', width=1500, height=1900)
|
||||||
#' export_graph(gr, "tree.png", width = 1500, height = 1900)
|
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.plot.tree <- function(model = NULL, trees = NULL, plot_width = NULL, plot_height = NULL,
|
xgb.plot.tree <- function(feature_names = NULL, model = NULL, trees = NULL, plot_width = NULL, plot_height = NULL,
|
||||||
render = TRUE, show_node_id = FALSE, style = c("R", "xgboost"), ...) {
|
render = TRUE, show_node_id = FALSE, ...) {
|
||||||
check.deprecation(...)
|
check.deprecation(...)
|
||||||
if (!inherits(model, "xgb.Booster")) {
|
if (!inherits(model, "xgb.Booster")) {
|
||||||
stop("model: Has to be an object of class xgb.Booster")
|
stop("model: Has to be an object of class xgb.Booster")
|
||||||
@ -113,20 +78,9 @@ xgb.plot.tree <- function(model = NULL, trees = NULL, plot_width = NULL, plot_he
|
|||||||
stop("DiagrammeR package is required for xgb.plot.tree", call. = FALSE)
|
stop("DiagrammeR package is required for xgb.plot.tree", call. = FALSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
style <- as.character(head(style, 1L))
|
dt <- xgb.model.dt.tree(feature_names = feature_names, model = model, trees = trees)
|
||||||
stopifnot(style %in% c("R", "xgboost"))
|
|
||||||
if (style == "xgboost") {
|
|
||||||
if (NROW(trees) != 1L || !render || show_node_id) {
|
|
||||||
stop("style='xgboost' is only supported for single, rendered tree, without node IDs.")
|
|
||||||
}
|
|
||||||
|
|
||||||
txt <- xgb.dump(model, dump_format = "dot")
|
dt[, label := paste0(Feature, "\nCover: ", Cover, ifelse(Feature == "Leaf", "\nValue: ", "\nGain: "), Quality)]
|
||||||
return(DiagrammeR::grViz(txt[[trees + 1]], width = plot_width, height = plot_height))
|
|
||||||
}
|
|
||||||
|
|
||||||
dt <- xgb.model.dt.tree(model = model, trees = trees)
|
|
||||||
|
|
||||||
dt[, label := paste0(Feature, "\nCover: ", Cover, ifelse(Feature == "Leaf", "\nValue: ", "\nGain: "), Gain)]
|
|
||||||
if (show_node_id)
|
if (show_node_id)
|
||||||
dt[, label := paste0(ID, ": ", label)]
|
dt[, label := paste0(ID, ": ", label)]
|
||||||
dt[Node == 0, label := paste0("Tree ", Tree, "\n", label)]
|
dt[Node == 0, label := paste0("Tree ", Tree, "\n", label)]
|
||||||
@ -193,4 +147,4 @@ xgb.plot.tree <- function(model = NULL, trees = NULL, plot_width = NULL, plot_he
|
|||||||
# Avoid error messages during CRAN check.
|
# Avoid error messages during CRAN check.
|
||||||
# The reason is that these variables are never declared
|
# The reason is that these variables are never declared
|
||||||
# They are mainly column names inferred by Data.table...
|
# They are mainly column names inferred by Data.table...
|
||||||
globalVariables(c("Feature", "ID", "Cover", "Gain", "Split", "Yes", "No", "Missing", ".", "shape", "filledcolor", "label"))
|
globalVariables(c("Feature", "ID", "Cover", "Quality", "Split", "Yes", "No", "Missing", ".", "shape", "filledcolor", "label"))
|
||||||
|
|||||||
@ -1,24 +1,12 @@
|
|||||||
#' Save xgboost model to binary file
|
#' Save xgboost model to binary file
|
||||||
#'
|
#'
|
||||||
#' Save xgboost model to a file in binary or JSON format.
|
#' Save xgboost model to a file in binary format.
|
||||||
#'
|
#'
|
||||||
#' @param model Model object of \code{xgb.Booster} class.
|
#' @param model model object of \code{xgb.Booster} class.
|
||||||
#' @param fname Name of the file to write.
|
#' @param fname name of the file to write.
|
||||||
#'
|
|
||||||
#' Note that the extension of this file name determined the serialization format to use:\itemize{
|
|
||||||
#' \item Extension ".ubj" will use the universal binary JSON format (recommended).
|
|
||||||
#' This format uses binary types for e.g. floating point numbers, thereby preventing any loss
|
|
||||||
#' of precision when converting to a human-readable JSON text or similar.
|
|
||||||
#' \item Extension ".json" will use plain JSON, which is a human-readable format.
|
|
||||||
#' \item Extension ".deprecated" will use a \bold{deprecated} binary format. This format will
|
|
||||||
#' not be able to save attributes introduced after v1 of XGBoost, such as the "best_iteration"
|
|
||||||
#' attribute that boosters might keep, nor feature names or user-specifiec attributes.
|
|
||||||
#' \item If the format is not specified by passing one of the file extensions above, will
|
|
||||||
#' default to UBJ.
|
|
||||||
#' }
|
|
||||||
#'
|
#'
|
||||||
#' @details
|
#' @details
|
||||||
#' This methods allows to save a model in an xgboost-internal binary or text format which is universal
|
#' This methods allows to save a model in an xgboost-internal binary format which is universal
|
||||||
#' among the various xgboost interfaces. In R, the saved model file could be read-in later
|
#' among the various xgboost interfaces. In R, the saved model file could be read-in later
|
||||||
#' using either the \code{\link{xgb.load}} function or the \code{xgb_model} parameter
|
#' using either the \code{\link{xgb.load}} function or the \code{xgb_model} parameter
|
||||||
#' of \code{\link{xgb.train}}.
|
#' of \code{\link{xgb.train}}.
|
||||||
@ -26,36 +14,25 @@
|
|||||||
#' Note: a model can also be saved as an R-object (e.g., by using \code{\link[base]{readRDS}}
|
#' Note: a model can also be saved as an R-object (e.g., by using \code{\link[base]{readRDS}}
|
||||||
#' or \code{\link[base]{save}}). However, it would then only be compatible with R, and
|
#' or \code{\link[base]{save}}). However, it would then only be compatible with R, and
|
||||||
#' corresponding R-methods would need to be used to load it. Moreover, persisting the model with
|
#' corresponding R-methods would need to be used to load it. Moreover, persisting the model with
|
||||||
#' \code{\link[base]{readRDS}} or \code{\link[base]{save}}) might cause compatibility problems in
|
#' \code{\link[base]{readRDS}} or \code{\link[base]{save}}) will cause compatibility problems in
|
||||||
#' future versions of XGBoost. Consult \code{\link{a-compatibility-note-for-saveRDS-save}} to learn
|
#' future versions of XGBoost. Consult \code{\link{a-compatibility-note-for-saveRDS-save}} to learn
|
||||||
#' how to persist models in a future-proof way, i.e. to make the model accessible in future
|
#' how to persist models in a future-proof way, i.e. to make the model accessible in future
|
||||||
#' releases of XGBoost.
|
#' releases of XGBoost.
|
||||||
#'
|
#'
|
||||||
#' @seealso
|
#' @seealso
|
||||||
#' \code{\link{xgb.load}}
|
#' \code{\link{xgb.load}}, \code{\link{xgb.Booster.complete}}.
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package='xgboost')
|
#' data(agaricus.test, package='xgboost')
|
||||||
#'
|
|
||||||
#' ## Keep the number of threads to 1 for examples
|
|
||||||
#' nthread <- 1
|
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
|
||||||
#' train <- agaricus.train
|
#' train <- agaricus.train
|
||||||
#' test <- agaricus.test
|
#' test <- agaricus.test
|
||||||
#' bst <- xgb.train(
|
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
#' data = xgb.DMatrix(train$data, label = train$label),
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
#' max_depth = 2,
|
#' xgb.save(bst, 'xgb.model')
|
||||||
#' eta = 1,
|
#' bst <- xgb.load('xgb.model')
|
||||||
#' nthread = nthread,
|
#' if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||||
#' nrounds = 2,
|
#' pred <- predict(bst, test$data)
|
||||||
#' objective = "binary:logistic"
|
|
||||||
#' )
|
|
||||||
#' fname <- file.path(tempdir(), "xgb.ubj")
|
|
||||||
#' xgb.save(bst, fname)
|
|
||||||
#' bst <- xgb.load(fname)
|
|
||||||
#' @export
|
#' @export
|
||||||
xgb.save <- function(model, fname) {
|
xgb.save <- function(model, fname) {
|
||||||
if (typeof(fname) != "character")
|
if (typeof(fname) != "character")
|
||||||
@ -64,7 +41,8 @@ xgb.save <- function(model, fname) {
|
|||||||
stop("model must be xgb.Booster.",
|
stop("model must be xgb.Booster.",
|
||||||
if (inherits(model, "xgb.DMatrix")) " Use xgb.DMatrix.save to save an xgb.DMatrix object." else "")
|
if (inherits(model, "xgb.DMatrix")) " Use xgb.DMatrix.save to save an xgb.DMatrix object." else "")
|
||||||
}
|
}
|
||||||
|
model <- xgb.Booster.complete(model, saveraw = FALSE)
|
||||||
fname <- path.expand(fname)
|
fname <- path.expand(fname)
|
||||||
.Call(XGBoosterSaveModel_R, xgb.get.handle(model), enc2utf8(fname[1]))
|
.Call(XGBoosterSaveModel_R, model$handle, fname[1])
|
||||||
return(TRUE)
|
return(TRUE)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,25 +11,21 @@
|
|||||||
#' \item \code{deprecated}: Encode the booster into old customized binary format.
|
#' \item \code{deprecated}: Encode the booster into old customized binary format.
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
|
#' Right now the default is \code{deprecated} but will be changed to \code{ubj} in upcoming release.
|
||||||
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package='xgboost')
|
#' data(agaricus.test, package='xgboost')
|
||||||
#'
|
|
||||||
#' ## Keep the number of threads to 2 for examples
|
|
||||||
#' nthread <- 2
|
|
||||||
#' data.table::setDTthreads(nthread)
|
|
||||||
#'
|
|
||||||
#' train <- agaricus.train
|
#' train <- agaricus.train
|
||||||
#' test <- agaricus.test
|
#' test <- agaricus.test
|
||||||
#' bst <- xgb.train(data = xgb.DMatrix(train$data, label = train$label), max_depth = 2,
|
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
#' eta = 1, nthread = nthread, nrounds = 2,objective = "binary:logistic")
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
#'
|
|
||||||
#' raw <- xgb.save.raw(bst)
|
#' raw <- xgb.save.raw(bst)
|
||||||
#' bst <- xgb.load.raw(raw)
|
#' bst <- xgb.load.raw(raw)
|
||||||
|
#' pred <- predict(bst, test$data)
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.save.raw <- function(model, raw_format = "ubj") {
|
xgb.save.raw <- function(model, raw_format = "deprecated") {
|
||||||
handle <- xgb.get.handle(model)
|
handle <- xgb.get.handle(model)
|
||||||
args <- list(format = raw_format)
|
args <- list(format = raw_format)
|
||||||
.Call(XGBoosterSaveModelToRaw_R, handle, jsonlite::toJSON(args, auto_unbox = TRUE))
|
.Call(XGBoosterSaveModelToRaw_R, handle, jsonlite::toJSON(args, auto_unbox = TRUE))
|
||||||
|
|||||||
21
R-package/R/xgb.serialize.R
Normal file
21
R-package/R/xgb.serialize.R
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#' Serialize the booster instance into R's raw vector. The serialization method differs
|
||||||
|
#' from \code{\link{xgb.save.raw}} as the latter one saves only the model but not
|
||||||
|
#' parameters. This serialization format is not stable across different xgboost versions.
|
||||||
|
#'
|
||||||
|
#' @param booster the booster instance
|
||||||
|
#'
|
||||||
|
#' @examples
|
||||||
|
#' data(agaricus.train, package='xgboost')
|
||||||
|
#' data(agaricus.test, package='xgboost')
|
||||||
|
#' train <- agaricus.train
|
||||||
|
#' test <- agaricus.test
|
||||||
|
#' bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
|
#' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic")
|
||||||
|
#' raw <- xgb.serialize(bst)
|
||||||
|
#' bst <- xgb.unserialize(raw)
|
||||||
|
#'
|
||||||
|
#' @export
|
||||||
|
xgb.serialize <- function(booster) {
|
||||||
|
handle <- xgb.get.handle(booster)
|
||||||
|
.Call(XGBoosterSerializeToBuffer_R, handle)
|
||||||
|
}
|
||||||
@ -114,13 +114,13 @@
|
|||||||
#' @param data training dataset. \code{xgb.train} accepts only an \code{xgb.DMatrix} as the input.
|
#' @param data training dataset. \code{xgb.train} accepts only an \code{xgb.DMatrix} as the input.
|
||||||
#' \code{xgboost}, in addition, also accepts \code{matrix}, \code{dgCMatrix}, or name of a local data file.
|
#' \code{xgboost}, in addition, also accepts \code{matrix}, \code{dgCMatrix}, or name of a local data file.
|
||||||
#' @param nrounds max number of boosting iterations.
|
#' @param nrounds max number of boosting iterations.
|
||||||
#' @param evals Named list of `xgb.DMatrix` datasets to use for evaluating model performance.
|
#' @param watchlist named list of xgb.DMatrix datasets to use for evaluating model performance.
|
||||||
#' Metrics specified in either \code{eval_metric} or \code{feval} will be computed for each
|
#' Metrics specified in either \code{eval_metric} or \code{feval} will be computed for each
|
||||||
#' of these datasets during each boosting iteration, and stored in the end as a field named
|
#' of these datasets during each boosting iteration, and stored in the end as a field named
|
||||||
#' \code{evaluation_log} in the resulting object. When either \code{verbose>=1} or
|
#' \code{evaluation_log} in the resulting object. When either \code{verbose>=1} or
|
||||||
#' \code{\link{xgb.cb.print.evaluation}} callback is engaged, the performance results are continuously
|
#' \code{\link{cb.print.evaluation}} callback is engaged, the performance results are continuously
|
||||||
#' printed out during the training.
|
#' printed out during the training.
|
||||||
#' E.g., specifying \code{evals=list(validation1=mat1, validation2=mat2)} allows to track
|
#' E.g., specifying \code{watchlist=list(validation1=mat1, validation2=mat2)} allows to track
|
||||||
#' the performance of each round's model on mat1 and mat2.
|
#' the performance of each round's model on mat1 and mat2.
|
||||||
#' @param obj customized objective function. Returns gradient and second order
|
#' @param obj customized objective function. Returns gradient and second order
|
||||||
#' gradient with given prediction and dtrain.
|
#' gradient with given prediction and dtrain.
|
||||||
@ -130,33 +130,28 @@
|
|||||||
#' @param verbose If 0, xgboost will stay silent. If 1, it will print information about performance.
|
#' @param verbose If 0, xgboost will stay silent. If 1, it will print information about performance.
|
||||||
#' If 2, some additional information will be printed out.
|
#' If 2, some additional information will be printed out.
|
||||||
#' Note that setting \code{verbose > 0} automatically engages the
|
#' Note that setting \code{verbose > 0} automatically engages the
|
||||||
#' \code{xgb.cb.print.evaluation(period=1)} callback function.
|
#' \code{cb.print.evaluation(period=1)} callback function.
|
||||||
#' @param print_every_n Print each n-th iteration evaluation messages when \code{verbose>0}.
|
#' @param print_every_n Print each n-th iteration evaluation messages when \code{verbose>0}.
|
||||||
#' Default is 1 which means all messages are printed. This parameter is passed to the
|
#' Default is 1 which means all messages are printed. This parameter is passed to the
|
||||||
#' \code{\link{xgb.cb.print.evaluation}} callback.
|
#' \code{\link{cb.print.evaluation}} callback.
|
||||||
#' @param early_stopping_rounds If \code{NULL}, the early stopping function is not triggered.
|
#' @param early_stopping_rounds If \code{NULL}, the early stopping function is not triggered.
|
||||||
#' If set to an integer \code{k}, training with a validation set will stop if the performance
|
#' If set to an integer \code{k}, training with a validation set will stop if the performance
|
||||||
#' doesn't improve for \code{k} rounds.
|
#' doesn't improve for \code{k} rounds.
|
||||||
#' Setting this parameter engages the \code{\link{xgb.cb.early.stop}} callback.
|
#' Setting this parameter engages the \code{\link{cb.early.stop}} callback.
|
||||||
#' @param maximize If \code{feval} and \code{early_stopping_rounds} are set,
|
#' @param maximize If \code{feval} and \code{early_stopping_rounds} are set,
|
||||||
#' then this parameter must be set as well.
|
#' then this parameter must be set as well.
|
||||||
#' When it is \code{TRUE}, it means the larger the evaluation score the better.
|
#' When it is \code{TRUE}, it means the larger the evaluation score the better.
|
||||||
#' This parameter is passed to the \code{\link{xgb.cb.early.stop}} callback.
|
#' This parameter is passed to the \code{\link{cb.early.stop}} callback.
|
||||||
#' @param save_period when it is non-NULL, model is saved to disk after every \code{save_period} rounds,
|
#' @param save_period when it is non-NULL, model is saved to disk after every \code{save_period} rounds,
|
||||||
#' 0 means save at the end. The saving is handled by the \code{\link{xgb.cb.save.model}} callback.
|
#' 0 means save at the end. The saving is handled by the \code{\link{cb.save.model}} callback.
|
||||||
#' @param save_name the name or path for periodically saved model file.
|
#' @param save_name the name or path for periodically saved model file.
|
||||||
#' @param xgb_model a previously built model to continue the training from.
|
#' @param xgb_model a previously built model to continue the training from.
|
||||||
#' Could be either an object of class \code{xgb.Booster}, or its raw data, or the name of a
|
#' Could be either an object of class \code{xgb.Booster}, or its raw data, or the name of a
|
||||||
#' file with a previously saved model.
|
#' file with a previously saved model.
|
||||||
#' @param callbacks a list of callback functions to perform various task during boosting.
|
#' @param callbacks a list of callback functions to perform various task during boosting.
|
||||||
#' See \code{\link{xgb.Callback}}. Some of the callbacks are automatically created depending on the
|
#' See \code{\link{callbacks}}. Some of the callbacks are automatically created depending on the
|
||||||
#' parameters' values. User can provide either existing or their own callback methods in order
|
#' parameters' values. User can provide either existing or their own callback methods in order
|
||||||
#' to customize the training process.
|
#' to customize the training process.
|
||||||
#'
|
|
||||||
#' Note that some callbacks might try to leave attributes in the resulting model object,
|
|
||||||
#' such as an evaluation log (a `data.table` object) - be aware that these objects are kept
|
|
||||||
#' as R attributes, and thus do not get saved when using XGBoost's own serializaters like
|
|
||||||
#' \link{xgb.save} (but are kept when using R serializers like \link{saveRDS}).
|
|
||||||
#' @param ... other parameters to pass to \code{params}.
|
#' @param ... other parameters to pass to \code{params}.
|
||||||
#' @param label vector of response values. Should not be provided when data is
|
#' @param label vector of response values. Should not be provided when data is
|
||||||
#' a local data file name or an \code{xgb.DMatrix}.
|
#' a local data file name or an \code{xgb.DMatrix}.
|
||||||
@ -165,24 +160,15 @@
|
|||||||
#' This parameter is only used when input is a dense matrix.
|
#' This parameter is only used when input is a dense matrix.
|
||||||
#' @param weight a vector indicating the weight for each row of the input.
|
#' @param weight a vector indicating the weight for each row of the input.
|
||||||
#'
|
#'
|
||||||
#' @return
|
|
||||||
#' An object of class \code{xgb.Booster}.
|
|
||||||
#'
|
|
||||||
#' @details
|
#' @details
|
||||||
#' These are the training functions for \code{xgboost}.
|
#' These are the training functions for \code{xgboost}.
|
||||||
#'
|
#'
|
||||||
#' The \code{xgb.train} interface supports advanced features such as \code{evals},
|
#' The \code{xgb.train} interface supports advanced features such as \code{watchlist},
|
||||||
#' customized objective and evaluation metric functions, therefore it is more flexible
|
#' customized objective and evaluation metric functions, therefore it is more flexible
|
||||||
#' than the \code{xgboost} interface.
|
#' than the \code{xgboost} interface.
|
||||||
#'
|
#'
|
||||||
#' Parallelization is automatically enabled if \code{OpenMP} is present.
|
#' Parallelization is automatically enabled if \code{OpenMP} is present.
|
||||||
#' Number of threads can also be manually specified via the \code{nthread}
|
#' Number of threads can also be manually specified via \code{nthread} parameter.
|
||||||
#' parameter.
|
|
||||||
#'
|
|
||||||
#' While in other interfaces, the default random seed defaults to zero, in R, if a parameter `seed`
|
|
||||||
#' is not manually supplied, it will generate a random seed through R's own random number generator,
|
|
||||||
#' whose seed in turn is controllable through `set.seed`. If `seed` is passed, it will override the
|
|
||||||
#' RNG from R.
|
|
||||||
#'
|
#'
|
||||||
#' The evaluation metric is chosen automatically by XGBoost (according to the objective)
|
#' The evaluation metric is chosen automatically by XGBoost (according to the objective)
|
||||||
#' when the \code{eval_metric} parameter is not provided.
|
#' when the \code{eval_metric} parameter is not provided.
|
||||||
@ -207,31 +193,38 @@
|
|||||||
#'
|
#'
|
||||||
#' The following callbacks are automatically created when certain parameters are set:
|
#' The following callbacks are automatically created when certain parameters are set:
|
||||||
#' \itemize{
|
#' \itemize{
|
||||||
#' \item \code{xgb.cb.print.evaluation} is turned on when \code{verbose > 0};
|
#' \item \code{cb.print.evaluation} is turned on when \code{verbose > 0};
|
||||||
#' and the \code{print_every_n} parameter is passed to it.
|
#' and the \code{print_every_n} parameter is passed to it.
|
||||||
#' \item \code{xgb.cb.evaluation.log} is on when \code{evals} is present.
|
#' \item \code{cb.evaluation.log} is on when \code{watchlist} is present.
|
||||||
#' \item \code{xgb.cb.early.stop}: when \code{early_stopping_rounds} is set.
|
#' \item \code{cb.early.stop}: when \code{early_stopping_rounds} is set.
|
||||||
#' \item \code{xgb.cb.save.model}: when \code{save_period > 0} is set.
|
#' \item \code{cb.save.model}: when \code{save_period > 0} is set.
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' Note that objects of type `xgb.Booster` as returned by this function behave a bit differently
|
#' @return
|
||||||
#' from typical R objects (it's an 'altrep' list class), and it makes a separation between
|
#' An object of class \code{xgb.Booster} with the following elements:
|
||||||
#' internal booster attributes (restricted to jsonifyable data), accessed through \link{xgb.attr}
|
#' \itemize{
|
||||||
#' and shared between interfaces through serialization functions like \link{xgb.save}; and
|
#' \item \code{handle} a handle (pointer) to the xgboost model in memory.
|
||||||
#' R-specific attributes (typically the result from a callback), accessed through \link{attributes}
|
#' \item \code{raw} a cached memory dump of the xgboost model saved as R's \code{raw} type.
|
||||||
#' and \link{attr}, which are otherwise
|
#' \item \code{niter} number of boosting iterations.
|
||||||
#' only used in the R interface, only kept when using R's serializers like \link{saveRDS}, and
|
#' \item \code{evaluation_log} evaluation history stored as a \code{data.table} with the
|
||||||
#' not anyhow used by functions like \link{predict.xgb.Booster}.
|
#' first column corresponding to iteration number and the rest corresponding to evaluation
|
||||||
#'
|
#' metrics' values. It is created by the \code{\link{cb.evaluation.log}} callback.
|
||||||
#' Be aware that one such R attribute that is automatically added is `params` - this attribute
|
#' \item \code{call} a function call.
|
||||||
#' is assigned from the `params` argument to this function, and is only meant to serve as a
|
#' \item \code{params} parameters that were passed to the xgboost library. Note that it does not
|
||||||
#' reference for what went into the booster, but is not used in other methods that take a booster
|
#' capture parameters changed by the \code{\link{cb.reset.parameters}} callback.
|
||||||
#' object - so for example, changing the booster's configuration requires calling `xgb.config<-`
|
#' \item \code{callbacks} callback functions that were either automatically assigned or
|
||||||
#' or 'xgb.parameters<-', while simply modifying `attributes(model)$params$<...>` will have no
|
#' explicitly passed.
|
||||||
#' effect elsewhere.
|
#' \item \code{best_iteration} iteration number with the best evaluation metric value
|
||||||
|
#' (only available with early stopping).
|
||||||
|
#' \item \code{best_score} the best evaluation metric value during early stopping.
|
||||||
|
#' (only available with early stopping).
|
||||||
|
#' \item \code{feature_names} names of the training dataset features
|
||||||
|
#' (only when column names were defined in training data).
|
||||||
|
#' \item \code{nfeatures} number of features in training data.
|
||||||
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @seealso
|
#' @seealso
|
||||||
#' \code{\link{xgb.Callback}},
|
#' \code{\link{callbacks}},
|
||||||
#' \code{\link{predict.xgb.Booster}},
|
#' \code{\link{predict.xgb.Booster}},
|
||||||
#' \code{\link{xgb.cv}}
|
#' \code{\link{xgb.cv}}
|
||||||
#'
|
#'
|
||||||
@ -244,25 +237,17 @@
|
|||||||
#' data(agaricus.train, package='xgboost')
|
#' data(agaricus.train, package='xgboost')
|
||||||
#' data(agaricus.test, package='xgboost')
|
#' data(agaricus.test, package='xgboost')
|
||||||
#'
|
#'
|
||||||
#' ## Keep the number of threads to 1 for examples
|
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
#' nthread <- 1
|
#' dtest <- with(agaricus.test, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
#' data.table::setDTthreads(nthread)
|
#' watchlist <- list(train = dtrain, eval = dtest)
|
||||||
#'
|
|
||||||
#' 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:
|
#' ## A simple xgb.train example:
|
||||||
#' param <- list(max_depth = 2, eta = 1, nthread = nthread,
|
#' param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
|
||||||
#' objective = "binary:logistic", eval_metric = "auc")
|
#' objective = "binary:logistic", eval_metric = "auc")
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals, verbose = 0)
|
#' bst <- xgb.train(param, dtrain, nrounds = 2, watchlist)
|
||||||
#'
|
#'
|
||||||
#' ## An xgb.train example where custom objective and evaluation metric are
|
#'
|
||||||
#' ## used:
|
#' ## An xgb.train example where custom objective and evaluation metric are used:
|
||||||
#' logregobj <- function(preds, dtrain) {
|
#' logregobj <- function(preds, dtrain) {
|
||||||
#' labels <- getinfo(dtrain, "label")
|
#' labels <- getinfo(dtrain, "label")
|
||||||
#' preds <- 1/(1 + exp(-preds))
|
#' preds <- 1/(1 + exp(-preds))
|
||||||
@ -278,40 +263,40 @@
|
|||||||
#'
|
#'
|
||||||
#' # These functions could be used by passing them either:
|
#' # These functions could be used by passing them either:
|
||||||
#' # as 'objective' and 'eval_metric' parameters in the params list:
|
#' # as 'objective' and 'eval_metric' parameters in the params list:
|
||||||
#' param <- list(max_depth = 2, eta = 1, nthread = nthread,
|
#' param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
|
||||||
#' objective = logregobj, eval_metric = evalerror)
|
#' objective = logregobj, eval_metric = evalerror)
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals, verbose = 0)
|
#' bst <- xgb.train(param, dtrain, nrounds = 2, watchlist)
|
||||||
#'
|
#'
|
||||||
#' # or through the ... arguments:
|
#' # or through the ... arguments:
|
||||||
#' param <- list(max_depth = 2, eta = 1, nthread = nthread)
|
#' param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2)
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals, verbose = 0,
|
#' bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
|
||||||
#' objective = logregobj, eval_metric = evalerror)
|
#' objective = logregobj, eval_metric = evalerror)
|
||||||
#'
|
#'
|
||||||
#' # or as dedicated 'obj' and 'feval' parameters of xgb.train:
|
#' # or as dedicated 'obj' and 'feval' parameters of xgb.train:
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals,
|
#' bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
|
||||||
#' obj = logregobj, feval = evalerror)
|
#' obj = logregobj, feval = evalerror)
|
||||||
#'
|
#'
|
||||||
#'
|
#'
|
||||||
#' ## An xgb.train example of using variable learning rates at each iteration:
|
#' ## An xgb.train example of using variable learning rates at each iteration:
|
||||||
#' param <- list(max_depth = 2, eta = 1, nthread = nthread,
|
#' param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
|
||||||
#' objective = "binary:logistic", eval_metric = "auc")
|
#' objective = "binary:logistic", eval_metric = "auc")
|
||||||
#' my_etas <- list(eta = c(0.5, 0.1))
|
#' my_etas <- list(eta = c(0.5, 0.1))
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 2, evals = evals, verbose = 0,
|
#' bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
|
||||||
#' callbacks = list(xgb.cb.reset.parameters(my_etas)))
|
#' callbacks = list(cb.reset.parameters(my_etas)))
|
||||||
#'
|
#'
|
||||||
#' ## Early stopping:
|
#' ## Early stopping:
|
||||||
#' bst <- xgb.train(param, dtrain, nrounds = 25, evals = evals,
|
#' bst <- xgb.train(param, dtrain, nrounds = 25, watchlist,
|
||||||
#' early_stopping_rounds = 3)
|
#' early_stopping_rounds = 3)
|
||||||
#'
|
#'
|
||||||
#' ## An 'xgboost' interface example:
|
#' ## An 'xgboost' interface example:
|
||||||
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label,
|
#' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label,
|
||||||
#' max_depth = 2, eta = 1, nthread = nthread, nrounds = 2,
|
#' max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
|
||||||
#' objective = "binary:logistic")
|
#' objective = "binary:logistic")
|
||||||
#' pred <- predict(bst, agaricus.test$data)
|
#' pred <- predict(bst, agaricus.test$data)
|
||||||
#'
|
#'
|
||||||
#' @rdname xgb.train
|
#' @rdname xgb.train
|
||||||
#' @export
|
#' @export
|
||||||
xgb.train <- function(params = list(), data, nrounds, evals = list(),
|
xgb.train <- function(params = list(), data, nrounds, watchlist = list(),
|
||||||
obj = NULL, feval = NULL, verbose = 1, print_every_n = 1L,
|
obj = NULL, feval = NULL, verbose = 1, print_every_n = 1L,
|
||||||
early_stopping_rounds = NULL, maximize = NULL,
|
early_stopping_rounds = NULL, maximize = NULL,
|
||||||
save_period = NULL, save_name = "xgboost.model",
|
save_period = NULL, save_name = "xgboost.model",
|
||||||
@ -324,78 +309,75 @@ xgb.train <- function(params = list(), data, nrounds, evals = list(),
|
|||||||
check.custom.obj()
|
check.custom.obj()
|
||||||
check.custom.eval()
|
check.custom.eval()
|
||||||
|
|
||||||
# data & evals checks
|
# data & watchlist checks
|
||||||
dtrain <- data
|
dtrain <- data
|
||||||
if (!inherits(dtrain, "xgb.DMatrix"))
|
if (!inherits(dtrain, "xgb.DMatrix"))
|
||||||
stop("second argument dtrain must be xgb.DMatrix")
|
stop("second argument dtrain must be xgb.DMatrix")
|
||||||
if (length(evals) > 0) {
|
if (length(watchlist) > 0) {
|
||||||
if (typeof(evals) != "list" ||
|
if (typeof(watchlist) != "list" ||
|
||||||
!all(vapply(evals, inherits, logical(1), what = 'xgb.DMatrix')))
|
!all(vapply(watchlist, inherits, logical(1), what = 'xgb.DMatrix')))
|
||||||
stop("'evals' must be a list of xgb.DMatrix elements")
|
stop("watchlist must be a list of xgb.DMatrix elements")
|
||||||
evnames <- names(evals)
|
evnames <- names(watchlist)
|
||||||
if (is.null(evnames) || any(evnames == ""))
|
if (is.null(evnames) || any(evnames == ""))
|
||||||
stop("each element of 'evals' must have a name tag")
|
stop("each element of the watchlist must have a name tag")
|
||||||
}
|
}
|
||||||
# Handle multiple evaluation metrics given as a list
|
# Handle multiple evaluation metrics given as a list
|
||||||
for (m in params$eval_metric) {
|
for (m in params$eval_metric) {
|
||||||
params <- c(params, list(eval_metric = m))
|
params <- c(params, list(eval_metric = m))
|
||||||
}
|
}
|
||||||
|
|
||||||
params <- c(params)
|
|
||||||
params['validate_parameters'] <- TRUE
|
|
||||||
if (!("seed" %in% names(params))) {
|
|
||||||
params[["seed"]] <- sample(.Machine$integer.max, size = 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
# callbacks
|
|
||||||
tmp <- .process.callbacks(callbacks, is_cv = FALSE)
|
|
||||||
callbacks <- tmp$callbacks
|
|
||||||
cb_names <- tmp$cb_names
|
|
||||||
rm(tmp)
|
|
||||||
|
|
||||||
# Early stopping callback (should always come first)
|
|
||||||
if (!is.null(early_stopping_rounds) && !("early_stop" %in% cb_names)) {
|
|
||||||
callbacks <- add.callback(
|
|
||||||
callbacks,
|
|
||||||
xgb.cb.early.stop(
|
|
||||||
early_stopping_rounds,
|
|
||||||
maximize = maximize,
|
|
||||||
verbose = verbose
|
|
||||||
),
|
|
||||||
as_first_elt = TRUE
|
|
||||||
)
|
|
||||||
}
|
|
||||||
# evaluation printing callback
|
# evaluation printing callback
|
||||||
|
params <- c(params)
|
||||||
print_every_n <- max(as.integer(print_every_n), 1L)
|
print_every_n <- max(as.integer(print_every_n), 1L)
|
||||||
if (verbose && !("print_evaluation" %in% cb_names)) {
|
if (!has.callbacks(callbacks, 'cb.print.evaluation') &&
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.print.evaluation(print_every_n))
|
verbose) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.print.evaluation(print_every_n))
|
||||||
}
|
}
|
||||||
# evaluation log callback: it is automatically enabled when 'evals' is provided
|
# evaluation log callback: it is automatically enabled when watchlist is provided
|
||||||
if (length(evals) && !("evaluation_log" %in% cb_names)) {
|
evaluation_log <- list()
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.evaluation.log())
|
if (!has.callbacks(callbacks, 'cb.evaluation.log') &&
|
||||||
|
length(watchlist) > 0) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.evaluation.log())
|
||||||
}
|
}
|
||||||
# Model saving callback
|
# Model saving callback
|
||||||
if (!is.null(save_period) && !("save_model" %in% cb_names)) {
|
if (!is.null(save_period) &&
|
||||||
callbacks <- add.callback(callbacks, xgb.cb.save.model(save_period, save_name))
|
!has.callbacks(callbacks, 'cb.save.model')) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.save.model(save_period, save_name))
|
||||||
|
}
|
||||||
|
# Early stopping callback
|
||||||
|
stop_condition <- FALSE
|
||||||
|
if (!is.null(early_stopping_rounds) &&
|
||||||
|
!has.callbacks(callbacks, 'cb.early.stop')) {
|
||||||
|
callbacks <- add.cb(callbacks, cb.early.stop(early_stopping_rounds,
|
||||||
|
maximize = maximize, verbose = verbose))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sort the callbacks into categories
|
||||||
|
cb <- categorize.callbacks(callbacks)
|
||||||
|
params['validate_parameters'] <- TRUE
|
||||||
|
if (!is.null(params[['seed']])) {
|
||||||
|
warning("xgb.train: `seed` is ignored in R package. Use `set.seed()` instead.")
|
||||||
}
|
}
|
||||||
|
|
||||||
# The tree updating process would need slightly different handling
|
# The tree updating process would need slightly different handling
|
||||||
is_update <- NVL(params[['process_type']], '.') == 'update'
|
is_update <- NVL(params[['process_type']], '.') == 'update'
|
||||||
|
|
||||||
# Construct a booster (either a new one or load from xgb_model)
|
# Construct a booster (either a new one or load from xgb_model)
|
||||||
bst <- xgb.Booster(
|
handle <- xgb.Booster.handle(params, append(watchlist, dtrain), xgb_model)
|
||||||
params = params,
|
bst <- xgb.handleToBooster(handle)
|
||||||
cachelist = append(evals, dtrain),
|
|
||||||
modelfile = xgb_model
|
|
||||||
)
|
|
||||||
niter_init <- bst$niter
|
|
||||||
bst <- bst$bst
|
|
||||||
.Call(
|
|
||||||
XGBoosterCopyInfoFromDMatrix_R,
|
|
||||||
xgb.get.handle(bst),
|
|
||||||
dtrain
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# extract parameters that can affect the relationship b/w #trees and #iterations
|
||||||
|
num_class <- max(as.numeric(NVL(params[['num_class']], 1)), 1)
|
||||||
|
num_parallel_tree <- max(as.numeric(NVL(params[['num_parallel_tree']], 1)), 1)
|
||||||
|
|
||||||
|
# When the 'xgb_model' was set, find out how many boosting iterations it has
|
||||||
|
niter_init <- 0
|
||||||
|
if (!is.null(xgb_model)) {
|
||||||
|
niter_init <- as.numeric(xgb.attr(bst, 'niter')) + 1
|
||||||
|
if (length(niter_init) == 0) {
|
||||||
|
niter_init <- xgb.ntree(bst) %/% (num_parallel_tree * num_class)
|
||||||
|
}
|
||||||
|
}
|
||||||
if (is_update && nrounds > niter_init)
|
if (is_update && nrounds > niter_init)
|
||||||
stop("nrounds cannot be larger than ", niter_init, " (nrounds of xgb_model)")
|
stop("nrounds cannot be larger than ", niter_init, " (nrounds of xgb_model)")
|
||||||
|
|
||||||
@ -403,83 +385,49 @@ xgb.train <- function(params = list(), data, nrounds, evals = list(),
|
|||||||
begin_iteration <- niter_skip + 1
|
begin_iteration <- niter_skip + 1
|
||||||
end_iteration <- niter_skip + nrounds
|
end_iteration <- niter_skip + nrounds
|
||||||
|
|
||||||
.execute.cb.before.training(
|
|
||||||
callbacks,
|
|
||||||
bst,
|
|
||||||
dtrain,
|
|
||||||
evals,
|
|
||||||
begin_iteration,
|
|
||||||
end_iteration
|
|
||||||
)
|
|
||||||
|
|
||||||
# the main loop for boosting iterations
|
# the main loop for boosting iterations
|
||||||
for (iteration in begin_iteration:end_iteration) {
|
for (iteration in begin_iteration:end_iteration) {
|
||||||
|
|
||||||
.execute.cb.before.iter(
|
for (f in cb$pre_iter) f()
|
||||||
callbacks,
|
|
||||||
bst,
|
|
||||||
dtrain,
|
|
||||||
evals,
|
|
||||||
iteration
|
|
||||||
)
|
|
||||||
|
|
||||||
xgb.iter.update(
|
xgb.iter.update(bst$handle, dtrain, iteration - 1, obj)
|
||||||
bst = bst,
|
|
||||||
dtrain = dtrain,
|
|
||||||
iter = iteration - 1,
|
|
||||||
obj = obj
|
|
||||||
)
|
|
||||||
|
|
||||||
bst_evaluation <- NULL
|
if (length(watchlist) > 0)
|
||||||
if (length(evals) > 0) {
|
bst_evaluation <- xgb.iter.eval(bst$handle, watchlist, iteration - 1, feval) # nolint: object_usage_linter
|
||||||
bst_evaluation <- xgb.iter.eval(
|
|
||||||
bst = bst,
|
xgb.attr(bst$handle, 'niter') <- iteration - 1
|
||||||
evals = evals,
|
|
||||||
iter = iteration - 1,
|
for (f in cb$post_iter) f()
|
||||||
feval = feval
|
|
||||||
)
|
if (stop_condition) break
|
||||||
|
}
|
||||||
|
for (f in cb$finalize) f(finalize = TRUE)
|
||||||
|
|
||||||
|
bst <- xgb.Booster.complete(bst, saveraw = TRUE)
|
||||||
|
|
||||||
|
# store the total number of boosting iterations
|
||||||
|
bst$niter <- end_iteration
|
||||||
|
|
||||||
|
# store the evaluation results
|
||||||
|
if (length(evaluation_log) > 0 &&
|
||||||
|
nrow(evaluation_log) > 0) {
|
||||||
|
# include the previous compatible history when available
|
||||||
|
if (inherits(xgb_model, 'xgb.Booster') &&
|
||||||
|
!is_update &&
|
||||||
|
!is.null(xgb_model$evaluation_log) &&
|
||||||
|
isTRUE(all.equal(colnames(evaluation_log),
|
||||||
|
colnames(xgb_model$evaluation_log)))) {
|
||||||
|
evaluation_log <- rbindlist(list(xgb_model$evaluation_log, evaluation_log))
|
||||||
|
}
|
||||||
|
bst$evaluation_log <- evaluation_log
|
||||||
}
|
}
|
||||||
|
|
||||||
should_stop <- .execute.cb.after.iter(
|
bst$call <- match.call()
|
||||||
callbacks,
|
bst$params <- params
|
||||||
bst,
|
bst$callbacks <- callbacks
|
||||||
dtrain,
|
if (!is.null(colnames(dtrain)))
|
||||||
evals,
|
bst$feature_names <- colnames(dtrain)
|
||||||
iteration,
|
bst$nfeatures <- ncol(dtrain)
|
||||||
bst_evaluation
|
|
||||||
)
|
|
||||||
|
|
||||||
if (should_stop) break
|
|
||||||
}
|
|
||||||
|
|
||||||
cb_outputs <- .execute.cb.after.training(
|
|
||||||
callbacks,
|
|
||||||
bst,
|
|
||||||
dtrain,
|
|
||||||
evals,
|
|
||||||
iteration,
|
|
||||||
bst_evaluation
|
|
||||||
)
|
|
||||||
|
|
||||||
extra_attrs <- list(
|
|
||||||
call = match.call(),
|
|
||||||
params = params
|
|
||||||
)
|
|
||||||
|
|
||||||
curr_attrs <- attributes(bst)
|
|
||||||
if (NROW(curr_attrs)) {
|
|
||||||
curr_attrs <- curr_attrs[
|
|
||||||
setdiff(
|
|
||||||
names(curr_attrs),
|
|
||||||
c(names(extra_attrs), names(cb_outputs))
|
|
||||||
)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
curr_attrs <- c(extra_attrs, curr_attrs)
|
|
||||||
if (NROW(cb_outputs)) {
|
|
||||||
curr_attrs <- c(curr_attrs, cb_outputs)
|
|
||||||
}
|
|
||||||
attributes(bst) <- curr_attrs
|
|
||||||
|
|
||||||
return(bst)
|
return(bst)
|
||||||
}
|
}
|
||||||
|
|||||||
41
R-package/R/xgb.unserialize.R
Normal file
41
R-package/R/xgb.unserialize.R
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#' Load the instance back from \code{\link{xgb.serialize}}
|
||||||
|
#'
|
||||||
|
#' @param buffer the buffer containing booster instance saved by \code{\link{xgb.serialize}}
|
||||||
|
#' @param handle An \code{xgb.Booster.handle} object which will be overwritten with
|
||||||
|
#' the new deserialized object. Must be a null handle (e.g. when loading the model through
|
||||||
|
#' `readRDS`). If not provided, a new handle will be created.
|
||||||
|
#' @return An \code{xgb.Booster.handle} object.
|
||||||
|
#'
|
||||||
|
#' @export
|
||||||
|
xgb.unserialize <- function(buffer, handle = NULL) {
|
||||||
|
cachelist <- list()
|
||||||
|
if (is.null(handle)) {
|
||||||
|
handle <- .Call(XGBoosterCreate_R, cachelist)
|
||||||
|
} else {
|
||||||
|
if (!is.null.handle(handle))
|
||||||
|
stop("'handle' is not null/empty. Cannot overwrite existing handle.")
|
||||||
|
.Call(XGBoosterCreateInEmptyObj_R, cachelist, handle)
|
||||||
|
}
|
||||||
|
tryCatch(
|
||||||
|
.Call(XGBoosterUnserializeFromBuffer_R, handle, buffer),
|
||||||
|
error = function(e) {
|
||||||
|
error_msg <- conditionMessage(e)
|
||||||
|
m <- regexec("(src[\\\\/]learner.cc:[0-9]+): Check failed: (header == serialisation_header_)",
|
||||||
|
error_msg, perl = TRUE)
|
||||||
|
groups <- regmatches(error_msg, m)[[1]]
|
||||||
|
if (length(groups) == 3) {
|
||||||
|
warning(paste("The model had been generated by XGBoost version 1.0.0 or earlier and was ",
|
||||||
|
"loaded from a RDS file. We strongly ADVISE AGAINST using saveRDS() ",
|
||||||
|
"function, to ensure that your model can be read in current and upcoming ",
|
||||||
|
"XGBoost releases. Please use xgb.save() instead to preserve models for the ",
|
||||||
|
"long term. For more details and explanation, see ",
|
||||||
|
"https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html",
|
||||||
|
sep = ""))
|
||||||
|
.Call(XGBoosterLoadModelFromRaw_R, handle, buffer)
|
||||||
|
} else {
|
||||||
|
stop(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
class(handle) <- "xgb.Booster.handle"
|
||||||
|
return (handle)
|
||||||
|
}
|
||||||
@ -10,21 +10,15 @@ xgboost <- function(data = NULL, label = NULL, missing = NA, weight = NULL,
|
|||||||
save_period = NULL, save_name = "xgboost.model",
|
save_period = NULL, save_name = "xgboost.model",
|
||||||
xgb_model = NULL, callbacks = list(), ...) {
|
xgb_model = NULL, callbacks = list(), ...) {
|
||||||
merged <- check.booster.params(params, ...)
|
merged <- check.booster.params(params, ...)
|
||||||
dtrain <- xgb.get.DMatrix(
|
dtrain <- xgb.get.DMatrix(data, label, missing, weight, nthread = merged$nthread)
|
||||||
data = data,
|
|
||||||
label = label,
|
|
||||||
missing = missing,
|
|
||||||
weight = weight,
|
|
||||||
nthread = merged$nthread
|
|
||||||
)
|
|
||||||
|
|
||||||
evals <- list(train = dtrain)
|
watchlist <- list(train = dtrain)
|
||||||
|
|
||||||
bst <- xgb.train(params, dtrain, nrounds, evals, verbose = verbose, print_every_n = print_every_n,
|
bst <- xgb.train(params, dtrain, nrounds, watchlist, verbose = verbose, print_every_n = print_every_n,
|
||||||
early_stopping_rounds = early_stopping_rounds, maximize = maximize,
|
early_stopping_rounds = early_stopping_rounds, maximize = maximize,
|
||||||
save_period = save_period, save_name = save_name,
|
save_period = save_period, save_name = save_name,
|
||||||
xgb_model = xgb_model, callbacks = callbacks, ...)
|
xgb_model = xgb_model, callbacks = callbacks, ...)
|
||||||
return(bst)
|
return (bst)
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Training part from Mushroom Data Set
|
#' Training part from Mushroom Data Set
|
||||||
@ -40,10 +34,10 @@ xgboost <- function(data = NULL, label = NULL, missing = NA, weight = NULL,
|
|||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @references
|
#' @references
|
||||||
#' <https://archive.ics.uci.edu/ml/datasets/Mushroom>
|
#' https://archive.ics.uci.edu/ml/datasets/Mushroom
|
||||||
#'
|
#'
|
||||||
#' Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
#' Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
||||||
#' <http://archive.ics.uci.edu/ml>. Irvine, CA: University of California,
|
#' [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
|
||||||
#' School of Information and Computer Science.
|
#' School of Information and Computer Science.
|
||||||
#'
|
#'
|
||||||
#' @docType data
|
#' @docType data
|
||||||
@ -67,10 +61,10 @@ NULL
|
|||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
#' @references
|
#' @references
|
||||||
#' <https://archive.ics.uci.edu/ml/datasets/Mushroom>
|
#' https://archive.ics.uci.edu/ml/datasets/Mushroom
|
||||||
#'
|
#'
|
||||||
#' Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
#' Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
||||||
#' <http://archive.ics.uci.edu/ml>. Irvine, CA: University of California,
|
#' [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
|
||||||
#' School of Information and Computer Science.
|
#' School of Information and Computer Science.
|
||||||
#'
|
#'
|
||||||
#' @docType data
|
#' @docType data
|
||||||
@ -82,8 +76,12 @@ NULL
|
|||||||
NULL
|
NULL
|
||||||
|
|
||||||
# Various imports
|
# Various imports
|
||||||
#' @importClassesFrom Matrix dgCMatrix dgRMatrix CsparseMatrix
|
#' @importClassesFrom Matrix dgCMatrix dgeMatrix
|
||||||
|
#' @importFrom Matrix colSums
|
||||||
#' @importFrom Matrix sparse.model.matrix
|
#' @importFrom Matrix sparse.model.matrix
|
||||||
|
#' @importFrom Matrix sparseVector
|
||||||
|
#' @importFrom Matrix sparseMatrix
|
||||||
|
#' @importFrom Matrix t
|
||||||
#' @importFrom data.table data.table
|
#' @importFrom data.table data.table
|
||||||
#' @importFrom data.table is.data.table
|
#' @importFrom data.table is.data.table
|
||||||
#' @importFrom data.table as.data.table
|
#' @importFrom data.table as.data.table
|
||||||
@ -94,13 +92,9 @@ NULL
|
|||||||
#' @importFrom data.table setnames
|
#' @importFrom data.table setnames
|
||||||
#' @importFrom jsonlite fromJSON
|
#' @importFrom jsonlite fromJSON
|
||||||
#' @importFrom jsonlite toJSON
|
#' @importFrom jsonlite toJSON
|
||||||
#' @importFrom methods new
|
|
||||||
#' @importFrom utils object.size str tail
|
#' @importFrom utils object.size str tail
|
||||||
#' @importFrom stats coef
|
|
||||||
#' @importFrom stats predict
|
#' @importFrom stats predict
|
||||||
#' @importFrom stats median
|
#' @importFrom stats median
|
||||||
#' @importFrom stats sd
|
|
||||||
#' @importFrom stats variable.names
|
|
||||||
#' @importFrom utils head
|
#' @importFrom utils head
|
||||||
#' @importFrom graphics barplot
|
#' @importFrom graphics barplot
|
||||||
#' @importFrom graphics lines
|
#' @importFrom graphics lines
|
||||||
|
|||||||
18
R-package/configure
vendored
18
R-package/configure
vendored
@ -1,6 +1,6 @@
|
|||||||
#! /bin/sh
|
#! /bin/sh
|
||||||
# Guess values for system-dependent variables and create Makefiles.
|
# Guess values for system-dependent variables and create Makefiles.
|
||||||
# Generated by GNU Autoconf 2.71 for xgboost 2.1.0.
|
# Generated by GNU Autoconf 2.71 for xgboost 2.0.0.
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Copyright (C) 1992-1996, 1998-2017, 2020-2021 Free Software Foundation,
|
# Copyright (C) 1992-1996, 1998-2017, 2020-2021 Free Software Foundation,
|
||||||
@ -607,8 +607,8 @@ MAKEFLAGS=
|
|||||||
# Identity of this package.
|
# Identity of this package.
|
||||||
PACKAGE_NAME='xgboost'
|
PACKAGE_NAME='xgboost'
|
||||||
PACKAGE_TARNAME='xgboost'
|
PACKAGE_TARNAME='xgboost'
|
||||||
PACKAGE_VERSION='2.1.0'
|
PACKAGE_VERSION='2.0.0'
|
||||||
PACKAGE_STRING='xgboost 2.1.0'
|
PACKAGE_STRING='xgboost 2.0.0'
|
||||||
PACKAGE_BUGREPORT=''
|
PACKAGE_BUGREPORT=''
|
||||||
PACKAGE_URL=''
|
PACKAGE_URL=''
|
||||||
|
|
||||||
@ -1225,7 +1225,7 @@ if test "$ac_init_help" = "long"; then
|
|||||||
# Omit some internal or obsolete options to make the list less imposing.
|
# Omit some internal or obsolete options to make the list less imposing.
|
||||||
# This message is too long to be a string in the A/UX 3.1 sh.
|
# This message is too long to be a string in the A/UX 3.1 sh.
|
||||||
cat <<_ACEOF
|
cat <<_ACEOF
|
||||||
\`configure' configures xgboost 2.1.0 to adapt to many kinds of systems.
|
\`configure' configures xgboost 2.0.0 to adapt to many kinds of systems.
|
||||||
|
|
||||||
Usage: $0 [OPTION]... [VAR=VALUE]...
|
Usage: $0 [OPTION]... [VAR=VALUE]...
|
||||||
|
|
||||||
@ -1287,7 +1287,7 @@ fi
|
|||||||
|
|
||||||
if test -n "$ac_init_help"; then
|
if test -n "$ac_init_help"; then
|
||||||
case $ac_init_help in
|
case $ac_init_help in
|
||||||
short | recursive ) echo "Configuration of xgboost 2.1.0:";;
|
short | recursive ) echo "Configuration of xgboost 2.0.0:";;
|
||||||
esac
|
esac
|
||||||
cat <<\_ACEOF
|
cat <<\_ACEOF
|
||||||
|
|
||||||
@ -1367,7 +1367,7 @@ fi
|
|||||||
test -n "$ac_init_help" && exit $ac_status
|
test -n "$ac_init_help" && exit $ac_status
|
||||||
if $ac_init_version; then
|
if $ac_init_version; then
|
||||||
cat <<\_ACEOF
|
cat <<\_ACEOF
|
||||||
xgboost configure 2.1.0
|
xgboost configure 2.0.0
|
||||||
generated by GNU Autoconf 2.71
|
generated by GNU Autoconf 2.71
|
||||||
|
|
||||||
Copyright (C) 2021 Free Software Foundation, Inc.
|
Copyright (C) 2021 Free Software Foundation, Inc.
|
||||||
@ -1533,7 +1533,7 @@ cat >config.log <<_ACEOF
|
|||||||
This file contains any messages produced by compilers while
|
This file contains any messages produced by compilers while
|
||||||
running configure, to aid debugging if configure makes a mistake.
|
running configure, to aid debugging if configure makes a mistake.
|
||||||
|
|
||||||
It was created by xgboost $as_me 2.1.0, which was
|
It was created by xgboost $as_me 2.0.0, which was
|
||||||
generated by GNU Autoconf 2.71. Invocation command line was
|
generated by GNU Autoconf 2.71. Invocation command line was
|
||||||
|
|
||||||
$ $0$ac_configure_args_raw
|
$ $0$ac_configure_args_raw
|
||||||
@ -3412,7 +3412,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
|
|||||||
# report actual input values of CONFIG_FILES etc. instead of their
|
# report actual input values of CONFIG_FILES etc. instead of their
|
||||||
# values after options handling.
|
# values after options handling.
|
||||||
ac_log="
|
ac_log="
|
||||||
This file was extended by xgboost $as_me 2.1.0, which was
|
This file was extended by xgboost $as_me 2.0.0, which was
|
||||||
generated by GNU Autoconf 2.71. Invocation command line was
|
generated by GNU Autoconf 2.71. Invocation command line was
|
||||||
|
|
||||||
CONFIG_FILES = $CONFIG_FILES
|
CONFIG_FILES = $CONFIG_FILES
|
||||||
@ -3467,7 +3467,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\
|
|||||||
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
|
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
|
||||||
ac_cs_config='$ac_cs_config_escaped'
|
ac_cs_config='$ac_cs_config_escaped'
|
||||||
ac_cs_version="\\
|
ac_cs_version="\\
|
||||||
xgboost config.status 2.1.0
|
xgboost config.status 2.0.0
|
||||||
configured by $0, generated by GNU Autoconf 2.71,
|
configured by $0, generated by GNU Autoconf 2.71,
|
||||||
with options \\"\$ac_cs_config\\"
|
with options \\"\$ac_cs_config\\"
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
AC_PREREQ(2.69)
|
AC_PREREQ(2.69)
|
||||||
|
|
||||||
AC_INIT([xgboost],[2.1.0],[],[xgboost],[])
|
AC_INIT([xgboost],[2.0.0],[],[xgboost],[])
|
||||||
|
|
||||||
: ${R_HOME=`R RHOME`}
|
: ${R_HOME=`R RHOME`}
|
||||||
if test -z "${R_HOME}"; then
|
if test -z "${R_HOME}"; then
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
basic_walkthrough Basic feature walkthrough
|
basic_walkthrough Basic feature walkthrough
|
||||||
|
caret_wrapper Use xgboost to train in caret library
|
||||||
custom_objective Customize loss function, and evaluation metric
|
custom_objective Customize loss function, and evaluation metric
|
||||||
boost_from_prediction Boosting from existing prediction
|
boost_from_prediction Boosting from existing prediction
|
||||||
predict_first_ntree Predicting using first n trees
|
predict_first_ntree Predicting using first n trees
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
XGBoost R Feature Walkthrough
|
XGBoost R Feature Walkthrough
|
||||||
====
|
====
|
||||||
* [Basic walkthrough of wrappers](basic_walkthrough.R)
|
* [Basic walkthrough of wrappers](basic_walkthrough.R)
|
||||||
|
* [Train a xgboost model from caret library](caret_wrapper.R)
|
||||||
* [Customize loss function, and evaluation metric](custom_objective.R)
|
* [Customize loss function, and evaluation metric](custom_objective.R)
|
||||||
* [Boosting from existing prediction](boost_from_prediction.R)
|
* [Boosting from existing prediction](boost_from_prediction.R)
|
||||||
* [Predicting using first n trees](predict_first_ntree.R)
|
* [Predicting using first n trees](predict_first_ntree.R)
|
||||||
|
|||||||
@ -55,8 +55,6 @@ print(paste("test-error=", err))
|
|||||||
# save model to binary local file
|
# save model to binary local file
|
||||||
xgb.save(bst, "xgboost.model")
|
xgb.save(bst, "xgboost.model")
|
||||||
# load binary model to R
|
# load binary model to R
|
||||||
# Function doesn't take 'nthreads', but can be set like this:
|
|
||||||
RhpcBLASctl::omp_set_num_threads(1)
|
|
||||||
bst2 <- xgb.load("xgboost.model")
|
bst2 <- xgb.load("xgboost.model")
|
||||||
pred2 <- predict(bst2, test$data)
|
pred2 <- predict(bst2, test$data)
|
||||||
# pred2 should be identical to pred
|
# pred2 should be identical to pred
|
||||||
@ -74,17 +72,17 @@ print(paste("sum(abs(pred3-pred))=", sum(abs(pred3 - pred))))
|
|||||||
# to use advanced features, we need to put data in xgb.DMatrix
|
# to use advanced features, we need to put data in xgb.DMatrix
|
||||||
dtrain <- xgb.DMatrix(data = train$data, label = train$label)
|
dtrain <- xgb.DMatrix(data = train$data, label = train$label)
|
||||||
dtest <- xgb.DMatrix(data = test$data, label = test$label)
|
dtest <- xgb.DMatrix(data = test$data, label = test$label)
|
||||||
#---------------Using an evaluation set----------------
|
#---------------Using watchlist----------------
|
||||||
# 'evals' is a list of xgb.DMatrix, each of them is tagged with name
|
# watchlist is a list of xgb.DMatrix, each of them is tagged with name
|
||||||
evals <- list(train = dtrain, test = dtest)
|
watchlist <- list(train = dtrain, test = dtest)
|
||||||
# to train with an evaluation set, use xgb.train, which contains more advanced features
|
# to train with watchlist, use xgb.train, which contains more advanced features
|
||||||
# 'evals' argument allows us to monitor the evaluation result on all data in the list
|
# watchlist allows us to monitor the evaluation result on all data in the list
|
||||||
print("Train xgboost using xgb.train with evaluation data")
|
print("Train xgboost using xgb.train with watchlist")
|
||||||
bst <- xgb.train(data = dtrain, max_depth = 2, eta = 1, nrounds = 2, evals = evals,
|
bst <- xgb.train(data = dtrain, max_depth = 2, eta = 1, nrounds = 2, watchlist = watchlist,
|
||||||
nthread = 2, objective = "binary:logistic")
|
nthread = 2, objective = "binary:logistic")
|
||||||
# we can change evaluation metrics, or use multiple evaluation metrics
|
# we can change evaluation metrics, or use multiple evaluation metrics
|
||||||
print("train xgboost using xgb.train with evaluation data, watch logloss and error")
|
print("train xgboost using xgb.train with watchlist, watch logloss and error")
|
||||||
bst <- xgb.train(data = dtrain, max_depth = 2, eta = 1, nrounds = 2, evals = evals,
|
bst <- xgb.train(data = dtrain, max_depth = 2, eta = 1, nrounds = 2, watchlist = watchlist,
|
||||||
eval_metric = "error", eval_metric = "logloss",
|
eval_metric = "error", eval_metric = "logloss",
|
||||||
nthread = 2, objective = "binary:logistic")
|
nthread = 2, objective = "binary:logistic")
|
||||||
|
|
||||||
@ -92,7 +90,7 @@ bst <- xgb.train(data = dtrain, max_depth = 2, eta = 1, nrounds = 2, evals = eva
|
|||||||
xgb.DMatrix.save(dtrain, "dtrain.buffer")
|
xgb.DMatrix.save(dtrain, "dtrain.buffer")
|
||||||
# to load it in, simply call xgb.DMatrix
|
# to load it in, simply call xgb.DMatrix
|
||||||
dtrain2 <- xgb.DMatrix("dtrain.buffer")
|
dtrain2 <- xgb.DMatrix("dtrain.buffer")
|
||||||
bst <- xgb.train(data = dtrain2, max_depth = 2, eta = 1, nrounds = 2, evals = evals,
|
bst <- xgb.train(data = dtrain2, max_depth = 2, eta = 1, nrounds = 2, watchlist = watchlist,
|
||||||
nthread = 2, objective = "binary:logistic")
|
nthread = 2, objective = "binary:logistic")
|
||||||
# information can be extracted from xgb.DMatrix using getinfo
|
# information can be extracted from xgb.DMatrix using getinfo
|
||||||
label <- getinfo(dtest, "label")
|
label <- getinfo(dtest, "label")
|
||||||
|
|||||||
@ -5,14 +5,14 @@ data(agaricus.test, package = 'xgboost')
|
|||||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||||
|
|
||||||
evals <- list(eval = dtest, train = dtrain)
|
watchlist <- list(eval = dtest, train = dtrain)
|
||||||
###
|
###
|
||||||
# advanced: start from a initial base prediction
|
# advanced: start from a initial base prediction
|
||||||
#
|
#
|
||||||
print('start running example to start from a initial prediction')
|
print('start running example to start from a initial prediction')
|
||||||
# train xgboost for 1 round
|
# train xgboost for 1 round
|
||||||
param <- list(max_depth = 2, eta = 1, nthread = 2, objective = 'binary:logistic')
|
param <- list(max_depth = 2, eta = 1, nthread = 2, objective = 'binary:logistic')
|
||||||
bst <- xgb.train(param, dtrain, 1, evals)
|
bst <- xgb.train(param, dtrain, 1, watchlist)
|
||||||
# Note: we need the margin value instead of transformed prediction in set_base_margin
|
# Note: we need the margin value instead of transformed prediction in set_base_margin
|
||||||
# do predict with output_margin=TRUE, will always give you margin values before logistic transformation
|
# do predict with output_margin=TRUE, will always give you margin values before logistic transformation
|
||||||
ptrain <- predict(bst, dtrain, outputmargin = TRUE)
|
ptrain <- predict(bst, dtrain, outputmargin = TRUE)
|
||||||
@ -23,4 +23,4 @@ setinfo(dtrain, "base_margin", ptrain)
|
|||||||
setinfo(dtest, "base_margin", ptest)
|
setinfo(dtest, "base_margin", ptest)
|
||||||
|
|
||||||
print('this is result of boost from initial prediction')
|
print('this is result of boost from initial prediction')
|
||||||
bst <- xgb.train(params = param, data = dtrain, nrounds = 1, evals = evals)
|
bst <- xgb.train(params = param, data = dtrain, nrounds = 1, watchlist = watchlist)
|
||||||
|
|||||||
44
R-package/demo/caret_wrapper.R
Normal file
44
R-package/demo/caret_wrapper.R
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# install development version of caret library that contains xgboost models
|
||||||
|
require(caret)
|
||||||
|
require(xgboost)
|
||||||
|
require(data.table)
|
||||||
|
require(vcd)
|
||||||
|
require(e1071)
|
||||||
|
|
||||||
|
# Load Arthritis dataset in memory.
|
||||||
|
data(Arthritis)
|
||||||
|
# Create a copy of the dataset with data.table package
|
||||||
|
# (data.table is 100% compliant with R dataframe but its syntax is a lot more consistent
|
||||||
|
# and its performance are really good).
|
||||||
|
df <- data.table(Arthritis, keep.rownames = FALSE)
|
||||||
|
|
||||||
|
# Let's add some new categorical features to see if it helps.
|
||||||
|
# Of course these feature are highly correlated to the Age feature.
|
||||||
|
# Usually it's not a good thing in ML, but Tree algorithms (including boosted trees) are able to select the best features,
|
||||||
|
# even in case of highly correlated features.
|
||||||
|
# For the first feature we create groups of age by rounding the real age.
|
||||||
|
# Note that we transform it to factor (categorical data) so the algorithm treat them as independant values.
|
||||||
|
df[, AgeDiscret := as.factor(round(Age / 10, 0))]
|
||||||
|
|
||||||
|
# Here is an even stronger simplification of the real age with an arbitrary split at 30 years old.
|
||||||
|
# I choose this value based on nothing.
|
||||||
|
# We will see later if simplifying the information based on arbitrary values is a good strategy
|
||||||
|
# (I am sure you already have an idea of how well it will work!).
|
||||||
|
df[, AgeCat := as.factor(ifelse(Age > 30, "Old", "Young"))]
|
||||||
|
|
||||||
|
# We remove ID as there is nothing to learn from this feature (it will just add some noise as the dataset is small).
|
||||||
|
df[, ID := NULL]
|
||||||
|
|
||||||
|
#-------------Basic Training using XGBoost in caret Library-----------------
|
||||||
|
# Set up control parameters for caret::train
|
||||||
|
# Here we use 10-fold cross-validation, repeating twice, and using random search for tuning hyper-parameters.
|
||||||
|
fitControl <- trainControl(method = "repeatedcv", number = 10, repeats = 2, search = "random")
|
||||||
|
# train a xgbTree model using caret::train
|
||||||
|
model <- train(factor(Improved) ~ ., data = df, method = "xgbTree", trControl = fitControl)
|
||||||
|
|
||||||
|
# Instead of tree for our boosters, you can also fit a linear regression or logistic regression model
|
||||||
|
# using xgbLinear
|
||||||
|
# model <- train(factor(Improved)~., data = df, method = "xgbLinear", trControl = fitControl)
|
||||||
|
|
||||||
|
# See model results
|
||||||
|
print(model)
|
||||||
@ -81,7 +81,7 @@ output_vector <- df[, Y := 0][Improved == "Marked", Y := 1][, Y]
|
|||||||
|
|
||||||
# Following is the same process as other demo
|
# Following is the same process as other demo
|
||||||
cat("Learning...\n")
|
cat("Learning...\n")
|
||||||
bst <- xgb.train(data = xgb.DMatrix(sparse_matrix, label = output_vector), max_depth = 9,
|
bst <- xgboost(data = sparse_matrix, label = output_vector, max_depth = 9,
|
||||||
eta = 1, nthread = 2, nrounds = 10, objective = "binary:logistic")
|
eta = 1, nthread = 2, nrounds = 10, objective = "binary:logistic")
|
||||||
|
|
||||||
importance <- xgb.importance(feature_names = colnames(sparse_matrix), model = bst)
|
importance <- xgb.importance(feature_names = colnames(sparse_matrix), model = bst)
|
||||||
|
|||||||
@ -25,7 +25,7 @@ xgb.cv(param, dtrain, nrounds, nfold = 5,
|
|||||||
# you can also do cross validation with customized loss function
|
# you can also do cross validation with customized loss function
|
||||||
# See custom_objective.R
|
# See custom_objective.R
|
||||||
##
|
##
|
||||||
print('running cross validation, with customized loss function')
|
print ('running cross validation, with customized loss function')
|
||||||
|
|
||||||
logregobj <- function(preds, dtrain) {
|
logregobj <- function(preds, dtrain) {
|
||||||
labels <- getinfo(dtrain, "label")
|
labels <- getinfo(dtrain, "label")
|
||||||
|
|||||||
@ -8,7 +8,7 @@ dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
|||||||
# note: for customized objective function, we leave objective as default
|
# note: for customized objective function, we leave objective as default
|
||||||
# note: what we are getting is margin value in prediction
|
# note: what we are getting is margin value in prediction
|
||||||
# you must know what you are doing
|
# you must know what you are doing
|
||||||
evals <- list(eval = dtest, train = dtrain)
|
watchlist <- list(eval = dtest, train = dtrain)
|
||||||
num_round <- 2
|
num_round <- 2
|
||||||
|
|
||||||
# user define objective function, given prediction, return gradient and second order gradient
|
# user define objective function, given prediction, return gradient and second order gradient
|
||||||
@ -35,10 +35,10 @@ evalerror <- function(preds, dtrain) {
|
|||||||
|
|
||||||
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0,
|
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0,
|
||||||
objective = logregobj, eval_metric = evalerror)
|
objective = logregobj, eval_metric = evalerror)
|
||||||
print('start training with user customized objective')
|
print ('start training with user customized objective')
|
||||||
# training with customized objective, we can also do step by step training
|
# training with customized objective, we can also do step by step training
|
||||||
# simply look at xgboost.py's implementation of train
|
# simply look at xgboost.py's implementation of train
|
||||||
bst <- xgb.train(param, dtrain, num_round, evals)
|
bst <- xgb.train(param, dtrain, num_round, watchlist)
|
||||||
|
|
||||||
#
|
#
|
||||||
# there can be cases where you want additional information
|
# there can be cases where you want additional information
|
||||||
@ -59,7 +59,7 @@ logregobjattr <- function(preds, dtrain) {
|
|||||||
}
|
}
|
||||||
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0,
|
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0,
|
||||||
objective = logregobjattr, eval_metric = evalerror)
|
objective = logregobjattr, eval_metric = evalerror)
|
||||||
print('start training with user customized objective, with additional attributes in DMatrix')
|
print ('start training with user customized objective, with additional attributes in DMatrix')
|
||||||
# training with customized objective, we can also do step by step training
|
# training with customized objective, we can also do step by step training
|
||||||
# simply look at xgboost.py's implementation of train
|
# simply look at xgboost.py's implementation of train
|
||||||
bst <- xgb.train(param, dtrain, num_round, evals)
|
bst <- xgb.train(param, dtrain, num_round, watchlist)
|
||||||
|
|||||||
@ -8,7 +8,7 @@ dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
|||||||
# note: what we are getting is margin value in prediction
|
# note: what we are getting is margin value in prediction
|
||||||
# you must know what you are doing
|
# you must know what you are doing
|
||||||
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0)
|
param <- list(max_depth = 2, eta = 1, nthread = 2, verbosity = 0)
|
||||||
evals <- list(eval = dtest)
|
watchlist <- list(eval = dtest)
|
||||||
num_round <- 20
|
num_round <- 20
|
||||||
# user define objective function, given prediction, return gradient and second order gradient
|
# user define objective function, given prediction, return gradient and second order gradient
|
||||||
# this is log likelihood loss
|
# this is log likelihood loss
|
||||||
@ -30,9 +30,9 @@ evalerror <- function(preds, dtrain) {
|
|||||||
err <- as.numeric(sum(labels != (preds > 0))) / length(labels)
|
err <- as.numeric(sum(labels != (preds > 0))) / length(labels)
|
||||||
return(list(metric = "error", value = err))
|
return(list(metric = "error", value = err))
|
||||||
}
|
}
|
||||||
print('start training with early Stopping setting')
|
print ('start training with early Stopping setting')
|
||||||
|
|
||||||
bst <- xgb.train(param, dtrain, num_round, evals,
|
bst <- xgb.train(param, dtrain, num_round, watchlist,
|
||||||
objective = logregobj, eval_metric = evalerror, maximize = FALSE,
|
objective = logregobj, eval_metric = evalerror, maximize = FALSE,
|
||||||
early_stopping_round = 3)
|
early_stopping_round = 3)
|
||||||
bst <- xgb.cv(param, dtrain, num_round, nfold = 5,
|
bst <- xgb.cv(param, dtrain, num_round, nfold = 5,
|
||||||
|
|||||||
@ -25,9 +25,9 @@ param <- list(objective = "binary:logistic", booster = "gblinear",
|
|||||||
##
|
##
|
||||||
# the rest of settings are the same
|
# the rest of settings are the same
|
||||||
##
|
##
|
||||||
evals <- list(eval = dtest, train = dtrain)
|
watchlist <- list(eval = dtest, train = dtrain)
|
||||||
num_round <- 2
|
num_round <- 2
|
||||||
bst <- xgb.train(param, dtrain, num_round, evals)
|
bst <- xgb.train(param, dtrain, num_round, watchlist)
|
||||||
ypred <- predict(bst, dtest)
|
ypred <- predict(bst, dtest)
|
||||||
labels <- getinfo(dtest, 'label')
|
labels <- getinfo(dtest, 'label')
|
||||||
cat('error of preds=', mean(as.numeric(ypred > 0.5) != labels), '\n')
|
cat('error of preds=', mean(as.numeric(ypred > 0.5) != labels), '\n')
|
||||||
|
|||||||
@ -23,7 +23,7 @@ y <- rbinom(N, 1, plogis(m))
|
|||||||
tr <- sample.int(N, N * 0.75)
|
tr <- sample.int(N, N * 0.75)
|
||||||
dtrain <- xgb.DMatrix(X[tr, ], label = y[tr])
|
dtrain <- xgb.DMatrix(X[tr, ], label = y[tr])
|
||||||
dtest <- xgb.DMatrix(X[-tr, ], label = y[-tr])
|
dtest <- xgb.DMatrix(X[-tr, ], label = y[-tr])
|
||||||
evals <- list(train = dtrain, test = dtest)
|
wl <- list(train = dtrain, test = dtest)
|
||||||
|
|
||||||
# An example of running 'gpu_hist' algorithm
|
# An example of running 'gpu_hist' algorithm
|
||||||
# which is
|
# which is
|
||||||
@ -35,11 +35,11 @@ evals <- list(train = dtrain, test = dtest)
|
|||||||
param <- list(objective = 'reg:logistic', eval_metric = 'auc', subsample = 0.5, nthread = 4,
|
param <- list(objective = 'reg:logistic', eval_metric = 'auc', subsample = 0.5, nthread = 4,
|
||||||
max_bin = 64, tree_method = 'gpu_hist')
|
max_bin = 64, tree_method = 'gpu_hist')
|
||||||
pt <- proc.time()
|
pt <- proc.time()
|
||||||
bst_gpu <- xgb.train(param, dtrain, evals = evals, nrounds = 50)
|
bst_gpu <- xgb.train(param, dtrain, watchlist = wl, nrounds = 50)
|
||||||
proc.time() - pt
|
proc.time() - pt
|
||||||
|
|
||||||
# Compare to the 'hist' algorithm:
|
# Compare to the 'hist' algorithm:
|
||||||
param$tree_method <- 'hist'
|
param$tree_method <- 'hist'
|
||||||
pt <- proc.time()
|
pt <- proc.time()
|
||||||
bst_hist <- xgb.train(param, dtrain, evals = evals, nrounds = 50)
|
bst_hist <- xgb.train(param, dtrain, watchlist = wl, nrounds = 50)
|
||||||
proc.time() - pt
|
proc.time() - pt
|
||||||
|
|||||||
@ -44,7 +44,7 @@ treeInteractions <- function(input_tree, input_max_depth) {
|
|||||||
|
|
||||||
# Remove non-interactions (same variable)
|
# Remove non-interactions (same variable)
|
||||||
interaction_list <- lapply(interaction_list, unique) # remove same variables
|
interaction_list <- lapply(interaction_list, unique) # remove same variables
|
||||||
interaction_length <- lengths(interaction_list)
|
interaction_length <- sapply(interaction_list, length)
|
||||||
interaction_list <- interaction_list[interaction_length > 1]
|
interaction_list <- interaction_list[interaction_length > 1]
|
||||||
interaction_list <- unique(lapply(interaction_list, sort))
|
interaction_list <- unique(lapply(interaction_list, sort))
|
||||||
return(interaction_list)
|
return(interaction_list)
|
||||||
@ -74,7 +74,7 @@ cols2ids <- function(object, col_names) {
|
|||||||
interaction_list_fid <- cols2ids(interaction_list, colnames(train))
|
interaction_list_fid <- cols2ids(interaction_list, colnames(train))
|
||||||
|
|
||||||
# Fit model with interaction constraints
|
# Fit model with interaction constraints
|
||||||
bst <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
|
bst <- xgboost(data = train, label = y, max_depth = 4,
|
||||||
eta = 0.1, nthread = 2, nrounds = 1000,
|
eta = 0.1, nthread = 2, nrounds = 1000,
|
||||||
interaction_constraints = interaction_list_fid)
|
interaction_constraints = interaction_list_fid)
|
||||||
|
|
||||||
@ -83,14 +83,14 @@ bst_interactions <- treeInteractions(bst_tree, 4)
|
|||||||
# interactions constrained to combinations of V1*V2 and V3*V4*V5
|
# interactions constrained to combinations of V1*V2 and V3*V4*V5
|
||||||
|
|
||||||
# Fit model without interaction constraints
|
# Fit model without interaction constraints
|
||||||
bst2 <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
|
bst2 <- xgboost(data = train, label = y, max_depth = 4,
|
||||||
eta = 0.1, nthread = 2, nrounds = 1000)
|
eta = 0.1, nthread = 2, nrounds = 1000)
|
||||||
|
|
||||||
bst2_tree <- xgb.model.dt.tree(colnames(train), bst2)
|
bst2_tree <- xgb.model.dt.tree(colnames(train), bst2)
|
||||||
bst2_interactions <- treeInteractions(bst2_tree, 4) # much more interactions
|
bst2_interactions <- treeInteractions(bst2_tree, 4) # much more interactions
|
||||||
|
|
||||||
# Fit model with both interaction and monotonicity constraints
|
# Fit model with both interaction and monotonicity constraints
|
||||||
bst3 <- xgb.train(data = xgb.DMatrix(train, label = y), max_depth = 4,
|
bst3 <- xgboost(data = train, label = y, max_depth = 4,
|
||||||
eta = 0.1, nthread = 2, nrounds = 1000,
|
eta = 0.1, nthread = 2, nrounds = 1000,
|
||||||
interaction_constraints = interaction_list_fid,
|
interaction_constraints = interaction_list_fid,
|
||||||
monotone_constraints = c(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0))
|
monotone_constraints = c(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0))
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
data(mtcars)
|
data(mtcars)
|
||||||
head(mtcars)
|
head(mtcars)
|
||||||
bst <- xgb.train(data = xgb.DMatrix(as.matrix(mtcars[, -11]), label = mtcars[, 11]),
|
bst <- xgboost(data = as.matrix(mtcars[, -11]), label = mtcars[, 11],
|
||||||
objective = 'count:poisson', nrounds = 5)
|
objective = 'count:poisson', nrounds = 5)
|
||||||
pred <- predict(bst, as.matrix(mtcars[, -11]))
|
pred <- predict(bst, as.matrix(mtcars[, -11]))
|
||||||
sqrt(mean((pred - mtcars[, 11]) ^ 2))
|
sqrt(mean((pred - mtcars[, 11]) ^ 2))
|
||||||
|
|||||||
@ -6,16 +6,16 @@ dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
|||||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||||
|
|
||||||
param <- list(max_depth = 2, eta = 1, objective = 'binary:logistic')
|
param <- list(max_depth = 2, eta = 1, objective = 'binary:logistic')
|
||||||
evals <- list(eval = dtest, train = dtrain)
|
watchlist <- list(eval = dtest, train = dtrain)
|
||||||
nrounds <- 2
|
nrounds <- 2
|
||||||
|
|
||||||
# training the model for two rounds
|
# training the model for two rounds
|
||||||
bst <- xgb.train(param, dtrain, nrounds, nthread = 2, evals = evals)
|
bst <- xgb.train(param, dtrain, nrounds, nthread = 2, watchlist)
|
||||||
cat('start testing prediction from first n trees\n')
|
cat('start testing prediction from first n trees\n')
|
||||||
labels <- getinfo(dtest, 'label')
|
labels <- getinfo(dtest, 'label')
|
||||||
|
|
||||||
### predict using first 1 tree
|
### predict using first 1 tree
|
||||||
ypred1 <- predict(bst, dtest, iterationrange = c(1, 1))
|
ypred1 <- predict(bst, dtest, ntreelimit = 1)
|
||||||
# by default, we predict using all the trees
|
# by default, we predict using all the trees
|
||||||
ypred2 <- predict(bst, dtest)
|
ypred2 <- predict(bst, dtest)
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ head(pred_with_leaf)
|
|||||||
create.new.tree.features <- function(model, original.features) {
|
create.new.tree.features <- function(model, original.features) {
|
||||||
pred_with_leaf <- predict(model, original.features, predleaf = TRUE)
|
pred_with_leaf <- predict(model, original.features, predleaf = TRUE)
|
||||||
cols <- list()
|
cols <- list()
|
||||||
for (i in 1:xgb.get.num.boosted.rounds(model)) {
|
for (i in 1:model$niter) {
|
||||||
# max is not the real max but it s not important for the purpose of adding features
|
# max is not the real max but it s not important for the purpose of adding features
|
||||||
leaf.id <- sort(unique(pred_with_leaf[, i]))
|
leaf.id <- sort(unique(pred_with_leaf[, i]))
|
||||||
cols[[i]] <- factor(x = pred_with_leaf[, i], level = leaf.id)
|
cols[[i]] <- factor(x = pred_with_leaf[, i], level = leaf.id)
|
||||||
@ -43,6 +43,7 @@ colnames(new.features.test) <- colnames(new.features.train)
|
|||||||
# learning with new features
|
# learning with new features
|
||||||
new.dtrain <- xgb.DMatrix(data = new.features.train, label = agaricus.train$label)
|
new.dtrain <- xgb.DMatrix(data = new.features.train, label = agaricus.train$label)
|
||||||
new.dtest <- xgb.DMatrix(data = new.features.test, label = agaricus.test$label)
|
new.dtest <- xgb.DMatrix(data = new.features.test, label = agaricus.test$label)
|
||||||
|
watchlist <- list(train = new.dtrain)
|
||||||
bst <- xgb.train(params = param, data = new.dtrain, nrounds = nrounds, nthread = 2)
|
bst <- xgb.train(params = param, data = new.dtrain, nrounds = nrounds, nthread = 2)
|
||||||
|
|
||||||
# Model accuracy with new features
|
# Model accuracy with new features
|
||||||
|
|||||||
@ -9,5 +9,6 @@ demo(create_sparse_matrix, package = 'xgboost')
|
|||||||
demo(predict_leaf_indices, package = 'xgboost')
|
demo(predict_leaf_indices, package = 'xgboost')
|
||||||
demo(early_stopping, package = 'xgboost')
|
demo(early_stopping, package = 'xgboost')
|
||||||
demo(poisson_regression, package = 'xgboost')
|
demo(poisson_regression, package = 'xgboost')
|
||||||
|
demo(caret_wrapper, package = 'xgboost')
|
||||||
demo(tweedie_regression, package = 'xgboost')
|
demo(tweedie_regression, package = 'xgboost')
|
||||||
#demo(gpu_accelerated, package = 'xgboost') # can only run when built with GPU support
|
#demo(gpu_accelerated, package = 'xgboost') # can only run when built with GPU support
|
||||||
|
|||||||
@ -39,7 +39,7 @@ bst <- xgb.train(
|
|||||||
data = d_train,
|
data = d_train,
|
||||||
params = params,
|
params = params,
|
||||||
maximize = FALSE,
|
maximize = FALSE,
|
||||||
evals = list(train = d_train),
|
watchlist = list(train = d_train),
|
||||||
nrounds = 20)
|
nrounds = 20)
|
||||||
|
|
||||||
var_imp <- xgb.importance(attr(x, 'Dimnames')[[2]], model = bst)
|
var_imp <- xgb.importance(attr(x, 'Dimnames')[[2]], model = bst)
|
||||||
|
|||||||
@ -55,7 +55,7 @@ message(sprintf("Creating '%s' from '%s'", OUT_DEF_FILE, IN_DLL_FILE))
|
|||||||
}
|
}
|
||||||
|
|
||||||
# use objdump to dump all the symbols
|
# use objdump to dump all the symbols
|
||||||
OBJDUMP_FILE <- file.path(tempdir(), "objdump-out.txt")
|
OBJDUMP_FILE <- "objdump-out.txt"
|
||||||
.pipe_shell_command_to_stdout(
|
.pipe_shell_command_to_stdout(
|
||||||
command = "objdump"
|
command = "objdump"
|
||||||
, args = c("-p", IN_DLL_FILE)
|
, args = c("-p", IN_DLL_FILE)
|
||||||
|
|||||||
@ -2,44 +2,16 @@
|
|||||||
% Please edit documentation in R/utils.R
|
% Please edit documentation in R/utils.R
|
||||||
\name{a-compatibility-note-for-saveRDS-save}
|
\name{a-compatibility-note-for-saveRDS-save}
|
||||||
\alias{a-compatibility-note-for-saveRDS-save}
|
\alias{a-compatibility-note-for-saveRDS-save}
|
||||||
\title{Model Serialization and Compatibility}
|
\title{Do not use \code{\link[base]{saveRDS}} or \code{\link[base]{save}} for long-term archival of
|
||||||
|
models. Instead, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}}.}
|
||||||
\description{
|
\description{
|
||||||
When it comes to serializing XGBoost models, it's possible to use R serializers such as
|
It is a common practice to use the built-in \code{\link[base]{saveRDS}} function (or
|
||||||
\link{save} or \link{saveRDS} to serialize an XGBoost R model, but XGBoost also provides
|
\code{\link[base]{save}}) to persist R objects to the disk. While it is possible to persist
|
||||||
its own serializers with better compatibility guarantees, which allow loading
|
\code{xgb.Booster} objects using \code{\link[base]{saveRDS}}, it is not advisable to do so if
|
||||||
said models in other language bindings of XGBoost.
|
the model is to be accessed in the future. If you train a model with the current version of
|
||||||
|
XGBoost and persist it with \code{\link[base]{saveRDS}}, the model is not guaranteed to be
|
||||||
Note that an \code{xgb.Booster} object, outside of its core components, might also keep:\itemize{
|
accessible in later releases of XGBoost. To ensure that your model can be accessed in future
|
||||||
\item Additional model configuration (accessible through \link{xgb.config}),
|
releases of XGBoost, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}} instead.
|
||||||
which includes model fitting parameters like \code{max_depth} and runtime parameters like \code{nthread}.
|
|
||||||
These are not necessarily useful for prediction/importance/plotting.
|
|
||||||
\item Additional R-specific attributes - e.g. results of callbacks, such as evaluation logs,
|
|
||||||
which are kept as a \code{data.table} object, accessible through \code{attributes(model)$evaluation_log}
|
|
||||||
if present.
|
|
||||||
}
|
|
||||||
|
|
||||||
The first one (configurations) does not have the same compatibility guarantees as
|
|
||||||
the model itself, including attributes that are set and accessed through \link{xgb.attributes} - that is, such configuration
|
|
||||||
might be lost after loading the booster in a different XGBoost version, regardless of the
|
|
||||||
serializer that was used. These are saved when using \link{saveRDS}, but will be discarded
|
|
||||||
if loaded into an incompatible XGBoost version. They are not saved when using XGBoost's
|
|
||||||
serializers from its public interface including \link{xgb.save} and \link{xgb.save.raw}.
|
|
||||||
|
|
||||||
The second ones (R attributes) are not part of the standard XGBoost model structure, and thus are
|
|
||||||
not saved when using XGBoost's own serializers. These attributes are only used for informational
|
|
||||||
purposes, such as keeping track of evaluation metrics as the model was fit, or saving the R
|
|
||||||
call that produced the model, but are otherwise not used for prediction / importance / plotting / etc.
|
|
||||||
These R attributes are only preserved when using R's serializers.
|
|
||||||
|
|
||||||
Note that XGBoost models in R starting from version \verb{2.1.0} and onwards, and XGBoost models
|
|
||||||
before version \verb{2.1.0}; have a very different R object structure and are incompatible with
|
|
||||||
each other. Hence, models that were saved with R serializers live \code{saveRDS} or \code{save} before
|
|
||||||
version \verb{2.1.0} will not work with latter \code{xgboost} versions and vice versa. Be aware that
|
|
||||||
the structure of R model objects could in theory change again in the future, so XGBoost's serializers
|
|
||||||
should be preferred for long-term storage.
|
|
||||||
|
|
||||||
Furthermore, note that using the package \code{qs} for serialization will require version 0.26 or
|
|
||||||
higher of said package, and will have the same compatibility restrictions as R serializers.
|
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
Use \code{\link{xgb.save}} to save the XGBoost model as a stand-alone file. You may opt into
|
Use \code{\link{xgb.save}} to save the XGBoost model as a stand-alone file. You may opt into
|
||||||
@ -52,29 +24,26 @@ re-construct the corresponding model. To read the model back, use \code{\link{xg
|
|||||||
The \code{\link{xgb.save.raw}} function is useful if you'd like to persist the XGBoost model
|
The \code{\link{xgb.save.raw}} function is useful if you'd like to persist the XGBoost model
|
||||||
as part of another R object.
|
as part of another R object.
|
||||||
|
|
||||||
Use \link{saveRDS} if you require the R-specific attributes that a booster might have, such
|
Note: Do not use \code{\link{xgb.serialize}} to store models long-term. It persists not only the
|
||||||
as evaluation logs, but note that future compatibility of such objects is outside XGBoost's
|
model but also internal configurations and parameters, and its format is not stable across
|
||||||
control as it relies on R's serialization format (see e.g. the details section in
|
multiple XGBoost versions. Use \code{\link{xgb.serialize}} only for checkpointing.
|
||||||
\link{serialize} and \link{save} from base R).
|
|
||||||
|
|
||||||
For more details and explanation about model persistence and archival, consult the page
|
For more details and explanation about model persistence and archival, consult the page
|
||||||
\url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}.
|
\url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}.
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
bst <- xgb.train(data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
|
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2,
|
||||||
max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
|
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
objective = "binary:logistic")
|
|
||||||
|
|
||||||
# Save as a stand-alone file; load it with xgb.load()
|
# Save as a stand-alone file; load it with xgb.load()
|
||||||
fname <- file.path(tempdir(), "xgb_model.ubj")
|
xgb.save(bst, 'xgb.model')
|
||||||
xgb.save(bst, fname)
|
bst2 <- xgb.load('xgb.model')
|
||||||
bst2 <- xgb.load(fname)
|
|
||||||
|
|
||||||
# Save as a stand-alone file (JSON); load it with xgb.load()
|
# Save as a stand-alone file (JSON); load it with xgb.load()
|
||||||
fname <- file.path(tempdir(), "xgb_model.json")
|
xgb.save(bst, 'xgb.model.json')
|
||||||
xgb.save(bst, fname)
|
bst2 <- xgb.load('xgb.model.json')
|
||||||
bst2 <- xgb.load(fname)
|
if (file.exists('xgb.model.json')) file.remove('xgb.model.json')
|
||||||
|
|
||||||
# Save as a raw byte vector; load it with xgb.load.raw()
|
# Save as a raw byte vector; load it with xgb.load.raw()
|
||||||
xgb_bytes <- xgb.save.raw(bst)
|
xgb_bytes <- xgb.save.raw(bst)
|
||||||
@ -85,11 +54,11 @@ obj <- list(xgb_model_bytes = xgb.save.raw(bst), description = "My first XGBoost
|
|||||||
# Persist the R object. Here, saveRDS() is okay, since it doesn't persist
|
# Persist the R object. Here, saveRDS() is okay, since it doesn't persist
|
||||||
# xgb.Booster directly. What's being persisted is the future-proof byte representation
|
# xgb.Booster directly. What's being persisted is the future-proof byte representation
|
||||||
# as given by xgb.save.raw().
|
# as given by xgb.save.raw().
|
||||||
fname <- file.path(tempdir(), "my_object.Rds")
|
saveRDS(obj, 'my_object.rds')
|
||||||
saveRDS(obj, fname)
|
|
||||||
# Read back the R object
|
# Read back the R object
|
||||||
obj2 <- readRDS(fname)
|
obj2 <- readRDS('my_object.rds')
|
||||||
# Re-construct xgb.Booster object from the bytes
|
# Re-construct xgb.Booster object from the bytes
|
||||||
bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
|
bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
|
||||||
|
if (file.exists('my_object.rds')) file.remove('my_object.rds')
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,15 +19,15 @@ UCI Machine Learning Repository.
|
|||||||
This data set includes the following fields:
|
This data set includes the following fields:
|
||||||
|
|
||||||
\itemize{
|
\itemize{
|
||||||
\item \code{label} the label for each record
|
\item \code{label} the label for each record
|
||||||
\item \code{data} a sparse Matrix of \code{dgCMatrix} class, with 126 columns.
|
\item \code{data} a sparse Matrix of \code{dgCMatrix} class, with 126 columns.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
\references{
|
\references{
|
||||||
\url{https://archive.ics.uci.edu/ml/datasets/Mushroom}
|
https://archive.ics.uci.edu/ml/datasets/Mushroom
|
||||||
|
|
||||||
Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
||||||
\url{http://archive.ics.uci.edu/ml}. Irvine, CA: University of California,
|
[http://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
|
||||||
School of Information and Computer Science.
|
School of Information and Computer Science.
|
||||||
}
|
}
|
||||||
\keyword{datasets}
|
\keyword{datasets}
|
||||||
|
|||||||
@ -19,15 +19,15 @@ UCI Machine Learning Repository.
|
|||||||
This data set includes the following fields:
|
This data set includes the following fields:
|
||||||
|
|
||||||
\itemize{
|
\itemize{
|
||||||
\item \code{label} the label for each record
|
\item \code{label} the label for each record
|
||||||
\item \code{data} a sparse Matrix of \code{dgCMatrix} class, with 126 columns.
|
\item \code{data} a sparse Matrix of \code{dgCMatrix} class, with 126 columns.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
\references{
|
\references{
|
||||||
\url{https://archive.ics.uci.edu/ml/datasets/Mushroom}
|
https://archive.ics.uci.edu/ml/datasets/Mushroom
|
||||||
|
|
||||||
Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
|
||||||
\url{http://archive.ics.uci.edu/ml}. Irvine, CA: University of California,
|
[http://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
|
||||||
School of Information and Computer Science.
|
School of Information and Computer Science.
|
||||||
}
|
}
|
||||||
\keyword{datasets}
|
\keyword{datasets}
|
||||||
|
|||||||
37
R-package/man/callbacks.Rd
Normal file
37
R-package/man/callbacks.Rd
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/callbacks.R
|
||||||
|
\name{callbacks}
|
||||||
|
\alias{callbacks}
|
||||||
|
\title{Callback closures for booster training.}
|
||||||
|
\description{
|
||||||
|
These are used to perform various service tasks either during boosting iterations or at the end.
|
||||||
|
This approach helps to modularize many of such tasks without bloating the main training methods,
|
||||||
|
and it offers .
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
By default, a callback function is run after each boosting iteration.
|
||||||
|
An R-attribute \code{is_pre_iteration} could be set for a callback to define a pre-iteration function.
|
||||||
|
|
||||||
|
When a callback function has \code{finalize} parameter, its finalizer part will also be run after
|
||||||
|
the boosting is completed.
|
||||||
|
|
||||||
|
WARNING: side-effects!!! Be aware that these callback functions access and modify things in
|
||||||
|
the environment from which they are called from, which is a fairly uncommon thing to do in R.
|
||||||
|
|
||||||
|
To write a custom callback closure, make sure you first understand the main concepts about R environments.
|
||||||
|
Check either R documentation on \code{\link[base]{environment}} or the
|
||||||
|
\href{http://adv-r.had.co.nz/Environments.html}{Environments chapter} from the "Advanced R"
|
||||||
|
book by Hadley Wickham. Further, the best option is to read the code of some of the existing callbacks -
|
||||||
|
choose ones that do something similar to what you want to achieve. Also, you would need to get familiar
|
||||||
|
with the objects available inside of the \code{xgb.train} and \code{xgb.cv} internal environments.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{cb.print.evaluation}},
|
||||||
|
\code{\link{cb.evaluation.log}},
|
||||||
|
\code{\link{cb.reset.parameters}},
|
||||||
|
\code{\link{cb.early.stop}},
|
||||||
|
\code{\link{cb.save.model}},
|
||||||
|
\code{\link{cb.cv.predict}},
|
||||||
|
\code{\link{xgb.train}},
|
||||||
|
\code{\link{xgb.cv}}
|
||||||
|
}
|
||||||
@ -1,27 +1,16 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/callbacks.R
|
% Please edit documentation in R/callbacks.R
|
||||||
\name{xgb.cb.cv.predict}
|
\name{cb.cv.predict}
|
||||||
\alias{xgb.cb.cv.predict}
|
\alias{cb.cv.predict}
|
||||||
\title{Callback for returning cross-validation based predictions.}
|
\title{Callback closure for returning cross-validation based predictions.}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.cb.cv.predict(save_models = FALSE, outputmargin = FALSE)
|
cb.cv.predict(save_models = FALSE)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{save_models}{A flag for whether to save the folds' models.}
|
\item{save_models}{a flag for whether to save the folds' models.}
|
||||||
|
|
||||||
\item{outputmargin}{Whether to save margin predictions (same effect as passing this
|
|
||||||
parameter to \link{predict.xgb.Booster}).}
|
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.cv},
|
Predictions are returned inside of the \code{pred} element, which is either a vector or a matrix,
|
||||||
but \bold{not} to \link{xgb.train}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
This callback function saves predictions for all of the test folds,
|
|
||||||
and also allows to save the folds' models.
|
|
||||||
}
|
|
||||||
\details{
|
|
||||||
Predictions are saved inside of the \code{pred} element, which is either a vector or a matrix,
|
|
||||||
depending on the number of prediction outputs per data row. The order of predictions corresponds
|
depending on the number of prediction outputs per data row. The order of predictions corresponds
|
||||||
to the order of rows in the original dataset. Note that when a custom \code{folds} list is
|
to the order of rows in the original dataset. Note that when a custom \code{folds} list is
|
||||||
provided in \code{xgb.cv}, the predictions would only be returned properly when this list is a
|
provided in \code{xgb.cv}, the predictions would only be returned properly when this list is a
|
||||||
@ -30,3 +19,25 @@ meaningful when user-provided folds have overlapping indices as in, e.g., random
|
|||||||
When some of the indices in the training dataset are not included into user-provided \code{folds},
|
When some of the indices in the training dataset are not included into user-provided \code{folds},
|
||||||
their prediction value would be \code{NA}.
|
their prediction value would be \code{NA}.
|
||||||
}
|
}
|
||||||
|
\description{
|
||||||
|
Callback closure for returning cross-validation based predictions.
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
This callback function saves predictions for all of the test folds,
|
||||||
|
and also allows to save the folds' models.
|
||||||
|
|
||||||
|
It is a "finalizer" callback and it uses early stopping information whenever it is available,
|
||||||
|
thus it must be run after the early stopping callback if the early stopping is used.
|
||||||
|
|
||||||
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{bst_folds},
|
||||||
|
\code{basket},
|
||||||
|
\code{data},
|
||||||
|
\code{end_iteration},
|
||||||
|
\code{params},
|
||||||
|
\code{num_parallel_tree},
|
||||||
|
\code{num_class}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}}
|
||||||
|
}
|
||||||
63
R-package/man/cb.early.stop.Rd
Normal file
63
R-package/man/cb.early.stop.Rd
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/callbacks.R
|
||||||
|
\name{cb.early.stop}
|
||||||
|
\alias{cb.early.stop}
|
||||||
|
\title{Callback closure to activate the early stopping.}
|
||||||
|
\usage{
|
||||||
|
cb.early.stop(
|
||||||
|
stopping_rounds,
|
||||||
|
maximize = FALSE,
|
||||||
|
metric_name = NULL,
|
||||||
|
verbose = TRUE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{stopping_rounds}{The number of rounds with no improvement in
|
||||||
|
the evaluation metric in order to stop the training.}
|
||||||
|
|
||||||
|
\item{maximize}{whether to maximize the evaluation metric}
|
||||||
|
|
||||||
|
\item{metric_name}{the name of an evaluation column to use as a criteria for early
|
||||||
|
stopping. If not set, the last column would be used.
|
||||||
|
Let's say the test data in \code{watchlist} was labelled as \code{dtest},
|
||||||
|
and one wants to use the AUC in test data for early stopping regardless of where
|
||||||
|
it is in the \code{watchlist}, then one of the following would need to be set:
|
||||||
|
\code{metric_name='dtest-auc'} or \code{metric_name='dtest_auc'}.
|
||||||
|
All dash '-' characters in metric names are considered equivalent to '_'.}
|
||||||
|
|
||||||
|
\item{verbose}{whether to print the early stopping information.}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Callback closure to activate the early stopping.
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
This callback function determines the condition for early stopping
|
||||||
|
by setting the \code{stop_condition = TRUE} flag in its calling frame.
|
||||||
|
|
||||||
|
The following additional fields are assigned to the model's R object:
|
||||||
|
\itemize{
|
||||||
|
\item \code{best_score} the evaluation score at the best iteration
|
||||||
|
\item \code{best_iteration} at which boosting iteration the best score has occurred (1-based index)
|
||||||
|
}
|
||||||
|
The Same values are also stored as xgb-attributes:
|
||||||
|
\itemize{
|
||||||
|
\item \code{best_iteration} is stored as a 0-based iteration index (for interoperability of binary models)
|
||||||
|
\item \code{best_msg} message string is also stored.
|
||||||
|
}
|
||||||
|
|
||||||
|
At least one data element is required in the evaluation watchlist for early stopping to work.
|
||||||
|
|
||||||
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{stop_condition},
|
||||||
|
\code{bst_evaluation},
|
||||||
|
\code{rank},
|
||||||
|
\code{bst} (or \code{bst_folds} and \code{basket}),
|
||||||
|
\code{iteration},
|
||||||
|
\code{begin_iteration},
|
||||||
|
\code{end_iteration},
|
||||||
|
\code{num_parallel_tree}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}},
|
||||||
|
\code{\link{xgb.attr}}
|
||||||
|
}
|
||||||
31
R-package/man/cb.evaluation.log.Rd
Normal file
31
R-package/man/cb.evaluation.log.Rd
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/callbacks.R
|
||||||
|
\name{cb.evaluation.log}
|
||||||
|
\alias{cb.evaluation.log}
|
||||||
|
\title{Callback closure for logging the evaluation history}
|
||||||
|
\usage{
|
||||||
|
cb.evaluation.log()
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Callback closure for logging the evaluation history
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
This callback function appends the current iteration evaluation results \code{bst_evaluation}
|
||||||
|
available in the calling parent frame to the \code{evaluation_log} list in a calling frame.
|
||||||
|
|
||||||
|
The finalizer callback (called with \code{finalize = TURE} in the end) converts
|
||||||
|
the \code{evaluation_log} list into a final data.table.
|
||||||
|
|
||||||
|
The iteration evaluation result \code{bst_evaluation} must be a named numeric vector.
|
||||||
|
|
||||||
|
Note: in the column names of the final data.table, the dash '-' character is replaced with
|
||||||
|
the underscore '_' in order to make the column names more like regular R identifiers.
|
||||||
|
|
||||||
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{evaluation_log},
|
||||||
|
\code{bst_evaluation},
|
||||||
|
\code{iteration}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}}
|
||||||
|
}
|
||||||
@ -1,69 +1,54 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/callbacks.R
|
% Please edit documentation in R/callbacks.R
|
||||||
\name{xgb.cb.gblinear.history}
|
\name{cb.gblinear.history}
|
||||||
\alias{xgb.cb.gblinear.history}
|
\alias{cb.gblinear.history}
|
||||||
\title{Callback for collecting coefficients history of a gblinear booster}
|
\title{Callback closure for collecting the model coefficients history of a gblinear booster
|
||||||
|
during its training.}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.cb.gblinear.history(sparse = FALSE)
|
cb.gblinear.history(sparse = FALSE)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{sparse}{when set to \code{FALSE}/\code{TRUE}, a dense/sparse matrix is used to store the result.
|
\item{sparse}{when set to FALSE/TRUE, a dense/sparse matrix is used to store the result.
|
||||||
Sparse format is useful when one expects only a subset of coefficients to be non-zero,
|
Sparse format is useful when one expects only a subset of coefficients to be non-zero,
|
||||||
when using the "thrifty" feature selector with fairly small number of top features
|
when using the "thrifty" feature selector with fairly small number of top features
|
||||||
selected per iteration.}
|
selected per iteration.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.train} or \link{xgb.cv}.
|
Results are stored in the \code{coefs} element of the closure.
|
||||||
|
The \code{\link{xgb.gblinear.history}} convenience function provides an easy
|
||||||
|
way to access it.
|
||||||
|
With \code{xgb.train}, it is either a dense of a sparse matrix.
|
||||||
|
While with \code{xgb.cv}, it is a list (an element per each fold) of such
|
||||||
|
matrices.
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Callback for collecting coefficients history of a gblinear booster
|
Callback closure for collecting the model coefficients history of a gblinear booster
|
||||||
|
during its training.
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
To keep things fast and simple, gblinear booster does not internally store the history of linear
|
To keep things fast and simple, gblinear booster does not internally store the history of linear
|
||||||
model coefficients at each boosting iteration. This callback provides a workaround for storing
|
model coefficients at each boosting iteration. This callback provides a workaround for storing
|
||||||
the coefficients' path, by extracting them after each training iteration.
|
the coefficients' path, by extracting them after each training iteration.
|
||||||
|
|
||||||
This callback will construct a matrix where rows are boosting iterations and columns are
|
Callback function expects the following values to be set in its calling frame:
|
||||||
feature coefficients (same order as when calling \link{coef.xgb.Booster}, with the intercept
|
\code{bst} (or \code{bst_folds}).
|
||||||
corresponding to the first column).
|
|
||||||
|
|
||||||
When there is more than one coefficient per feature (e.g. multi-class classification),
|
|
||||||
the result will be reshaped into a vector where coefficients are arranged first by features and
|
|
||||||
then by class (e.g. first 1 through N coefficients will be for the first class, then
|
|
||||||
coefficients N+1 through 2N for the second class, and so on).
|
|
||||||
|
|
||||||
If the result has only one coefficient per feature in the data, then the resulting matrix
|
|
||||||
will have column names matching with the feature names, otherwise (when there's more than
|
|
||||||
one coefficient per feature) the names will be composed as 'column name' + ':' + 'class index'
|
|
||||||
(so e.g. column 'c1' for class '0' will be named 'c1:0').
|
|
||||||
|
|
||||||
With \code{xgb.train}, the output is either a dense or a sparse matrix.
|
|
||||||
With with \code{xgb.cv}, it is a list (one element per each fold) of such
|
|
||||||
matrices.
|
|
||||||
|
|
||||||
Function \link{xgb.gblinear.history} function provides an easy way to retrieve the
|
|
||||||
outputs from this callback.
|
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
#### Binary classification:
|
#### Binary classification:
|
||||||
|
#
|
||||||
## Keep the number of threads to 1 for examples
|
|
||||||
nthread <- 1
|
|
||||||
data.table::setDTthreads(nthread)
|
|
||||||
|
|
||||||
# In the iris dataset, it is hard to linearly separate Versicolor class from the rest
|
# In the iris dataset, it is hard to linearly separate Versicolor class from the rest
|
||||||
# without considering the 2nd order interactions:
|
# without considering the 2nd order interactions:
|
||||||
x <- model.matrix(Species ~ .^2, iris)[,-1]
|
x <- model.matrix(Species ~ .^2, iris)[,-1]
|
||||||
colnames(x)
|
colnames(x)
|
||||||
dtrain <- xgb.DMatrix(scale(x), label = 1*(iris$Species == "versicolor"), nthread = nthread)
|
dtrain <- xgb.DMatrix(scale(x), label = 1*(iris$Species == "versicolor"), nthread = 2)
|
||||||
param <- list(booster = "gblinear", objective = "reg:logistic", eval_metric = "auc",
|
param <- list(booster = "gblinear", objective = "reg:logistic", eval_metric = "auc",
|
||||||
lambda = 0.0003, alpha = 0.0003, nthread = nthread)
|
lambda = 0.0003, alpha = 0.0003, nthread = 2)
|
||||||
# For 'shotgun', which is a default linear updater, using high eta values may result in
|
# For 'shotgun', which is a default linear updater, using high eta values may result in
|
||||||
# unstable behaviour in some datasets. With this simple dataset, however, the high learning
|
# unstable behaviour in some datasets. With this simple dataset, however, the high learning
|
||||||
# rate does not break the convergence, but allows us to illustrate the typical pattern of
|
# rate does not break the convergence, but allows us to illustrate the typical pattern of
|
||||||
# "stochastic explosion" behaviour of this lock-free algorithm at early boosting iterations.
|
# "stochastic explosion" behaviour of this lock-free algorithm at early boosting iterations.
|
||||||
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 200, eta = 1.,
|
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 200, eta = 1.,
|
||||||
callbacks = list(xgb.cb.gblinear.history()))
|
callbacks = list(cb.gblinear.history()))
|
||||||
# Extract the coefficients' path and plot them vs boosting iteration number:
|
# Extract the coefficients' path and plot them vs boosting iteration number:
|
||||||
coef_path <- xgb.gblinear.history(bst)
|
coef_path <- xgb.gblinear.history(bst)
|
||||||
matplot(coef_path, type = 'l')
|
matplot(coef_path, type = 'l')
|
||||||
@ -72,7 +57,7 @@ matplot(coef_path, type = 'l')
|
|||||||
# Will try the classical componentwise boosting which selects a single best feature per round:
|
# Will try the classical componentwise boosting which selects a single best feature per round:
|
||||||
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 200, eta = 0.8,
|
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 200, eta = 0.8,
|
||||||
updater = 'coord_descent', feature_selector = 'thrifty', top_k = 1,
|
updater = 'coord_descent', feature_selector = 'thrifty', top_k = 1,
|
||||||
callbacks = list(xgb.cb.gblinear.history()))
|
callbacks = list(cb.gblinear.history()))
|
||||||
matplot(xgb.gblinear.history(bst), type = 'l')
|
matplot(xgb.gblinear.history(bst), type = 'l')
|
||||||
# Componentwise boosting is known to have similar effect to Lasso regularization.
|
# Componentwise boosting is known to have similar effect to Lasso regularization.
|
||||||
# Try experimenting with various values of top_k, eta, nrounds,
|
# Try experimenting with various values of top_k, eta, nrounds,
|
||||||
@ -80,20 +65,20 @@ matplot(xgb.gblinear.history(bst), type = 'l')
|
|||||||
|
|
||||||
# For xgb.cv:
|
# For xgb.cv:
|
||||||
bst <- xgb.cv(param, dtrain, nfold = 5, nrounds = 100, eta = 0.8,
|
bst <- xgb.cv(param, dtrain, nfold = 5, nrounds = 100, eta = 0.8,
|
||||||
callbacks = list(xgb.cb.gblinear.history()))
|
callbacks = list(cb.gblinear.history()))
|
||||||
# coefficients in the CV fold #3
|
# coefficients in the CV fold #3
|
||||||
matplot(xgb.gblinear.history(bst)[[3]], type = 'l')
|
matplot(xgb.gblinear.history(bst)[[3]], type = 'l')
|
||||||
|
|
||||||
|
|
||||||
#### Multiclass classification:
|
#### Multiclass classification:
|
||||||
#
|
#
|
||||||
dtrain <- xgb.DMatrix(scale(x), label = as.numeric(iris$Species) - 1, nthread = nthread)
|
dtrain <- xgb.DMatrix(scale(x), label = as.numeric(iris$Species) - 1, nthread = 1)
|
||||||
param <- list(booster = "gblinear", objective = "multi:softprob", num_class = 3,
|
param <- list(booster = "gblinear", objective = "multi:softprob", num_class = 3,
|
||||||
lambda = 0.0003, alpha = 0.0003, nthread = nthread)
|
lambda = 0.0003, alpha = 0.0003, nthread = 1)
|
||||||
# For the default linear updater 'shotgun' it sometimes is helpful
|
# For the default linear updater 'shotgun' it sometimes is helpful
|
||||||
# to use smaller eta to reduce instability
|
# to use smaller eta to reduce instability
|
||||||
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 50, eta = 0.5,
|
bst <- xgb.train(param, dtrain, list(tr=dtrain), nrounds = 50, eta = 0.5,
|
||||||
callbacks = list(xgb.cb.gblinear.history()))
|
callbacks = list(cb.gblinear.history()))
|
||||||
# Will plot the coefficient paths separately for each class:
|
# Will plot the coefficient paths separately for each class:
|
||||||
matplot(xgb.gblinear.history(bst, class_index = 0), type = 'l')
|
matplot(xgb.gblinear.history(bst, class_index = 0), type = 'l')
|
||||||
matplot(xgb.gblinear.history(bst, class_index = 1), type = 'l')
|
matplot(xgb.gblinear.history(bst, class_index = 1), type = 'l')
|
||||||
@ -101,11 +86,11 @@ matplot(xgb.gblinear.history(bst, class_index = 2), type = 'l')
|
|||||||
|
|
||||||
# CV:
|
# CV:
|
||||||
bst <- xgb.cv(param, dtrain, nfold = 5, nrounds = 70, eta = 0.5,
|
bst <- xgb.cv(param, dtrain, nfold = 5, nrounds = 70, eta = 0.5,
|
||||||
callbacks = list(xgb.cb.gblinear.history(FALSE)))
|
callbacks = list(cb.gblinear.history(FALSE)))
|
||||||
# 1st fold of 1st class
|
# 1st fold of 1st class
|
||||||
matplot(xgb.gblinear.history(bst, class_index = 0)[[1]], type = 'l')
|
matplot(xgb.gblinear.history(bst, class_index = 0)[[1]], type = 'l')
|
||||||
|
|
||||||
}
|
}
|
||||||
\seealso{
|
\seealso{
|
||||||
\link{xgb.gblinear.history}, \link{coef.xgb.Booster}.
|
\code{\link{callbacks}}, \code{\link{xgb.gblinear.history}}.
|
||||||
}
|
}
|
||||||
29
R-package/man/cb.print.evaluation.Rd
Normal file
29
R-package/man/cb.print.evaluation.Rd
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/callbacks.R
|
||||||
|
\name{cb.print.evaluation}
|
||||||
|
\alias{cb.print.evaluation}
|
||||||
|
\title{Callback closure for printing the result of evaluation}
|
||||||
|
\usage{
|
||||||
|
cb.print.evaluation(period = 1, showsd = TRUE)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{period}{results would be printed every number of periods}
|
||||||
|
|
||||||
|
\item{showsd}{whether standard deviations should be printed (when available)}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Callback closure for printing the result of evaluation
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
The callback function prints the result of evaluation at every \code{period} iterations.
|
||||||
|
The initial and the last iteration's evaluations are always printed.
|
||||||
|
|
||||||
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{bst_evaluation} (also \code{bst_evaluation_err} when available),
|
||||||
|
\code{iteration},
|
||||||
|
\code{begin_iteration},
|
||||||
|
\code{end_iteration}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}}
|
||||||
|
}
|
||||||
@ -1,10 +1,10 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/callbacks.R
|
% Please edit documentation in R/callbacks.R
|
||||||
\name{xgb.cb.reset.parameters}
|
\name{cb.reset.parameters}
|
||||||
\alias{xgb.cb.reset.parameters}
|
\alias{cb.reset.parameters}
|
||||||
\title{Callback for resetting the booster's parameters at each iteration.}
|
\title{Callback closure for resetting the booster's parameters at each iteration.}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.cb.reset.parameters(new_params)
|
cb.reset.parameters(new_params)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{new_params}{a list where each element corresponds to a parameter that needs to be reset.
|
\item{new_params}{a list where each element corresponds to a parameter that needs to be reset.
|
||||||
@ -14,16 +14,23 @@ or a function of two parameters \code{learning_rates(iteration, nrounds)}
|
|||||||
which returns a new parameter value by using the current iteration number
|
which returns a new parameter value by using the current iteration number
|
||||||
and the total number of boosting rounds.}
|
and the total number of boosting rounds.}
|
||||||
}
|
}
|
||||||
\value{
|
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.train} or \link{xgb.cv}.
|
|
||||||
}
|
|
||||||
\description{
|
\description{
|
||||||
Callback for resetting the booster's parameters at each iteration.
|
Callback closure for resetting the booster's parameters at each iteration.
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
|
This is a "pre-iteration" callback function used to reset booster's parameters
|
||||||
|
at the beginning of each iteration.
|
||||||
|
|
||||||
Note that when training is resumed from some previous model, and a function is used to
|
Note that when training is resumed from some previous model, and a function is used to
|
||||||
reset a parameter value, the \code{nrounds} argument in this function would be the
|
reset a parameter value, the \code{nrounds} argument in this function would be the
|
||||||
the number of boosting rounds in the current training.
|
the number of boosting rounds in the current training.
|
||||||
|
|
||||||
Does not leave any attribute in the booster.
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{bst} or \code{bst_folds},
|
||||||
|
\code{iteration},
|
||||||
|
\code{begin_iteration},
|
||||||
|
\code{end_iteration}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}}
|
||||||
}
|
}
|
||||||
33
R-package/man/cb.save.model.Rd
Normal file
33
R-package/man/cb.save.model.Rd
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/callbacks.R
|
||||||
|
\name{cb.save.model}
|
||||||
|
\alias{cb.save.model}
|
||||||
|
\title{Callback closure for saving a model file.}
|
||||||
|
\usage{
|
||||||
|
cb.save.model(save_period = 0, save_name = "xgboost.model")
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{save_period}{save the model to disk after every
|
||||||
|
\code{save_period} iterations; 0 means save the model at the end.}
|
||||||
|
|
||||||
|
\item{save_name}{the name or path for the saved model file.
|
||||||
|
It can contain a \code{\link[base]{sprintf}} formatting specifier
|
||||||
|
to include the integer iteration number in the file name.
|
||||||
|
E.g., with \code{save_name} = 'xgboost_%04d.model',
|
||||||
|
the file saved at iteration 50 would be named "xgboost_0050.model".}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Callback closure for saving a model file.
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
This callback function allows to save an xgb-model file, either periodically after each \code{save_period}'s or at the end.
|
||||||
|
|
||||||
|
Callback function expects the following values to be set in its calling frame:
|
||||||
|
\code{bst},
|
||||||
|
\code{iteration},
|
||||||
|
\code{begin_iteration},
|
||||||
|
\code{end_iteration}.
|
||||||
|
}
|
||||||
|
\seealso{
|
||||||
|
\code{\link{callbacks}}
|
||||||
|
}
|
||||||
@ -1,50 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.Booster.R
|
|
||||||
\name{coef.xgb.Booster}
|
|
||||||
\alias{coef.xgb.Booster}
|
|
||||||
\title{Extract coefficients from linear booster}
|
|
||||||
\usage{
|
|
||||||
\method{coef}{xgb.Booster}(object, ...)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{object}{A fitted booster of 'gblinear' type.}
|
|
||||||
|
|
||||||
\item{...}{Not used.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
The extracted coefficients:\itemize{
|
|
||||||
\item If there's only one coefficient per column in the data, will be returned as a
|
|
||||||
vector, potentially containing the feature names if available, with the intercept
|
|
||||||
as first column.
|
|
||||||
\item If there's more than one coefficient per column in the data (e.g. when using
|
|
||||||
\code{objective="multi:softmax"}), will be returned as a matrix with dimensions equal
|
|
||||||
to \verb{[num_features, num_cols]}, with the intercepts as first row. Note that the column
|
|
||||||
(classes in multi-class classification) dimension will not be named.
|
|
||||||
}
|
|
||||||
|
|
||||||
The intercept returned here will include the 'base_score' parameter (unlike the 'bias'
|
|
||||||
or the last coefficient in the model dump, which doesn't have 'base_score' added to it),
|
|
||||||
hence one should get the same values from calling \code{predict(..., outputmargin = TRUE)} and
|
|
||||||
from performing a matrix multiplication with \code{model.matrix(~., ...)}.
|
|
||||||
|
|
||||||
Be aware that the coefficients are obtained by first converting them to strings and
|
|
||||||
back, so there will always be some very small lose of precision compared to the actual
|
|
||||||
coefficients as used by \link{predict.xgb.Booster}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Extracts the coefficients from a 'gblinear' booster object,
|
|
||||||
as produced by \code{xgb.train} when using parameter \code{booster="gblinear"}.
|
|
||||||
|
|
||||||
Note: this function will error out if passing a booster model
|
|
||||||
which is not of "gblinear" type.
|
|
||||||
}
|
|
||||||
\examples{
|
|
||||||
library(xgboost)
|
|
||||||
data(mtcars)
|
|
||||||
y <- mtcars[, 1]
|
|
||||||
x <- as.matrix(mtcars[, -1])
|
|
||||||
dm <- xgb.DMatrix(data = x, label = y, nthread = 1)
|
|
||||||
params <- list(booster = "gblinear", nthread = 1)
|
|
||||||
model <- xgb.train(data = dm, params = params, nrounds = 2)
|
|
||||||
coef(model)
|
|
||||||
}
|
|
||||||
@ -1,78 +1,36 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/xgb.Booster.R, R/xgb.DMatrix.R
|
% Please edit documentation in R/xgb.DMatrix.R
|
||||||
\name{getinfo.xgb.Booster}
|
\name{getinfo}
|
||||||
\alias{getinfo.xgb.Booster}
|
|
||||||
\alias{setinfo.xgb.Booster}
|
|
||||||
\alias{getinfo}
|
\alias{getinfo}
|
||||||
\alias{getinfo.xgb.DMatrix}
|
\alias{getinfo.xgb.DMatrix}
|
||||||
\alias{setinfo}
|
\title{Get information of an xgb.DMatrix object}
|
||||||
\alias{setinfo.xgb.DMatrix}
|
|
||||||
\title{Get or set information of xgb.DMatrix and xgb.Booster objects}
|
|
||||||
\usage{
|
\usage{
|
||||||
\method{getinfo}{xgb.Booster}(object, name)
|
getinfo(object, ...)
|
||||||
|
|
||||||
\method{setinfo}{xgb.Booster}(object, name, info)
|
\method{getinfo}{xgb.DMatrix}(object, name, ...)
|
||||||
|
|
||||||
getinfo(object, name)
|
|
||||||
|
|
||||||
\method{getinfo}{xgb.DMatrix}(object, name)
|
|
||||||
|
|
||||||
setinfo(object, name, info)
|
|
||||||
|
|
||||||
\method{setinfo}{xgb.DMatrix}(object, name, info)
|
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{object}{Object of class \code{xgb.DMatrix} of \code{xgb.Booster}.}
|
\item{object}{Object of class \code{xgb.DMatrix}}
|
||||||
|
|
||||||
|
\item{...}{other parameters}
|
||||||
|
|
||||||
\item{name}{the name of the information field to get (see details)}
|
\item{name}{the name of the information field to get (see details)}
|
||||||
|
|
||||||
\item{info}{the specific field of information to set}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
For \code{getinfo}, will return the requested field. For \code{setinfo}, will always return value \code{TRUE}
|
|
||||||
if it succeeds.
|
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Get or set information of xgb.DMatrix and xgb.Booster objects
|
Get information of an xgb.DMatrix object
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
The \code{name} field can be one of the following for \code{xgb.DMatrix}:
|
The \code{name} field can be one of the following:
|
||||||
|
|
||||||
\itemize{
|
\itemize{
|
||||||
\item \code{label}
|
\item \code{label}: label XGBoost learn from ;
|
||||||
\item \code{weight}
|
\item \code{weight}: to do a weight rescale ;
|
||||||
\item \code{base_margin}
|
\item \code{base_margin}: base margin is the base prediction XGBoost will boost from ;
|
||||||
\item \code{label_lower_bound}
|
\item \code{nrow}: number of rows of the \code{xgb.DMatrix}.
|
||||||
\item \code{label_upper_bound}
|
|
||||||
\item \code{group}
|
|
||||||
\item \code{feature_type}
|
|
||||||
\item \code{feature_name}
|
|
||||||
\item \code{nrow}
|
|
||||||
}
|
|
||||||
See the documentation for \link{xgb.DMatrix} for more information about these fields.
|
|
||||||
|
|
||||||
For \code{xgb.Booster}, can be one of the following:
|
|
||||||
\itemize{
|
|
||||||
\item \code{feature_type}
|
|
||||||
\item \code{feature_name}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Note that, while 'qid' cannot be retrieved, it's possible to get the equivalent 'group'
|
\code{group} can be setup by \code{setinfo} but can't be retrieved by \code{getinfo}.
|
||||||
for a DMatrix that had 'qid' assigned.
|
|
||||||
|
|
||||||
\bold{Important}: when calling \code{setinfo}, the objects are modified in-place. See
|
|
||||||
\link{xgb.copy.Booster} for an idea of this in-place assignment works.
|
|
||||||
|
|
||||||
See the documentation for \link{xgb.DMatrix} for possible fields that can be set
|
|
||||||
(which correspond to arguments in that function).
|
|
||||||
|
|
||||||
Note that the following fields are allowed in the construction of an \code{xgb.DMatrix}
|
|
||||||
but \bold{aren't} allowed here:\itemize{
|
|
||||||
\item data
|
|
||||||
\item missing
|
|
||||||
\item silent
|
|
||||||
\item nthread
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
@ -83,11 +41,4 @@ setinfo(dtrain, 'label', 1-labels)
|
|||||||
|
|
||||||
labels2 <- getinfo(dtrain, 'label')
|
labels2 <- getinfo(dtrain, 'label')
|
||||||
stopifnot(all(labels2 == 1-labels))
|
stopifnot(all(labels2 == 1-labels))
|
||||||
data(agaricus.train, package='xgboost')
|
|
||||||
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
|
||||||
|
|
||||||
labels <- getinfo(dtrain, 'label')
|
|
||||||
setinfo(dtrain, 'label', 1-labels)
|
|
||||||
labels2 <- getinfo(dtrain, 'label')
|
|
||||||
stopifnot(all.equal(labels2, 1-labels))
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
R-package/man/normalize.Rd
Normal file
18
R-package/man/normalize.Rd
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/xgb.ggplot.R
|
||||||
|
\name{normalize}
|
||||||
|
\alias{normalize}
|
||||||
|
\title{Scale feature value to have mean 0, standard deviation 1}
|
||||||
|
\usage{
|
||||||
|
normalize(x)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{x}{Numeric vector}
|
||||||
|
}
|
||||||
|
\value{
|
||||||
|
Numeric vector with mean 0 and sd 1.
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
This is used to compare multiple features on the same plot.
|
||||||
|
Internal utility function
|
||||||
|
}
|
||||||
@ -2,13 +2,15 @@
|
|||||||
% Please edit documentation in R/xgb.Booster.R
|
% Please edit documentation in R/xgb.Booster.R
|
||||||
\name{predict.xgb.Booster}
|
\name{predict.xgb.Booster}
|
||||||
\alias{predict.xgb.Booster}
|
\alias{predict.xgb.Booster}
|
||||||
\title{Predict method for XGBoost model}
|
\alias{predict.xgb.Booster.handle}
|
||||||
|
\title{Predict method for eXtreme Gradient Boosting model}
|
||||||
\usage{
|
\usage{
|
||||||
\method{predict}{xgb.Booster}(
|
\method{predict}{xgb.Booster}(
|
||||||
object,
|
object,
|
||||||
newdata,
|
newdata,
|
||||||
missing = NA,
|
missing = NA,
|
||||||
outputmargin = FALSE,
|
outputmargin = FALSE,
|
||||||
|
ntreelimit = NULL,
|
||||||
predleaf = FALSE,
|
predleaf = FALSE,
|
||||||
predcontrib = FALSE,
|
predcontrib = FALSE,
|
||||||
approxcontrib = FALSE,
|
approxcontrib = FALSE,
|
||||||
@ -17,142 +19,96 @@
|
|||||||
training = FALSE,
|
training = FALSE,
|
||||||
iterationrange = NULL,
|
iterationrange = NULL,
|
||||||
strict_shape = FALSE,
|
strict_shape = FALSE,
|
||||||
validate_features = FALSE,
|
|
||||||
base_margin = NULL,
|
|
||||||
...
|
...
|
||||||
)
|
)
|
||||||
|
|
||||||
|
\method{predict}{xgb.Booster.handle}(object, ...)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{object}{Object of class \code{xgb.Booster}.}
|
\item{object}{Object of class \code{xgb.Booster} or \code{xgb.Booster.handle}}
|
||||||
|
|
||||||
\item{newdata}{Takes \code{data.frame}, \code{matrix}, \code{dgCMatrix}, \code{dgRMatrix}, \code{dsparseVector},
|
\item{newdata}{takes \code{matrix}, \code{dgCMatrix}, \code{dgRMatrix}, \code{dsparseVector},
|
||||||
local data file, or \code{xgb.DMatrix}.
|
local data file or \code{xgb.DMatrix}.
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ For single-row predictions on sparse data, it's recommended to use CSR format. If passing
|
For single-row predictions on sparse data, it's recommended to use CSR format. If passing
|
||||||
a sparse vector, it will take it as a row vector.
|
a sparse vector, it will take it as a row vector.}
|
||||||
|
|
||||||
Note that, for repeated predictions on the same data, one might want to create a DMatrix to
|
\item{missing}{Missing is only used when input is dense matrix. Pick a float value that represents
|
||||||
pass here instead of passing R types like matrices or data frames, as predictions will be
|
missing values in data (e.g., sometimes 0 or some other extreme value is used).}
|
||||||
faster on DMatrix.
|
|
||||||
|
|
||||||
If `newdata` is a `data.frame`, be aware that:\\itemize\{
|
\item{outputmargin}{whether the prediction should be returned in the for of original untransformed
|
||||||
\\item Columns will be converted to numeric if they aren't already, which could potentially make
|
|
||||||
the operation slower than in an equivalent `matrix` object.
|
|
||||||
\\item The order of the columns must match with that of the data from which the model was fitted
|
|
||||||
(i.e. columns will not be referenced by their names, just by their order in the data).
|
|
||||||
\\item If the model was fitted to data with categorical columns, these columns must be of
|
|
||||||
`factor` type here, and must use the same encoding (i.e. have the same levels).
|
|
||||||
\\item If `newdata` contains any `factor` columns, they will be converted to base-0
|
|
||||||
encoding (same as during DMatrix creation) - hence, one should not pass a `factor`
|
|
||||||
under a column which during training had a different type.
|
|
||||||
\}
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{missing}{Float value that represents missing values in data (e.g., 0 or some other extreme value).
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ This parameter is not used when `newdata` is an `xgb.DMatrix` - in such cases, should pass
|
|
||||||
this as an argument to the DMatrix constructor instead.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{outputmargin}{Whether the prediction should be returned in the form of original untransformed
|
|
||||||
sum of predictions from boosting iterations' results. E.g., setting \code{outputmargin=TRUE} for
|
sum of predictions from boosting iterations' results. E.g., setting \code{outputmargin=TRUE} for
|
||||||
logistic regression would return log-odds instead of probabilities.}
|
logistic regression would result in predictions for log-odds instead of probabilities.}
|
||||||
|
|
||||||
\item{predleaf}{Whether to predict per-tree leaf indices.}
|
\item{ntreelimit}{Deprecated, use \code{iterationrange} instead.}
|
||||||
|
|
||||||
\item{predcontrib}{Whether to return feature contributions to individual predictions (see Details).}
|
\item{predleaf}{whether predict leaf index.}
|
||||||
|
|
||||||
\item{approxcontrib}{Whether to use a fast approximation for feature contributions (see Details).}
|
\item{predcontrib}{whether to return feature contributions to individual predictions (see Details).}
|
||||||
|
|
||||||
\item{predinteraction}{Whether to return contributions of feature interactions to individual predictions (see Details).}
|
\item{approxcontrib}{whether to use a fast approximation for feature contributions (see Details).}
|
||||||
|
|
||||||
\item{reshape}{Whether to reshape the vector of predictions to matrix form when there are several
|
\item{predinteraction}{whether to return contributions of feature interactions to individual predictions (see Details).}
|
||||||
prediction outputs per case. No effect if \code{predleaf}, \code{predcontrib},
|
|
||||||
or \code{predinteraction} is \code{TRUE}.}
|
|
||||||
|
|
||||||
\item{training}{Whether the prediction result is used for training. For dart booster,
|
\item{reshape}{whether to reshape the vector of predictions to a matrix form when there are several
|
||||||
|
prediction outputs per case. This option has no effect when either of predleaf, predcontrib,
|
||||||
|
or predinteraction flags is TRUE.}
|
||||||
|
|
||||||
|
\item{training}{whether is the prediction result used for training. For dart booster,
|
||||||
training predicting will perform dropout.}
|
training predicting will perform dropout.}
|
||||||
|
|
||||||
\item{iterationrange}{Sequence of rounds/iterations from the model to use for prediction, specified by passing
|
\item{iterationrange}{Specifies which layer of trees are used in prediction. For
|
||||||
a two-dimensional vector with the start and end numbers in the sequence (same format as R's \code{seq} - i.e.
|
example, if a random forest is trained with 100 rounds. Specifying
|
||||||
base-1 indexing, and inclusive of both ends).
|
`iterationrange=(1, 21)`, then only the forests built during [1, 21) (half open set)
|
||||||
|
rounds are used in this prediction. It's 1-based index just like R vector. When set
|
||||||
|
to \code{c(1, 1)} XGBoost will use all trees.}
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ For example, passing `c(1,20)` will predict using the first twenty iterations, while passing `c(1,1)` will
|
\item{strict_shape}{Default is \code{FALSE}. When it's set to \code{TRUE}, output
|
||||||
predict using only the first one.
|
type and shape of prediction are invariant to model type.}
|
||||||
|
|
||||||
If passing `NULL`, will either stop at the best iteration if the model used early stopping, or use all
|
\item{...}{Parameters passed to \code{predict.xgb.Booster}}
|
||||||
of the iterations (rounds) otherwise.
|
|
||||||
|
|
||||||
If passing "all", will use all of the rounds regardless of whether the model had early stopping or not.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{strict_shape}{Default is \code{FALSE}. When set to \code{TRUE}, the output
|
|
||||||
type and shape of predictions are invariant to the model type.}
|
|
||||||
|
|
||||||
\item{validate_features}{When \code{TRUE}, validate that the Booster's and newdata's feature_names
|
|
||||||
match (only applicable when both \code{object} and \code{newdata} have feature names).
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ If the column names differ and `newdata` is not an `xgb.DMatrix`, will try to reorder
|
|
||||||
the columns in `newdata` to match with the booster's.
|
|
||||||
|
|
||||||
If the booster has feature types and `newdata` is either an `xgb.DMatrix` or `data.frame`,
|
|
||||||
will additionally verify that categorical columns are of the correct type in `newdata`,
|
|
||||||
throwing an error if they do not match.
|
|
||||||
|
|
||||||
If passing `FALSE`, it is assumed that the feature names and types are the same,
|
|
||||||
and come in the same order as in the training data.
|
|
||||||
|
|
||||||
Note that this check might add some sizable latency to the predictions, so it's
|
|
||||||
recommended to disable it for performance-sensitive applications.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{base_margin}{Base margin used for boosting from existing model.
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ Note that, if `newdata` is an `xgb.DMatrix` object, this argument will
|
|
||||||
be ignored as it needs to be added to the DMatrix instead (e.g. by passing it as
|
|
||||||
an argument in its constructor, or by calling \link{setinfo.xgb.DMatrix}).
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{...}{Not used.}
|
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
The return type depends on \code{strict_shape}. If \code{FALSE} (default):
|
The return type is different depending whether \code{strict_shape} is set to \code{TRUE}. By default,
|
||||||
\itemize{
|
for regression or binary classification, it returns a vector of length \code{nrows(newdata)}.
|
||||||
\item For regression or binary classification: A vector of length \code{nrows(newdata)}.
|
For multiclass classification, either a \code{num_class * nrows(newdata)} vector or
|
||||||
\item For multiclass classification: A vector of length \code{num_class * nrows(newdata)} or
|
a \code{(nrows(newdata), num_class)} dimension matrix is returned, depending on
|
||||||
a \verb{(nrows(newdata), num_class)} matrix, depending on the \code{reshape} value.
|
the \code{reshape} value.
|
||||||
\item When \code{predleaf = TRUE}: A matrix with one column per tree.
|
|
||||||
\item When \code{predcontrib = TRUE}: When not multiclass, a matrix with
|
|
||||||
\code{ num_features + 1} columns. The last "+ 1" column corresponds to the baseline value.
|
|
||||||
In the multiclass case, a list of \code{num_class} such matrices.
|
|
||||||
The contribution values are on the scale of untransformed margin
|
|
||||||
(e.g., for binary classification, the values are log-odds deviations from the baseline).
|
|
||||||
\item When \code{predinteraction = TRUE}: When not multiclass, the output is a 3d array of
|
|
||||||
dimension \code{c(nrow, num_features + 1, num_features + 1)}. The off-diagonal (in the last two dimensions)
|
|
||||||
elements represent different feature interaction contributions. The array is symmetric WRT the last
|
|
||||||
two dimensions. The "+ 1" columns corresponds to the baselines. Summing this array along the last dimension should
|
|
||||||
produce practically the same result as \code{predcontrib = TRUE}.
|
|
||||||
In the multiclass case, a list of \code{num_class} such arrays.
|
|
||||||
}
|
|
||||||
|
|
||||||
When \code{strict_shape = TRUE}, the output is always an array:
|
When \code{predleaf = TRUE}, the output is a matrix object with the
|
||||||
\itemize{
|
number of columns corresponding to the number of trees.
|
||||||
\item For normal predictions, the output has dimension \verb{(num_class, nrow(newdata))}.
|
|
||||||
\item For \code{predcontrib = TRUE}, the dimension is \verb{(ncol(newdata) + 1, num_class, nrow(newdata))}.
|
When \code{predcontrib = TRUE} and it is not a multiclass setting, the output is a matrix object with
|
||||||
\item For \code{predinteraction = TRUE}, the dimension is \verb{(ncol(newdata) + 1, ncol(newdata) + 1, num_class, nrow(newdata))}.
|
\code{num_features + 1} columns. The last "+ 1" column in a matrix corresponds to bias.
|
||||||
\item For \code{predleaf = TRUE}, the dimension is \verb{(n_trees_in_forest, num_class, n_iterations, nrow(newdata))}.
|
For a multiclass case, a list of \code{num_class} elements is returned, where each element is
|
||||||
}
|
such a matrix. The contribution values are on the scale of untransformed margin
|
||||||
|
(e.g., for binary classification would mean that the contributions are log-odds deviations from bias).
|
||||||
|
|
||||||
|
When \code{predinteraction = TRUE} and it is not a multiclass setting, the output is a 3d array with
|
||||||
|
dimensions \code{c(nrow, num_features + 1, num_features + 1)}. The off-diagonal (in the last two dimensions)
|
||||||
|
elements represent different features interaction contributions. The array is symmetric WRT the last
|
||||||
|
two dimensions. The "+ 1" columns corresponds to bias. Summing this array along the last dimension should
|
||||||
|
produce practically the same result as predict with \code{predcontrib = TRUE}.
|
||||||
|
For a multiclass case, a list of \code{num_class} elements is returned, where each element is
|
||||||
|
such an array.
|
||||||
|
|
||||||
|
When \code{strict_shape} is set to \code{TRUE}, the output is always an array. For
|
||||||
|
normal prediction, the output is a 2-dimension array \code{(num_class, nrow(newdata))}.
|
||||||
|
|
||||||
|
For \code{predcontrib = TRUE}, output is \code{(ncol(newdata) + 1, num_class, nrow(newdata))}
|
||||||
|
For \code{predinteraction = TRUE}, output is \code{(ncol(newdata) + 1, ncol(newdata) + 1, num_class, nrow(newdata))}
|
||||||
|
For \code{predleaf = TRUE}, output is \code{(n_trees_in_forest, num_class, n_iterations, nrow(newdata))}
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Predict values on data based on xgboost model.
|
Predicted values based on either xgboost model or model handle object.
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
Note that \code{iterationrange} would currently do nothing for predictions from "gblinear",
|
Note that \code{iterationrange} would currently do nothing for predictions from gblinear,
|
||||||
since "gblinear" doesn't keep its boosting history.
|
since gblinear doesn't keep its boosting history.
|
||||||
|
|
||||||
One possible practical applications of the \code{predleaf} option is to use the model
|
One possible practical applications of the \code{predleaf} option is to use the model
|
||||||
as a generator of new features which capture non-linearity and interactions,
|
as a generator of new features which capture non-linearity and interactions,
|
||||||
e.g., as implemented in \code{\link[=xgb.create.features]{xgb.create.features()}}.
|
e.g., as implemented in \code{\link{xgb.create.features}}.
|
||||||
|
|
||||||
Setting \code{predcontrib = TRUE} allows to calculate contributions of each feature to
|
Setting \code{predcontrib = TRUE} allows to calculate contributions of each feature to
|
||||||
individual predictions. For "gblinear" booster, feature contributions are simply linear terms
|
individual predictions. For "gblinear" booster, feature contributions are simply linear terms
|
||||||
@ -168,35 +124,23 @@ Since it quadratically depends on the number of features, it is recommended to p
|
|||||||
of the most important features first. See below about the format of the returned results.
|
of the most important features first. See below about the format of the returned results.
|
||||||
|
|
||||||
The \code{predict()} method uses as many threads as defined in \code{xgb.Booster} object (all by default).
|
The \code{predict()} method uses as many threads as defined in \code{xgb.Booster} object (all by default).
|
||||||
If you want to change their number, assign a new number to \code{nthread} using \code{\link[=xgb.parameters<-]{xgb.parameters<-()}}.
|
If you want to change their number, then assign a new number to \code{nthread} using \code{\link{xgb.parameters<-}}.
|
||||||
Note that converting a matrix to \code{\link[=xgb.DMatrix]{xgb.DMatrix()}} uses multiple threads too.
|
Note also that converting a matrix to \code{\link{xgb.DMatrix}} uses multiple threads too.
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
## binary classification:
|
## binary classification:
|
||||||
|
|
||||||
data(agaricus.train, package = "xgboost")
|
data(agaricus.train, package='xgboost')
|
||||||
data(agaricus.test, package = "xgboost")
|
data(agaricus.test, package='xgboost')
|
||||||
|
|
||||||
## Keep the number of threads to 2 for examples
|
|
||||||
nthread <- 2
|
|
||||||
data.table::setDTthreads(nthread)
|
|
||||||
|
|
||||||
train <- agaricus.train
|
train <- agaricus.train
|
||||||
test <- agaricus.test
|
test <- agaricus.test
|
||||||
|
|
||||||
bst <- xgb.train(
|
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
data = xgb.DMatrix(train$data, label = train$label),
|
eta = 0.5, nthread = 2, nrounds = 5, objective = "binary:logistic")
|
||||||
max_depth = 2,
|
|
||||||
eta = 0.5,
|
|
||||||
nthread = nthread,
|
|
||||||
nrounds = 5,
|
|
||||||
objective = "binary:logistic"
|
|
||||||
)
|
|
||||||
|
|
||||||
# use all trees by default
|
# use all trees by default
|
||||||
pred <- predict(bst, test$data)
|
pred <- predict(bst, test$data)
|
||||||
# use only the 1st tree
|
# use only the 1st tree
|
||||||
pred1 <- predict(bst, test$data, iterationrange = c(1, 1))
|
pred1 <- predict(bst, test$data, iterationrange = c(1, 2))
|
||||||
|
|
||||||
# Predicting tree leafs:
|
# Predicting tree leafs:
|
||||||
# the result is an nsamples X ntrees matrix
|
# the result is an nsamples X ntrees matrix
|
||||||
@ -224,61 +168,39 @@ par(mar = old_mar)
|
|||||||
|
|
||||||
lb <- as.numeric(iris$Species) - 1
|
lb <- as.numeric(iris$Species) - 1
|
||||||
num_class <- 3
|
num_class <- 3
|
||||||
|
|
||||||
set.seed(11)
|
set.seed(11)
|
||||||
|
bst <- xgboost(data = as.matrix(iris[, -5]), label = lb,
|
||||||
bst <- xgb.train(
|
max_depth = 4, eta = 0.5, nthread = 2, nrounds = 10, subsample = 0.5,
|
||||||
data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
|
objective = "multi:softprob", num_class = num_class)
|
||||||
max_depth = 4,
|
|
||||||
eta = 0.5,
|
|
||||||
nthread = 2,
|
|
||||||
nrounds = 10,
|
|
||||||
subsample = 0.5,
|
|
||||||
objective = "multi:softprob",
|
|
||||||
num_class = num_class
|
|
||||||
)
|
|
||||||
|
|
||||||
# predict for softmax returns num_class probability numbers per case:
|
# predict for softmax returns num_class probability numbers per case:
|
||||||
pred <- predict(bst, as.matrix(iris[, -5]))
|
pred <- predict(bst, as.matrix(iris[, -5]))
|
||||||
str(pred)
|
str(pred)
|
||||||
# reshape it to a num_class-columns matrix
|
# reshape it to a num_class-columns matrix
|
||||||
pred <- matrix(pred, ncol = num_class, byrow = TRUE)
|
pred <- matrix(pred, ncol=num_class, byrow=TRUE)
|
||||||
# convert the probabilities to softmax labels
|
# convert the probabilities to softmax labels
|
||||||
pred_labels <- max.col(pred) - 1
|
pred_labels <- max.col(pred) - 1
|
||||||
# the following should result in the same error as seen in the last iteration
|
# the following should result in the same error as seen in the last iteration
|
||||||
sum(pred_labels != lb) / length(lb)
|
sum(pred_labels != lb)/length(lb)
|
||||||
|
|
||||||
# compare with predictions from softmax:
|
# compare that to the predictions from softmax:
|
||||||
set.seed(11)
|
set.seed(11)
|
||||||
|
bst <- xgboost(data = as.matrix(iris[, -5]), label = lb,
|
||||||
bst <- xgb.train(
|
max_depth = 4, eta = 0.5, nthread = 2, nrounds = 10, subsample = 0.5,
|
||||||
data = xgb.DMatrix(as.matrix(iris[, -5]), label = lb),
|
objective = "multi:softmax", num_class = num_class)
|
||||||
max_depth = 4,
|
|
||||||
eta = 0.5,
|
|
||||||
nthread = 2,
|
|
||||||
nrounds = 10,
|
|
||||||
subsample = 0.5,
|
|
||||||
objective = "multi:softmax",
|
|
||||||
num_class = num_class
|
|
||||||
)
|
|
||||||
|
|
||||||
pred <- predict(bst, as.matrix(iris[, -5]))
|
pred <- predict(bst, as.matrix(iris[, -5]))
|
||||||
str(pred)
|
str(pred)
|
||||||
all.equal(pred, pred_labels)
|
all.equal(pred, pred_labels)
|
||||||
# prediction from using only 5 iterations should result
|
# prediction from using only 5 iterations should result
|
||||||
# in the same error as seen in iteration 5:
|
# in the same error as seen in iteration 5:
|
||||||
pred5 <- predict(bst, as.matrix(iris[, -5]), iterationrange = c(1, 5))
|
pred5 <- predict(bst, as.matrix(iris[, -5]), iterationrange=c(1, 6))
|
||||||
sum(pred5 != lb) / length(lb)
|
sum(pred5 != lb)/length(lb)
|
||||||
|
|
||||||
}
|
}
|
||||||
\references{
|
\references{
|
||||||
\enumerate{
|
Scott M. Lundberg, Su-In Lee, "A Unified Approach to Interpreting Model Predictions", NIPS Proceedings 2017, \url{https://arxiv.org/abs/1705.07874}
|
||||||
\item Scott M. Lundberg, Su-In Lee, "A Unified Approach to Interpreting Model Predictions",
|
|
||||||
NIPS Proceedings 2017, \url{https://arxiv.org/abs/1705.07874}
|
Scott M. Lundberg, Su-In Lee, "Consistent feature attribution for tree ensembles", \url{https://arxiv.org/abs/1706.06060}
|
||||||
\item Scott M. Lundberg, Su-In Lee, "Consistent feature attribution for tree ensembles",
|
|
||||||
\url{https://arxiv.org/abs/1706.06060}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
\seealso{
|
\seealso{
|
||||||
\code{\link[=xgb.train]{xgb.train()}}
|
\code{\link{xgb.train}}.
|
||||||
}
|
}
|
||||||
|
|||||||
27
R-package/man/prepare.ggplot.shap.data.Rd
Normal file
27
R-package/man/prepare.ggplot.shap.data.Rd
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/xgb.ggplot.R
|
||||||
|
\name{prepare.ggplot.shap.data}
|
||||||
|
\alias{prepare.ggplot.shap.data}
|
||||||
|
\title{Combine and melt feature values and SHAP contributions for sample
|
||||||
|
observations.}
|
||||||
|
\usage{
|
||||||
|
prepare.ggplot.shap.data(data_list, normalize = FALSE)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{data_list}{List containing 'data' and 'shap_contrib' returned by
|
||||||
|
\code{xgb.shap.data()}.}
|
||||||
|
|
||||||
|
\item{normalize}{Whether to standardize feature values to have mean 0 and
|
||||||
|
standard deviation 1 (useful for comparing multiple features on the same
|
||||||
|
plot). Default \code{FALSE}.}
|
||||||
|
}
|
||||||
|
\value{
|
||||||
|
A data.table containing the observation ID, the feature name, the
|
||||||
|
feature value (normalized if specified), and the SHAP contribution value.
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Conforms to data format required for ggplot functions.
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
Internal utility function.
|
||||||
|
}
|
||||||
@ -4,35 +4,26 @@
|
|||||||
\alias{print.xgb.Booster}
|
\alias{print.xgb.Booster}
|
||||||
\title{Print xgb.Booster}
|
\title{Print xgb.Booster}
|
||||||
\usage{
|
\usage{
|
||||||
\method{print}{xgb.Booster}(x, ...)
|
\method{print}{xgb.Booster}(x, verbose = FALSE, ...)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{x}{An \code{xgb.Booster} object.}
|
\item{x}{an xgb.Booster object}
|
||||||
|
|
||||||
\item{...}{Not used.}
|
\item{verbose}{whether to print detailed data (e.g., attribute values)}
|
||||||
}
|
|
||||||
\value{
|
\item{...}{not currently used}
|
||||||
The same \code{x} object, returned invisibly
|
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Print information about \code{xgb.Booster}.
|
Print information about xgb.Booster.
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package = "xgboost")
|
data(agaricus.train, package='xgboost')
|
||||||
train <- agaricus.train
|
train <- agaricus.train
|
||||||
|
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
bst <- xgboost(
|
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
data = train$data,
|
attr(bst, 'myattr') <- 'memo'
|
||||||
label = train$label,
|
|
||||||
max_depth = 2,
|
|
||||||
eta = 1,
|
|
||||||
nthread = 2,
|
|
||||||
nrounds = 2,
|
|
||||||
objective = "binary:logistic"
|
|
||||||
)
|
|
||||||
|
|
||||||
attr(bst, "myattr") <- "memo"
|
|
||||||
|
|
||||||
print(bst)
|
print(bst)
|
||||||
|
print(bst, verbose=TRUE)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ including the best iteration (when available).
|
|||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
train <- agaricus.train
|
train <- agaricus.train
|
||||||
cv <- xgb.cv(data = xgb.DMatrix(train$data, label = train$label), nfold = 5, max_depth = 2,
|
cv <- xgb.cv(data = train$data, label = train$label, nfold = 5, max_depth = 2,
|
||||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
print(cv)
|
print(cv)
|
||||||
print(cv, verbose=TRUE)
|
print(cv, verbose=TRUE)
|
||||||
|
|||||||
42
R-package/man/setinfo.Rd
Normal file
42
R-package/man/setinfo.Rd
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/xgb.DMatrix.R
|
||||||
|
\name{setinfo}
|
||||||
|
\alias{setinfo}
|
||||||
|
\alias{setinfo.xgb.DMatrix}
|
||||||
|
\title{Set information of an xgb.DMatrix object}
|
||||||
|
\usage{
|
||||||
|
setinfo(object, ...)
|
||||||
|
|
||||||
|
\method{setinfo}{xgb.DMatrix}(object, name, info, ...)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{object}{Object of class "xgb.DMatrix"}
|
||||||
|
|
||||||
|
\item{...}{other parameters}
|
||||||
|
|
||||||
|
\item{name}{the name of the field to get}
|
||||||
|
|
||||||
|
\item{info}{the specific field of information to set}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Set information of an xgb.DMatrix object
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
The \code{name} field can be one of the following:
|
||||||
|
|
||||||
|
\itemize{
|
||||||
|
\item \code{label}: label XGBoost learn from ;
|
||||||
|
\item \code{weight}: to do a weight rescale ;
|
||||||
|
\item \code{base_margin}: base margin is the base prediction XGBoost will boost from ;
|
||||||
|
\item \code{group}: number of rows in each group (to use with \code{rank:pairwise} objective).
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\examples{
|
||||||
|
data(agaricus.train, package='xgboost')
|
||||||
|
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
|
|
||||||
|
labels <- getinfo(dtrain, 'label')
|
||||||
|
setinfo(dtrain, 'label', 1-labels)
|
||||||
|
labels2 <- getinfo(dtrain, 'label')
|
||||||
|
stopifnot(all.equal(labels2, 1-labels))
|
||||||
|
}
|
||||||
@ -1,23 +1,24 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
% Please edit documentation in R/xgb.DMatrix.R
|
||||||
\name{xgb.slice.DMatrix}
|
\name{slice}
|
||||||
\alias{xgb.slice.DMatrix}
|
\alias{slice}
|
||||||
|
\alias{slice.xgb.DMatrix}
|
||||||
\alias{[.xgb.DMatrix}
|
\alias{[.xgb.DMatrix}
|
||||||
\title{Get a new DMatrix containing the specified rows of
|
\title{Get a new DMatrix containing the specified rows of
|
||||||
original xgb.DMatrix object}
|
original xgb.DMatrix object}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.slice.DMatrix(object, idxset, allow_groups = FALSE)
|
slice(object, ...)
|
||||||
|
|
||||||
|
\method{slice}{xgb.DMatrix}(object, idxset, ...)
|
||||||
|
|
||||||
\method{[}{xgb.DMatrix}(object, idxset, colset = NULL)
|
\method{[}{xgb.DMatrix}(object, idxset, colset = NULL)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{object}{Object of class "xgb.DMatrix".}
|
\item{object}{Object of class "xgb.DMatrix"}
|
||||||
|
|
||||||
\item{idxset}{An integer vector of indices of rows needed (base-1 indexing).}
|
\item{...}{other parameters (currently not used)}
|
||||||
|
|
||||||
\item{allow_groups}{Whether to allow slicing an \code{xgb.DMatrix} with \code{group} (or
|
\item{idxset}{a integer vector of indices of rows needed}
|
||||||
equivalently \code{qid}) field. Note that in such case, the result will not have
|
|
||||||
the groups anymore - they need to be set manually through \code{setinfo}.}
|
|
||||||
|
|
||||||
\item{colset}{currently not used (columns subsetting is not available)}
|
\item{colset}{currently not used (columns subsetting is not available)}
|
||||||
}
|
}
|
||||||
@ -29,7 +30,7 @@ original xgb.DMatrix object
|
|||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
|
|
||||||
dsub <- xgb.slice.DMatrix(dtrain, 1:42)
|
dsub <- slice(dtrain, 1:42)
|
||||||
labels1 <- getinfo(dsub, 'label')
|
labels1 <- getinfo(dsub, 'label')
|
||||||
dsub <- dtrain[1:42, ]
|
dsub <- dtrain[1:42, ]
|
||||||
labels2 <- getinfo(dsub, 'label')
|
labels2 <- getinfo(dsub, 'label')
|
||||||
@ -1,22 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.Booster.R
|
|
||||||
\name{variable.names.xgb.Booster}
|
|
||||||
\alias{variable.names.xgb.Booster}
|
|
||||||
\title{Get Features Names from Booster}
|
|
||||||
\usage{
|
|
||||||
\method{variable.names}{xgb.Booster}(object, ...)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{object}{An \code{xgb.Booster} object.}
|
|
||||||
|
|
||||||
\item{...}{Not used.}
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Returns the feature / variable / column names from a fitted
|
|
||||||
booster object, which are set automatically during the call to \link{xgb.train}
|
|
||||||
from the DMatrix names, or which can be set manually through \link{setinfo}.
|
|
||||||
|
|
||||||
If the object doesn't have feature names, will return \code{NULL}.
|
|
||||||
|
|
||||||
It is equivalent to calling \code{getinfo(object, "feature_name")}.
|
|
||||||
}
|
|
||||||
52
R-package/man/xgb.Booster.complete.Rd
Normal file
52
R-package/man/xgb.Booster.complete.Rd
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/xgb.Booster.R
|
||||||
|
\name{xgb.Booster.complete}
|
||||||
|
\alias{xgb.Booster.complete}
|
||||||
|
\title{Restore missing parts of an incomplete xgb.Booster object.}
|
||||||
|
\usage{
|
||||||
|
xgb.Booster.complete(object, saveraw = TRUE)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{object}{object of class \code{xgb.Booster}}
|
||||||
|
|
||||||
|
\item{saveraw}{a flag indicating whether to append \code{raw} Booster memory dump data
|
||||||
|
when it doesn't already exist.}
|
||||||
|
}
|
||||||
|
\value{
|
||||||
|
An object of \code{xgb.Booster} class.
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
It attempts to complete an \code{xgb.Booster} object by restoring either its missing
|
||||||
|
raw model memory dump (when it has no \code{raw} data but its \code{xgb.Booster.handle} is valid)
|
||||||
|
or its missing internal handle (when its \code{xgb.Booster.handle} is not valid
|
||||||
|
but it has a raw Booster memory dump).
|
||||||
|
}
|
||||||
|
\details{
|
||||||
|
While this method is primarily for internal use, it might be useful in some practical situations.
|
||||||
|
|
||||||
|
E.g., when an \code{xgb.Booster} model is saved as an R object and then is loaded as an R object,
|
||||||
|
its handle (pointer) to an internal xgboost model would be invalid. The majority of xgboost methods
|
||||||
|
should still work for such a model object since those methods would be using
|
||||||
|
\code{xgb.Booster.complete} internally. However, one might find it to be more efficient to call the
|
||||||
|
\code{xgb.Booster.complete} function explicitly once after loading a model as an R-object.
|
||||||
|
That would prevent further repeated implicit reconstruction of an internal booster model.
|
||||||
|
}
|
||||||
|
\examples{
|
||||||
|
|
||||||
|
data(agaricus.train, package='xgboost')
|
||||||
|
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2,
|
||||||
|
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
|
saveRDS(bst, "xgb.model.rds")
|
||||||
|
|
||||||
|
# Warning: The resulting RDS file is only compatible with the current XGBoost version.
|
||||||
|
# Refer to the section titled "a-compatibility-note-for-saveRDS-save".
|
||||||
|
bst1 <- readRDS("xgb.model.rds")
|
||||||
|
if (file.exists("xgb.model.rds")) file.remove("xgb.model.rds")
|
||||||
|
# the handle is invalid:
|
||||||
|
print(bst1$handle)
|
||||||
|
|
||||||
|
bst1 <- xgb.Booster.complete(bst1)
|
||||||
|
# now the handle points to a valid internal booster model:
|
||||||
|
print(bst1$handle)
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,248 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/callbacks.R
|
|
||||||
\name{xgb.Callback}
|
|
||||||
\alias{xgb.Callback}
|
|
||||||
\title{XGBoost Callback Constructor}
|
|
||||||
\usage{
|
|
||||||
xgb.Callback(
|
|
||||||
cb_name = "custom_callback",
|
|
||||||
env = new.env(),
|
|
||||||
f_before_training = function(env, model, data, evals, begin_iteration, end_iteration)
|
|
||||||
NULL,
|
|
||||||
f_before_iter = function(env, model, data, evals, iteration) NULL,
|
|
||||||
f_after_iter = function(env, model, data, evals, iteration, iter_feval) NULL,
|
|
||||||
f_after_training = function(env, model, data, evals, iteration, final_feval,
|
|
||||||
prev_cb_res) NULL
|
|
||||||
)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{cb_name}{Name for the callback.
|
|
||||||
|
|
||||||
If the callback produces some non-NULL result (from executing the function passed under
|
|
||||||
\code{f_after_training}), that result will be added as an R attribute to the resulting booster
|
|
||||||
(or as a named element in the result of CV), with the attribute name specified here.
|
|
||||||
|
|
||||||
Names of callbacks must be unique - i.e. there cannot be two callbacks with the same name.}
|
|
||||||
|
|
||||||
\item{env}{An environment object that will be passed to the different functions in the callback.
|
|
||||||
Note that this environment will not be shared with other callbacks.}
|
|
||||||
|
|
||||||
\item{f_before_training}{A function that will be executed before the training has started.
|
|
||||||
|
|
||||||
If passing \code{NULL} for this or for the other function inputs, then no function will be executed.
|
|
||||||
|
|
||||||
If passing a function, it will be called with parameters supplied as non-named arguments
|
|
||||||
matching the function signatures that are shown in the default value for each function argument.}
|
|
||||||
|
|
||||||
\item{f_before_iter}{A function that will be executed before each boosting round.
|
|
||||||
|
|
||||||
This function can signal whether the training should be finalized or not, by outputting
|
|
||||||
a value that evaluates to \code{TRUE} - i.e. if the output from the function provided here at
|
|
||||||
a given round is \code{TRUE}, then training will be stopped before the current iteration happens.
|
|
||||||
|
|
||||||
Return values of \code{NULL} will be interpreted as \code{FALSE}.}
|
|
||||||
|
|
||||||
\item{f_after_iter}{A function that will be executed after each boosting round.
|
|
||||||
|
|
||||||
This function can signal whether the training should be finalized or not, by outputting
|
|
||||||
a value that evaluates to \code{TRUE} - i.e. if the output from the function provided here at
|
|
||||||
a given round is \code{TRUE}, then training will be stopped at that round.
|
|
||||||
|
|
||||||
Return values of \code{NULL} will be interpreted as \code{FALSE}.}
|
|
||||||
|
|
||||||
\item{f_after_training}{A function that will be executed after training is finished.
|
|
||||||
|
|
||||||
This function can optionally output something non-NULL, which will become part of the R
|
|
||||||
attributes of the booster (assuming one passes \code{keep_extra_attributes=TRUE} to \link{xgb.train})
|
|
||||||
under the name supplied for parameter \code{cb_name} imn the case of \link{xgb.train}; or a part
|
|
||||||
of the named elements in the result of \link{xgb.cv}.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.train} or \link{xgb.cv}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Constructor for defining the structure of callback functions that can be executed
|
|
||||||
at different stages of model training (before / after training, before / after each boosting
|
|
||||||
iteration).
|
|
||||||
}
|
|
||||||
\details{
|
|
||||||
Arguments that will be passed to the supplied functions are as follows:\itemize{
|
|
||||||
|
|
||||||
\item env The same environment that is passed under argument \code{env}.
|
|
||||||
|
|
||||||
It may be modified by the functions in order to e.g. keep tracking of what happens
|
|
||||||
across iterations or similar.
|
|
||||||
|
|
||||||
This environment is only used by the functions supplied to the callback, and will
|
|
||||||
not be kept after the model fitting function terminates (see parameter \code{f_after_training}).
|
|
||||||
|
|
||||||
\item model The booster object when using \link{xgb.train}, or the folds when using
|
|
||||||
\link{xgb.cv}.
|
|
||||||
|
|
||||||
For \link{xgb.cv}, folds are a list with a structure as follows:\itemize{
|
|
||||||
\item \code{dtrain}: The training data for the fold (as an \code{xgb.DMatrix} object).
|
|
||||||
\item \code{bst}: Rhe \code{xgb.Booster} object for the fold.
|
|
||||||
\item \code{evals}: A list containing two DMatrices, with names \code{train} and \code{test}
|
|
||||||
(\code{test} is the held-out data for the fold).
|
|
||||||
\item \code{index}: The indices of the hold-out data for that fold (base-1 indexing),
|
|
||||||
from which the \code{test} entry in \code{evals} was obtained.
|
|
||||||
}
|
|
||||||
|
|
||||||
This object should \bold{not} be in-place modified in ways that conflict with the
|
|
||||||
training (e.g. resetting the parameters for a training update in a way that resets
|
|
||||||
the number of rounds to zero in order to overwrite rounds).
|
|
||||||
|
|
||||||
Note that any R attributes that are assigned to the booster during the callback functions,
|
|
||||||
will not be kept thereafter as the booster object variable is not re-assigned during
|
|
||||||
training. It is however possible to set C-level attributes of the booster through
|
|
||||||
\link{xgb.attr} or \link{xgb.attributes}, which should remain available for the rest
|
|
||||||
of the iterations and after the training is done.
|
|
||||||
|
|
||||||
For keeping variables across iterations, it's recommended to use \code{env} instead.
|
|
||||||
\item data The data to which the model is being fit, as an \code{xgb.DMatrix} object.
|
|
||||||
|
|
||||||
Note that, for \link{xgb.cv}, this will be the full data, while data for the specific
|
|
||||||
folds can be found in the \code{model} object.
|
|
||||||
|
|
||||||
\item evals The evaluation data, as passed under argument \code{evals} to
|
|
||||||
\link{xgb.train}.
|
|
||||||
|
|
||||||
For \link{xgb.cv}, this will always be \code{NULL}.
|
|
||||||
|
|
||||||
\item begin_iteration Index of the first boosting iteration that will be executed
|
|
||||||
(base-1 indexing).
|
|
||||||
|
|
||||||
This will typically be '1', but when using training continuation, depending on the
|
|
||||||
parameters for updates, boosting rounds will be continued from where the previous
|
|
||||||
model ended, in which case this will be larger than 1.
|
|
||||||
|
|
||||||
\item end_iteration Index of the last boostign iteration that will be executed
|
|
||||||
(base-1 indexing, inclusive of this end).
|
|
||||||
|
|
||||||
It should match with argument \code{nrounds} passed to \link{xgb.train} or \link{xgb.cv}.
|
|
||||||
|
|
||||||
Note that boosting might be interrupted before reaching this last iteration, for
|
|
||||||
example by using the early stopping callback \link{xgb.cb.early.stop}.
|
|
||||||
|
|
||||||
\item iteration Index of the iteration number that is being executed (first iteration
|
|
||||||
will be the same as parameter \code{begin_iteration}, then next one will add +1, and so on).
|
|
||||||
|
|
||||||
\item iter_feval Evaluation metrics for \code{evals} that were supplied, either
|
|
||||||
determined by the objective, or by parameter \code{feval}.
|
|
||||||
|
|
||||||
For \link{xgb.train}, this will be a named vector with one entry per element in
|
|
||||||
\code{evals}, where the names are determined as 'evals name' + '-' + 'metric name' - for
|
|
||||||
example, if \code{evals} contains an entry named "tr" and the metric is "rmse",
|
|
||||||
this will be a one-element vector with name "tr-rmse".
|
|
||||||
|
|
||||||
For \link{xgb.cv}, this will be a 2d matrix with dimensions \verb{[length(evals), nfolds]},
|
|
||||||
where the row names will follow the same naming logic as the one-dimensional vector
|
|
||||||
that is passed in \link{xgb.train}.
|
|
||||||
|
|
||||||
Note that, internally, the built-in callbacks such as \link{xgb.cb.print.evaluation} summarize
|
|
||||||
this table by calculating the row-wise means and standard deviations.
|
|
||||||
|
|
||||||
\item final_feval The evaluation results after the last boosting round is executed
|
|
||||||
(same format as \code{iter_feval}, and will be the exact same input as passed under
|
|
||||||
\code{iter_feval} to the last round that is executed during model fitting).
|
|
||||||
|
|
||||||
\item prev_cb_res Result from a previous run of a callback sharing the same name
|
|
||||||
(as given by parameter \code{cb_name}) when conducting training continuation, if there
|
|
||||||
was any in the booster R attributes.
|
|
||||||
|
|
||||||
Some times, one might want to append the new results to the previous one, and this will
|
|
||||||
be done automatically by the built-in callbacks such as \link{xgb.cb.evaluation.log},
|
|
||||||
which will append the new rows to the previous table.
|
|
||||||
|
|
||||||
If no such previous callback result is available (which it never will when fitting
|
|
||||||
a model from start instead of updating an existing model), this will be \code{NULL}.
|
|
||||||
|
|
||||||
For \link{xgb.cv}, which doesn't support training continuation, this will always be \code{NULL}.
|
|
||||||
}
|
|
||||||
|
|
||||||
The following names (\code{cb_name} values) are reserved for internal callbacks:\itemize{
|
|
||||||
\item print_evaluation
|
|
||||||
\item evaluation_log
|
|
||||||
\item reset_parameters
|
|
||||||
\item early_stop
|
|
||||||
\item save_model
|
|
||||||
\item cv_predict
|
|
||||||
\item gblinear_history
|
|
||||||
}
|
|
||||||
|
|
||||||
The following names are reserved for other non-callback attributes:\itemize{
|
|
||||||
\item names
|
|
||||||
\item class
|
|
||||||
\item call
|
|
||||||
\item params
|
|
||||||
\item niter
|
|
||||||
\item nfeatures
|
|
||||||
\item folds
|
|
||||||
}
|
|
||||||
|
|
||||||
When using the built-in early stopping callback (\link{xgb.cb.early.stop}), said callback
|
|
||||||
will always be executed before the others, as it sets some booster C-level attributes
|
|
||||||
that other callbacks might also use. Otherwise, the order of execution will match with
|
|
||||||
the order in which the callbacks are passed to the model fitting function.
|
|
||||||
}
|
|
||||||
\examples{
|
|
||||||
# Example constructing a custom callback that calculates
|
|
||||||
# squared error on the training data (no separate test set),
|
|
||||||
# and outputs the per-iteration results.
|
|
||||||
ssq_callback <- xgb.Callback(
|
|
||||||
cb_name = "ssq",
|
|
||||||
f_before_training = function(env, model, data, evals,
|
|
||||||
begin_iteration, end_iteration) {
|
|
||||||
# A vector to keep track of a number at each iteration
|
|
||||||
env$logs <- rep(NA_real_, end_iteration - begin_iteration + 1)
|
|
||||||
},
|
|
||||||
f_after_iter = function(env, model, data, evals, iteration, iter_feval) {
|
|
||||||
# This calculates the sum of squared errors on the training data.
|
|
||||||
# Note that this can be better done by passing an 'evals' entry,
|
|
||||||
# but this demonstrates a way in which callbacks can be structured.
|
|
||||||
pred <- predict(model, data)
|
|
||||||
err <- pred - getinfo(data, "label")
|
|
||||||
sq_err <- sum(err^2)
|
|
||||||
env$logs[iteration] <- sq_err
|
|
||||||
cat(
|
|
||||||
sprintf(
|
|
||||||
"Squared error at iteration \%d: \%.2f\n",
|
|
||||||
iteration, sq_err
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# A return value of 'TRUE' here would signal to finalize the training
|
|
||||||
return(FALSE)
|
|
||||||
},
|
|
||||||
f_after_training = function(env, model, data, evals, iteration,
|
|
||||||
final_feval, prev_cb_res) {
|
|
||||||
return(env$logs)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
data(mtcars)
|
|
||||||
y <- mtcars$mpg
|
|
||||||
x <- as.matrix(mtcars[, -1])
|
|
||||||
dm <- xgb.DMatrix(x, label = y, nthread = 1)
|
|
||||||
model <- xgb.train(
|
|
||||||
data = dm,
|
|
||||||
params = list(objective = "reg:squarederror", nthread = 1),
|
|
||||||
nrounds = 5,
|
|
||||||
callbacks = list(ssq_callback),
|
|
||||||
keep_extra_attributes = TRUE
|
|
||||||
)
|
|
||||||
|
|
||||||
# Result from 'f_after_iter' will be available as an attribute
|
|
||||||
attributes(model)$ssq
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
Built-in callbacks:\itemize{
|
|
||||||
\item \link{xgb.cb.print.evaluation}
|
|
||||||
\item \link{xgb.cb.evaluation.log}
|
|
||||||
\item \link{xgb.cb.reset.parameters}
|
|
||||||
\item \link{xgb.cb.early.stop}
|
|
||||||
\item \link{xgb.cb.save.model}
|
|
||||||
\item \link{xgb.cb.cv.predict}
|
|
||||||
\item \link{xgb.cb.gblinear.history}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,199 +2,44 @@
|
|||||||
% Please edit documentation in R/xgb.DMatrix.R
|
% Please edit documentation in R/xgb.DMatrix.R
|
||||||
\name{xgb.DMatrix}
|
\name{xgb.DMatrix}
|
||||||
\alias{xgb.DMatrix}
|
\alias{xgb.DMatrix}
|
||||||
\alias{xgb.QuantileDMatrix}
|
|
||||||
\title{Construct xgb.DMatrix object}
|
\title{Construct xgb.DMatrix object}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.DMatrix(
|
xgb.DMatrix(
|
||||||
data,
|
data,
|
||||||
label = NULL,
|
info = list(),
|
||||||
weight = NULL,
|
|
||||||
base_margin = NULL,
|
|
||||||
missing = NA,
|
missing = NA,
|
||||||
silent = FALSE,
|
silent = FALSE,
|
||||||
feature_names = colnames(data),
|
|
||||||
feature_types = NULL,
|
|
||||||
nthread = NULL,
|
nthread = NULL,
|
||||||
group = NULL,
|
...
|
||||||
qid = NULL,
|
|
||||||
label_lower_bound = NULL,
|
|
||||||
label_upper_bound = NULL,
|
|
||||||
feature_weights = NULL,
|
|
||||||
data_split_mode = "row"
|
|
||||||
)
|
|
||||||
|
|
||||||
xgb.QuantileDMatrix(
|
|
||||||
data,
|
|
||||||
label = NULL,
|
|
||||||
weight = NULL,
|
|
||||||
base_margin = NULL,
|
|
||||||
missing = NA,
|
|
||||||
feature_names = colnames(data),
|
|
||||||
feature_types = NULL,
|
|
||||||
nthread = NULL,
|
|
||||||
group = NULL,
|
|
||||||
qid = NULL,
|
|
||||||
label_lower_bound = NULL,
|
|
||||||
label_upper_bound = NULL,
|
|
||||||
feature_weights = NULL,
|
|
||||||
ref = NULL,
|
|
||||||
max_bin = NULL
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{data}{Data from which to create a DMatrix, which can then be used for fitting models or
|
\item{data}{a \code{matrix} object (either numeric or integer), a \code{dgCMatrix} object,
|
||||||
for getting predictions out of a fitted model.
|
a \code{dgRMatrix} object (only when making predictions from a fitted model),
|
||||||
|
a \code{dsparseVector} object (only when making predictions from a fitted model, will be
|
||||||
|
interpreted as a row vector), or a character string representing a filename.}
|
||||||
|
|
||||||
Supported input types are as follows:\itemize{
|
\item{info}{a named list of additional information to store in the \code{xgb.DMatrix} object.
|
||||||
\item \code{matrix} objects, with types \code{numeric}, \code{integer}, or \code{logical}.
|
See \code{\link{setinfo}} for the specific allowed kinds of}
|
||||||
\item \code{data.frame} objects, with columns of types \code{numeric}, \code{integer}, \code{logical}, or \code{factor}.
|
|
||||||
|
|
||||||
Note that xgboost uses base-0 encoding for categorical types, hence \code{factor} types (which use base-1
|
\item{missing}{a float value to represents missing values in data (used only when input is a dense matrix).
|
||||||
encoding') will be converted inside the function call. Be aware that the encoding used for \code{factor}
|
It is useful when a 0 or some other extreme value represents missing values in data.}
|
||||||
types is not kept as part of the model, so in subsequent calls to \code{predict}, it is the user's
|
|
||||||
responsibility to ensure that factor columns have the same levels as the ones from which the DMatrix
|
|
||||||
was constructed.
|
|
||||||
|
|
||||||
Other column types are not supported.
|
|
||||||
\item CSR matrices, as class \code{dgRMatrix} from package \code{Matrix}.
|
|
||||||
\item CSC matrices, as class \code{dgCMatrix} from package \code{Matrix}. These are \bold{not} supported for
|
|
||||||
'xgb.QuantileDMatrix'.
|
|
||||||
\item Single-row CSR matrices, as class \code{dsparseVector} from package \code{Matrix}, which is interpreted
|
|
||||||
as a single row (only when making predictions from a fitted model).
|
|
||||||
\item Text files in a supported format, passed as a \code{character} variable containing the URI path to
|
|
||||||
the file, with an optional format specifier.
|
|
||||||
|
|
||||||
These are \bold{not} supported for \code{xgb.QuantileDMatrix}. Supported formats are:\itemize{
|
|
||||||
\item XGBoost's own binary format for DMatrices, as produced by \link{xgb.DMatrix.save}.
|
|
||||||
\item SVMLight (a.k.a. LibSVM) format for CSR matrices. This format can be signaled by suffix
|
|
||||||
\code{?format=libsvm} at the end of the file path. It will be the default format if not
|
|
||||||
otherwise specified.
|
|
||||||
\item CSV files (comma-separated values). This format can be specified by adding suffix
|
|
||||||
\code{?format=csv} at the end ofthe file path. It will \bold{not} be auto-deduced from file extensions.
|
|
||||||
}
|
|
||||||
|
|
||||||
Be aware that the format of the file will not be auto-deduced - for example, if a file is named 'file.csv',
|
|
||||||
it will not look at the extension or file contents to determine that it is a comma-separated value.
|
|
||||||
Instead, the format must be specified following the URI format, so the input to \code{data} should be passed
|
|
||||||
like this: \code{"file.csv?format=csv"} (or \code{"file.csv?format=csv&label_column=0"} if the first column
|
|
||||||
corresponds to the labels).
|
|
||||||
|
|
||||||
For more information about passing text files as input, see the articles
|
|
||||||
\href{https://xgboost.readthedocs.io/en/stable/tutorials/input_format.html}{Text Input Format of DMatrix} and
|
|
||||||
\href{https://xgboost.readthedocs.io/en/stable/python/python_intro.html#python-data-interface}{Data Interface}.
|
|
||||||
}}
|
|
||||||
|
|
||||||
\item{label}{Label of the training data. For classification problems, should be passed encoded as
|
|
||||||
integers with numeration starting at zero.}
|
|
||||||
|
|
||||||
\item{weight}{Weight for each instance.
|
|
||||||
|
|
||||||
Note that, for ranking task, weights are per-group. In ranking task, one weight
|
|
||||||
is assigned to each group (not each data point). This is because we
|
|
||||||
only care about the relative ordering of data points within each group,
|
|
||||||
so it doesn't make sense to assign weights to individual data points.}
|
|
||||||
|
|
||||||
\item{base_margin}{Base margin used for boosting from existing model.
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ In the case of multi-output models, one can also pass multi-dimensional base_margin.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{missing}{A float value to represents missing values in data (not used when creating DMatrix
|
|
||||||
from text files).
|
|
||||||
It is useful to change when a zero, infinite, or some other extreme value represents missing
|
|
||||||
values in data.}
|
|
||||||
|
|
||||||
\item{silent}{whether to suppress printing an informational message after loading from a file.}
|
\item{silent}{whether to suppress printing an informational message after loading from a file.}
|
||||||
|
|
||||||
\item{feature_names}{Set names for features. Overrides column names in data
|
|
||||||
frame and matrix.
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ Note: columns are not referenced by name when calling `predict`, so the column order there
|
|
||||||
must be the same as in the DMatrix construction, regardless of the column names.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{feature_types}{Set types for features.
|
|
||||||
|
|
||||||
If \code{data} is a \code{data.frame} and passing \code{feature_types} is not supplied, feature types will be deduced
|
|
||||||
automatically from the column types.
|
|
||||||
|
|
||||||
Otherwise, one can pass a character vector with the same length as number of columns in \code{data},
|
|
||||||
with the following possible values:\itemize{
|
|
||||||
\item "c", which represents categorical columns.
|
|
||||||
\item "q", which represents numeric columns.
|
|
||||||
\item "int", which represents integer columns.
|
|
||||||
\item "i", which represents logical (boolean) columns.
|
|
||||||
}
|
|
||||||
|
|
||||||
Note that, while categorical types are treated differently from the rest for model fitting
|
|
||||||
purposes, the other types do not influence the generated model, but have effects in other
|
|
||||||
functionalities such as feature importances.
|
|
||||||
|
|
||||||
\bold{Important}: categorical features, if specified manually through \code{feature_types}, must
|
|
||||||
be encoded as integers with numeration starting at zero, and the same encoding needs to be
|
|
||||||
applied when passing data to \code{predict}. Even if passing \code{factor} types, the encoding will
|
|
||||||
not be saved, so make sure that \code{factor} columns passed to \code{predict} have the same \code{levels}.}
|
|
||||||
|
|
||||||
\item{nthread}{Number of threads used for creating DMatrix.}
|
\item{nthread}{Number of threads used for creating DMatrix.}
|
||||||
|
|
||||||
\item{group}{Group size for all ranking group.}
|
\item{...}{the \code{info} data could be passed directly as parameters, without creating an \code{info} list.}
|
||||||
|
|
||||||
\item{qid}{Query ID for data samples, used for ranking.}
|
|
||||||
|
|
||||||
\item{label_lower_bound}{Lower bound for survival training.}
|
|
||||||
|
|
||||||
\item{label_upper_bound}{Upper bound for survival training.}
|
|
||||||
|
|
||||||
\item{feature_weights}{Set feature weights for column sampling.}
|
|
||||||
|
|
||||||
\item{data_split_mode}{When passing a URI (as R \code{character}) as input, this signals
|
|
||||||
whether to split by row or column. Allowed values are \code{"row"} and \code{"col"}.
|
|
||||||
|
|
||||||
In distributed mode, the file is split accordingly; otherwise this is only an indicator on
|
|
||||||
how the file was split beforehand. Default to row.
|
|
||||||
|
|
||||||
This is not used when \code{data} is not a URI.}
|
|
||||||
|
|
||||||
\item{ref}{The training dataset that provides quantile information, needed when creating
|
|
||||||
validation/test dataset with \code{xgb.QuantileDMatrix}. Supplying the training DMatrix
|
|
||||||
as a reference means that the same quantisation applied to the training data is
|
|
||||||
applied to the validation/test data}
|
|
||||||
|
|
||||||
\item{max_bin}{The number of histogram bin, should be consistent with the training parameter
|
|
||||||
\code{max_bin}.
|
|
||||||
|
|
||||||
This is only supported when constructing a QuantileDMatrix.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An 'xgb.DMatrix' object. If calling 'xgb.QuantileDMatrix', it will have additional
|
|
||||||
subclass 'xgb.QuantileDMatrix'.
|
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Construct an 'xgb.DMatrix' object from a given data source, which can then be passed to functions
|
Construct xgb.DMatrix object from either a dense matrix, a sparse matrix, or a local file.
|
||||||
such as \link{xgb.train} or \link{predict.xgb.Booster}.
|
Supported input file formats are either a LIBSVM text file or a binary file that was created previously by
|
||||||
}
|
\code{\link{xgb.DMatrix.save}}).
|
||||||
\details{
|
|
||||||
Function 'xgb.QuantileDMatrix' will construct a DMatrix with quantization for the histogram
|
|
||||||
method already applied to it, which can be used to reduce memory usage (compared to using a
|
|
||||||
a regular DMatrix first and then creating a quantization out of it) when using the histogram
|
|
||||||
method (\code{tree_method = "hist"}, which is the default algorithm), but is not usable for the
|
|
||||||
sorted-indices method (\code{tree_method = "exact"}), nor for the approximate method
|
|
||||||
(\code{tree_method = "approx"}).
|
|
||||||
|
|
||||||
Note that DMatrix objects are not serializable through R functions such as \code{saveRDS} or \code{save}.
|
|
||||||
If a DMatrix gets serialized and then de-serialized (for example, when saving data in an R session or caching
|
|
||||||
chunks in an Rmd file), the resulting object will not be usable anymore and will need to be reconstructed
|
|
||||||
from the original source of data.
|
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
## Keep the number of threads to 1 for examples
|
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
nthread <- 1
|
xgb.DMatrix.save(dtrain, 'xgb.DMatrix.data')
|
||||||
data.table::setDTthreads(nthread)
|
dtrain <- xgb.DMatrix('xgb.DMatrix.data')
|
||||||
dtrain <- with(
|
if (file.exists('xgb.DMatrix.data')) file.remove('xgb.DMatrix.data')
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
|
||||||
\name{xgb.DMatrix.hasinfo}
|
|
||||||
\alias{xgb.DMatrix.hasinfo}
|
|
||||||
\title{Check whether DMatrix object has a field}
|
|
||||||
\usage{
|
|
||||||
xgb.DMatrix.hasinfo(object, info)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{object}{The DMatrix object to check for the given \code{info} field.}
|
|
||||||
|
|
||||||
\item{info}{The field to check for presence or absence in \code{object}.}
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Checks whether an xgb.DMatrix object has a given field assigned to
|
|
||||||
it, such as weights, labels, etc.
|
|
||||||
}
|
|
||||||
\examples{
|
|
||||||
library(xgboost)
|
|
||||||
x <- matrix(1:10, nrow = 5)
|
|
||||||
dm <- xgb.DMatrix(x, nthread = 1)
|
|
||||||
|
|
||||||
# 'dm' so far doesn't have any fields set
|
|
||||||
xgb.DMatrix.hasinfo(dm, "label")
|
|
||||||
|
|
||||||
# Fields can be added after construction
|
|
||||||
setinfo(dm, "label", 1:5)
|
|
||||||
xgb.DMatrix.hasinfo(dm, "label")
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.DMatrix}, \link{getinfo.xgb.DMatrix}, \link{setinfo.xgb.DMatrix}
|
|
||||||
}
|
|
||||||
@ -15,10 +15,9 @@ xgb.DMatrix.save(dmatrix, fname)
|
|||||||
Save xgb.DMatrix object to binary file
|
Save xgb.DMatrix object to binary file
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
\dontshow{RhpcBLASctl::omp_set_num_threads(1)}
|
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||||
fname <- file.path(tempdir(), "xgb.DMatrix.data")
|
xgb.DMatrix.save(dtrain, 'xgb.DMatrix.data')
|
||||||
xgb.DMatrix.save(dtrain, fname)
|
dtrain <- xgb.DMatrix('xgb.DMatrix.data')
|
||||||
dtrain <- xgb.DMatrix(fname)
|
if (file.exists('xgb.DMatrix.data')) file.remove('xgb.DMatrix.data')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,112 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
|
||||||
\name{xgb.DataBatch}
|
|
||||||
\alias{xgb.DataBatch}
|
|
||||||
\title{Structure for Data Batches}
|
|
||||||
\usage{
|
|
||||||
xgb.DataBatch(
|
|
||||||
data,
|
|
||||||
label = NULL,
|
|
||||||
weight = NULL,
|
|
||||||
base_margin = NULL,
|
|
||||||
feature_names = colnames(data),
|
|
||||||
feature_types = NULL,
|
|
||||||
group = NULL,
|
|
||||||
qid = NULL,
|
|
||||||
label_lower_bound = NULL,
|
|
||||||
label_upper_bound = NULL,
|
|
||||||
feature_weights = NULL
|
|
||||||
)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{data}{Batch of data belonging to this batch.
|
|
||||||
|
|
||||||
Note that not all of the input types supported by \link{xgb.DMatrix} are possible
|
|
||||||
to pass here. Supported types are:\itemize{
|
|
||||||
\item \code{matrix}, with types \code{numeric}, \code{integer}, and \code{logical}. Note that for types
|
|
||||||
\code{integer} and \code{logical}, missing values might not be automatically recognized as
|
|
||||||
as such - see the documentation for parameter \code{missing} in \link{xgb.ExternalDMatrix}
|
|
||||||
for details on this.
|
|
||||||
\item \code{data.frame}, with the same types as supported by 'xgb.DMatrix' and same
|
|
||||||
conversions applied to it. See the documentation for parameter \code{data} in
|
|
||||||
\link{xgb.DMatrix} for details on it.
|
|
||||||
\item CSR matrices, as class \code{dgRMatrix} from package \code{Matrix}.
|
|
||||||
}}
|
|
||||||
|
|
||||||
\item{label}{Label of the training data. For classification problems, should be passed encoded as
|
|
||||||
integers with numeration starting at zero.}
|
|
||||||
|
|
||||||
\item{weight}{Weight for each instance.
|
|
||||||
|
|
||||||
Note that, for ranking task, weights are per-group. In ranking task, one weight
|
|
||||||
is assigned to each group (not each data point). This is because we
|
|
||||||
only care about the relative ordering of data points within each group,
|
|
||||||
so it doesn't make sense to assign weights to individual data points.}
|
|
||||||
|
|
||||||
\item{base_margin}{Base margin used for boosting from existing model.
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ In the case of multi-output models, one can also pass multi-dimensional base_margin.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{feature_names}{Set names for features. Overrides column names in data
|
|
||||||
frame and matrix.
|
|
||||||
|
|
||||||
\if{html}{\out{<div class="sourceCode">}}\preformatted{ Note: columns are not referenced by name when calling `predict`, so the column order there
|
|
||||||
must be the same as in the DMatrix construction, regardless of the column names.
|
|
||||||
}\if{html}{\out{</div>}}}
|
|
||||||
|
|
||||||
\item{feature_types}{Set types for features.
|
|
||||||
|
|
||||||
If \code{data} is a \code{data.frame} and passing \code{feature_types} is not supplied, feature types will be deduced
|
|
||||||
automatically from the column types.
|
|
||||||
|
|
||||||
Otherwise, one can pass a character vector with the same length as number of columns in \code{data},
|
|
||||||
with the following possible values:\itemize{
|
|
||||||
\item "c", which represents categorical columns.
|
|
||||||
\item "q", which represents numeric columns.
|
|
||||||
\item "int", which represents integer columns.
|
|
||||||
\item "i", which represents logical (boolean) columns.
|
|
||||||
}
|
|
||||||
|
|
||||||
Note that, while categorical types are treated differently from the rest for model fitting
|
|
||||||
purposes, the other types do not influence the generated model, but have effects in other
|
|
||||||
functionalities such as feature importances.
|
|
||||||
|
|
||||||
\bold{Important}: categorical features, if specified manually through \code{feature_types}, must
|
|
||||||
be encoded as integers with numeration starting at zero, and the same encoding needs to be
|
|
||||||
applied when passing data to \code{predict}. Even if passing \code{factor} types, the encoding will
|
|
||||||
not be saved, so make sure that \code{factor} columns passed to \code{predict} have the same \code{levels}.}
|
|
||||||
|
|
||||||
\item{group}{Group size for all ranking group.}
|
|
||||||
|
|
||||||
\item{qid}{Query ID for data samples, used for ranking.}
|
|
||||||
|
|
||||||
\item{label_lower_bound}{Lower bound for survival training.}
|
|
||||||
|
|
||||||
\item{label_upper_bound}{Upper bound for survival training.}
|
|
||||||
|
|
||||||
\item{feature_weights}{Set feature weights for column sampling.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An object of class \code{xgb.DataBatch}, which is just a list containing the
|
|
||||||
data and parameters passed here. It does \bold{not} inherit from \code{xgb.DMatrix}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Helper function to supply data in batches of a data iterator when
|
|
||||||
constructing a DMatrix from external memory through \link{xgb.ExternalDMatrix}
|
|
||||||
or through \link{xgb.QuantileDMatrix.from_iterator}.
|
|
||||||
|
|
||||||
This function is \bold{only} meant to be called inside of a callback function (which
|
|
||||||
is passed as argument to function \link{xgb.DataIter} to construct a data iterator)
|
|
||||||
when constructing a DMatrix through external memory - otherwise, one should call
|
|
||||||
\link{xgb.DMatrix} or \link{xgb.QuantileDMatrix}.
|
|
||||||
|
|
||||||
The object that results from calling this function directly is \bold{not} like
|
|
||||||
an \code{xgb.DMatrix} - i.e. cannot be used to train a model, nor to get predictions - only
|
|
||||||
possible usage is to supply data to an iterator, from which a DMatrix is then constructed.
|
|
||||||
|
|
||||||
For more information and for example usage, see the documentation for \link{xgb.ExternalDMatrix}.
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.DataIter}, \link{xgb.ExternalDMatrix}.
|
|
||||||
}
|
|
||||||
@ -1,51 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
|
||||||
\name{xgb.DataIter}
|
|
||||||
\alias{xgb.DataIter}
|
|
||||||
\title{XGBoost Data Iterator}
|
|
||||||
\usage{
|
|
||||||
xgb.DataIter(env = new.env(), f_next, f_reset)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{env}{An R environment to pass to the callback functions supplied here, which can be
|
|
||||||
used to keep track of variables to determine how to handle the batches.
|
|
||||||
|
|
||||||
For example, one might want to keep track of an iteration number in this environment in order
|
|
||||||
to know which part of the data to pass next.}
|
|
||||||
|
|
||||||
\item{f_next}{\verb{function(env)} which is responsible for:\itemize{
|
|
||||||
\item Accessing or retrieving the next batch of data in the iterator.
|
|
||||||
\item Supplying this data by calling function \link{xgb.DataBatch} on it and returning the result.
|
|
||||||
\item Keeping track of where in the iterator batch it is or will go next, which can for example
|
|
||||||
be done by modifiying variables in the \code{env} variable that is passed here.
|
|
||||||
\item Signaling whether there are more batches to be consumed or not, by returning \code{NULL}
|
|
||||||
when the stream of data ends (all batches in the iterator have been consumed), or the result from
|
|
||||||
calling \link{xgb.DataBatch} when there are more batches in the line to be consumed.
|
|
||||||
}}
|
|
||||||
|
|
||||||
\item{f_reset}{\verb{function(env)} which is responsible for reseting the data iterator
|
|
||||||
(i.e. taking it back to the first batch, called before and after the sequence of batches
|
|
||||||
has been consumed).
|
|
||||||
|
|
||||||
Note that, after resetting the iterator, the batches will be accessed again, so the same data
|
|
||||||
(and in the same order) must be passed in subsequent iterations.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An \code{xgb.DataIter} object, containing the same inputs supplied here, which can then
|
|
||||||
be passed to \link{xgb.ExternalDMatrix}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Interface to create a custom data iterator in order to construct a DMatrix
|
|
||||||
from external memory.
|
|
||||||
|
|
||||||
This function is responsible for generating an R object structure containing callback
|
|
||||||
functions and an environment shared with them.
|
|
||||||
|
|
||||||
The output structure from this function is then meant to be passed to \link{xgb.ExternalDMatrix},
|
|
||||||
which will consume the data and create a DMatrix from it by executing the callback functions.
|
|
||||||
|
|
||||||
For more information, and for a usage example, see the documentation for \link{xgb.ExternalDMatrix}.
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.ExternalDMatrix}, \link{xgb.DataBatch}.
|
|
||||||
}
|
|
||||||
@ -1,122 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
|
||||||
\name{xgb.ExternalDMatrix}
|
|
||||||
\alias{xgb.ExternalDMatrix}
|
|
||||||
\title{DMatrix from External Data}
|
|
||||||
\usage{
|
|
||||||
xgb.ExternalDMatrix(
|
|
||||||
data_iterator,
|
|
||||||
cache_prefix = tempdir(),
|
|
||||||
missing = NA,
|
|
||||||
nthread = NULL
|
|
||||||
)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{data_iterator}{A data iterator structure as returned by \link{xgb.DataIter},
|
|
||||||
which includes an environment shared between function calls, and functions to access
|
|
||||||
the data in batches on-demand.}
|
|
||||||
|
|
||||||
\item{cache_prefix}{The path of cache file, caller must initialize all the directories in this path.}
|
|
||||||
|
|
||||||
\item{missing}{A float value to represents missing values in data.
|
|
||||||
|
|
||||||
Note that, while functions like \link{xgb.DMatrix} can take a generic \code{NA} and interpret it
|
|
||||||
correctly for different types like \code{numeric} and \code{integer}, if an \code{NA} value is passed here,
|
|
||||||
it will not be adapted for different input types.
|
|
||||||
|
|
||||||
For example, in R \code{integer} types, missing values are represented by integer number \code{-2147483648}
|
|
||||||
(since machine 'integer' types do not have an inherent 'NA' value) - hence, if one passes \code{NA},
|
|
||||||
which is interpreted as a floating-point NaN by 'xgb.ExternalDMatrix' and by
|
|
||||||
'xgb.QuantileDMatrix.from_iterator', these integer missing values will not be treated as missing.
|
|
||||||
This should not pose any problem for \code{numeric} types, since they do have an inheret NaN value.}
|
|
||||||
|
|
||||||
\item{nthread}{Number of threads used for creating DMatrix.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An 'xgb.DMatrix' object, with subclass 'xgb.ExternalDMatrix', in which the data is not
|
|
||||||
held internally but accessed through the iterator when needed.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Create a special type of xgboost 'DMatrix' object from external data
|
|
||||||
supplied by an \link{xgb.DataIter} object, potentially passed in batches from a
|
|
||||||
bigger set that might not fit entirely in memory.
|
|
||||||
|
|
||||||
The data supplied by the iterator is accessed on-demand as needed, multiple times,
|
|
||||||
without being concatenated, but note that fields like 'label' \bold{will} be
|
|
||||||
concatenated from multiple calls to the data iterator.
|
|
||||||
|
|
||||||
For more information, see the guide 'Using XGBoost External Memory Version':
|
|
||||||
\url{https://xgboost.readthedocs.io/en/stable/tutorials/external_memory.html}
|
|
||||||
}
|
|
||||||
\examples{
|
|
||||||
library(xgboost)
|
|
||||||
data(mtcars)
|
|
||||||
|
|
||||||
# this custom environment will be passed to the iterator
|
|
||||||
# functions at each call. It's up to the user to keep
|
|
||||||
# track of the iteration number in this environment.
|
|
||||||
iterator_env <- as.environment(
|
|
||||||
list(
|
|
||||||
iter = 0,
|
|
||||||
x = mtcars[, -1],
|
|
||||||
y = mtcars[, 1]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Data is passed in two batches.
|
|
||||||
# In this example, batches are obtained by subsetting the 'x' variable.
|
|
||||||
# This is not advantageous to do, since the data is already loaded in memory
|
|
||||||
# and can be passed in full in one go, but there can be situations in which
|
|
||||||
# only a subset of the data will fit in the computer's memory, and it can
|
|
||||||
# be loaded in batches that are accessed one-at-a-time only.
|
|
||||||
iterator_next <- function(iterator_env) {
|
|
||||||
curr_iter <- iterator_env[["iter"]]
|
|
||||||
if (curr_iter >= 2) {
|
|
||||||
# there are only two batches, so this signals end of the stream
|
|
||||||
return(NULL)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (curr_iter == 0) {
|
|
||||||
x_batch <- iterator_env[["x"]][1:16, ]
|
|
||||||
y_batch <- iterator_env[["y"]][1:16]
|
|
||||||
} else {
|
|
||||||
x_batch <- iterator_env[["x"]][17:32, ]
|
|
||||||
y_batch <- iterator_env[["y"]][17:32]
|
|
||||||
}
|
|
||||||
on.exit({
|
|
||||||
iterator_env[["iter"]] <- curr_iter + 1
|
|
||||||
})
|
|
||||||
|
|
||||||
# Function 'xgb.DataBatch' must be called manually
|
|
||||||
# at each batch with all the appropriate attributes,
|
|
||||||
# such as feature names and feature types.
|
|
||||||
return(xgb.DataBatch(data = x_batch, label = y_batch))
|
|
||||||
}
|
|
||||||
|
|
||||||
# This moves the iterator back to its beginning
|
|
||||||
iterator_reset <- function(iterator_env) {
|
|
||||||
iterator_env[["iter"]] <- 0
|
|
||||||
}
|
|
||||||
|
|
||||||
data_iterator <- xgb.DataIter(
|
|
||||||
env = iterator_env,
|
|
||||||
f_next = iterator_next,
|
|
||||||
f_reset = iterator_reset
|
|
||||||
)
|
|
||||||
cache_prefix <- tempdir()
|
|
||||||
|
|
||||||
# DMatrix will be constructed from the iterator's batches
|
|
||||||
dm <- xgb.ExternalDMatrix(data_iterator, cache_prefix, nthread = 1)
|
|
||||||
|
|
||||||
# After construction, can be used as a regular DMatrix
|
|
||||||
params <- list(nthread = 1, objective = "reg:squarederror")
|
|
||||||
model <- xgb.train(data = dm, nrounds = 2, params = params)
|
|
||||||
|
|
||||||
# Predictions can also be called on it, and should be the same
|
|
||||||
# as if the data were passed differently.
|
|
||||||
pred_dm <- predict(model, dm)
|
|
||||||
pred_mat <- predict(model, as.matrix(mtcars[, -1]))
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.DataIter}, \link{xgb.DataBatch}, \link{xgb.QuantileDMatrix.from_iterator}
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/xgb.DMatrix.R
|
|
||||||
\name{xgb.QuantileDMatrix.from_iterator}
|
|
||||||
\alias{xgb.QuantileDMatrix.from_iterator}
|
|
||||||
\title{QuantileDMatrix from External Data}
|
|
||||||
\usage{
|
|
||||||
xgb.QuantileDMatrix.from_iterator(
|
|
||||||
data_iterator,
|
|
||||||
missing = NA,
|
|
||||||
nthread = NULL,
|
|
||||||
ref = NULL,
|
|
||||||
max_bin = NULL
|
|
||||||
)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{data_iterator}{A data iterator structure as returned by \link{xgb.DataIter},
|
|
||||||
which includes an environment shared between function calls, and functions to access
|
|
||||||
the data in batches on-demand.}
|
|
||||||
|
|
||||||
\item{missing}{A float value to represents missing values in data.
|
|
||||||
|
|
||||||
Note that, while functions like \link{xgb.DMatrix} can take a generic \code{NA} and interpret it
|
|
||||||
correctly for different types like \code{numeric} and \code{integer}, if an \code{NA} value is passed here,
|
|
||||||
it will not be adapted for different input types.
|
|
||||||
|
|
||||||
For example, in R \code{integer} types, missing values are represented by integer number \code{-2147483648}
|
|
||||||
(since machine 'integer' types do not have an inherent 'NA' value) - hence, if one passes \code{NA},
|
|
||||||
which is interpreted as a floating-point NaN by 'xgb.ExternalDMatrix' and by
|
|
||||||
'xgb.QuantileDMatrix.from_iterator', these integer missing values will not be treated as missing.
|
|
||||||
This should not pose any problem for \code{numeric} types, since they do have an inheret NaN value.}
|
|
||||||
|
|
||||||
\item{nthread}{Number of threads used for creating DMatrix.}
|
|
||||||
|
|
||||||
\item{ref}{The training dataset that provides quantile information, needed when creating
|
|
||||||
validation/test dataset with \code{xgb.QuantileDMatrix}. Supplying the training DMatrix
|
|
||||||
as a reference means that the same quantisation applied to the training data is
|
|
||||||
applied to the validation/test data}
|
|
||||||
|
|
||||||
\item{max_bin}{The number of histogram bin, should be consistent with the training parameter
|
|
||||||
\code{max_bin}.
|
|
||||||
|
|
||||||
This is only supported when constructing a QuantileDMatrix.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An 'xgb.DMatrix' object, with subclass 'xgb.QuantileDMatrix'.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Create an \code{xgb.QuantileDMatrix} object (exact same class as would be returned by
|
|
||||||
calling function \link{xgb.QuantileDMatrix}, with the same advantages and limitations) from
|
|
||||||
external data supplied by an \link{xgb.DataIter} object, potentially passed in batches from
|
|
||||||
a bigger set that might not fit entirely in memory, same way as \link{xgb.ExternalDMatrix}.
|
|
||||||
|
|
||||||
Note that, while external data will only be loaded through the iterator (thus the full data
|
|
||||||
might not be held entirely in-memory), the quantized representation of the data will get
|
|
||||||
created in-memory, being concatenated from multiple calls to the data iterator. The quantized
|
|
||||||
version is typically lighter than the original data, so there might be cases in which this
|
|
||||||
representation could potentially fit in memory even if the full data doesn't.
|
|
||||||
|
|
||||||
For more information, see the guide 'Using XGBoost External Memory Version':
|
|
||||||
\url{https://xgboost.readthedocs.io/en/stable/tutorials/external_memory.html}
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.DataIter}, \link{xgb.DataBatch}, \link{xgb.ExternalDMatrix},
|
|
||||||
\link{xgb.QuantileDMatrix}
|
|
||||||
}
|
|
||||||
@ -5,7 +5,7 @@
|
|||||||
\alias{xgb.attr<-}
|
\alias{xgb.attr<-}
|
||||||
\alias{xgb.attributes}
|
\alias{xgb.attributes}
|
||||||
\alias{xgb.attributes<-}
|
\alias{xgb.attributes<-}
|
||||||
\title{Accessors for serializable attributes of a model}
|
\title{Accessors for serializable attributes of a model.}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.attr(object, name)
|
xgb.attr(object, name)
|
||||||
|
|
||||||
@ -16,71 +16,64 @@ xgb.attributes(object)
|
|||||||
xgb.attributes(object) <- value
|
xgb.attributes(object) <- value
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{object}{Object of class \code{xgb.Booster}. \bold{Will be modified in-place} when assigning to it.}
|
\item{object}{Object of class \code{xgb.Booster} or \code{xgb.Booster.handle}.}
|
||||||
|
|
||||||
\item{name}{A non-empty character string specifying which attribute is to be accessed.}
|
\item{name}{a non-empty character string specifying which attribute is to be accessed.}
|
||||||
|
|
||||||
\item{value}{For \verb{xgb.attr<-}, a value of an attribute; for \verb{xgb.attributes<-},
|
\item{value}{a value of an attribute for \code{xgb.attr<-}; for \code{xgb.attributes<-}
|
||||||
it is a list (or an object coercible to a list) with the names of attributes to set
|
it's a list (or an object coercible to a list) with the names of attributes to set
|
||||||
and the elements corresponding to attribute values.
|
and the elements corresponding to attribute values.
|
||||||
Non-character values are converted to character.
|
Non-character values are converted to character.
|
||||||
When an attribute value is not a scalar, only the first index is used.
|
When attribute value is not a scalar, only the first index is used.
|
||||||
Use \code{NULL} to remove an attribute.}
|
Use \code{NULL} to remove an attribute.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
\itemize{
|
\code{xgb.attr} returns either a string value of an attribute
|
||||||
\item \code{xgb.attr()} returns either a string value of an attribute
|
|
||||||
or \code{NULL} if an attribute wasn't stored in a model.
|
or \code{NULL} if an attribute wasn't stored in a model.
|
||||||
\item \code{xgb.attributes()} returns a list of all attributes stored in a model
|
|
||||||
|
\code{xgb.attributes} returns a list of all attribute stored in a model
|
||||||
or \code{NULL} if a model has no stored attributes.
|
or \code{NULL} if a model has no stored attributes.
|
||||||
}
|
}
|
||||||
}
|
|
||||||
\description{
|
\description{
|
||||||
These methods allow to manipulate the key-value attribute strings of an xgboost model.
|
These methods allow to manipulate the key-value attribute strings of an xgboost model.
|
||||||
}
|
}
|
||||||
\details{
|
\details{
|
||||||
The primary purpose of xgboost model attributes is to store some meta data about the model.
|
The primary purpose of xgboost model attributes is to store some meta-data about the model.
|
||||||
Note that they are a separate concept from the object attributes in R.
|
Note that they are a separate concept from the object attributes in R.
|
||||||
Specifically, they refer to key-value strings that can be attached to an xgboost model,
|
Specifically, they refer to key-value strings that can be attached to an xgboost model,
|
||||||
stored together with the model's binary representation, and accessed later
|
stored together with the model's binary representation, and accessed later
|
||||||
(from R or any other interface).
|
(from R or any other interface).
|
||||||
In contrast, any R attribute assigned to an R object of \code{xgb.Booster} class
|
In contrast, any R-attribute assigned to an R-object of \code{xgb.Booster} class
|
||||||
would not be saved by \code{\link[=xgb.save]{xgb.save()}} because an xgboost model is an external memory object
|
would not be saved by \code{xgb.save} because an xgboost model is an external memory object
|
||||||
and its serialization is handled externally.
|
and its serialization is handled externally.
|
||||||
Also, setting an attribute that has the same name as one of xgboost's parameters wouldn't
|
Also, setting an attribute that has the same name as one of xgboost's parameters wouldn't
|
||||||
change the value of that parameter for a model.
|
change the value of that parameter for a model.
|
||||||
Use \code{\link[=xgb.parameters<-]{xgb.parameters<-()}} to set or change model parameters.
|
Use \code{\link{xgb.parameters<-}} to set or change model parameters.
|
||||||
|
|
||||||
The \verb{xgb.attributes<-} setter either updates the existing or adds one or several attributes,
|
The attribute setters would usually work more efficiently for \code{xgb.Booster.handle}
|
||||||
|
than for \code{xgb.Booster}, since only just a handle (pointer) would need to be copied.
|
||||||
|
That would only matter if attributes need to be set many times.
|
||||||
|
Note, however, that when feeding a handle of an \code{xgb.Booster} object to the attribute setters,
|
||||||
|
the raw model cache of an \code{xgb.Booster} object would not be automatically updated,
|
||||||
|
and it would be user's responsibility to call \code{xgb.serialize} to update it.
|
||||||
|
|
||||||
|
The \code{xgb.attributes<-} setter either updates the existing or adds one or several attributes,
|
||||||
but it doesn't delete the other existing attributes.
|
but it doesn't delete the other existing attributes.
|
||||||
|
|
||||||
Important: since this modifies the booster's C object, semantics for assignment here
|
|
||||||
will differ from R's, as any object reference to the same booster will be modified
|
|
||||||
too, while assignment of R attributes through \verb{attributes(model)$<attr> <- <value>}
|
|
||||||
will follow the usual copy-on-write R semantics (see \link{xgb.copy.Booster} for an
|
|
||||||
example of these behaviors).
|
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
data(agaricus.train, package = "xgboost")
|
data(agaricus.train, package='xgboost')
|
||||||
train <- agaricus.train
|
train <- agaricus.train
|
||||||
|
|
||||||
bst <- xgboost(
|
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||||
data = train$data,
|
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic")
|
||||||
label = train$label,
|
|
||||||
max_depth = 2,
|
|
||||||
eta = 1,
|
|
||||||
nthread = 2,
|
|
||||||
nrounds = 2,
|
|
||||||
objective = "binary:logistic"
|
|
||||||
)
|
|
||||||
|
|
||||||
xgb.attr(bst, "my_attribute") <- "my attribute value"
|
xgb.attr(bst, "my_attribute") <- "my attribute value"
|
||||||
print(xgb.attr(bst, "my_attribute"))
|
print(xgb.attr(bst, "my_attribute"))
|
||||||
xgb.attributes(bst) <- list(a = 123, b = "abc")
|
xgb.attributes(bst) <- list(a = 123, b = "abc")
|
||||||
|
|
||||||
fname <- file.path(tempdir(), "xgb.ubj")
|
xgb.save(bst, 'xgb.model')
|
||||||
xgb.save(bst, fname)
|
bst1 <- xgb.load('xgb.model')
|
||||||
bst1 <- xgb.load(fname)
|
if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||||
print(xgb.attr(bst1, "my_attribute"))
|
print(xgb.attr(bst1, "my_attribute"))
|
||||||
print(xgb.attributes(bst1))
|
print(xgb.attributes(bst1))
|
||||||
|
|
||||||
|
|||||||
@ -1,55 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/callbacks.R
|
|
||||||
\name{xgb.cb.early.stop}
|
|
||||||
\alias{xgb.cb.early.stop}
|
|
||||||
\title{Callback to activate early stopping}
|
|
||||||
\usage{
|
|
||||||
xgb.cb.early.stop(
|
|
||||||
stopping_rounds,
|
|
||||||
maximize = FALSE,
|
|
||||||
metric_name = NULL,
|
|
||||||
verbose = TRUE,
|
|
||||||
keep_all_iter = TRUE
|
|
||||||
)
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{stopping_rounds}{The number of rounds with no improvement in
|
|
||||||
the evaluation metric in order to stop the training.}
|
|
||||||
|
|
||||||
\item{maximize}{Whether to maximize the evaluation metric.}
|
|
||||||
|
|
||||||
\item{metric_name}{The name of an evaluation column to use as a criteria for early
|
|
||||||
stopping. If not set, the last column would be used.
|
|
||||||
Let's say the test data in \code{evals} was labelled as \code{dtest},
|
|
||||||
and one wants to use the AUC in test data for early stopping regardless of where
|
|
||||||
it is in the \code{evals}, then one of the following would need to be set:
|
|
||||||
\code{metric_name='dtest-auc'} or \code{metric_name='dtest_auc'}.
|
|
||||||
All dash '-' characters in metric names are considered equivalent to '_'.}
|
|
||||||
|
|
||||||
\item{verbose}{Whether to print the early stopping information.}
|
|
||||||
|
|
||||||
\item{keep_all_iter}{Whether to keep all of the boosting rounds that were produced
|
|
||||||
in the resulting object. If passing \code{FALSE}, will only keep the boosting rounds
|
|
||||||
up to the detected best iteration, discarding the ones that come after.}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.train} or \link{xgb.cv}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
This callback function determines the condition for early stopping.
|
|
||||||
|
|
||||||
The following attributes are assigned to the booster's object:
|
|
||||||
\itemize{
|
|
||||||
\item \code{best_score} the evaluation score at the best iteration
|
|
||||||
\item \code{best_iteration} at which boosting iteration the best score has occurred
|
|
||||||
(0-based index for interoperability of binary models)
|
|
||||||
}
|
|
||||||
|
|
||||||
The same values are also stored as R attributes as a result of the callback, plus an additional
|
|
||||||
attribute \code{stopped_by_max_rounds} which indicates whether an early stopping by the \code{stopping_rounds}
|
|
||||||
condition occurred. Note that the \code{best_iteration} that is stored under R attributes will follow
|
|
||||||
base-1 indexing, so it will be larger by '1' than the C-level 'best_iteration' that is accessed
|
|
||||||
through \link{xgb.attr} or \link{xgb.attributes}.
|
|
||||||
|
|
||||||
At least one dataset is required in \code{evals} for early stopping to work.
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/callbacks.R
|
|
||||||
\name{xgb.cb.evaluation.log}
|
|
||||||
\alias{xgb.cb.evaluation.log}
|
|
||||||
\title{Callback for logging the evaluation history}
|
|
||||||
\usage{
|
|
||||||
xgb.cb.evaluation.log()
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
An \code{xgb.Callback} object, which can be passed to \link{xgb.train} or \link{xgb.cv}.
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Callback for logging the evaluation history
|
|
||||||
}
|
|
||||||
\details{
|
|
||||||
This callback creates a table with per-iteration evaluation metrics (see parameters
|
|
||||||
\code{evals} and \code{feval} in \link{xgb.train}).
|
|
||||||
|
|
||||||
Note: in the column names of the final data.table, the dash '-' character is replaced with
|
|
||||||
the underscore '_' in order to make the column names more like regular R identifiers.
|
|
||||||
}
|
|
||||||
\seealso{
|
|
||||||
\link{xgb.cb.print.evaluation}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user