{"id":47728,"date":"2024-12-02T16:59:26","date_gmt":"2024-12-02T08:59:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47728\/"},"modified":"2024-12-02T16:59:26","modified_gmt":"2024-12-02T08:59:26","slug":"php%e5%87%bd%e6%95%b0%e7%ae%97%e6%b3%95%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%ba%ba%e5%b7%a5%e6%99%ba%e8%83%bd%e4%b8%8e%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47728\/","title":{"rendered":"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u4eba\u5de5\u667a\u80fd\u6280\u5de7\uff1a1. \u5229\u7528 AI \u7b97\u6cd5\u9009\u62e9\u548c\u8d85\u53c2\u6570\u4f18\u5316\u7b97\u6cd5\uff0c\u63d0\u9ad8\u7b97\u6cd5\u6548\u7387\u3002\u6027\u80fd\u8c03\u4f18\u6280\u5de7\uff1a2. \u5206\u6790\u65f6\u95f4\u590d\u6742\u5ea6\u8bc6\u522b\u6027\u80fd\u74f6\u9888\uff1b3. \u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u4f18\u5316\u5185\u5b58\uff1b4. \u5e76\u53d1\u7f16\u7a0b\u63d0\u9ad8\u9ad8\u8d1f\u8f7d\u8bf7\u6c42\u7684\u5904\u7406\u6027\u80fd\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4f18\u5316\u641c\u7d22\u5f15\u64ce\u51fd\u6570\uff0c\u901a\u8fc7\u4f7f\u7528\u54c8\u5e0c\u8868\u548c\u5e76\u53d1\u641c\u7d22\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729932690671cad929ab7a.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18\u63d2\u56fe\" alt=\"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5229\u7528\u4eba\u5de5\u667a\u80fd\u548c\u6027\u80fd\u8c03\u4f18<\/strong><\/p>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<p>\u4f18\u5316 PHP \u51fd\u6570\u7b97\u6cd5\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5229\u7528\u4eba\u5de5\u667a\u80fd\u548c\u6027\u80fd\u8c03\u4f18\u7684\u6280\u5de7\uff0c\u5e2e\u52a9\u60a8\u589e\u5f3a PHP \u51fd\u6570\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5229\u7528\u4eba\u5de5\u667a\u80fd<\/strong><\/p>\n<p><strong>1. \u7b97\u6cd5\u9009\u62e9<\/strong><\/p>\n<p>\u4eba\u5de5\u667a\u80fd\u7b97\u6cd5\u53ef\u4ee5\u6839\u636e\u7ed9\u5b9a\u7684\u6570\u636e\u96c6\u63a8\u8350\u6700\u5408\u9002\u7684\u7b97\u6cd5\u3002\u4f8b\u5982\uff0cXGBoost \u662f\u4e00\u4e2a\u6811\u5f62\u96c6\u6210\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u5206\u7c7b\u6216\u56de\u5f52\u6570\u636e\u96c6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 XGBoost \u7684 <code>train()<\/code> \u65b9\u6cd5\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528 <code>predict()<\/code> \u65b9\u6cd5\u6765\u8fdb\u884c\u9884\u6d4b\u3002<\/p>\n<pre>use XGBoost\\Classifier;\n\n$data = [\n    ['feature1' =&gt; 0.1, 'feature2' =&gt; 0.2, 'label' =&gt; 1],\n    ... \/\/ \u5176\u4ed6\u6570\u636e\u70b9\n];\n\n$model = new Classifier();\n$model-&gt;train($data);\n\n$new_data = ['feature1' =&gt; 0.3, 'feature2' =&gt; 0.4];\n$prediction = $model-&gt;predict($new_data);<\/pre>\n<p><strong>2. \u8d85\u53c2\u6570\u4f18\u5316<\/strong><\/p>\n<p>\u4eba\u5de5\u667a\u80fd\u7b97\u6cd5\u901a\u5e38\u5305\u542b\u9700\u8981\u8c03\u6574\u7684\u8d85\u53c2\u6570\u3002\u8d85\u53c2\u6570\u4f18\u5316\u7b97\u6cd5\u53ef\u4ee5\u81ea\u52a8\u641c\u7d22\u6700\u4f73\u8d85\u53c2\u6570\u7ec4\u5408\u3002\u4f8b\u5982\uff0cHyperopt \u662f\u4e00\u4e2a\u8d85\u53c2\u6570\u4f18\u5316\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u4f18\u5316\u7b97\u6cd5\u8d85\u53c2\u6570\u3002<\/p>\n<pre>use HyperoptPHP\\PhpGP\\GpHyperopt;\n\n$optimizer = new GpHyperopt();\n$search_space = [\n    'n_estimators' =&gt; hp.randint('min_n_estimators', 'max_n_estimators')\n];\n\n$best_params = $optimizer-&gt;minimize(fn($params) =&gt; xgboost_evaluate($params, $data), $search_space);<\/pre>\n<p><strong>\u6027\u80fd\u8c03\u4f18<\/strong><\/p>\n<p><strong>1. \u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n<p>\u5206\u6790\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8bc6\u522b\u6027\u80fd\u74f6\u9888\u3002\u4f8b\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0cn \u4e3a\u6570\u7ec4\u5927\u5c0f\u3002\u5982\u679c\u51fd\u6570\u904d\u5386\u591a\u4e2a\u5d4c\u5957\u6570\u7ec4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5448\u6307\u6570\u7ea7\u589e\u957f\u3002<\/p>\n<p><strong>2. \u5185\u5b58\u4f18\u5316<\/strong><\/p>\n<p>\u4f7f\u7528\u5927\u6570\u636e\u7ed3\u6784\uff0c\u5982\u6570\u7ec4\u6216\u54c8\u5e0c\u8868\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u8003\u8651\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6216\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u7ba1\u7406\u5185\u5b58\u3002<\/p>\n<p><strong>3. \u5e76\u53d1\u7f16\u7a0b<\/strong><\/p>\n<p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u534f\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u5904\u7406\u9ad8\u8d1f\u8f7d\u8bf7\u6c42\u65f6\u51fd\u6570\u7684\u6027\u80fd\u3002PHP \u63d0\u4f9b\u4e86 <code>Parallel<\/code> \u7c7b\u6765\u652f\u6301\u5e76\u53d1\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u5904\u7406\u5927\u91cf\u6587\u672c\u6587\u6863\u4e14\u9700\u8981\u9ad8\u6027\u80fd\u7684\u641c\u7d22\u5f15\u64ce\u3002<\/p>\n<pre>function search_documents($query, $documents) {\n    foreach ($documents as $document) {\n        if (stripos($document, $query) !== false) {\n            return $document;\n        }\n    }\n\n    return null;\n}<\/pre>\n<p><strong>\u4f18\u5316 1\uff1a\u4f7f\u7528\u54c8\u5e0c\u8868<\/strong><\/p>\n<pre>$hash_table = [];\nforeach ($documents as $document) {\n    $hash_table[$document] = true;\n}\n\nfunction search_documents($query, $hash_table) {\n    return isset($hash_table[$query]) ? $query : null;\n}<\/pre>\n<p><strong>\u4f18\u5316 2\uff1a\u5e76\u53d1\u641c\u7d22<\/strong><\/p>\n<pre>function search_documents($query, $documents) {\n    $results = new SplQueue();\n\n    Parallel::each($documents, function($document) use ($query, $results) {\n        if (stripos($document, $query) !== false) {\n            $results-&gt;enqueue($document);\n        }\n    });\n\n    if ($results-&gt;isEmpty()) {\n        return null;\n    }\n\n    return $results-&gt;dequeue();\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18 \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u4eba\u5de5\u667a\u80fd\u4e0e\u6027\u80fd\u8c03\u4f18\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u4eba\u5de5\u667a\u80fd\u6280\u5de7\uff1a1. \u5229\u7528 AI \u7b97\u6cd5\u9009\u62e9\u548c\u8d85\u53c2\u6570\u4f18\u5316\u7b97\u6cd5\uff0c\u63d0\u9ad8\u7b97\u6cd5\u6548\u7387\u3002\u6027\u80fd\u8c03\u4f18\u6280\u5de7\uff1a2. \u5206\u6790\u65f6\u95f4\u590d\u6742\u5ea6\u8bc6\u522b\u6027\u80fd\u74f6\u9888\uff1b3. \u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u4f18\u5316\u5185\u5b58\uff1b4. \u5e76\u53d1\u7f16\u7a0b\u63d0\u9ad8\u9ad8\u8d1f\u8f7d\u8bf7\u6c42\u7684\u5904\u7406\u6027\u80fd\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4f18\u5316\u641c\u7d22\u5f15\u64ce\u51fd\u6570\uff0c\u901a\u8fc7\u4f7f\u7528\u54c8\u5e0c\u8868\u548c\u5e76\u53d1\u641c\u7d22\u63d0\u9ad8\u6027\u80fd\u3002 PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5229\u7528\u4eba\u5de5\u667a\u80fd\u548c\u6027\u80fd\u8c03\u4f18 \u6982\u8ff0 \u4f18\u5316 PHP \u51fd\u6570\u7b97\u6cd5\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5229\u7528\u4eba\u5de5\u667a\u80fd\u548c\u6027\u80fd\u8c03\u4f18\u7684\u6280\u5de7\uff0c\u5e2e\u52a9\u60a8\u589e\u5f3a PHP \u51fd\u6570\u7684\u6548\u7387\u3002 \u5229\u7528\u4eba\u5de5\u667a\u80fd 1. \u7b97\u6cd5\u9009\u62e9 \u4eba\u5de5\u667a\u80fd\u7b97\u6cd5\u53ef\u4ee5\u6839\u636e\u7ed9\u5b9a\u7684\u6570\u636e\u96c6\u63a8\u8350\u6700\u5408\u9002\u7684\u7b97\u6cd5\u3002\u4f8b\u5982\uff0cXGBoost \u662f\u4e00\u4e2a\u6811\u5f62\u96c6\u6210\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u5206\u7c7b\u6216\u56de\u5f52\u6570\u636e\u96c6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 XGBoost \u7684 train() \u65b9\u6cd5\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528 predict() \u65b9\u6cd5\u6765\u8fdb\u884c\u9884\u6d4b\u3002 use XGBoost\\Classifier; $data = [ [&#8216;feature1&#8217; =&gt; 0.1, &#8216;feature2&#8217; =&gt; 0.2, &#8216;label&#8217; =&gt; 1], &#8230; \/\/ \u5176\u4ed6\u6570\u636e\u70b9 ]; $model = new Classifier(); $model-&gt;train($data); $new_data = [&#8216;feature1&#8217; =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47728","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47728"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47728\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}