未验证 提交 238e0d48 编写于 作者: 片刻小哥哥's avatar 片刻小哥哥 提交者: GitHub

Merge pull request #28 from jiangzhonglian/master

更新模版
# 贡献指南
> 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科)
可能有用的链接:
+ [0.72 中文文档](https://xgboost.apachecn.org/docs/0.72)
+ [0.72 英文文档](https://xgboost.readthedocs.io/en/release_0.72)
负责人:
* [1266](https://github.com/wangweitong): 1097828409
* [腻味](https://github.com/xxxx): 1185685810
## 章节列表
* [Introduction](README.md)
* [安装指南](https://xgboost.readthedocs.io/en/latest/build.html)
* [XGBoost入门](https://xgboost.readthedocs.io/en/latest/get_started.html)
* [XGBoost教程](https://xgboost.readthedocs.io/en/latest/tutorials/index.html)
* [Boosted Trees简介](https://xgboost.readthedocs.io/en/latest/tutorials/model.html)
* [使用AWS YARN分布式XGBoost](https://xgboost.readthedocs.io/en/latest/tutorials/aws_yarn.html)
* [使用XGBoost4J-Spark的分布式XGBoost](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [DART助推器](https://xgboost.readthedocs.io/en/latest/tutorials/dart.html)
* [单调约束](https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html)
* [XGBoost中的随机森林](https://xgboost.readthedocs.io/en/latest/tutorials/rf.html)
* [特征交互约束](https://xgboost.readthedocs.io/en/latest/tutorials/feature_interaction_constraint.html)
* [DMatrix的文本输入格式](https://xgboost.readthedocs.io/en/latest/tutorials/input_format.html)
* [参数调整注意事项](https://xgboost.readthedocs.io/en/latest/tutorials/param_tuning.html)
* [使用XGBoost外部存储器版本(测试版)](https://xgboost.readthedocs.io/en/latest/tutorials/external_memory.html)
* [自定义目标和评估指标](https://xgboost.readthedocs.io/en/latest/tutorials/custom_metric_obj.html)
* [经常问的问题](https://xgboost.readthedocs.io/en/latest/faq.html)
* [XGBoost用户论坛](https://discuss.xgboost.ai/)
* [GPU支持](https://xgboost.readthedocs.io/en/latest/gpu/index.html)
* [XGBoost参数](https://xgboost.readthedocs.io/en/latest/parameter.html)
* [Python包](https://xgboost.readthedocs.io/en/latest/python/index.html)
* [Python包介绍](https://xgboost.readthedocs.io/en/latest/python/python_intro.html)
* [Python API参考](https://xgboost.readthedocs.io/en/latest/python/python_api.html)
* [Python示例](https://github.com/dmlc/xgboost/tree/master/demo/guide-python)
* [R包](https://xgboost.readthedocs.io/en/latest/R-package/index.html)
* [R中的XGBoost简介](https://xgboost.readthedocs.io/en/latest/R-package/xgboostPresentation.html)
* [使用XGBoost了解您的数据集](https://xgboost.readthedocs.io/en/latest/R-package/discoverYourData.html)
* [JVM包](https://xgboost.readthedocs.io/en/latest/jvm/index.html)
* [XGBoost4J入门](https://xgboost.readthedocs.io/en/latest/jvm/java_intro.html)
* [XGBoost4J-Spark教程](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [代码示例](https://github.com/dmlc/xgboost/tree/master/jvm-packages/xgboost4j-example)
* [XGBoost4J Java API](https://xgboost.readthedocs.io/en/latest/jvm/javadocs/index.html)
* [XGBoost4J Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j/index.html)
* [XGBoost4J-Spark Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-spark/index.html)
* [XGBoost4J-Flink Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-flink/index.html)
* [Julia包](https://xgboost.readthedocs.io/en/latest/julia.html)
* [CLI界面](https://xgboost.readthedocs.io/en/latest/cli.html)
* [有助于XGBoost](https://xgboost.readthedocs.io/en/latest/contribute.html)
## 流程
### 一、认领
首先查看[整体进度](https://github.com/apachecn/pytorch-doc-zh/issues/274),确认没有人认领了你想认领的章节。
然后回复 ISSUE,注明“章节 + QQ 号”(一定要留 QQ)。
### 二、翻译
可以合理利用翻译引擎(例如[谷歌](https://translate.google.cn/)),但一定要把它变得可读!
可以参照之前版本的中文文档,如果有用的话。
如果遇到格式问题,请随手把它改正。
### 三、提交
**提交的时候不要改动文件名称,即使它跟章节标题不一样也不要改,因为文件名和原文的链接是对应的!!!**
+ `fork` Github 项目
+ 将译文放在`docs/0.90`文件夹下
+ `push`
+ `pull request`
请见 [Github 入门指南](https://github.com/apachecn/kaggle/blob/master/docs/GitHub)
# XGBoost 中文文档
# <center>XGBoost 中文文档</center>
灵活的
<center>XGBoost是一个优化的分布式梯度增强库,旨在实现高效,灵活和便携。</center>
支持回归, 分类, 排序和用户定义的目标函数.
> 它在 [Gradient Boosting](https://en.wikipedia.org/wiki/Gradient_boosting) 框架下实现机器学习算法。XGBoost提供并行树提升(也称为GBDT,GBM),可以快速准确地解决许多数据科学问题。相同的代码在主要的分布式环境(Hadoop,SGE,MPI)上运行,并且可以解决数十亿个示例之外的问题。
可移植的
<center>
可以在 Windows, Linux 和 OS X , 以及各种云平台环境上运行
![](docs/img/logo.png)
</center>
多语言的
> 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远。
支持多种语言, 包括 C++, Python, R, Java, Scala, Julia.
* [中文地址](https://xgboost.apachecn.org/)
* [英文地址](https://xgboost.ai/)
+ [ApacheCN 机器学习交流群 629470233](http://shang.qq.com/wpa/qunwpa?idkey=30e5f1123a79867570f665aa3a483ca404b1c3f77737bc01ec520ed5f078ddef)
+ [ApacheCN 学习资源](https://www.ibooker.org.cn/docs)
实战检验的
## 翻译进度
赢得了许多数据科学和机器学习方面的挑战. 多家公司已经在生产环境中使用.
[**贡献指南**](CONTRIBUTING.md)
分布式的
> 项目翻译 xgboost 0.90
支持在多台机器上进行分布式训练, 包括 AWS, GCE, Azure 和 Yarn 集群. 可以被 Flink, Spark 以及其它的云数据流系统进行集成.
* 贡献者: 记得留言和更新翻译进度
* 地址: https://github.com/apachecn/xgboost-doc-zh/issues/27
高性能的
## 项目看板
经过充分优化的后端系统在资源有限的情况下可以获得最佳性能. 在代码一致的情况下, 分布式的版本解决了超过数十亿样本的问题.
> 项目 XGBoost 0.90 看板
+ [在线阅读](https://xgboost.apachecn.org/)
* 负责人: 记得更新和优化
* 地址: https://github.com/apachecn/xgboost-doc-zh/projects/1
## 贡献指南
为了使项目更加便于维护,我们将文档格式全部转换成了 Markdown,同时更换了页面生成器。后续维护工作将完全在 Markdown 上进行。
## 项目负责人
格式: GitHub + QQ
> 第一期 (2017-xx-xx)
* [那伊抹微笑](https://github.com/wangyangting): 1042658081
* [Peppa](https://github.com/chenyyx): 190442212
> 第2期 (2019-07-07)
* [1266](https://github.com/wangweitong): 1097828409
* [腻味](https://github.com/xxxx): 1185685810
-- 负责人要求: (欢迎一起为 `XGBoost 中文版本` 做贡献)
* 热爱开源,喜欢装逼
* 长期使用 XGBoost
* 能够有时间及时优化页面bug和用户issues
* 试用期: 2个月
* 欢迎联系: [片刻](https://github.com/jiangzhonglian) 529815144
## 建议反馈
* 在我们的 [apachecn/xgboost-doc-zh](https://github.com/apachecn/xgboost-doc-zh) github 上提 issue.
* 发邮件到 Email: `apachecn@163.com`.
* 在我们的 [QQ群-搜索: 交流方式](https://github.com/apachecn/home) 中联系群主/管理员即可.
## 赞助我们
<img src="http://data.apachecn.org/img/about/donate.jpg" alt="微信&支付宝" />
小部分格式仍然存在问题,主要是链接和表格。需要大家帮忙找到,并提 PullRequest 来修复。
# Summary
+ [Introduction](README.md)
+ [开始使用 XGBoost](docs/1.md)
+ [XGBoost 教程](docs/2.md)
+ [Boosted Trees 介绍](docs/3.md)
......@@ -15,4 +18,4 @@
+ [XGBoost Python Package](docs/12.md)
+ [Python 软件包介绍](docs/13.md)
+ [Python API 参考](docs/14.md)
+ [XGBoost 参数](docs/15.md)
\ No newline at end of file
+ [XGBoost 参数](docs/15.md)
# 开始使用 XGBoost
这里是一个快速入门的教程, 它展示了让你快速在示例数据集上进行二元分类任务时的 xgboost 的代码片段.
## Links to Helpful Other Resources
* 请参阅 [_安装指南_](../build.html) 以了解如何去安装 xgboost.
* 请参阅 [_入门指引_](../how_to/index.html) 以了解有关使用 xgboost 的各种技巧.
* 请参阅 [_学习教程_](../tutorials/index.html) 以了解有关特定任务的教程.
* 请参阅 [通过例子来学习使用 XGBoost](https://github.com/dmlc/xgboost/tree/master/doc/../demo) 以了解更多代码示例.
## Python
```
import xgboost as xgb
# 读取数据
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# 通过 map 指定参数
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }
num_round = 2
bst = xgb.train(param, dtrain, num_round)
# 预测
preds = bst.predict(dtest)
```
## R
```
# 加载数据
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, nround = 2,
nthread = 2, objective = "binary:logistic")
# 预测
pred <- predict(bst, test$data)
```
## Julia
```
using XGBoost
# 读取数据
train_X, train_Y = readlibsvm("demo/data/agaricus.txt.train", (6513, 126))
test_X, test_Y = readlibsvm("demo/data/agaricus.txt.test", (1611, 126))
# 拟合模型
num_round = 2
bst = xgboost(train_X, num_round, label=train_Y, eta=1, max_depth=2)
# 预测
pred = predict(bst, test_X)
```
## Scala
```
import ml.dmlc.xgboost4j.scala.DMatrix
import ml.dmlc.xgboost4j.scala.XGBoost
object XGBoostScalaExample {
def main(args: Array[String]) {
// 读取 xgboost/demo/data 目录中可用的训练数据
val trainData =
new DMatrix("/path/to/agaricus.txt.train")
// 定义参数
val paramMap = List(
"eta" -> 0.1,
"max_depth" -> 2,
"objective" -> "binary:logistic").toMap
// 迭代次数
val round = 2
// train the model
val model = XGBoost.train(trainData, paramMap, round)
// 预测
val predTrain = model.predict(trainData)
// 保存模型至文件
model.saveModel("/local/path/to/model")
}
}
```
\ No newline at end of file
# 为 XGBoost 做贡献
XGBoost 已经被一群活跃的社区成员开发和使用。 欢迎每个人来做贡献。这是使项目更好,更方便用户使用的一种方法。
* 当你的补丁被 merged 后,请将您的名字添加到 [CONTRIBUTORS.md](https://github.com/dmlc/xgboost/tree/master/doc/CONTRIBUTORS.md)
* 请同时更新 [NEWS.md](https://github.com/dmlc/xgboost/tree/master/doc/NEWS.md) ,以便对 API 的更改添加注释或添加新文档。
## 指导方针
* [提交 Pull Request](#submit-pull-request)
* [Git 工作流程](#git-workflow-howtos)
* [如何解决与 master 的冲突](#how-to-resolve-conflict-with-master)
* [如何将多个提交合并为一个](#how-to-combine-multiple-commits-into-one)
* [force push(强制 push)的后果是什么](#what-is-the-consequence-of-force-push)
* [文档](#document)
* [测试用例](#testcases)
* [例子](#examples)
* [核心库](#core-library)
* [Python 包](#python-package)
* [R 包](#r-package)
## 提交 Pull Request
* 在提交之前,请将您的代码重新与最新版本的 master 上的代码做一次同步(rebase),您可以这么做
```
git remote add upstream https://github.com/dmlc/xgboost
git fetch upstream
git rebase upstream/master
```
* 如果你有多个小提交,可以将它们 merge 到一起 (使用 git rebase 然后 squash) 成为更有意义的组。
* 发送 pull request!
* 修复自动检查报告的问题
* 如果您正在贡献一个新模块,请考虑在 [tests](https://github.com/dmlc/xgboost/tree/master/doc/tests) 中添加一个测试用例
## Git 工作流程
### 如何解决与 master 的冲突
* 首先 rebase 到最新的 master
```
# 做完一次后,可以跳过前两个步骤
git remote add upstream https://github.com/dmlc/xgboost
git fetch upstream
git rebase upstream/master
```
* git 可能会显示一些它不能合并的冲突,比如说 `conflicted.py`
* 手动修改文件以解决冲突。
* 解决冲突后,将其标记为已解决
```
git add conflicted.py
```
* 那么你可以继续 rebase 通过
```
git rebase --continue
```
* 最后 push 到你的 fork 上,你可能需要在这里 force push(强制 push)。
```
git push --force
```
### 如何将多个提交合并为一个
有时候我们想要合并多个 commits(提交),特别是当以后的 commits(提交)只能修复以前的提交时,创建一个有意义的提交集合的 PR 。你可以通过以下步骤来完成。
* 在此之前,如果您之前没有这样做,请配置 git 的默认编辑器。
```
git config core.editor the-editor-you-like
```
* 假设我们要 merge 最后 3 个 commits,请输入以下命令
```
git rebase -i HEAD~3
```
* 它会弹出一个文本编辑器。将第一个 commit 设置为 `pick` ,将后一个 commit 更改为 `squash`
* 保存文件之后,会弹出另一个文本编辑器,要求你修改合并的 commit message(提交信息)。
* 将更改 push 到你的 fork ,你需要强制 push(force push)。
```
git push --force
```
### force push(强制 push)的后果是什么
前两个提示需要强制 push(force push),这是因为我们改变了提交的路径。 只要提交的内容只属于你自己的内容,就可以强制 push 到自己的 fork 。
## 文档
* 该文档是使用 sphinx 和 [recommonmark](http://recommonmark.readthedocs.org/en/latest/) 创建的。
* 你可以在本地构建文档以查看效果。
## 测试用例
* 所有的测试用例在 [tests](https://github.com/dmlc/xgboost/tree/master/doc/tests) 中。
* 我们使用 python nose 进行 python 测试用例。
## 示例
* 用例和示例将在 [demo](https://github.com/dmlc/xgboost/tree/master/doc/demo)
* 我们非常高兴听到您的故事,如果您有使用 xgboost 的博客,教程代码解决方案,请告诉我们,我们将在示例页面中添加一个链接。
## 核心库
* 遵循 C++ 的 Google C 的风格。
* 我们使用 doxygen 来记录所有的接口代码。
* 你可以通过输入 `make lint` 来重新产生 linter 。
## Python 包
* 总是将 docstring(文档字符串) 添加到 numpydoc 格式的新函数中。
* 你可以通过输入 `make lint` 来重新生成 linter 。
## R 包
### 代码风格
* 我们在 C++ 代码上遵循 Google 的 C++ 风格指南。
* 这主要与项目的其他部分保持一致。
* 另一个原因是我们可以用 linter 自动检查样式。
* 你可以通过在根文件夹中键入以下命令来检查代码的样式。
```
make rcpplint
```
* 在需要的时候,你可以用 `// NOLINT(*)` 注释来禁用某行的 linter 警告。
### Rmarkdown Vignettes
Rmarkdown vignettes 放置在 [R-package/vignettes](https://github.com/dmlc/xgboost/tree/master/doc/R-package/vignettes) 这些 Rmarkdown 文件不被编译。我们在 [doc/R-package](https://github.com/dmlc/xgboost/tree/master/doc/how_to/R-package) 上托管编译的版本。
按照以下步骤添加一个新的 Rmarkdown vignettes:
* 将原始的 rmarkdown 添加到 `R-package/vignettes`
* 修改 `doc/R-package/Makefile` 来添加要构建的 markdown 文件
* Clone [dmlc/web-data](https://github.com/dmlc/web-data) 仓库到 `doc` 文件夹中
* 现在在 `doc/R-package` 上输入以下命令
```
make the-markdown-to-make.md
```
* 这将生成 markdown 以及 figure(图形)到 `doc/web-data/xgboost/knitr`
* 修改 `doc/R-package/index.md` 来指向生成的 markdown 。
* 将生成的 figure 添加到 `dmlc/web-data` 仓库。
* 如果你已经将 repo clone 到 doc,这意味着一个 `git add`
* 为 markdown 和 `dmlc/web-data` 创建 PR
* 你也可以通过在 `doc` 处输入以下命令在本地生成文档
```
make html
```
我们这样做的原因是为了避免由于生成的图像 size 导致 repo 的 size 爆炸。
\ No newline at end of file
# 常见问题
此文档包含有关 xgboost 的常见问题。
## 如何调参
参阅 [_参数调整指南_](how_to/param_tuning.html)
## 模型的描述
参阅 [_介绍 Boosted Trees_](model.html)
## 我有一个很大的数据集
XGBoost 被设计为 memory efficient(高效使用内存)的。通常数据符合你的内存就可以处理问题(这通常意味着数百万个实例)。 如果内存不足,请查看 [外部存储器版本](https://github.com/dmlc/xgboost/tree/master/doc/external_memory.md) 或 xgboost 的 [分布式版本](https://github.com/dmlc/wormhole/tree/master/learn/xgboost)
## 在平台 X(Hadoop/Yarn, Mesos)上运行 xgboost
XGBoost 的分布式版本被设计为可移植到各种环境。 分布式 XGBoost 可以移植到任何支持 [rabit](https://github.com/dmlc/rabit) 的平台上。 你可以直接在 Yarn 上运行 xgboost 。理论上 Mesos 和其他资源分配引擎也可以很容易地支持。
## 为什么不在 X(Spark, Hadoop)之上实现分布式 xgboost
我们需要知道的第一个事实是分布式并不一定能解决所有的问题。 相反,它会产生更多的问题,如更多的通信开销和容错。 最终的问题还是要回到如何推动每个计算节点的限制,并使用更少的资源来完成任务(从而减少通信和失败的机会)。
为了实现这些,我们决定在单个节点 xgboost 中重用优化,并在其上构建分布式版本。 机器学习中的通信需求是相当简单的,因为我们可以依赖于一套有限的 API(在我们的案例中)。 这样的设计使我们能够重用大部分代码,同时可以移植到 Hadoop/Yarn, MPI, SGE 等主流平台上。 最重要的是,它推动了我们可以使用的计算资源的限制。
## 如何将模型移植到我自己的系统中
XGBoost 的模型和数据格式是可交换的,这意味着一种语言训练的模型可以加载到另一种语言。 这意味着您可以使用 R 训练模型,同时使用 Java 或 C++ 进行预测,这在生产系统中更为常见。 您还可以使用分布式版本训练模型,并从 Python 加载它们以进行一些交互式分析。
## 你支持 LambdaMART 吗
是的,xgboost 实现了 LambdaMART 。在 [_参数_](parameter.html) 中可以查看到你想要的部分。
## 如何处理缺失值
xgboost 默认是支持缺失值的。
## 运行结果略有不同
由于浮点求和顺序和多线程的非确定性,可能会发生这种情况。 虽然一般的准确性通常会保持不变。
\ No newline at end of file
# XGBoost Python Package
该页面包含软件包上所有与 python 相关的文档链接. 要安装软件包, 请参阅 [_构建和安装说明_](../build.html).
## 内容目录
* [Python 教程概述](python_intro.html)
* [通过例子来学习 XGBoost](https://github.com/apachecn/xgboost-doc-zh/tree/v0.60/demo/zh)
* [Python API 参考](python_api.html)
\ No newline at end of file
# Python 软件包介绍
本文档给出了有关 xgboost python 软件包的基本演练.
**_其他有用的链接列表_**
* [Python 演练代码集合](https://github.com/tqchen/xgboost/blob/master/demo/guide-python)
* [Python API 参考](python_api.html)
## 安装 XGBoost
要安装 XGBoost, 请执行以下步骤:
* 您需要在项目的根目录下运行 `make` 命令
*`python-package` 目录下运行
```
python setup.py install
```
```
import xgboost as xgb
```
## 数据接口
XGBoost python 模块能够使用以下方式加载数据:
* libsvm txt format file(libsvm 文本格式的文件)
* Numpy 2D array, and(Numpy 2维数组, 以及)
* xgboost binary buffer file. (xgboost 二进制缓冲文件)
这些数据将会被存在一个名为 `DMatrix` 的对象中.
* 要加载 ligbsvm 文本格式或者 XGBoost 二进制文件到 `DMatrix` 对象中. 代码如下:
```
dtrain = xgb.DMatrix('train.svm.txt')
dtest = xgb.DMatrix('test.svm.buffer')
```
* 要加载 numpy 的数组到 `DMatrix` 对象中, 代码如下:
```
data = np.random.rand(5,10) # 5 entities, each contains 10 features
label = np.random.randint(2, size=5) # binary target
dtrain = xgb.DMatrix( data, label=label)
```
* 要加载 scpiy.sparse 数组到 `DMatrix` 对象中, 代码如下:
```
csr = scipy.sparse.csr_matrix((dat, (row, col)))
dtrain = xgb.DMatrix(csr)
```
* 保存 `DMatrix` 到 XGBoost 二进制文件中后, 会在下次加载时更快:
```
dtrain = xgb.DMatrix('train.svm.txt')
dtrain.save_binary("train.buffer")
```
* 要处理 `DMatrix` 中的缺失值, 您可以通过指定缺失值的参数来初始化 `DMatrix`:
```
dtrain = xgb.DMatrix(data, label=label, missing = -999.0)
```
* 在需要时可以设置权重:
```
w = np.random.rand(5, 1)
dtrain = xgb.DMatrix(data, label=label, missing = -999.0, weight=w)
```
## 设置参数
XGBoost 使用 pair 格式的 list 来保存 [_参数_](../parameter.html). 例如:
* Booster(提升)参数
```
param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' }
param['nthread'] = 4
param['eval_metric'] = 'auc'
```
* 您也可以指定多个评估的指标:
```
param['eval_metric'] = ['auc', 'ams@0']
# alternativly:
# plst = param.items()
# plst += [('eval_metric', 'ams@0')]
```
* 指定验证集以观察性能
```
evallist = [(dtest,'eval'), (dtrain,'train')]
```
## 训练
有用参数列表和数据以后, 您现在可以训练一个模型了.
* 训练
```
num_round = 10
bst = xgb.train( plst, dtrain, num_round, evallist )
```
* 保存模型 训练之后,您可以保存模型并将其转储出去.
```
bst.save_model('0001.model')
```
* 转储模型和特征映射 您可以将模型转储到 txt 文件并查看模型的含义
```
# 转存模型
bst.dump_model('dump.raw.txt')
# 转储模型和特征映射
bst.dump_model('dump.raw.txt','featmap.txt')
```
* 加载模型 当您保存模型后, 您可以使用如下方式在任何时候加载模型文件
```
bst = xgb.Booster({'nthread':4}) #init model
bst.load_model("model.bin") # load data
```
## 提前停止
如果您有一个验证集, 你可以使用提前停止找到最佳数量的 boosting rounds(梯度次数). 提前停止至少需要一个 `evals` 集合. 如果有多个, 它将使用最后一个.
`train(..., evals=evals, early_stopping_rounds=10)`
该模型将开始训练, 直到验证得分停止提高为止. 验证错误需要至少每个 `early_stopping_rounds` 减少以继续训练.
如果提前停止,模型将有三个额外的字段: `bst.best_score`, `bst.best_iteration``bst.best_ntree_limit`. 请注意 `train()` 将从上一次迭代中返回一个模型, 而不是最好的一个.
这与两个度量标准一起使用以达到最小化(RMSE, 对数损失等)和最大化(MAP, NDCG, AUC). 请注意, 如果您指定多个评估指标, 则 `param ['eval_metric']` 中的最后一个用于提前停止.
## 预测
当您 训练/加载 一个模型并且准备好数据之后, 即可以开始做预测了.
```
# 7 个样本, 每一个包含 10 个特征
data = np.random.rand(7, 10)
dtest = xgb.DMatrix(data)
ypred = bst.predict(xgmat)
```
如果在训练过程中提前停止, 可以用 `bst.best_ntree_limit` 从最佳迭代中获得预测结果:
```
ypred = bst.predict(xgmat,ntree_limit=bst.best_ntree_limit)
```
## 绘图
您可以使用 plotting(绘图)模块来绘制出 importance(重要性)以及输出的 tree(树).
要绘制出 importance(重要性), 可以使用 `plot_importance`. 该函数需要安装 `matplotlib`.
```
xgb.plot_importance(bst)
```
输出的 tree(树)会通过 `matplotlib` 来展示, 使用 `plot_tree` 指定 target tree(目标树)的序号. 该函数需要 `graphviz``matplotlib`.
```
xgb.plot_tree(bst, num_trees=2)
```
当您使用 `IPython` 时, 你可以使用 `to_graphviz` 函数, 它可以将 target tree(目标树)转换成 `graphviz` 实例. `graphviz` 实例会自动的在 `IPython` 上呈现.
```
xgb.to_graphviz(bst, num_trees=2)
```
\ No newline at end of file
此差异已折叠。
# XGBoost 参数
在运行 XGBoost 之前, 我们必须设置三种类型的参数: 常规参数, 提升器参数和任务参数.
* 常规参数与我们用于提升的提升器有关,通常是树模型或线性模型
* 提升器参数取决于你所选择的提升器
* 学习任务的参数决定了学习场景, 例如回归任务可以使用不同的参数进行排序相关的任务
* 命令行参数的行为与 xgboost 的 CLI 版本相关
## R 中的参数
在 R 包中. 您可以使用 .(点)替换参数中下划线, 例如, 可以使用 max.depth 作为 max_depth. 下划线参数在 R 中也是有效的.
## 常规参数
* booster [default=gbtree]
* which booster to use, can be gbtree, gblinear or dart. gbtree and dart use tree based model while gblinear uses linear function.
* silent [default=0]
* 0 means printing running messages, 1 means silent mode.
* nthread [default to maximum number of threads available if not set]
* number of parallel threads used to run xgboost
* num_pbuffer [set automatically by xgboost, no need to be set by user]
* size of prediction buffer, normally set to number of training instances. The buffers are used to save the prediction results of last boosting step.
* num_feature [set automatically by xgboost, no need to be set by user]
* feature dimension used in boosting, set to maximum dimension of the feature
## 用于 Tree 提升的参数
* eta [default=0.3]
* step size shrinkage used in update to prevents overfitting. After each boosting step, we can directly get the weights of new features. and eta actually shrinks the feature weights to make the boosting process more conservative.
* range: [0,1]
* gamma [default=0]
* minimum loss reduction required to make a further partition on a leaf node of the tree. the larger, the more conservative the algorithm will be.
* range: [0,∞]
* max_depth [default=6]
* maximum depth of a tree, increase this value will make model more complex / likely to be overfitting.
* range: [1,∞]
* min_child_weight [default=1]
* minimum sum of instance weight(hessian) needed in a child. If the tree partition step results in a leaf node with the sum of instance weight less than min_child_weight, then the building process will give up further partitioning. In linear regression mode, this simply corresponds to minimum number of instances needed to be in each node. The larger, the more conservative the algorithm will be.
* range: [0,∞]
* max_delta_step [default=0]
* Maximum delta step we allow each tree’s weight estimation to be. If the value is set to 0, it means there is no constraint. If it is set to a positive value, it can help making the update step more conservative. Usually this parameter is not needed, but it might help in logistic regression when class is extremely imbalanced. Set it to value of 1-10 might help control the update
* range: [0,∞]
* subsample [default=1]
* subsample ratio of the training instance. Setting it to 0.5 means that XGBoost randomly collected half of the data instances to grow trees and this will prevent overfitting.
* range: (0,1]
* colsample_bytree [default=1]
* subsample ratio of columns when constructing each tree.
* range: (0,1]
* colsample_bylevel [default=1]
* subsample ratio of columns for each split, in each level.
* range: (0,1]
* lambda [default=1]
* L2 regularization term on weights, increase this value will make model more conservative.
* alpha [default=0]
* L1 regularization term on weights, increase this value will make model more conservative.
* tree_method, string [default=’auto’]
* The tree constructtion algorithm used in XGBoost(see description in the [reference paper](http://arxiv.org/abs/1603.02754))
* Distributed and external memory version only support approximate algorithm.
* Choices: {‘auto’, ‘exact’, ‘approx’}
* ‘auto’: Use heuristic to choose faster one.
* For small to medium dataset, exact greedy will be used.
* For very large-dataset, approximate algorithm will be choosed.
* Because old behavior is always use exact greedy in single machine, user will get a message when approximate algorithm is choosed to notify this choice.
* ‘exact’: Exact greedy algorithm.
* ‘approx’: Approximate greedy algorithm using sketching and histogram.
* sketch_eps, [default=0.03]
* This is only used for approximate greedy algorithm.
* This roughly translated into `O(1 / sketch_eps)` number of bins. Compared to directly select number of bins, this comes with theoretical ganrantee with sketch accuracy.
* Usuaully user do not have to tune this. but consider set to lower number for more accurate enumeration.
* range: (0, 1)
* scale_pos_weight, [default=0]
* Control the balance of positive and negative weights, useful for unbalanced classes. A typical value to consider: sum(negative cases) / sum(positive cases) See [_Parameters Tuning_](how_to/param_tuning.html) for more discussion. Also see Higgs Kaggle competition demo for examples: [R](https://github.com/dmlc/xgboost/tree/master/doc/../demo/kaggle-higgs/higgs-train.R), [py1](https://github.com/dmlc/xgboost/tree/master/doc/../demo/kaggle-higgs/higgs-numpy.py), [py2](https://github.com/dmlc/xgboost/tree/master/doc/../demo/kaggle-higgs/higgs-cv.py), [py3](https://github.com/dmlc/xgboost/tree/master/doc/../demo/guide-python/cross_validation.py)
## 用于 Dart Booster 的其它参数
* sample_type [default=”uniform”]
* type of sampling algorithm.
* “uniform”: dropped trees are selected uniformly.
* “weighted”: dropped trees are selected in proportion to weight.
* normalize_type [default=”tree”]
* type of normalization algorithm.
* “tree”: new trees have the same weight of each of dropped trees.
* weight of new trees are 1 / (k + learnig_rate)
* dropped trees are scaled by a factor of k / (k + learning_rate)
* “forest”: new trees have the same weight of sum of dropped trees (forest).
* weight of new trees are 1 / (1 + learning_rate)
* dropped trees are scaled by a factor of 1 / (1 + learning_rate)
* rate_drop [default=0.0]
* dropout rate.
* range: [0.0, 1.0]
* skip_drop [default=0.0]
* probability of skip dropout.
* If a dropout is skipped, new trees are added in the same manner as gbtree.
* range: [0.0, 1.0]
## 用于 Linear Booster 的参数
* lambda [default=0]
* L2 regularization term on weights, increase this value will make model more conservative.
* alpha [default=0]
* L1 regularization term on weights, increase this value will make model more conservative.
* lambda_bias
* L2 regularization term on bias, default 0(no L1 reg on bias because it is not important)
## 学习任务的参数
Specify the learning task and the corresponding learning objective. The objective options are below:
* objective [ default=reg:linear ]
* “reg:linear” –linear regression
* “reg:logistic” –logistic regression
* “binary:logistic” –logistic regression for binary classification, output probability
* “binary:logitraw” –logistic regression for binary classification, output score before logistic transformation
* “count:poisson” –poisson regression for count data, output mean of poisson distribution
* max_delta_step is set to 0.7 by default in poisson regression (used to safeguard optimization)
* “multi:softmax” –set XGBoost to do multiclass classification using the softmax objective, you also need to set num_class(number of classes)
* “multi:softprob” –same as softmax, but output a vector of ndata * nclass, which can be further reshaped to ndata, nclass matrix. The result contains predicted probability of each data point belonging to each class.
* “rank:pairwise” –set XGBoost to do ranking task by minimizing the pairwise loss
* “reg:gamma” –gamma regression for severity data, output mean of gamma distribution
* base_score [ default=0.5 ]
* the initial prediction score of all instances, global bias
* for sufficent number of iterations, changing this value will not have too much effect.
* eval_metric [ default according to objective ]
* evaluation metrics for validation data, a default metric will be assigned according to objective( rmse for regression, and error for classification, mean average precision for ranking )
* User can add multiple evaluation metrics, for python user, remember to pass the metrics in as list of parameters pairs instead of map, so that latter ‘eval_metric’ won’t override previous one
* The choices are listed below:
* “rmse”: [root mean square error](http://en.wikipedia.org/wiki/Root_mean_square_error)
* “mae”: [mean absolute error](https://en.wikipedia.org/wiki/Mean_absolute_error)
* “logloss”: negative [log-likelihood](http://en.wikipedia.org/wiki/Log-likelihood)
* “error”: Binary classification error rate. It is calculated as #(wrong cases)/#(all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
* “merror”: Multiclass classification error rate. It is calculated as #(wrong cases)/#(all cases).
* “mlogloss”: [Multiclass logloss](https://www.kaggle.com/wiki/MultiClassLogLoss)
* “auc”: [Area under the curve](http://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_curve) for ranking evaluation.
* “ndcg”:[Normalized Discounted Cumulative Gain](http://en.wikipedia.org/wiki/NDCG)
* “map”:[Mean average precision](http://en.wikipedia.org/wiki/Mean_average_precision#Mean_average_precision)
* “ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
* “ndcg-”,”map-”,”ndcg@n-”,”map@n-”: In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1\. By adding “-” in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions. training repeatively
* “gamma-deviance”: [residual deviance for gamma regression]
* seed [ default=0 ]
* random number seed.
## 命令行参数
The following parameters are only used in the console version of xgboost
* use_buffer [ default=1 ]
* Whether to create a binary buffer from text input. Doing so normally will speed up loading times
* num_round
* The number of rounds for boosting
* data
* The path of training data
* test:data
* The path of test data to do prediction
* save_period [default=0]
* the period to save the model, setting save_period=10 means that for every 10 rounds XGBoost will save the model, setting it to 0 means not save any model during training.
* task [default=train] options: train, pred, eval, dump
* train: training using data
* pred: making prediction for test:data
* eval: for evaluating statistics specified by eval[name]=filename
* dump: for dump the learned model into text format(preliminary)
* model_in [default=NULL]
* path to input model, needed for test, eval, dump, if it is specified in training, xgboost will continue training from the input model
* model_out [default=NULL]
* path to output model after training finishes, if not specified, will output like 0003.model where 0003 is number of rounds to do boosting.
* model_dir [default=models]
* The output directory of the saved models during training
* fmap
* feature map, used for dump model
* name_dump [default=dump.txt]
* name of model dump file
* name_pred [default=pred.txt]
* name of prediction file, used in pred mode
* pred_margin [default=0]
* predict margin instead of transformed probability
\ No newline at end of file
# XGBoost 教程
本节包含 XGBoost 包中的官方教程。 参阅 [Awesome XGBoost](https://github.com/dmlc/xgboost/tree/master/demo) 找到更多的资源。
## 目录
* [Boosted Trees 介绍](../model.html)
* [在 AWS 上的分布式 XGBoost YARN](aws_yarn.html)
* [DART booster](dart.html)
\ No newline at end of file
# Boosted Trees 介绍
XGBoost 是 “Extreme Gradient Boosting” 的缩写,其中 “Gradient Boosting” 一词在论文 _Greedy Function Approximation: A Gradient Boosting Machine_ 中,由 Friedman 提出。 XGBoost 基于这个原始模型。 这是 gradient boosted trees(梯度增强树)的教程,大部分内容是基于 xgboost 的作者的这些 [slides](http://homes.cs.washington.edu/~tqchen/pdf/BoostedTree.pdf)
GBM (boosted trees,增强树)已经有一段时间了,关于这个话题有很多的材料。 这个教程试图用监督学习的元素以独立和有原则的方式解释 boosted trees (增强树)。 我们认为这个解释更加清晰,更加正式,并激发了 xgboost 中使用的变体。
## 监督学习的要素
XGBoost 用于监督学习问题,我们使用训练数据 ![](img/0.72/585ba860aa933d05565a569d8d1ddf93.jpg) 来预测目标变量 ![](img/0.72/1c13f41f3ec9bd4445ae9fb223b4ce4a.jpg) 。 在我们深入了解 boosted trees 之前,首先回顾一下监督学习的重要组成部件。
### 模型和参数
监督学习中的 **_model(模型)_** 通常是指给定输入 ![](img/0.72/585ba860aa933d05565a569d8d1ddf93.jpg) 如何去预测输出 ![](img/0.72/1c13f41f3ec9bd4445ae9fb223b4ce4a.jpg) 的数学结构。 例如,一个常见的模型是一个 _linear model(线性模型)_(如线性回归和 logistic regression),其中的预测是由 ![](img/0.72/7d80469ba9822a0d05b7140b4ca0290a.jpg) 给出的,这是加权输入特征的线性组合(线性叠加的方式)。 其实这里的预测 ![](img/0.72/4028c0134b3b847f0e97b4cd284bc927.jpg) 可以有不同的解释,取决于做的任务。 例如,我们可以通过 logistic 转换得到 logistic regression 中 positive 类别的概率,当我们想要对输出结果排序的时候,也可以作为排序得分指标等。
**_parameters(参数)_** 是我们需要从数据中学习的未确定部分。在线性回归问题中,参数是系数 ![](img/0.72/228674793358bc993a8dcb3c2861a3ee.jpg) 。 通常我们使用 ![](img/0.72/87142829e4107d70b9cae3f8b2d095fc.jpg) 来表示参数。
### 目标函数:训练损失 + 正则
基于对 ![](img/0.72/1c13f41f3ec9bd4445ae9fb223b4ce4a.jpg) 的不同理解,我们可以得到不同的问题,比如回归,分类,排序等。 我们需要找到一种方法来找到训练数据的最佳参数。为了做到这一点,我们需要定义一个所谓的 **_objective function(目标函数)_** 以给定一组参数来衡量模型的性能。
关于目标函数的一个非常重要的事实是,它们 **_must always(必须总是)_** 包含两个部分:training loss (训练损失) 和 regularization(正则化)。
![](img/0.72/f9a24042b2fe37eb28c8462d0195c985.jpg)
其中 ![](img/0.72/dd45717d5b8500473d4d5cc5e8da2d43.jpg) 是训练损失函数, ![](img/0.72/94f79ae2273f820cc565fdebf14a9f63.jpg) 是正则化项。 training loss (训练损失)衡量我们的数据对训练数据的预测性。 例如,常用的训练损失是 mean squared error(均方误差,MSE)。
![](img/0.72/1d3fcd8d09d65c0ee710f95bf8422ad9.jpg)
另一个常用的损失函数是 logistic 回归的 logistic 损失。
![](img/0.72/0507a5548594983588fdc7c98687b36b.jpg)
**_regularization term(正则化项)_** 是人们通常忘记添加的内容。正则化项控制模型的复杂性,这有助于避免过拟合。 这听起来有些抽象,那么我们在下面的图片中考虑下面的问题。在图像左上角给出输入数据点的情况下,要求您在视觉上 _fit(拟合)_ 一个 step function(阶梯函数)。 您认为三种中的哪一种解决方案是最拟合效果最好的?
![Step function](img/0.72/2d3bec80be56303ea525a39cd4395d5c.jpg)
答案已经标注为红色了。请思考一下这个是否在你的视觉上较为合理?总的原则是我们想要一个 **_simple(简单)_****_predictive(可预测)_** 的模型。 两者之间的权衡也被称为机器学习中的 bias-variance tradeoff(偏差-方差 权衡)。
对于线性模型常见的正则化项有 ![](img/0.72/c9379d9b2e521341735b00d845051d04.jpg) 正则和 ![](img/0.72/ff439882e70c47df87d58c837a372490.jpg) 正则。这样的目标函数的设计来自于统计学习中的一个重要概念,也是我们刚才说的, bias-variance tradeoff(偏差-方差 权衡)。比较感性的理解, Bias 可以理解为假设我们有无限多数据的时候,可以训练出最好的模型所拿到的误差。而 Variance 是因为我们只有有限数据,其中随机性带来的误差。目标中误差函数鼓励我们的模型尽可能去拟合训练数据,这样相对来说最后的模型会有比较小的 bias 。而正则化项则鼓励更加简单的模型。因为当模型简单之后,有限数据拟合出来结果的随机性比较小,不容易过拟合,使得最后模型的预测更加稳定。
### 为什么要介绍 general principle(一般原则)
上面介绍的要素构成了监督学习的基本要素,它们自然是机器学习工具包的基石。 例如,你应该能够描述 boosted trees 和 random forests 之间的差异和共同点。 以正式的方式理解这个过程也有助于我们理解我们正在学习的目标以及启发式算法背后的原因,例如 pruning 和 smoothing 。
## tree ensembles(树集成)
既然我们已经介绍了监督学习的内容,那么接下来让我们开始介绍真正的 trees 吧。 首先,让我们先来了解一下 xgboost 的 **_model(模型)_** : tree ensembles(树集成)。 树集成模型是一组 classification and regression trees (CART)。 下面是一个 CART 的简单的示例,它可以分类是否有人喜欢电脑游戏。
![CART](img/0.72/1476c1e968148d38644efcce09c2bef4.jpg)
我们把一个家庭的成员分成不同的叶子,并把他们分配到相应的叶子节点上。 CART 与 decision trees(决策树)有些许的不同,就是叶子只包含决策值。在 CART 中,每个叶子都有一个 real score (真实的分数),这给了我们更丰富的解释,超越了分类。 这也使得统一的优化步骤更容易,我们将在本教程的后面部分看到。
通常情况下,单棵树由于过于简单而不够强大到可以支持在实践中使用的。实际使用的是所谓的 tree ensemble model(树集成模型),它将多棵树的预测加到一起。
![TwoCART](img/0.72/35b6f55e2b5953ff94ee568f744b4efb.jpg)
上图是两棵树的集成的例子。将每棵树的预测分数加起来得到最终分数。 如果你看一下这个例子,一个重要的事实就是两棵树互相 _complement(补充)_ 。 在数学表示上,我们可以在表单中编写我们的模型。
![](img/0.72/8a5adca27b799741caabbdeae20be5cf.jpg)
其中 ![](img/0.72/ce9bf02722174f83b83dc21985022455.jpg) 是树的数量, ![](img/0.72/a96052742f7423a92cfcad03a185fa63.jpg) 是函数空间 ![](img/0.72/a787da64e8c74cd0b52223ce8d686f68.jpg) 的函数, ![](img/0.72/a787da64e8c74cd0b52223ce8d686f68.jpg) 是所有可能的 CARTs 的集合。所以我们优化的目标可以写成
![](img/0.72/b999689ef453cce06ffd631a571e1f29.jpg)
那么问题来了,random forests(随机森林)的 _model(模型)_ 是什么?这正是 tree ensembles(树集成)!所以 random forests 和 boosted trees 在模型上并没有什么不同,不同之处在于我们如何训练它们。这意味着如果你写一个 tree ensembles(树集成)的预测服务,你只需要编写它们中的一个,它们应该直接为 random forests(随机森林)和 boosted trees(增强树)工作。这也是监督学习基石元素的一个例子。
## Tree Boosting
在介绍完模型之后,我们从真正的训练部分开始。我们应该怎么学习 trees 呢? 答案是,对于所有的监督学习模型都一样的处理:_定义一个合理的目标函数,然后去尝试优化它_!
假设我们有以下目标函数(记住它总是需要包含训练损失和正则化)
![](img/0.72/4dc70a02830b485f3f26015c8fb1fdb9.jpg)
### 附加训练
我们想要问的第一件事就是树的 **_parameters(参数)_** 是什么。 你可能已经发现了,我们要学习的是那些函数 ![](img/0.72/0d22e8726e5829598054d2585d7c9370.jpg) ,每个函数都包含树的结构和叶子分数。这比传统的你可以找到捷径的优化问题要难得多。在 tree ensemble 中,参数对应了树的结构,以及每个叶子节点上面的预测分数。 一次性训练所有的树并不容易。 相反,我们使用一个附加的策略:优化好我们已经学习完成的树,然后一次添加一棵新的树。 我们通过 ![](img/0.72/17131c421b074dc314397e82a4528438.jpg) 来关注步骤 ![](img/0.72/654b00d1036ba7f7d93e02f57fc00a75.jpg) 的预测值,所以我们有
![](img/0.72/09858ff3ca719faf578ad5b13b3c98fa.jpg)
另外还有一个问题,每一步我们想要哪棵 tree 呢?一个自然而然的事情就是添加一个优化我们目标的方法。
![](img/0.72/1fe2cf4fb357117e177388becb68d9f9.jpg)
如果我们考虑使用 MSE 作为我们的损失函数,它将是下面的形式。
![](img/0.72/25bce8f7365849fcbccf06c572280bfd.jpg)
MSE 的形式比较友好,具有一阶项(通常称为残差)和二次项。 对于其他形式的损失(例如,logistic loss),获得这么好的形式并不是那么容易。 所以在一般情况下,我们把损失函数的泰勒展开到二阶
![](img/0.72/8c50b9fb6c2c85cbc672ab618d01c39b.jpg)
其中 ![](img/0.72/a3b9b8a59a7320d10d212f59e05036c4.jpg) 和 ![](img/0.72/390b4cd4db1c07e3db74b0810913ffc8.jpg) 被定义为
![](img/0.72/77116c2aaf28d1d862f8095806911151.jpg)
我们删除了所有的常量之后, ![](img/0.72/654b00d1036ba7f7d93e02f57fc00a75.jpg) 步骤中的具体目标就变成了
![](img/0.72/b201e8f1610bd6f5f4a4799b885df578.jpg)
这成为了新树的优化目标。这个定义的一个重要优点是它只依赖于 ![](img/0.72/a3b9b8a59a7320d10d212f59e05036c4.jpg) 和 ![](img/0.72/390b4cd4db1c07e3db74b0810913ffc8.jpg) 。这就是 xgboost 如何支持自定义损失函数。 我们可以使用完全相同的使用 ![](img/0.72/a3b9b8a59a7320d10d212f59e05036c4.jpg) 和 ![](img/0.72/390b4cd4db1c07e3db74b0810913ffc8.jpg) 作为输入的 solver(求解器)来对每个损失函数进行优化,包括 logistic regression, weighted logistic regression。
### 模型复杂度
我们已经介绍了训练步骤,但是等等,还有一个重要的事情,那就是 **_regularization(正则化)_** ! 我们需要定义树的复杂度 ![](img/0.72/683c9a76e2b8489a4a3d0ffdec2ad441.jpg) 。为了做到这一点,让我们首先改进一棵树的定义 ![](img/0.72/98b4f83629c037523fd162ed4b216f51.jpg) 如下
![](img/0.72/11710d80af8ff3712f529745adf5409a.jpg)
这里 ![](img/0.72/228674793358bc993a8dcb3c2861a3ee.jpg) 是树叶上的分数向量,![](img/0.72/ab0e7d4ab7d7b6e0a5aa4383fcdd2ee5.jpg) 是将每个数据点分配给叶子的函数,![](img/0.72/907939506800cf89ad2ed0d2d05cfabc.jpg) 是树叶的数量。 在 XGBoost 中,我们将复杂度定义为
![](img/0.72/aa297e24fc75c747a07b290638760b43.jpg)
当然有不止一种方法来定义复杂度,但是这个具体的方法在实践中运行良好。正则化是大多数树的包不那么谨慎或简单忽略的一部分。这是因为对传统的树学习算法的对待只强调提高 impurity(不纯性),而复杂度控制则是启发式的。 通过正式定义,我们可以更好地了解我们正在学习什么,是的,它在实践中运行良好。
### The Structure Score(结构分数)
这是 derivation(派生)的神奇部分。在对树模型进行重新格式化之后,我们可以用第 ![](img/0.72/8e83474bbce57dfc2d0399adceb655b3.jpg) 棵树来编写目标值如 :
![](img/0.72/2c05306f7baa0066128b8f8afd95ec37.jpg)
其中 ![](img/0.72/4ef880d0d67e58f282ce4ed79d4f9549.jpg) 是分配给第 ![](img/0.72/d9d5f5978eda3e268a05e689f897c9c6.jpg) 个叶子的数据点的索引的集合。 请注意,在第二行中,我们更改了总和的索引,因为同一叶上的所有数据点都得到了相同的分数。 我们可以通过定义 ![](img/0.72/2c864507ad886978650e8f67f3224afe.jpg) 和 ![](img/0.72/51b7cc80809f3eada5745a2e5f79b18d.jpg) 来进一步压缩表达式 :
![](img/0.72/4330c12a952e7f831231b51700bcd390.jpg)
在这个等式中 ![](img/0.72/af220a242098880c9f5524587fac68c5.jpg) 是相互独立的,形式 ![](img/0.72/ad3e76b49257d997698faa2d585c2a0a.jpg) 是二次的,对于给定的结构 ![](img/0.72/3c11d8d84433d2138c31a421b1be11c7.jpg) 的最好的 ![](img/0.72/af220a242098880c9f5524587fac68c5.jpg),我们可以得到最好的客观规约:
![](img/0.72/560afea477f6995994b0cfff8262e34d.jpg)
最后一个方程度量一个树结构 ![](img/0.72/3c11d8d84433d2138c31a421b1be11c7.jpg) **_how good(到底有多好)_**
![Structure Score](img/0.72/4194e8522c4e867f714b492a4439dae8.jpg)
如果这一切听起来有些复杂,我们来看一下图片,看看分数是如何计算的。 基本上,对于一个给定的树结构,我们把统计 ![](img/0.72/a3b9b8a59a7320d10d212f59e05036c4.jpg) 和 ![](img/0.72/390b4cd4db1c07e3db74b0810913ffc8.jpg) push 到它们所属的叶子上,统计数据加和到一起,然后使用公式计算树是多好。 除了考虑到模型的复杂度,这个分数就像决策树中的杂质测量一样(例如,熵/GINI系数)。
### 学习树结构
既然我们有了一个方法来衡量一棵树有多好,理想情况下我们会列举所有可能的树并挑选出最好的树。 在实践中,这种方法是比较棘手的,所以我们会尽量一次优化树的一个层次。 具体来说,我们试图将一片叶子分成两片,并得到分数
![](img/0.72/b8afcf3f3647596ed0584af454f840a9.jpg)
这个公式可以分解为 1) 新左叶上的得分 2) 新右叶上的得分 3) 原始叶子上的得分 4) additional leaf(附加叶子)上的正则化。 我们可以在这里看到一个重要的事实:如果增益小于 ![](img/0.72/cdab9437b701fd21fb3294cfba7c4bc2.jpg),我们最好不要添加那个分支。这正是基于树模型的 **_pruning(剪枝)_** 技术!通过使用监督学习的原则,我们自然会想出这些技术工作的原因 :)
对于真实有价值的数据,我们通常要寻找一个最佳的分割。为了有效地做到这一点,我们把所有的实例按照排序顺序排列,如下图所示。 ![Best split](img/0.72/728afccdae39c1bac6479386dab442e8.jpg)
然后从左到右的扫描就足以计算所有可能的拆分解决方案的结构得分,我们可以有效地找到最佳的拆分。
## XGBoost 最后的话
既然你明白了什么是 boosted trees 了,你可能会问这在 [XGBoost](https://github.com/dmlc/xgboost) 中的介绍在哪里? XGBoost 恰好是本教程中引入的正式原则的动力! 更重要的是,在 **_systems optimization(系统优化)_****_principles in machine learning(机器学习原理)_** 方面都有深入的研究。 这个库的目标是推动机器计算极限的极端,以提供一个 **_scalable(可扩展)_**, **_portable(可移植)_****_accurate(精确的)_** 库。 确保你 [试一试](https://github.com/dmlc/xgboost),最重要的是,向社区贡献你的智慧(代码,例子,教程)!
\ No newline at end of file
# AWS 上的分布式 XGBoost YARN
这是关于如何在 AWS EC2 集群上设置和运行分布式 [XGBoost](https://github.com/dmlc/xgboost) 的分步教程。分布式 XGBoost 运行在各种平台上,如 MPI, SGE 和 Hadoop YARN 。 在本教程中,我们使用 YARN 作为示例,因为这是分布式计算广泛使用的解决方案。
## 准备条件
我们需要获得一个 [AWS key-pair(密钥对)](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html) 来访问 AWS 服务。我们假设我们正在使用一个 `mykey` 的 key(键)和相应的权限文件 `mypem.pem`
我们还需要 [AWS credentials](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html),其中包括 `ACCESS_KEY_ID``SECRET_ACCESS_KEY`
最后,我们需要一个 S3 bucket(桶)来托管数据和模型, `s3://mybucket/`
## 设置 Hadoop YARN 集群
本节将介绍如何从头开始部署 Hadoop YARN 集群。 如果你已经有一个部署好的 Hadoop YARN 集群,那么你可以直接跳过这一步。 我们将使用 [yarn-ec2](https://github.com/tqchen/yarn-ec2) 来启动集群。
我们可以先使用下面的命令来 clone yarn-ec2 脚本
```
git clone https://github.com/tqchen/yarn-ec2
```
要使用该脚本,我们必须将环境变量 `AWS_ACCESS_KEY_ID``AWS_SECRET_ACCESS_KEY` 设置正确。这可以通过在 `~/.bashrc` 中添加以下两行来完成(用正确的替换字符串)
```
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```
现在我们可以从 EC2 启动集群的 master 了
```
./yarn-ec2 -k mykey -i mypem.pem launch xgboost
```
等待几分钟,直到 master 启动完成。
master 机器启动之后,可以使用以下命令查询 master 机器的公共 DNS 。
```
./yarn-ec2 -k mykey -i mypem.pem get-master xgboost
```
它会显示 master 机器的公共 DNS 就像下面这样 `ec2-xx-xx-xx.us-west-2.compute.amazonaws.com` 现在我们可以打开浏览器,输入(用 master DNS 替换 DNS)
```
ec2-xx-xx-xx.us-west-2.compute.amazonaws.com:8088
```
这将显示 YARN 集群的 job tracker 。需要注意的是,在 master 完成引导和启动 job tracker 之前我们可能需要等待几分钟。
在 master 机器启动后,我们可以自由添加更多的 slave 机器到集群中。 以下的命令将 m3.xlarge 实例添加到集群中了。
```
./yarn-ec2 -k mykey -i mypem.pem -t m3.xlarge -s 2 addslave xgboost
```
我们也可以选择添加两个 spot 实例
```
./yarn-ec2 -k mykey -i mypem.pem -t m3.xlarge -s 2 addspot xgboost
```
slave 机器将启动,引导并向 master 报告。 你可以单击 job tracker 上的 Nodes 链接来检查 slave 机器是否已连接。 或者只需输入以下 URL(将 DNS 替换为 master DNS)
```
ec2-xx-xx-xx.us-west-2.compute.amazonaws.com:8088/cluster/nodes
```
我们需要注意的一件事情是,并不是 job tracker 中的所有的链接都会起作用。 这是由于它们中的许多使用 AWS 的私有 IP,只能由 EC2 访问。 我们可以使用 ssh 代理来访问这些包。 现在我们已经建立了一个 master 和两个 slaves 的集群。我们准备好运行这个实验。
## 使用 S3 构建 XGBoost
我们可以通过以下命令登录到 master 服务器上。
```
./yarn-ec2 -k mykey -i mypem.pem login xgboost
```
我们将使用 S3 来托管数据和结果模型,因此在集群关闭后数据不会丢失。 要做到这一点,我们需要构建 S3 支持的 xgboost 。我们唯一需要做的就是设置 `USE_S3` 变量为 true 。这可以通过以下的命令来实现。
```
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost
cp make/config.mk config.mk
echo "USE_S3=1" >> config.mk
make -j4
```
现在我们已经构建了 S3 支持的 XGBoost 。如果您打算在 HDFS 上存储数据,您也可以启用 HDFS 支持,只需要打开 `USE_HDFS` 就可以了。
XGBoost 也依赖环境变量来访问 S3,所以你需要在 `~/.bashrc` 中添加以下两行(用正确的字符串替换)在 master 机器上也是如此。
```
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export BUCKET=mybucket
```
## 在 S3 上托管数据
在这个例子中,我们将把 xgboost 中的示例数据集复制到 S3 bucket(存储桶)中作为输入。 在正常使用情况下,数据集通常是从 existing distributed processing pipeline (现有的分布式处理 pipeline)创建的。 我们可以使用 [s3cmd](http://s3tools.org/s3cmd) 将数据复制到 mybucket(用真实的 bucket 名称替换 ${BUCKET}) 。
```
cd xgboost
s3cmd put demo/data/agaricus.txt.train s3://${BUCKET}/xgb-demo/train/
s3cmd put demo/data/agaricus.txt.test s3://${BUCKET}/xgb-demo/test/
```
## 提交作业
现在一切准备就绪,我们可以将 xgboost 分布式作业提交到 YARN 集群了。 我们将使用 [dmlc-submit](https://github.com/dmlc/dmlc-core/tree/master/tracker) 脚本来提交作业。
现在我们可以在 distributed training folder(分布式训练文件夹)(用真实的 bucket 名称替换 ${BUCKET}) 中运行以下脚本。
```
cd xgboost/demo/distributed-training
# Use dmlc-submit to submit the job.
../../dmlc-core/tracker/dmlc-submit --cluster=yarn --num-workers=2 --worker-cores=2\
../../xgboost mushroom.aws.conf nthread=2\
data=s3://${BUCKET}/xgb-demo/train\
eval[test]=s3://${BUCKET}/xgb-demo/test\
model_dir=s3://${BUCKET}/xgb-demo/model
```
所有配置如 `data``model_dir` 也可以直接写入配置文件。 请注意,我们只指定文件的文件夹路径,而不是文件名。 XGBoost 将读取该文件夹下的所有文件作为训练和评估数据。
在这个命令中,我们使用了 2 个 workers,每个 worker 使用两个正在运行的线程。 XGBoost 可以从每个 worker 使用多个 cores 中受益。 工作 cores 的常见的选择范围从 4 到 8 。 训练好的模型将被保存到指定的模型文件夹中。您可以浏览模型文件夹。
```
s3cmd ls s3://${BUCKET}/xgb-demo/model/
```
以下是分布式训练的输出示例。
```
16/02/26 05:41:59 INFO dmlc.Client: jobname=DMLC[nworker=2]:xgboost,username=ubuntu
16/02/26 05:41:59 INFO dmlc.Client: Submitting application application_1456461717456_0015
16/02/26 05:41:59 INFO impl.YarnClientImpl: Submitted application application_1456461717456_0015
2016-02-26 05:42:05,230 INFO @tracker All of 2 nodes getting started
2016-02-26 05:42:14,027 INFO [05:42:14] [0] test-error:0.016139 train-error:0.014433
2016-02-26 05:42:14,186 INFO [05:42:14] [1] test-error:0.000000 train-error:0.001228
2016-02-26 05:42:14,947 INFO @tracker All nodes finishes job
2016-02-26 05:42:14,948 INFO @tracker 9.71754479408 secs between node start and job finish
Application application_1456461717456_0015 finished with state FINISHED at 1456465335961
```
## 分析模型
模型训练后,我们可以分析学习的模型,并将其用于未来的预测任务。 XGBoost 是一个可移植的框架,所有平台的模型都是 **_exchangable(可交换)_** 。 这意味着我们可以在 python/R/Julia 中加载训练好的模型,并利用这些语言中的 data science pipelines (数据科学 pipeline)来做模型分析和预测。
例如,你可以使用 [这个 ipython notebook](https://github.com/dmlc/xgboost/tree/master/demo/distributed-training/plot_model.ipynb) 来绘制 feature importance (特征重要性)和可视化的学习模型。
## 故障排除
遇到问题的时候,最好的方法可能是使用以下命令获取容器的 stdout 和 stderr 的日志,以检查导致问题的原因。
```
yarn logs -applicationId yourAppId
```
## 未来发展方向
在本教程中,您已经学会了在 YARN 上使用分布式 XGBoost 。 XGBoost 是用于渐变增强的可移植和可伸缩框架。 您可以在 [资源页面](https://github.com/dmlc/xgboost/blob/master/demo/README.md) 中查看更多的示例和资源。
该项目的目标是为所有平台提供最佳的可扩展机器学习解决方案。 API 被设计为可移植的,相同的代码也可以在其他平台上运行,例如 MPI 和 SGE 。 XGBoost 正在积极发展,我们正在开发更多令人兴奋的功能,如分布式 xgboost python/R 包。查看 [路线图](https://github.com/dmlc/xgboost/issues/873) 来了解更多的细节并且欢迎你为这个项目你做出贡献。
\ No newline at end of file
# DART booster
[XGBoost](https://github.com/dmlc/xgboost) 主要是将大量带有较小的 Learning rate (学习率) 的回归树做了混合。 在这种情况下,在构造前期增加树的意义是非常显著的,而在后期增加树并不那么重要。
Rasmi 等人从深度神经网络社区提出了一种新的方法来增加 boosted trees 的 dropout 技术,并且在某些情况下能得到更好的结果。
这是一种新型树结构 booster `dart` 的使用指南。
## 原始论文
Rashmi Korlakai Vinayak, Ran Gilad-Bachrach. “DART: Dropouts meet Multiple Additive Regression Trees.” [JMLR](http://www.jmlr.org/proceedings/papers/v38/korlakaivinayak15.pdf)
## 特性
* 直接 drop 树来解决 over-fitting(过拟合)。
* Trivial trees 会被阻止(纠正微不足道的错误)。
由于训练过程中的引入的随机性,会有下面的几点区别。
* 训练可能会比 `gbtree` 慢,因为随机地 dropout 会禁止使用 prediction buffer (预测缓存区)。
* 由于随机性,提早停止可能会不稳定。
## 它是如何运行的
* 在第 ![](img/0.72/bac9598d3338ed61b7a118621dedea77.jpg) 轮训练中,假设 ![](img/0.72/4c8dff663d8cee873b593e54d56baccb.jpg) 个树被选定 drop 。
* 使用 ![](img/0.72/43354e497c557600517f0d5839d8c4e7.jpg) 作为 drop 的树的 leaf scores (叶子分数)和 ![](img/0.72/4451c509ff07f4326844859d1bbb7a56.jpg) 作为新树的 leaf scores (叶子分数)。
* 下面是目标函数 :
![](img/0.72/5ec429f9c4fa5c7660b705bcf8723ac9.jpg)
* ![](img/0.72/f3d01ce46b3fe38877c9cc0f7d12db03.jpg) 和 ![](img/0.72/c347c8efe6d5830b8c0cf3828e8275db.jpg) 是 overshooting (超调), 所以使用 scale factor (比例因子)
![](img/0.72/40168c626a126a5ece60aa03c2ef69b0.jpg)
## 参数
### booster
* `dart`
这个 booster 继承了 `gbtree` ,所以 `dart` 还有 `eta`, `gamma`, `max_depth` 等等参数。
其他的参数如下所示。
### sample_type
sampling (采样)算法的类型。
* `uniform`: (默认) drop 的树被统一选择。
* `weighted`: 根据 weights(权重)选择 drop 的树。
### normalize_type
normalization (归一化)算法的类型。
* `tree`: (默认) 新树与 drop 的树的 weight(权重)相同。
![](img/0.72/d9f69ea79be64a6a07e67d338aafe6aa.jpg)
* `forest`: 新树具有与 drop 的树(森林)的权重的总和相同的权重。
![](img/0.72/5aa5a49c3668860ed0ac3e6b62d3cc93.jpg)
### rate_drop
dropout 比率.
* 范围: [0.0, 1.0]
### skip_drop
跳过 dropout 的概率。
* 如果一个 dropout 被跳过了,新的树将会像 gbtree 一样被添加。
* 范围: [0.0, 1.0]
## 示例脚本
```
import xgboost as xgb
# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = {'booster': 'dart',
'max_depth': 5, 'learning_rate': 0.1,
'objective': 'binary:logistic', 'silent': True,
'sample_type': 'uniform',
'normalize_type': 'tree',
'rate_drop': 0.1,
'skip_drop': 0.5}
num_round = 50
bst = xgb.train(param, dtrain, num_round)
# make prediction
# ntree_limit must not be 0
preds = bst.predict(dtest, ntree_limit=num_round)
```
\ No newline at end of file
# XGBoost 入门指引
该页面包含使用和开发 XGBoost 的一些指导。
## 安装
* [如何安装 XGBoost](../build.html)
## 以特定的方式使用 XGBoost
* [参数调整指南](param_tuning.html)
* [使用大数据集的 out of core 计算](external_memory.html)
## 开发和破解 XGBoost
* [为 XGBoost 做贡献](contribute.html)
## 常见问题
* [FAQ](../faq.html)
\ No newline at end of file
# 安装指南
该页面提供了有关如何在各种操作系统上构建和安装 xgboost 软件包的说明. 由如下两个步骤组成:
1. 首先, 从 C++ 代码中构建共享库(针对 linux/osx 的是 `libxgboost.so` 然后针对 windows 的是 `libxgboost.dll`).
* Exception: 针对 R 软件包的安装请直接参考 R 软件包的部分.
2. 然后, 安装相关的编程语言软件包(例如. Python 软件包).
**_重要提示_** 最新版本的 xgboost 使用子模块来维护软件包, 所以当你 clone repo 时, 记得按如下方式使用递归选项.
```
git clone --recursive https://github.com/dmlc/xgboost
```
针对使用 github 工具的 windows 用户, 可以打开 git shell, 然后输入以下命令.
```
git submodule init
git submodule update
```
如果你在安装期间有任何问题, 请首先参考 [故障排除部分](#id5). 如果其中的说明不适合你, 请放心的到 [xgboost-doc-zh/issues](https://github.com/apachecn/xgboost-doc-zh/issues) 上提问题, 或者如果你能解决该问题, 最好发起一个 pull request.
## 页面内容目录
* [构建共享库](#id3)
* [在 Ubuntu/Debian 上构建](#ubuntu-debian)
* [在 OSX 上构建](#osx)
* [在 Windows 上构建](#windows)
* [自定义构建](#id4)
* [Python 软件包安装](#python)
* [R 软件包安装](#r)
* [故障排查](#id5)
## 构建共享库
我们的目标是构建共享库:
* 在 Linux/OSX 上的目标库是 `libxgboost.so`
* 在 Windows 上的目标库是 `libxgboost.dll`
最小的构建要求是
* 最新的支持 C++ 11 的 c++ 编译器(g++-4.6 or higher)
我们可以编辑 `make/config.mk` 以改变编译选项, 然后通过 `make` 命令来构建. 如果一切顺利, 我们可以去特定的编程语言安装部分.
### 在 Ubuntu/Debian 上构建
在 Ubuntu 上, 构建 xgboost 的命令如下
```
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost; make -j4
```
### 在 OSX 上构建
在 OSX 上, 构建 xgboost 的命令如下
```
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost; cp make/minimum.mk ./config.mk; make -j4
```
这样就可以在不使用多线程的情况下构建 xgboost 了, 因为默认情况下, OSX 中的 clang 不会带有 open-mp. 有关启用 OpenMP 的 xgboost, 请参阅以下段落.
以下是使用 OpenMP 的编译器来安装 XGBoost 的完整解决方案. 通过命令 `brew install gcc --without-multilib` 获得 gcc-5.x.x 与 openmap 的支持. (brew 是 OSX 上 `apt-get` 的标准组件. 因此不建议单独安装 [HPC](http://hpc.sourceforge.net/), 但是它应该也是可用的.)
```
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost; cp make/config.mk ./config.mk; make -j4
```
### 在 Windows 上构建
您在 clone repo 时需要首先使用递归选项来 clone 子模块. 如果使用的是 github 工具, 可以打开 github-shell, 然后输入以下命令. 我们推荐使用 [Windows 版本的 Git](https://git-for-windows.github.io/), 因为它自带了一个标准的 bash shell. 将极大地简化安装过程.
```
git submodule init
git submodule update
```
XGBoost 支持由 MSVC 或 MinGW 来构建. 以下是你如何使用 MinGW 构建 xgboost 库的说明.
在安装完 [Windows 版本的 Git](https://git-for-windows.github.io/) 之后, 应该有一个 `Git Bash` 的快捷方式. 以下所有的步骤都可以在 `Git Bash` 中进行.
在 MinGW 中, `make` 命令是 `mingw32-make` 这个名字. 你可以添加下面的行道 `.bashrc` 文件中去.
```
alias make='mingw32-make'
```
要使用 MinGW 来构建
```
cp make/mingw64.mk config.mk; make -j4
```
要使用 cmake 来与 Visual Studio 2013 构建. 确保你有最新版本的 cmake 并且已添加到你的 path 中. 然后从 xgboost 目录执行以下命令:
```
mkdir build
cd build
cmake .. -G"Visual Studio 12 2013 Win64"
```
This specifies an out of source build using the MSVC 12 64 bit generator. Open the .sln file in the build directory and build with Visual Studio. To use the Python module you can copy libxgboost.dll into python-package\xgboost.
这指定使用 MSVC 12 64 位生成器的源代码来构建. 在构建目录中打开 .sln 文件并使用 Visual Studio 进行构建. 要使用 Python 模块, 您可以将 libxgboost.dll 复制到 python-package\xgboost 目录下去.
其他版本的 Visual Studio 可能会工作, 但未经测试.
### 自定义构建
xgboost 的配置可以通过 `config.mk` 来进行修改
* 修改各种分布式文件系统上的配置, 如 HDFS/Amazon S3/ ...
* 首先, 复制 [make/config.mk](https://github.com/dmlc/xgboost/tree/master/doc/../make/config.mk) 到项目的根目录下, 任何本地的修改都将被 git 忽略, 然后修改相应的 flags(标记).
## Python 软件包的安装
python 软件包位于 [python-package](https://github.com/dmlc/xgboost/tree/master/doc/../python-package) 目录下. 以下是几种安装软件包的方法:
1. 系统范围的安装, 需要 root 权限
```
cd python-package; sudo python setup.py install
```
但是你需要 Python `distutils` 模块才能工作. 它通常是核心 python 软件包的一部分, 或者可以使用你的软件包管理器进行安装. 例如. 在 Debian 中使用以下命令
```
sudo apt-get install python-setuptools
```
_注意: 如果您重新编译了 xgboost, 那么您需要重新安装它才能使新库生效_
2. 只设置环境变量 `PYTHONPATH` 来让 python 知道在哪里可以找相关的库. 例如, 假设我们在主目录 `〜` 上克隆了 `xgboost`. 那么我们可以在 `〜/ .bashrc` 文件中加入下面这行. 这是 _推荐给哪些可能更改代码的开发者_ 的. 一旦你 pull(拉取)代码并且重构该项目(不需要再次调用 `setup`), 这些更改就会立即体现出来.
```
export PYTHONPATH=~/xgboost/python-package
```
3. 只为当前用户安装.
```
cd python-package; python setup.py develop --user
```
4. 如果您正在安装需要编译的最新的 xgboost 版本, 请将 MinGW 添加到系统的 PATH 中去:
```
import os
os.environ['PATH'] = os.environ['PATH'] + ';C:\\Program Files\\mingw-w64\\x86_64-5.3.0-posix-seh-rt_v4-rev0\\mingw64\\bin'
```
## R 软件包安装
您可以像安装其他软件包一样从 cran 来安装 R 软件包, 也可以从我们每周更新的 drat 软件仓库来进行安装:
```
install.packages("drat", repos="https://cran.rstudio.com")
drat:::addRepo("dmlc")
install.packages("xgboost", repos="http://dmlc.ml/drat/", type = "source")
```
如果您想使用最新的 xgboost 版本, 并且已经编译了 xgboost, 请使用 `library(devtools); install('xgboost/R-package')` 来手动的安装 xgboost 软件包(根据你编译的 xgboost 来恰当的更改 path)
对于 OSX 用户, 将安装单线程版本, 要安装多线程版本. 首先按照 [在 OSX 上构建](#在 OSX 上构建) 上的说明来获取 OpenMP 激活的编译器, 然后:
* 为 R 设置最高优先级的 `Makevars` 文件.
重点是, 有三种 `Makevars` : `~/.R/Makevars`, `xgboost/R-package/src/Makevars` 以及`/usr/local/Cellar/r/3.2.0/R.framework/Resources/etc/Makeconf` (最后一个通过在 R 中运行 `file.path(R.home("etc"), "Makeconf")` 来获得), 并且 `SHLIB_OPENMP_CXXFLAGS` 不是默认设置!! 在尝试后, 似乎第一个具有最高的优先级 (震精吧!).
然后在 R 中, 允许
```
install.packages("drat", repos="https://cran.rstudio.com")
drat:::addRepo("dmlc")
install.packages("xgboost", repos="http://dmlc.ml/drat/", type = "source")
```
由于子模块的使用, `install_github` 不再支持安装最新版本的 R 软件包. 要安装最新的版本, 可以运行下面的 bash 脚本,
```
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost
git submodule init
git submodule update
alias make='mingw32-make'
cd dmlc-core
make -j4
cd ../rabit
make lib/librabit_empty.a -j4
cd ..
cp make/mingw64.mk config.mk
make -j4
```
## 故障排查
1. **在 `git pull` 后编译失败**
请先 update the submodules, clean all 然后重新编译:
```
git submodule update && make clean_all && make -j4
```
1. **在修改 `config.mk` 配置文件后编译失败**
需要先 clean all:
```
make clean_all && make -j4
```
1. **Makefile: dmlc-core/make/dmlc.mk: No such file or directory**
我们需要递归的 clone 子模块, 您可以这样做:
```
git submodule init
git submodule update
```
Alternatively, do another clone
```
git clone https://github.com/dmlc/xgboost --recursive
```
\ No newline at end of file
# 参数调整注意事项
参数调整是机器学习中的一门暗艺术,模型的最优参数可以依赖于很多场景。所以要创建一个全面的指导是不可能的。
本文档试图为 xgboost 中的参数提供一些指导意见。
## 理解 Bias-Variance(偏差-方差)权衡
如果你了解一些机器学习或者统计课程,你会发现这可能是最重要的概念之一。 当我们允许模型变得更复杂(例如深度更深)时,模型具有更好的拟合训练数据的能力,会产生一个较少的偏差模型。 但是,这样复杂的模型需要更多的数据来拟合。
xgboost 中的大部分参数都是关于偏差方差的权衡的。最好的模型应该仔细地将模型复杂性与其预测能力进行权衡。 [参数文档](https://github.com/dmlc/xgboost/tree/master/doc/how_to/parameter.md) 会告诉你每个参数是否会使得模型更加 conservative (保守)与否。这可以帮助您在复杂模型和简单模型之间灵活转换。
## 控制过拟合
当你观察到训练精度高,但是测试精度低时,你可能遇到了过拟合的问题。
通常有两种方法可以控制 xgboost 中的过拟合。
* 第一个方法是直接控制模型的复杂度
* 这包括 `max_depth`, `min_child_weight``gamma`
* 第二种方法是增加随机性,使训练对噪声强健
* 这包括 `subsample`, `colsample_bytree`
* 你也可以减小步长 `eta`, 但是当你这么做的时候需要记得增加 `num_round`
## 处理不平衡的数据集
对于广告点击日志等常见情况,数据集是极不平衡的。 这可能会影响 xgboost 模型的训练,有两种方法可以改善它。
* 如果你只关心预测的排名顺序(AUC)
* 通过 `scale_pos_weight` 来平衡 positive 和 negative 权重。
* 使用 AUC 进行评估
* 如果你关心预测正确的概率
* 在这种情况下,您无法重新平衡数据集
* 在这种情况下,将参数 `max_delta_step` 设置为有限数字(比如说1)将有助于收敛
\ No newline at end of file
# 使用 XGBoost 外部存储器版本(测试版)
使用外部存储器版本和内存版本没有太大的区别。 唯一的区别是文件名格式。
外部存储器版本采用以下文件名格式
```
filename#cacheprefix
```
`filename` 是要加载的 libsvm 文件的正常路径, `cacheprefix` 是一个 xgboost 将用于外部内存缓存的缓存文件的路径。
下面的代码是从 [../demo/guide-python/external_memory.py](https://github.com/dmlc/xgboost/tree/master/doc/demo/guide-python/external_memory.py) 中提取的。
```
dtrain = xgb.DMatrix('../data/agaricus.txt.train#dtrain.cache')
```
你可以在 libsvm 文件后面找到 `#dtrain.cache` ,这是缓存文件的名称。 对于 CLI 版本,只需在文件名中使用 `"../data/agaricus.txt.train#dtrain.cache"`
## 性能说明
* 参数 `nthread` 应该设置为 **_real_** cores 的数量
* 大多数现代 CPU 提供 hyperthreading (超线程),这意味着你可以拥有 8 个线程的 4 核 CPU
* 在这种情况下,将 nthread 设置为 4 以获得更高性能
## 分布式版本
外部存储器模式很自然地适用于分布式版本,您可以简单地设置路径就像这样
```
data = "hdfs:///path-to-data/#dtrain.cache"
```
xgboost 会将数据缓存到本地位置。在 YARN 上运行时,当前文件夹是暂时的,这样你就可以直接使用 `dtrain.cache` 来缓存当前文件夹。
## 用法说明
* 这是一个实验版本
* 如果你想尝试测试,请将结果报告至 https://github.com/dmlc/xgboost/issues/244
* 目前只支持从 libsvm 格式导入
* 欢迎从其他常见的外部存储器数据源采集的贡献
\ No newline at end of file
# 贡献指南
> 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科)
可能有用的链接:
+ [0.72 英文文档](https://xgboost.readthedocs.io/en/release_0.72)
负责人:
* [那伊抹微笑](https://github.com/wangyangting): 1042658081
* [Peppa](https://github.com/chenyyx): 190442212
## 章节列表
+ [Introduction](README.md)
+ [开始使用 XGBoost](docs/1.md)
+ [XGBoost 教程](docs/2.md)
+ [Boosted Trees 介绍](docs/3.md)
+ [AWS 上的分布式 XGBoost YARN](docs/4.md)
+ [DART booster](docs/5.md)
+ [XGBoost 入门指引](docs/6.md)
+ 安装
+ [安装指南](docs/7.md)
+ 以特定的方式使用 XGBoost
+ [参数调整注意事项](docs/8.md)
+ [使用 XGBoost 外部存储器版本(测试版)](docs/9.md)
+ 开发和破解 XGBoost
+ [为 XGBoost 做贡献](docs/10.md)
+ [常见问题](docs/11.md)
+ [XGBoost Python Package](docs/12.md)
+ [Python 软件包介绍](docs/13.md)
+ [Python API 参考](docs/14.md)
+ [XGBoost 参数](docs/15.md)
## 流程
### 一、认领
首先查看[整体进度](https://github.com/apachecn/pytorch-doc-zh/issues/274),确认没有人认领了你想认领的章节。
然后回复 ISSUE,注明“章节 + QQ 号”(一定要留 QQ)。
### 二、翻译
可以合理利用翻译引擎(例如[谷歌](https://translate.google.cn/)),但一定要把它变得可读!
可以参照之前版本的中文文档,如果有用的话。
如果遇到格式问题,请随手把它改正。
### 三、提交
**提交的时候不要改动文件名称,即使它跟章节标题不一样也不要改,因为文件名和原文的链接是对应的!!!**
+ `fork` Github 项目
+ 将译文放在`docs/0.72`文件夹下
+ `push`
+ `pull request`
请见 [Github 入门指南](https://github.com/apachecn/kaggle/blob/master/docs/GitHub)
# Summary
+ [Introduction](README.md)
+ [开始使用 XGBoost](docs/1.md)
+ [XGBoost 教程](docs/2.md)
+ [Boosted Trees 介绍](docs/3.md)
+ [AWS 上的分布式 XGBoost YARN](docs/4.md)
+ [DART booster](docs/5.md)
+ [XGBoost 入门指引](docs/6.md)
+ 安装
+ [安装指南](docs/7.md)
+ 以特定的方式使用 XGBoost
+ [参数调整注意事项](docs/8.md)
+ [使用 XGBoost 外部存储器版本(测试版)](docs/9.md)
+ 开发和破解 XGBoost
+ [为 XGBoost 做贡献](docs/10.md)
+ [常见问题](docs/11.md)
+ [XGBoost Python Package](docs/12.md)
+ [Python 软件包介绍](docs/13.md)
+ [Python API 参考](docs/14.md)
+ [XGBoost 参数](docs/15.md)
# 贡献指南
> 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科)
可能有用的链接:
+ [0.72 中文文档](https://xgboost.apachecn.org/docs/0.72)
+ [0.72 英文文档](https://xgboost.readthedocs.io/en/release_0.72)
负责人:
* [1266](https://github.com/wangweitong): 1097828409
* [腻味](https://github.com/xxxx): 1185685810
## 章节列表
* [Introduction](README.md)
* [安装指南](https://xgboost.readthedocs.io/en/latest/build.html)
* [XGBoost入门](https://xgboost.readthedocs.io/en/latest/get_started.html)
* [XGBoost教程](https://xgboost.readthedocs.io/en/latest/tutorials/index.html)
* [Boosted Trees简介](https://xgboost.readthedocs.io/en/latest/tutorials/model.html)
* [使用AWS YARN分布式XGBoost](https://xgboost.readthedocs.io/en/latest/tutorials/aws_yarn.html)
* [使用XGBoost4J-Spark的分布式XGBoost](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [DART助推器](https://xgboost.readthedocs.io/en/latest/tutorials/dart.html)
* [单调约束](https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html)
* [XGBoost中的随机森林](https://xgboost.readthedocs.io/en/latest/tutorials/rf.html)
* [特征交互约束](https://xgboost.readthedocs.io/en/latest/tutorials/feature_interaction_constraint.html)
* [DMatrix的文本输入格式](https://xgboost.readthedocs.io/en/latest/tutorials/input_format.html)
* [参数调整注意事项](https://xgboost.readthedocs.io/en/latest/tutorials/param_tuning.html)
* [使用XGBoost外部存储器版本(测试版)](https://xgboost.readthedocs.io/en/latest/tutorials/external_memory.html)
* [自定义目标和评估指标](https://xgboost.readthedocs.io/en/latest/tutorials/custom_metric_obj.html)
* [经常问的问题](https://xgboost.readthedocs.io/en/latest/faq.html)
* [XGBoost用户论坛](https://discuss.xgboost.ai/)
* [GPU支持](https://xgboost.readthedocs.io/en/latest/gpu/index.html)
* [XGBoost参数](https://xgboost.readthedocs.io/en/latest/parameter.html)
* [Python包](https://xgboost.readthedocs.io/en/latest/python/index.html)
* [Python包介绍](https://xgboost.readthedocs.io/en/latest/python/python_intro.html)
* [Python API参考](https://xgboost.readthedocs.io/en/latest/python/python_api.html)
* [Python示例](https://github.com/dmlc/xgboost/tree/master/demo/guide-python)
* [R包](https://xgboost.readthedocs.io/en/latest/R-package/index.html)
* [R中的XGBoost简介](https://xgboost.readthedocs.io/en/latest/R-package/xgboostPresentation.html)
* [使用XGBoost了解您的数据集](https://xgboost.readthedocs.io/en/latest/R-package/discoverYourData.html)
* [JVM包](https://xgboost.readthedocs.io/en/latest/jvm/index.html)
* [XGBoost4J入门](https://xgboost.readthedocs.io/en/latest/jvm/java_intro.html)
* [XGBoost4J-Spark教程](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [代码示例](https://github.com/dmlc/xgboost/tree/master/jvm-packages/xgboost4j-example)
* [XGBoost4J Java API](https://xgboost.readthedocs.io/en/latest/jvm/javadocs/index.html)
* [XGBoost4J Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j/index.html)
* [XGBoost4J-Spark Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-spark/index.html)
* [XGBoost4J-Flink Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-flink/index.html)
* [Julia包](https://xgboost.readthedocs.io/en/latest/julia.html)
* [CLI界面](https://xgboost.readthedocs.io/en/latest/cli.html)
* [有助于XGBoost](https://xgboost.readthedocs.io/en/latest/contribute.html)
## 流程
### 一、认领
首先查看[整体进度](https://github.com/apachecn/pytorch-doc-zh/issues/274),确认没有人认领了你想认领的章节。
然后回复 ISSUE,注明“章节 + QQ 号”(一定要留 QQ)。
### 二、翻译
可以合理利用翻译引擎(例如[谷歌](https://translate.google.cn/)),但一定要把它变得可读!
可以参照之前版本的中文文档,如果有用的话。
如果遇到格式问题,请随手把它改正。
### 三、提交
**提交的时候不要改动文件名称,即使它跟章节标题不一样也不要改,因为文件名和原文的链接是对应的!!!**
+ `fork` Github 项目
+ 将译文放在`docs/0.90`文件夹下
+ `push`
+ `pull request`
请见 [Github 入门指南](https://github.com/apachecn/kaggle/blob/master/docs/GitHub)
# <center>XGBoost 中文文档</center>
<center>XGBoost是一个优化的分布式梯度增强库,旨在实现高效,灵活和便携。</center>
> 它在 [Gradient Boosting](https://en.wikipedia.org/wiki/Gradient_boosting) 框架下实现机器学习算法。XGBoost提供并行树提升(也称为GBDT,GBM),可以快速准确地解决许多数据科学问题。相同的代码在主要的分布式环境(Hadoop,SGE,MPI)上运行,并且可以解决数十亿个示例之外的问题。
<center>
![](docs/img/logo.png)
</center>
> 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远。
* [中文地址](https://xgboost.apachecn.org/)
* [英文地址](https://xgboost.ai/)
+ [ApacheCN 机器学习交流群 629470233](http://shang.qq.com/wpa/qunwpa?idkey=30e5f1123a79867570f665aa3a483ca404b1c3f77737bc01ec520ed5f078ddef)
+ [ApacheCN 学习资源](https://www.ibooker.org.cn/docs)
## 翻译进度
[**贡献指南**](CONTRIBUTING.md)
> 项目翻译 xgboost 0.90
* 贡献者: 记得留言和更新翻译进度
* 地址: https://github.com/apachecn/xgboost-doc-zh/issues/27
## 项目看板
> 项目 XGBoost 0.90 看板
* 负责人: 记得更新和优化
* 地址: https://github.com/apachecn/xgboost-doc-zh/projects/1
## 项目负责人
格式: GitHub + QQ
* [1266](https://github.com/wangweitong): 1097828409
* [腻味](https://github.com/xxxx): 1185685810
-- 负责人要求: (欢迎一起为 `XGBoost 中文版本` 做贡献)
* 热爱开源,喜欢装逼
* 长期使用 XGBoost
* 能够有时间及时优化页面bug和用户issues
* 试用期: 2个月
* 欢迎联系: [片刻](https://github.com/jiangzhonglian) 529815144
## 建议反馈
* 在我们的 [apachecn/xgboost-doc-zh](https://github.com/apachecn/xgboost-doc-zh) github 上提 issue.
* 发邮件到 Email: `apachecn@163.com`.
* 在我们的 [QQ群-搜索: 交流方式](https://github.com/apachecn/home) 中联系群主/管理员即可.
## 赞助我们
<img src="http://data.apachecn.org/img/about/donate.jpg" alt="微信&支付宝" />
# Summary
* [Introduction](README.md)
* [安装指南](https://xgboost.readthedocs.io/en/latest/build.html)
* [XGBoost入门](https://xgboost.readthedocs.io/en/latest/get_started.html)
* [XGBoost教程](https://xgboost.readthedocs.io/en/latest/tutorials/index.html)
* [Boosted Trees简介](https://xgboost.readthedocs.io/en/latest/tutorials/model.html)
* [使用AWS YARN分布式XGBoost](https://xgboost.readthedocs.io/en/latest/tutorials/aws_yarn.html)
* [使用XGBoost4J-Spark的分布式XGBoost](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [DART助推器](https://xgboost.readthedocs.io/en/latest/tutorials/dart.html)
* [单调约束](https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html)
* [XGBoost中的随机森林](https://xgboost.readthedocs.io/en/latest/tutorials/rf.html)
* [特征交互约束](https://xgboost.readthedocs.io/en/latest/tutorials/feature_interaction_constraint.html)
* [DMatrix的文本输入格式](https://xgboost.readthedocs.io/en/latest/tutorials/input_format.html)
* [参数调整注意事项](https://xgboost.readthedocs.io/en/latest/tutorials/param_tuning.html)
* [使用XGBoost外部存储器版本(测试版)](https://xgboost.readthedocs.io/en/latest/tutorials/external_memory.html)
* [自定义目标和评估指标](https://xgboost.readthedocs.io/en/latest/tutorials/custom_metric_obj.html)
* [经常问的问题](https://xgboost.readthedocs.io/en/latest/faq.html)
* [XGBoost用户论坛](https://discuss.xgboost.ai/)
* [GPU支持](https://xgboost.readthedocs.io/en/latest/gpu/index.html)
* [XGBoost参数](https://xgboost.readthedocs.io/en/latest/parameter.html)
* [Python包](https://xgboost.readthedocs.io/en/latest/python/index.html)
* [Python包介绍](https://xgboost.readthedocs.io/en/latest/python/python_intro.html)
* [Python API参考](https://xgboost.readthedocs.io/en/latest/python/python_api.html)
* [Python示例](https://github.com/dmlc/xgboost/tree/master/demo/guide-python)
* [R包](https://xgboost.readthedocs.io/en/latest/R-package/index.html)
* [R中的XGBoost简介](https://xgboost.readthedocs.io/en/latest/R-package/xgboostPresentation.html)
* [使用XGBoost了解您的数据集](https://xgboost.readthedocs.io/en/latest/R-package/discoverYourData.html)
* [JVM包](https://xgboost.readthedocs.io/en/latest/jvm/index.html)
* [XGBoost4J入门](https://xgboost.readthedocs.io/en/latest/jvm/java_intro.html)
* [XGBoost4J-Spark教程](https://xgboost.readthedocs.io/en/latest/jvm/xgboost4j_spark_tutorial.html)
* [代码示例](https://github.com/dmlc/xgboost/tree/master/jvm-packages/xgboost4j-example)
* [XGBoost4J Java API](https://xgboost.readthedocs.io/en/latest/jvm/javadocs/index.html)
* [XGBoost4J Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j/index.html)
* [XGBoost4J-Spark Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-spark/index.html)
* [XGBoost4J-Flink Scala API](https://xgboost.readthedocs.io/en/latest/jvm/scaladocs/xgboost4j-flink/index.html)
* [Julia包](https://xgboost.readthedocs.io/en/latest/julia.html)
* [CLI界面](https://xgboost.readthedocs.io/en/latest/cli.html)
* [有助于XGBoost](https://xgboost.readthedocs.io/en/latest/contribute.html)
\ No newline at end of file
此差异已折叠。
......@@ -22,15 +22,15 @@ Rashmi Korlakai Vinayak, Ran Gilad-Bachrach. “DART: Dropouts meet Multiple Add
## 它是如何运行的
* 在第 ![](img/bac9598d3338ed61b7a118621dedea77.jpg) 轮训练中,假设 ![](img/4c8dff663d8cee873b593e54d56baccb.jpg) 个树被选定 drop 。
* 使用 ![](img/43354e497c557600517f0d5839d8c4e7.jpg) 作为 drop 的树的 leaf scores (叶子分数)和 ![](img/4451c509ff07f4326844859d1bbb7a56.jpg) 作为新树的 leaf scores (叶子分数)。
* 在第 ![](img/0.72/bac9598d3338ed61b7a118621dedea77.jpg) 轮训练中,假设 ![](img/0.72/4c8dff663d8cee873b593e54d56baccb.jpg) 个树被选定 drop 。
* 使用 ![](img/0.72/43354e497c557600517f0d5839d8c4e7.jpg) 作为 drop 的树的 leaf scores (叶子分数)和 ![](img/0.72/4451c509ff07f4326844859d1bbb7a56.jpg) 作为新树的 leaf scores (叶子分数)。
* 下面是目标函数 :
![](img/5ec429f9c4fa5c7660b705bcf8723ac9.jpg)
![](img/0.72/5ec429f9c4fa5c7660b705bcf8723ac9.jpg)
* ![](img/f3d01ce46b3fe38877c9cc0f7d12db03.jpg) 和 ![](img/c347c8efe6d5830b8c0cf3828e8275db.jpg) 是 overshooting (超调), 所以使用 scale factor (比例因子)
* ![](img/0.72/f3d01ce46b3fe38877c9cc0f7d12db03.jpg) 和 ![](img/0.72/c347c8efe6d5830b8c0cf3828e8275db.jpg) 是 overshooting (超调), 所以使用 scale factor (比例因子)
![](img/40168c626a126a5ece60aa03c2ef69b0.jpg)
![](img/0.72/40168c626a126a5ece60aa03c2ef69b0.jpg)
## 参数
......@@ -55,11 +55,11 @@ normalization (归一化)算法的类型。
* `tree`: (默认) 新树与 drop 的树的 weight(权重)相同。
![](img/d9f69ea79be64a6a07e67d338aafe6aa.jpg)
![](img/0.72/d9f69ea79be64a6a07e67d338aafe6aa.jpg)
* `forest`: 新树具有与 drop 的树(森林)的权重的总和相同的权重。
![](img/5aa5a49c3668860ed0ac3e6b62d3cc93.jpg)
![](img/0.72/5aa5a49c3668860ed0ac3e6b62d3cc93.jpg)
### rate_drop
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册