{"id":305,"date":"2024-07-28T09:50:35","date_gmt":"2024-07-28T01:50:35","guid":{"rendered":"https:\/\/www.xuzhe.tj.cn\/?p=305"},"modified":"2025-05-03T09:22:40","modified_gmt":"2025-05-03T01:22:40","slug":"%e9%80%92%e5%bd%92%e5%9c%a8%e5%ae%9e%e7%8e%b0%e5%86%b3%e7%ad%96%e6%a0%91%e7%ae%97%e6%b3%95%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.xuzhe.tj.cn\/index.php\/2024\/07\/28\/%e9%80%92%e5%bd%92%e5%9c%a8%e5%ae%9e%e7%8e%b0%e5%86%b3%e7%ad%96%e6%a0%91%e7%ae%97%e6%b3%95%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"\u51b3\u7b56\u6811\u7b97\u6cd5\u7684\u7f16\u7a0b\u5b9e\u73b0 \uff5c \u9012\u5f52\u7684\u7528\u6cd5"},"content":{"rendered":"\n<p>\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5206\u7c7b\u548c\u56de\u5f52\u4efb\u52a1\u4e2d\u3002\u9012\u5f52\u5728\u51b3\u7b56\u6811\u7684\u5b9e\u73b0\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6784\u5efa\u548c\u4f7f\u7528\u51b3\u7b56\u6811\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51b3\u7b56\u6811\u5b9e\u73b0\u7b97\u6cd5\u4e2d\u7684\u9012\u5f52\uff0c\u91cd\u70b9\u8bb2\u89e3\u57fa\u7ebf\u6761\u4ef6\u4e0e\u9012\u5f52\u8c03\u7528\u7684\u5b9e\u73b0\u3002<\/p>\n\n<!--more-->\n\n<h2 class=\"wp-block-heading\">\u9012\u5f52<\/h2>\n\n\n<h3 class=\"wp-block-heading\">\u9012\u5f52\u7684\u542b\u4e49<\/h3>\n\n\n<p>\u9012\u5f52\u662f\u6307\u51fd\u6570\u5728\u5176\u5b9a\u4e49\u4e2d\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u51fd\u6570\u901a\u5e38\u5305\u542b\u4e24\u4e2a\u90e8\u5206\uff1a\u57fa\u7ebf\u6761\u4ef6\u548c\u9012\u5f52\u8c03\u7528\u3002\u57fa\u7ebf\u6761\u4ef6\u7528\u4e8e\u7ec8\u6b62\u9012\u5f52\uff0c\u9632\u6b62\u65e0\u9650\u9012\u5f52\uff1b\u9012\u5f52\u8c03\u7528\u5219\u5904\u7406\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u9010\u6b65\u903c\u8fd1\u57fa\u7ebf\u6761\u4ef6\u3002<\/p>\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u7ebf\u6761\u4ef6\u7684\u91cd\u8981\u6027<\/h3>\n\n\n<p>\u57fa\u7ebf\u6761\u4ef6\u5728\u9012\u5f52\u7b97\u6cd5\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\">\n    <li>\u9632\u6b62\u65e0\u9650\u9012\u5f52\uff1a\u57fa\u7ebf\u6761\u4ef6\u786e\u4fdd\u9012\u5f52\u5728\u67d0\u4e2a\u70b9\u4e0a\u505c\u6b62\uff0c\u4ece\u800c\u9632\u6b62\u65e0\u9650\u9012\u5f52\u3002<\/li>\n    <li>\u63d0\u4f9b\u76f4\u63a5\u89e3\uff1a\u57fa\u7ebf\u6761\u4ef6\u901a\u5e38\u5bf9\u5e94\u4e8e\u95ee\u9898\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u6216\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u8fd9\u4e9b\u60c5\u51b5\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u89e3\u3002<\/li>\n    <li>\u5728\u51b3\u7b56\u6811\u7684\u6784\u5efa\u548c\u9884\u6d4b\u8fc7\u7a0b\u4e2d\uff0c\u57fa\u7ebf\u6761\u4ef6\u786e\u4fdd\u9012\u5f52\u5728\u9002\u5f53\u7684\u6761\u4ef6\u4e0b\u505c\u6b62\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u5b8c\u6574\u7684\u51b3\u7b56\u6811\u5e76\u8fdb\u884c\u6b63\u786e\u7684\u9884\u6d4b\u3002<br><\/li>\n<\/ul>\n\n\n<h3 class=\"wp-block-heading\">\u9012\u5f52\u4e0e\u8fed\u4ee3\u7684\u533a\u522b\u4e0e\u8054\u7cfb<\/h3>\n\n\n<p>\u8fed\u4ee3\u548c\u9012\u5f52\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u867d\u7136\u5b83\u4eec\u5728\u529f\u80fd\u4e0a\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\uff0c\u4f46\u5728\u5b9e\u73b0\u65b9\u5f0f\u548c\u5e94\u7528\u573a\u666f\u4e0a\u6709\u4e00\u4e9b\u663e\u8457\u7684\u533a\u522b\u3002\u4ee5\u4e0b\u662f\u8fed\u4ee3\u548c\u9012\u5f52\u7684\u4e3b\u8981\u533a\u522b\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\">\n    <li>\u8fed\u4ee3\uff1a\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\uff08for \u6216 while \u5faa\u73af\uff09\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u901a\u5e38\u5360\u7528\u8f83\u5c11\u7684\u5185\u5b58\uff0c\u9002\u7528\u4e8e\u5faa\u73af\u6b21\u6570\u5df2\u77e5\u7684\u60c5\u51b5\u3002<\/li>\n    <li>\u9012\u5f52\uff1a\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u9002\u7528\u4e8e\u53ef\u4ee5\u5206\u89e3\u4e3a\u76f8\u540c\u5b50\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u4f46\u53ef\u80fd\u5360\u7528\u8f83\u591a\u7684\u5185\u5b58\u3002<br><\/li>\n<\/ul>\n\n\n<p>\u793a\u4f8b\u4ee3\u7801<\/p>\n\n\n<p>\u8fed\u4ee3\u793a\u4f8b\uff1a\u8ba1\u7b97\u9636\u4e58<\/p>\n\n\n<pre class=\"wp-block-code\"><code>int factorial_iterative(int n) {\n    int result = 1;\n    for (int i = 1; i &lt;= n; ++i) {\n        result *= i;\n    }\n    return result;\n}\n<\/code><\/pre>\n\n\n<p>\u9012\u5f52\u793a\u4f8b\uff1a\u8ba1\u7b97\u9636\u4e58<\/p>\n\n\n<pre class=\"wp-block-code\"><code>int factorial_recursive(int n) {\n    if (n == 0) {\n        return 1;\n    }\n    return n * factorial_recursive(n - 1);\n}\n<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\">\u51b3\u7b56\u6811<\/h2>\n\n\n<h3 class=\"wp-block-heading\">\u51b3\u7b56\u6811\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n<p>\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u6811\u72b6\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u8868\u793a\u4e00\u4e2a\u7279\u5f81\uff0c\u6bcf\u4e2a\u5206\u652f\u8868\u793a\u7279\u5f81\u7684\u4e00\u4e2a\u53ef\u80fd\u53d6\u503c\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u8868\u793a\u4e00\u4e2a\u7c7b\u522b\u6216\u56de\u5f52\u503c\u3002\u51b3\u7b56\u6811\u7684\u6784\u5efa\u8fc7\u7a0b\u5305\u62ec\u9009\u62e9\u6700\u4f73\u7279\u5f81\u8fdb\u884c\u5206\u88c2\uff0c\u5e76\u9012\u5f52\u5730\u6784\u5efa\u5b50\u6811\uff1b\u9884\u6d4b\u8fc7\u7a0b\u5219\u901a\u8fc7\u9012\u5f52\u904d\u5386\u6811\u7ed3\u6784\uff0c\u6839\u636e\u7279\u5f81\u503c\u8fdb\u884c\u5206\u7c7b\u6216\u56de\u5f52\u3002<\/p>\n\n\n<h3 class=\"wp-block-heading\">\u51b3\u7b56\u6811\u7684\u7c7b\u578b<\/h3>\n\n\n<p>Hunt \u7b97\u6cd5\u4e8e 20 \u4e16\u7eaa 60 \u5e74\u4ee3\u63d0\u51fa\uff0c\u662f\u51b3\u7b56\u6811\u6784\u5efa\u7684\u4e00\u79cd\u57fa\u672c\u65b9\u6cd5\u3002\u539f\u59cb\u7684 Hunt \u7b97\u6cd5\u6ca1\u6709\u6307\u5b9a\u5177\u4f53\u7684\u9009\u62e9\u6807\u51c6\uff0c\u8fd9\u4e3a\u540e\u7eed\u7b97\u6cd5\u7684\u6539\u8fdb\u7559\u4e0b\u4e86\u7a7a\u95f4\uff0c\u4f8b\u5982\uff1a <\/p>\n\n\n<ul class=\"wp-block-list\">\n    <li>ID3\uff1a \u8be5\u7b97\u6cd5\u7531Ross Quinlan\u4e8e1986 \u5e74\u63d0\u51fa\uff0c\u5168\u79f0\u4e3a&#8221;\u8fed\u4ee3\u4e8c\u53c9\u6811 3 \u4ee3&#8221; (&#8220;Iterative Dichotomiser 3&#8221;)\u3002 \u8be5\u7b97\u6cd5\u5229\u7528\u4fe1\u606f\u71b5\u4e0e\u4fe1\u606f\u589e\u76ca\u4f5c\u4e3a\u8bc4\u4f30\u5019\u9009\u62c6\u5206\u7684\u6307\u6807\u3002<br><\/li>\n    <li>C4.5\uff1a\u8be5\u7b97\u6cd5\u662f ID3 \u7684\u540e\u671f\u6269\u5c55\uff0c\u540c\u6837\u7531 Quinlan \u63d0\u51fa\u3002 \u5b83\u4f7f\u7528\u4fe1\u606f\u589e\u76ca\u6216\u589e\u76ca\u7387\u6765\u8bc4\u4f30\u51b3\u7b56\u6811\u4e2d\u7684\u5207\u5206\u70b9\u3002<br><\/li>\n    <li>CART\uff1a\u5168\u79f0\u662f&#8221;\u5206\u7c7b\u548c\u56de\u5f52\u201d\uff0c\u7531 Leo Breiman\u63d0\u51fa\u3002 \u8be5\u7b97\u6cd5\u901a\u5e38\u5229\u7528&#8221;\u57fa\u5c3c\u4e0d\u7eaf\u5ea6&#8221;\u6765\u786e\u5b9a\u62c6\u5206\u70b9\u3002 \u57fa\u5c3c\u4e0d\u7eaf\u5ea6\u8861\u91cf\u968f\u673a\u9009\u62e9\u7684\u5c5e\u6027\u88ab\u9519\u8bef\u5206\u7c7b\u7684\u9891\u7387\u3002 \u4f7f\u7528\u8be5\u8bc4\u4f30\u65b9\u6cd5\u65f6\uff0c\u57fa\u5c3c\u4e0d\u7eaf\u5ea6\u8d8a\u5c0f\u8d8a\u7406\u60f3\u3002<br><\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\">\u51b3\u7b56\u6811\u6784\u5efa\u4e2d\u7684\u9012\u5f52\u5b9e\u73b0<\/h2>\n\n\n<h3 class=\"wp-block-heading\">\u6b65\u9aa4<\/h3>\n\n\n<p>\u6784\u5efa\u51b3\u7b56\u6811\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n\n\n<ol class=\"wp-block-list\">\n    <li>\u68c0\u67e5\u57fa\u7ebf\u6761\u4ef6\uff1a\u5982\u679c\u6240\u6709\u6837\u672c\u5c5e\u4e8e\u540c\u4e00\u7c7b\u522b\uff0c\u6216\u8005\u6ca1\u6709\u53ef\u7528\u7279\u5f81\uff0c\u6216\u8005\u6837\u672c\u96c6\u4e3a\u7a7a\uff0c\u5219\u521b\u5efa\u53f6\u8282\u70b9\u5e76\u8fd4\u56de\u3002<\/li>\n    <li>\u9009\u62e9\u6700\u4f73\u5206\u88c2\u7279\u5f81\u548c\u9608\u503c\u3002<\/li>\n    <li>\u6839\u636e\u9009\u62e9\u7684\u7279\u5f81\u548c\u9608\u503c\u5c06\u6837\u672c\u5206\u6210\u5de6\u53f3\u4e24\u4e2a\u5b50\u96c6\u3002<\/li>\n    <li>\u9012\u5f52\u5730\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u3002<br><\/li>\n<\/ol>\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801<\/h3>\n\n\n<pre class=\"wp-block-code\"><code>std::unique_ptr&lt;TreeNode&gt; buildDecisionTree(std::vector&lt;Sample&gt; samples, std::vector&lt;bool&gt; usedFeatures) {\n    auto node = std::make_unique&lt;TreeNode&gt;();\n\n    \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u6240\u6709\u6837\u672c\u5c5e\u4e8e\u540c\u4e00\u7c7b\u522b\n    if (std::all_of(samples.begin(), samples.end(), [&amp;](const Sample&amp; s) { return s.label == samples[0].label; })) {\n        node-&gt;isLeaf = true;\n        node-&gt;label = samples[0].label;\n        return node;\n    }\n\n    \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u6ca1\u6709\u53ef\u7528\u7279\u5f81\u6216\u6837\u672c\u4e3a\u7a7a\n    if (std::all_of(usedFeatures.begin(), usedFeatures.end(), [](bool b) { return b; }) || samples.empty()) {\n        node-&gt;isLeaf = true;\n        std::map&lt;std::string, int&gt; labelCount;\n        for (const auto&amp; sample : samples) {\n            labelCount[sample.label]++;\n        }\n        node-&gt;label = std::max_element(labelCount.begin(), labelCount.end(),\n            [](const std::pair&lt;std::string, int&gt;&amp; a, const std::pair&lt;std::string, int&gt;&amp; b) {\n                return a.second &lt; b.second;\n            })-&gt;first;\n        return node;\n    }\n\n    \/\/ \u9009\u62e9\u6700\u4f73\u5206\u88c2\u7279\u5f81\u548c\u9608\u503c\n    int bestFeature;\n    double bestThreshold;\n    std::tie(bestFeature, bestThreshold) = selectBestFeatureAndThreshold(samples, usedFeatures);\n    node-&gt;featureIndex = bestFeature;\n    node-&gt;threshold = bestThreshold;\n    node-&gt;isLeaf = false;\n\n    \/\/ \u5206\u88c2\u6837\u672c\n    std::vector&lt;Sample&gt; leftSamples, rightSamples;\n    for (const auto&amp; sample : samples) {\n        if (sample.features[bestFeature] &lt;= bestThreshold) {\n            leftSamples.push_back(sample);\n        } else {\n            rightSamples.push_back(sample);\n        }\n    }\n\n    \/\/ \u9012\u5f52\u6784\u5efa\u5b50\u6811\n    usedFeatures[bestFeature] = true;\n    node-&gt;left = buildDecisionTree(leftSamples, usedFeatures);\n    node-&gt;right = buildDecisionTree(rightSamples, usedFeatures);\n\n    return node;\n}\n<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\">\u51b3\u7b56\u6811\u9884\u6d4b\u4e2d\u7684\u9012\u5f52\u5b9e\u73b0<\/h2>\n\n\n<h3 class=\"wp-block-heading\">\u6b65\u9aa4<\/h3>\n\n\n<p>\u9884\u6d4b\u8fc7\u7a0b\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n\n\n<ol class=\"wp-block-list\">\n    <li>\u68c0\u67e5\u57fa\u7ebf\u6761\u4ef6\uff1a\u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u5219\u8fd4\u56de\u8be5\u8282\u70b9\u7684\u6807\u7b7e\u3002<\/li>\n    <li>\u9012\u5f52\u8c03\u7528\uff1a\u6839\u636e\u6837\u672c\u7684\u7279\u5f81\u503c\u9009\u62e9\u5de6\u5b50\u6811\u6216\u53f3\u5b50\u6811\u8fdb\u884c\u9012\u5f52\u8c03\u7528\u3002<br><\/li>\n<\/ol>\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801<\/h3>\n\n\n<pre class=\"wp-block-code\"><code>std::string predict(const TreeNode* node, const Sample&amp; sample) const {\n    if (node-&gt;isLeaf) {\n        return node-&gt;label;\n    }\n\n    if (sample.features[node-&gt;featureIndex] &lt;= node-&gt;threshold) {\n        return predict(node-&gt;left.get(), sample);\n    } else {\n        return predict(node-&gt;right.get(), sample);\n    }\n}\n<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\">\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662fC++\u5b9e\u73b0\u7684C4.5 \u51b3\u7b56\u6811\u7b97\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u5206\u7c7b\u95ee\u9898\u3002\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86\u4fe1\u606f\u589e\u76ca\u6bd4\u6765\u9009\u62e9\u6700\u4f73\u5206\u88c2\u7279\u5f81\uff0c\u5e76\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u6cd5\u6784\u5efa\u51b3\u7b56\u6811\u3002\u6b64\u4ee3\u7801\u8f83\u4e3a\u7b80\u5355\uff0c\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5305\u62ec\uff1a\u7f3a\u4e4f\u526a\u679d\u529f\u80fd\u3001\u4ec5\u5904\u7406\u8fde\u7eed\u7279\u5f81\u3001\u6ca1\u6709\u5904\u7406\u7f3a\u5931\u503c\u3001\u6ca1\u6709\u8003\u8651\u7279\u5f81\u7684\u91cd\u8981\u6027\u3001\u6ca1\u6709\u5b9e\u73b0\u6a21\u578b\u7684\u6301\u4e45\u5316\u7b49\u3002<\/p>\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;memory&gt;\n#include &lt;vector&gt;\n#include &lt;map&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\n#include &lt;stdexcept&gt;\n#include &lt;iostream&gt;\n\nstruct Sample {\n    std::vector&lt;double&gt; features; \/\/ \u5047\u8bbe\u7279\u5f81\u662f\u6570\u503c\u578b\u7684\n    std::string label;\n};\n\nclass C45DecisionTree {\npublic:\n    struct TreeNode {\n        bool isLeaf;\n        std::string label;\n        int featureIndex;\n        double threshold;\n        std::unique_ptr&lt;TreeNode&gt; left;\n        std::unique_ptr&lt;TreeNode&gt; right;\n    };\n\n    C45DecisionTree() : root(nullptr) {}\n\n    void train(const std::vector&lt;Sample&gt;&amp; samples) {\n        if (samples.empty()) {\n            throw std::runtime_error(\"\u8bad\u7ec3\u6837\u672c\u4e3a\u7a7a\");\n        }\n        std::vector&lt;bool&gt; usedFeatures(samples[0].features.size(), false);\n        root = buildDecisionTree(samples, usedFeatures);\n    }\n\n    std::string predict(const Sample&amp; sample) const {\n        if (!root) {\n            throw std::runtime_error(\"\u51b3\u7b56\u6811\u5c1a\u672a\u8bad\u7ec3\");\n        }\n        return predict(root.get(), sample);\n    }\n\nprivate:\n    std::unique_ptr&lt;TreeNode&gt; root;\n\n    std::unique_ptr&lt;TreeNode&gt; buildDecisionTree(std::vector&lt;Sample&gt; samples, std::vector&lt;bool&gt; usedFeatures) {\n        auto node = std::make_unique&lt;TreeNode&gt;();\n\n        \/\/ \u5982\u679c\u6240\u6709\u6837\u672c\u5c5e\u4e8e\u540c\u4e00\u7c7b\u522b,\u521b\u5efa\u53f6\u8282\u70b9\n        if (std::all_of(samples.begin(), samples.end(), [&amp;](const Sample&amp; s) { return s.label == samples[0].label; })) {\n            node-&gt;isLeaf = true;\n            node-&gt;label = samples[0].label;\n            return node;\n        }\n\n        \/\/ \u5982\u679c\u6ca1\u6709\u53ef\u7528\u7279\u5f81\u6216\u6837\u672c\u4e3a\u7a7a,\u521b\u5efa\u53f6\u8282\u70b9\u5e76\u8fd4\u56de\u591a\u6570\u7c7b\n        if (std::all_of(usedFeatures.begin(), usedFeatures.end(), [](bool b) { return b; }) || samples.empty()) {\n            node-&gt;isLeaf = true;\n            std::map&lt;std::string, int&gt; labelCount;\n            for (const auto&amp; sample : samples) {\n                labelCount[sample.label]++;\n            }\n            node-&gt;label = std::max_element(labelCount.begin(), labelCount.end(),\n                [](const std::pair&lt;std::string, int&gt;&amp; a, const std::pair&lt;std::string, int&gt;&amp; b) {\n                    return a.second &lt; b.second;\n                })-&gt;first;\n            return node;\n        }\n\n        \/\/ \u9009\u62e9\u6700\u4f73\u5206\u88c2\u7279\u5f81\u548c\u9608\u503c\n        int bestFeature;\n        double bestThreshold;\n        std::tie(bestFeature, bestThreshold) = selectBestFeatureAndThreshold(samples, usedFeatures);\n        node-&gt;featureIndex = bestFeature;\n        node-&gt;threshold = bestThreshold;\n        node-&gt;isLeaf = false;\n\n        \/\/ \u6839\u636e\u9009\u62e9\u7684\u7279\u5f81\u548c\u9608\u503c\u5206\u88c2\u6837\u672c\n        std::vector&lt;Sample&gt; leftSamples, rightSamples;\n        for (const auto&amp; sample : samples) {\n            if (sample.features[bestFeature] &lt;= bestThreshold) {\n                leftSamples.push_back(sample);\n            } else {\n                rightSamples.push_back(sample);\n            }\n        }\n\n        \/\/ \u9012\u5f52\u6784\u5efa\u5b50\u6811\n        usedFeatures[bestFeature] = true;\n        node-&gt;left = buildDecisionTree(leftSamples, usedFeatures);\n        node-&gt;right = buildDecisionTree(rightSamples, usedFeatures);\n\n        return node;\n    }\n\n    std::pair&lt;int, double&gt; selectBestFeatureAndThreshold(const std::vector&lt;Sample&gt;&amp; samples, const std::vector&lt;bool&gt;&amp; usedFeatures) const {\n        int bestFeature = -1;\n        double bestThreshold = 0.0;\n        double bestGainRatio = -1;\n\n        for (int i = 0; i &lt; samples[0].features.size(); i++) {\n            if (!usedFeatures[i]) {\n                std::vector&lt;double&gt; thresholds;\n                for (const auto&amp; sample : samples) {\n                    thresholds.push_back(sample.features[i]);\n                }\n                std::sort(thresholds.begin(), thresholds.end());\n                for (int j = 1; j &lt; thresholds.size(); j++) {\n                    double threshold = (thresholds[j - 1] + thresholds[j]) \/ 2;\n                    double gainRatio = calculateGainRatio(samples, i, threshold);\n                    if (gainRatio &gt; bestGainRatio) {\n                        bestGainRatio = gainRatio;\n                        bestFeature = i;\n                        bestThreshold = threshold;\n                    }\n                }\n            }\n        }\n\n        return {bestFeature, bestThreshold};\n    }\n\n    double calculateGainRatio(const std::vector&lt;Sample&gt;&amp; samples, int featureIndex, double threshold) const {\n        double entropyBefore = calculateEntropy(samples);\n        std::vector&lt;Sample&gt; leftSamples, rightSamples;\n        for (const auto&amp; sample : samples) {\n            if (sample.features[featureIndex] &lt;= threshold) {\n                leftSamples.push_back(sample);\n            } else {\n                rightSamples.push_back(sample);\n            }\n        }\n\n        double entropyAfter = (leftSamples.size() * calculateEntropy(leftSamples) + rightSamples.size() * calculateEntropy(rightSamples)) \/ samples.size();\n        double informationGain = entropyBefore - entropyAfter;\n\n        double splitInfo = 0.0;\n        double pLeft = static_cast&lt;double&gt;(leftSamples.size()) \/ samples.size();\n        double pRight = static_cast&lt;double&gt;(rightSamples.size()) \/ samples.size();\n        if (pLeft &gt; 0) splitInfo -= pLeft * std::log2(pLeft);\n        if (pRight &gt; 0) splitInfo -= pRight * std::log2(pRight);\n\n        return (splitInfo != 0) ? informationGain \/ splitInfo : 0;\n    }\n\n    double calculateEntropy(const std::vector&lt;Sample&gt;&amp; samples) const {\n        std::map&lt;std::string, int&gt; labelCount;\n        for (const auto&amp; sample : samples) {\n            labelCount[sample.label]++;\n        }\n\n        double entropy = 0.0;\n        int totalSamples = samples.size();\n        for (const auto&amp; pair : labelCount) {\n            double probability = static_cast&lt;double&gt;(pair.second) \/ totalSamples;\n            entropy -= probability * std::log2(probability);\n        }\n        return entropy;\n    }\n\n    std::string predict(const TreeNode* node, const Sample&amp; sample) const {\n        if (node-&gt;isLeaf) {\n            return node-&gt;label;\n        }\n\n        if (sample.features[node-&gt;featureIndex] &lt;= node-&gt;threshold) {\n            return predict(node-&gt;left.get(), sample);\n        } else {\n            return predict(node-&gt;right.get(), sample);\n        }\n    }\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5206\u7c7b\u548c\u56de\u5f52\u4efb\u52a1\u4e2d\u3002&#8230;<\/p>\n","protected":false},"author":1,"featured_media":513,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,16],"tags":[8,29],"class_list":["post-305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","category-programming","tag-c","tag-decision-tree"],"_links":{"self":[{"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":6,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions\/514"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/media\/513"}],"wp:attachment":[{"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xuzhe.tj.cn\/index.php\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}