决策树
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
决策森林模型由决策树组成。决策森林学习算法(例如随机森林)至少在部分上依赖于决策树的学习。
在本课程的这一部分中,您将研究一个小型示例数据集,并了解如何训练单个决策树。在下一部分中,您将了解如何组合决策树来训练决策森林。
在 YDF 中,使用 CART 学习器训练各个决策树模型:
# https://ydf.readthedocs.io/en/latest/py_api/CartLearner
import ydf
model = ydf.CartLearner(label="my_label").train(dataset)
模型
决策树是一种由一系列以树形结构分层排列的“问题”组成的模型。这些问题通常称为条件、分块或测试。我们将在此类中使用“条件”一词。每个非叶节点都包含一个条件,每个叶节点都包含一个预测结果。
植物树通常是从根部向下生长;然而,决策树通常是从顶部的根(第一个节点)向下生长。

图 1. 一个简单的分类决策树。绿色图例不属于决策树的一部分。
决策树模型的推理是通过根据条件将示例从根节点(位于顶部)路由到某个叶节点(位于底部)来计算得出的。所到达叶子的值就是决策树的预测结果。访问过的节点集称为推理路径。例如,请考虑以下地图项值:
预测结果为狗。推理路径将如下所示:
- num_legs ≥ 3 → 是
- num_eyes ≥ 3 → 否

图 2. 在示例 *{num_legs : 4, num_eyes : 2}* 中,以叶节点 *dog* 为终点的推理路径。
在前面的示例中,决策树的叶子包含分类预测;也就是说,每个叶子都包含一组可能的物种中的一种动物物种。
同样,决策树可以通过使用回归预测(数值)标记叶子来预测数值。例如,以下决策树会预测动物的可爱程度得分(介于 0 到 10 之间)。

图 3. 用于进行数值预测的决策树。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[[["\u003cp\u003eDecision forest models are built using decision trees, with algorithms like random forests relying on the learning of individual decision trees.\u003c/p\u003e\n"],["\u003cp\u003eA decision tree model uses a hierarchical structure of conditions to route an example from the root to a leaf node, where the leaf's value represents the prediction.\u003c/p\u003e\n"],["\u003cp\u003eDecision trees can be used for both classification tasks, predicting categories like animal species, and regression tasks, predicting numerical values like cuteness scores.\u003c/p\u003e\n"],["\u003cp\u003eYDF, a specific tool, utilizes the CART learner to train individual decision tree models.\u003c/p\u003e\n"]]],[],null,["# Decision trees\n\n\u003cbr /\u003e\n\nDecision forest models are composed of decision trees. Decision forest\nlearning algorithms (like random forests) rely, at least in part, on the\nlearning of decision trees.\n\nIn this section of the course, you will study a small example dataset, and learn\nhow a single decision tree is trained. In the next sections, you will learn how\ndecision trees are combined to train decision forests. \nYDF Code\n\nIn YDF, use the CART learner to train individual decision tree models: \n\n```python\n# https://ydf.readthedocs.io/en/latest/py_api/CartLearner\nimport ydf\nmodel = ydf.CartLearner(label=\"my_label\").train(dataset)\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThe model\n---------\n\nA **decision tree** is a model composed of a collection of \"questions\" organized\nhierarchically in the shape of a tree. The questions are usually called a\n**condition,** a **split** , or a **test.** We will use the term \"condition\" in\nthis class. Each non-leaf node contains a condition, and each leaf node contains\na prediction.\n\nBotanical trees generally grow with the root at the bottom; however, decision\ntrees are usually represented with the **root** (the first node) at the top.\n\n**Figure 1. A simple classification decision tree. The legend in green is not part\nof the decision tree.**\n\nInference of a decision tree model is computed by routing an example from the\nroot (at the top) to one of the leaf nodes (at the bottom) according to the\nconditions. The value of the reached leaf is the decision tree's prediction.\nThe set of visited nodes is called the **inference path**. For example,\nconsider the following feature values:\n\n| num_legs | num_eyes |\n|----------|----------|\n| 4 | 2 |\n\nThe prediction would be *dog*. The inference path would be:\n\n1. num_legs ≥ 3 → Yes\n2. num_eyes ≥ 3 → No\n\n**Figure 2. The inference path that culminates in the leaf \\*dog\\* on the example\n\\*{num_legs : 4, num_eyes : 2}\\*.**\n\nIn the previous example, the leaves of the decision tree contain classification\npredictions; that is, each leaf contains an animal species among a set\nof possible species.\n\nSimilarly, decision trees can predict numerical values by labeling leaves with\nregressive predictions (numerical values). For example, the following decision\ntree predicts a numerical cuteness score of an animal between 0 and 10.\n\n**Figure 3. A decision tree that makes numerical prediction.**"]]