{"id":726,"date":"2025-07-07T09:46:50","date_gmt":"2025-07-07T01:46:50","guid":{"rendered":"https:\/\/blog.heartwarming.online\/?p=726"},"modified":"2025-07-07T09:49:55","modified_gmt":"2025-07-07T01:49:55","slug":"%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0knn%e7%ae%97%e6%b3%95-%e9%b8%a2%e5%b0%be%e8%8a%b1%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.heartwarming.online\/index.php\/2025\/07\/07\/%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0knn%e7%ae%97%e6%b3%95-%e9%b8%a2%e5%b0%be%e8%8a%b1%e6%a1%88%e4%be%8b\/","title":{"rendered":"\u673a\u5668\u5b66\u4e60KNN\u7b97\u6cd5\u2014\u2014\u9e22\u5c3e\u82b1\u6848\u4f8b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ee3\u7801<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u7684\u4ee3\u7801\u6765\u81ea\u9ed1\u9a6cb\u7ad9\u8bfe\u7a0b\uff0c\u6211\u8fd9\u91cc\u4e5f\u53ea\u662f\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf\u6211\u540e\u7eed\u81ea\u5df1\u590d\u4e60\uff0c\u6211\u4e5f\u662f\u501f\u52a9AI\u5e2e\u6211\u89e3\u91ca\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def knn_iris_gscv():\n    \"\"\"\n    \u7528KNN\u7b97\u6cd5\u5bf9\u9e22\u5c3e\u82b1\u8fdb\u884c\u5206\u7c7b\uff0c\u6dfb\u52a0\u7f51\u683c\u641c\u7d22\u548c\u4ea4\u53c9\u9a8c\u8bc1\n    :return:\n    \"\"\"\n    # 1\uff09\u83b7\u53d6\u6570\u636e\n    iris = load_iris()\n\n    # 2\uff09\u5212\u5206\u6570\u636e\u96c6\n    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)\n\n    # 3\uff09\u7279\u5f81\u5de5\u7a0b\uff1a\u6807\u51c6\u5316\n    transfer = StandardScaler()\n    x_train = transfer.fit_transform(x_train)\n    x_test = transfer.transform(x_test)\n\n    # 4\uff09KNN\u7b97\u6cd5\u9884\u4f30\u5668\n    estimator = KNeighborsClassifier()\n\n    # \u52a0\u5165\u7f51\u683c\u641c\u7d22\u4e0e\u4ea4\u53c9\u9a8c\u8bc1\n    # \u53c2\u6570\u51c6\u5907\n    param_dict = {\"n_neighbors\": &#91;1, 3, 5, 7, 9, 11]}\n    estimator = GridSearchCV(estimator, param_grid=param_dict, cv=10) # \u9a8c\u8bc1\u96c6\n    estimator.fit(x_train, y_train)\n\n    # 5\uff09\u6a21\u578b\u8bc4\u4f30\n    # \u65b9\u6cd51\uff1a\u76f4\u63a5\u6bd4\u5bf9\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c\n    y_predict = estimator.predict(x_test)\n    print(\"y_predict:\\n\", y_predict)\n    print(\"\u76f4\u63a5\u6bd4\u5bf9\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c:\\n\", y_test == y_predict)\n\n    # \u65b9\u6cd52\uff1a\u8ba1\u7b97\u51c6\u786e\u7387\n    score = estimator.score(x_test, y_test)\n    print(\"\u51c6\u786e\u7387\u4e3a\uff1a\\n\", score)\n\n    # \u6700\u4f73\u53c2\u6570\uff1abestparams\n    print(\"\u6700\u4f73\u53c2\u6570\uff1a\\n\", estimator.bestparams)\n    # \u6700\u4f73\u7ed3\u679c\uff1abestscore\n    print(\"\u6700\u4f73\u7ed3\u679c\uff1a\\n\", estimator.bestscore)\n    # \u6700\u4f73\u4f30\u8ba1\u5668\uff1abestestimator\n    print(\"\u6700\u4f73\u4f30\u8ba1\u5668:\\n\", estimator.bestestimator)\n    # \u4ea4\u53c9\u9a8c\u8bc1\u7ed3\u679c\uff1acvresults\n    print(\"\u4ea4\u53c9\u9a8c\u8bc1\u7ed3\u679c:\\n\", estimator.cvresults)\n\n    return None<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6574\u4f53\u601d\u8def<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c31\u50cf\u662f\u5728\u8bad\u7ec3\u4e00\u4e2a&#8221;\u690d\u7269\u4e13\u5bb6&#8221;\uff0c\u8ba9\u5b83\u5b66\u4f1a\u6839\u636e\u82b1\u7684\u7279\u5f81\u6765\u5224\u65ad\u662f\u54ea\u79cd\u9e22\u5c3e\u82b1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9010\u6b65\u89e3\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u83b7\u53d6\u6570\u636e &#8211; &#8220;\u51c6\u5907\u5b66\u4e60\u6750\u6599&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>iris = load_iris()<\/code><\/code><\/pre>\n\n\n\n<p>\u5c31\u50cf\u7ed9\u5b66\u751f\u51c6\u5907\u6559\u79d1\u4e66\u4e00\u6837\uff0c\u8fd9\u91cc\u52a0\u8f7d\u4e86\u7ecf\u5178\u7684\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u5305\u542b150\u6735\u82b1\u7684\u4fe1\u606f\uff0c\u6bcf\u6735\u82b1\u67094\u4e2a\u7279\u5f81\uff08\u82b1\u74e3\u957f\u5ea6\u3001\u5bbd\u5ea6\u7b49\uff09\u548c1\u4e2a\u6807\u7b7e\uff08\u82b1\u7684\u79cd\u7c7b\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5212\u5206\u6570\u636e\u96c6 &#8211; &#8220;\u5206\u914d\u7ec3\u4e60\u9898\u548c\u8003\u8bd5\u9898&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>x_train, x_test, y_train, y_test = train_test_split(...)<\/code><\/code><\/pre>\n\n\n\n<p>\u5c31\u50cf\u5b66\u4e60\u65f6\u8981\u628a\u9898\u76ee\u5206\u6210&#8221;\u7ec3\u4e60\u9898&#8221;\u548c&#8221;\u8003\u8bd5\u9898&#8221;\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ec3\u4e60\u9898\uff08\u8bad\u7ec3\u96c6\uff09\uff1a\u8ba9\u6a21\u578b\u5b66\u4e60\u7528\u7684<\/li>\n\n\n\n<li>\u8003\u8bd5\u9898\uff08\u6d4b\u8bd5\u96c6\uff09\uff1a\u6700\u540e\u68c0\u9a8c\u6a21\u578b\u5b66\u5f97\u600e\u4e48\u6837<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7279\u5f81\u5de5\u7a0b &#8211; &#8220;\u7edf\u4e00\u5ea6\u91cf\u6807\u51c6&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>transfer = StandardScaler()\nx_train = transfer.fit_transform(x_train)\nx_test = transfer.transform(x_test)<\/code><\/code><\/pre>\n\n\n\n<p>\u60f3\u8c61\u4f60\u8981\u6bd4\u8f83\u8eab\u9ad8\u548c\u4f53\u91cd\uff0c\u4e00\u4e2a\u662f\u5398\u7c73\uff0c\u4e00\u4e2a\u662f\u516c\u65a4\uff0c\u5355\u4f4d\u4e0d\u540c\u6ca1\u6cd5\u76f4\u63a5\u6bd4\u8f83\u3002\u6807\u51c6\u5316\u5c31\u662f\u628a\u6240\u6709\u7279\u5f81\u90fd\u8f6c\u6362\u6210\u76f8\u540c\u7684\u5c3a\u5ea6\uff0c\u8ba9\u6a21\u578b\u80fd\u516c\u5e73\u5730\u5bf9\u5f85\u6bcf\u4e2a\u7279\u5f81\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. KNN\u7b97\u6cd5 &#8211; &#8220;\u627e\u6700\u76f8\u4f3c\u7684\u90bb\u5c45&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>estimator = KNeighborsClassifier()<\/code><\/code><\/pre>\n\n\n\n<p>KNN\u5c31\u50cf\u662f&#8221;\u95ee\u90bb\u5c45&#8221;\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9047\u5230\u4e00\u6735\u65b0\u82b1\uff0c\u5c31\u627e\u6700\u76f8\u4f3c\u7684K\u4e2a\u90bb\u5c45<\/li>\n\n\n\n<li>\u770b\u8fd9\u4e9b\u90bb\u5c45\u5927\u591a\u6570\u662f\u4ec0\u4e48\u7c7b\u578b\uff0c\u65b0\u82b1\u5c31\u5224\u65ad\u4e3a\u4ec0\u4e48\u7c7b\u578b<\/li>\n\n\n\n<li>\u6bd4\u5982K=3\uff0c\u627e\u52303\u4e2a\u6700\u76f8\u4f3c\u7684\u82b1\uff0c\u5982\u679c2\u4e2a\u662fA\u7c7b\uff0c1\u4e2a\u662fB\u7c7b\uff0c\u90a3\u65b0\u82b1\u5c31\u662fA\u7c7b<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u7f51\u683c\u641c\u7d22 &#8211; &#8220;\u627e\u6700\u4f73\u8bbe\u7f6e&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>param_dict = {\"n_neighbors\": &#91;1, 3, 5, 7, 9, 11]}\nestimator = GridSearchCV(estimator, param_grid=param_dict, cv=10)<\/code><\/code><\/pre>\n\n\n\n<p>\u5c31\u50cf\u8c03\u8bd5\u76f8\u673a\u53c2\u6570\u4e00\u6837\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6211\u4eec\u4e0d\u77e5\u9053K\u503c\uff08\u90bb\u5c45\u6570\u91cf\uff09\u8bbe\u591a\u5c11\u6700\u597d<\/li>\n\n\n\n<li>\u6240\u4ee5\u628a\u53ef\u80fd\u7684\u503c\u90fd\u8bd5\u4e00\u904d\uff1a1\u4e2a\u90bb\u5c45\u30013\u4e2a\u90bb\u5c45\u30015\u4e2a\u90bb\u5c45&#8230;<\/li>\n\n\n\n<li>\u770b\u54ea\u4e2a\u8bbe\u7f6e\u62cd\u51fa\u7684\u7167\u7247\u6700\u6e05\u695a\uff08\u51c6\u786e\u7387\u6700\u9ad8\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u4ea4\u53c9\u9a8c\u8bc1 &#8211; &#8220;\u591a\u6b21\u8003\u8bd5\u6c42\u5e73\u5747&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>cv=10<\/code><\/code><\/pre>\n\n\n\n<p>\u5c31\u50cf\u4e00\u4e2a\u5b66\u751f\u8003\u8bd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u80fd\u53ea\u8003\u4e00\u6b21\u5c31\u8bf4\u4ed6\u6c34\u5e73\u5982\u4f55<\/li>\n\n\n\n<li>\u8981\u800310\u6b21\uff0c\u53d6\u5e73\u5747\u5206\u624d\u9760\u8c31<\/li>\n\n\n\n<li>\u4ea4\u53c9\u9a8c\u8bc1\u5c31\u662f\u628a\u8bad\u7ec3\u6570\u636e\u5206\u621010\u4efd\uff0c\u8f6e\u6d41\u505a9\u4efd\u8bad\u7ec3\u30011\u4efd\u9a8c\u8bc1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u6a21\u578b\u8bc4\u4f30 &#8211; &#8220;\u68c0\u9a8c\u5b66\u4e60\u6548\u679c&#8221;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>y_predict = estimator.predict(x_test)\nscore = estimator.score(x_test, y_test)<\/code><\/code><\/pre>\n\n\n\n<p>\u5c31\u50cf\u671f\u672b\u8003\u8bd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u4ece\u6ca1\u89c1\u8fc7\u7684\u6d4b\u8bd5\u6570\u636e\u6765\u68c0\u9a8c\u6a21\u578b<\/li>\n\n\n\n<li>\u770b\u9884\u6d4b\u7ed3\u679c\u548c\u771f\u5b9e\u7b54\u6848\u6709\u591a\u5c11\u4e00\u81f4<\/li>\n\n\n\n<li>\u8ba1\u7b97\u51c6\u786e\u7387<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6838\u5fc3\u539f\u7406<\/h2>\n\n\n\n<p><strong>KNN\u7684\u5de5\u4f5c\u539f\u7406<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u65b0\u6837\u672c\u4e0e\u6240\u6709\u8bad\u7ec3\u6837\u672c\u7684\u8ddd\u79bb<\/li>\n\n\n\n<li>\u627e\u51fa\u8ddd\u79bb\u6700\u8fd1\u7684K\u4e2a\u90bb\u5c45<\/li>\n\n\n\n<li>\u7edf\u8ba1\u8fd9K\u4e2a\u90bb\u5c45\u4e2d\u54ea\u4e2a\u7c7b\u522b\u6700\u591a<\/li>\n\n\n\n<li>\u628a\u65b0\u6837\u672c\u5206\u7c7b\u4e3a\u6700\u591a\u7684\u90a3\u4e2a\u7c7b\u522b<\/li>\n<\/ol>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u7f51\u683c\u641c\u7d22<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>K\u503c\u592a\u5c0f\uff1a\u5bb9\u6613\u53d7\u566a\u58f0\u5f71\u54cd\uff08\u6bd4\u5982K=1\uff0c\u53ea\u770b1\u4e2a\u90bb\u5c45\uff09<\/li>\n\n\n\n<li>K\u503c\u592a\u5927\uff1a\u53ef\u80fd\u5ffd\u7565\u5c40\u90e8\u7279\u5f81<\/li>\n\n\n\n<li>\u7f51\u683c\u641c\u7d22\u81ea\u52a8\u627e\u5230\u6700\u5408\u9002\u7684K\u503c<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u4ea4\u53c9\u9a8c\u8bc1<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u907f\u514d&#8221;\u8fd0\u6c14\u597d&#8221;\u7684\u60c5\u51b5<\/li>\n\n\n\n<li>\u786e\u4fdd\u6a21\u578b\u7684\u7a33\u5b9a\u6027<\/li>\n\n\n\n<li>\u66f4\u51c6\u786e\u5730\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801 \u8fd9\u91cc\u7684\u4ee3\u7801\u6765\u81ea\u9ed1\u9a6cb\u7ad9\u8bfe\u7a0b\uff0c\u6211\u8fd9\u91cc\u4e5f\u53ea\u662f\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf\u6211\u540e\u7eed\u81ea\u5df1\u590d\u4e60\uff0c\u6211\u4e5f\u662f\u501f\u52a9AI\u5e2e\u6211\u89e3\u91ca\u4e86\u3002 \u6574\u4f53\u601d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[57],"tags":[63,64],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-python","tag-63","tag-64"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":2,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/726\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}