{"id":3040,"date":"2022-02-20T22:06:34","date_gmt":"2022-02-20T14:06:34","guid":{"rendered":"https:\/\/egonlin.com\/?p=3040"},"modified":"2022-02-20T22:07:49","modified_gmt":"2022-02-20T14:07:49","slug":"%e7%ac%ac%e4%b8%89%e8%8a%82%ef%bc%9ascikit-learn%e5%ba%93%e4%b9%8bk%e8%bf%91%e9%82%bb%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=3040","title":{"rendered":"\u7b2c\u4e09\u8282\uff1ascikit-learn\u5e93\u4e4bk\u8fd1\u90bb\u7b97\u6cd5"},"content":{"rendered":"<h1>scikit-learn\u5e93\u4e4bk\u8fd1\u90bb\u7b97\u6cd5<\/h1>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2021\/08\/\u6076\u641e\u56fe02-16.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2021\/08\/\u6076\u641e\u56fe02-16.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u7531\u4e8ek\u8fd1\u90bb\u53ef\u4ee5\u505a\u56de\u5f52\u53c8\u53ef\u4ee5\u505a\u5206\u7c7b\uff0c\u6240\u4ee5\u6700\u666e\u901a\u7684k\u8fd1\u90bb\u7b97\u6cd5\u5728scikit-learn\u5e93\u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0\uff0c\u5373KNeighborsClassifier\u548cKNeighborsRegressor\uff1b\u4e0a\u6b21\u8bb2\u5230\u4e86k\u8fd1\u90bb\u7684\u4e24\u4e2a\u6269\u5c55\u9650\u5b9a\u534a\u5f84k\u8fd1\u90bb\uff0c\u56e0\u6b64\u8be5\u65b9\u6cd5\u5728scikit-learn\u4e2d\u4e5f\u6709\u4e24\u79cd\u5b9e\u73b0\uff0c\u5373RadiusNeighborsClassifier\u548cRadiusNeighborsRegressor\uff1bk\u8fd1\u90bb\u8fd8\u6709\u4e00\u79cd\u6269\u5c55\uff0c\u5373\u6700\u8fd1\u8d28\u5fc3\u5206\u7c7b\u7b97\u6cd5NearestCentroid\u3002<\/p>\n<p>&emsp;&emsp;\u63a5\u4e0b\u6765\u5c06\u4f1a\u8ba8\u8bba\u8fd9\u4e94\u8005\u7684\u533a\u522b\uff0c\u7531\u4e8e\u662f\u4ece\u5b98\u65b9\u6587\u6863\u7ffb\u8bd1\u800c\u6765\uff0c\u7ffb\u8bd1\u4f1a\u7565\u6709\u504f\u9887\uff0c\u6709\u5174\u8da3\u7684\u4e5f\u53ef\u4ee5\u53bbscikit-learn\u5b98\u65b9\u6587\u6863\u67e5\u770bhttps:\/\/scikit-learn.org\/stable\/modules\/classes.html#module-sklearn.neighbors <\/p>\n<h1>KNeighborsClassifier<\/h1>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<p>&emsp;&emsp;<code>KNeighborsClassfier<\/code>\u6a21\u578b\u5c31\u662f\u6700\u666e\u901a\u7684k\u8fd1\u90bb\u7b97\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u63a7\u5236\u4f7f\u7528\u9ad8\u65af\u8ddd\u79bb\u3001kd\u6811\u3001\u7403\u6811\u627e\u5230\u5b9e\u4f8b\u7684$k$\u4e2a\u8fd1\u90bb\u3002<\/p>\n<h2>\u4ee3\u7801<\/h2>\n<pre><code class=\"language-python\">from sklearn.neighbors import KNeighborsClassifier\nX = [[0], [1], [2], [3]]\ny = [0, 0, 1, 1]<\/code><\/pre>\n<pre><code class=\"language-python\">neigh = KNeighborsClassifier(n_neighbors=3)\nneigh.fit(X, y)<\/code><\/pre>\n<pre><code>KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n           metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n           weights='uniform')<\/code><\/pre>\n<pre><code class=\"language-python\">print(neigh.predict([[1.1]]))<\/code><\/pre>\n<pre><code>[0]<\/code><\/pre>\n<pre><code class=\"language-python\">print(neigh.predict_proba([[0.9]]))<\/code><\/pre>\n<pre><code>[[0.66666667 0.33333333]]<\/code><\/pre>\n<h2>\u53c2\u6570\u8be6\u89e3<\/h2>\n<ul>\n<li><strong>n_neighbors\uff1a<\/strong>$k$\u503c\u9009\u62e9\uff0cint\u7c7b\u578b\u3002\u4e00\u822c\u9009\u62e9\u4e00\u4e2a\u8f83\u5c0f\u7684$k$\u503c\uff0c\u7136\u540e\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u9009\u62e9\u4e00\u4e2a\u8f83\u597d\u7684$k$\u503c\u3002\u9ed8\u8ba4\u4e3a5\u3002<\/li>\n<li><strong>weights\uff1a<\/strong>\u8fd1\u90bb\u6743\u91cd\uff0cstr\u7c7b\u578b\u3002\u5982\u679cweights=&#8217;uniform&#8217;\uff0c\u5219\u610f\u5473\u7740\u6240\u6709\u8fd1\u90bb\u7684\u6743\u91cd\u90fd\u4e00\u6837\uff1b\u5982\u679cweights=&#8217;distance&#8217;\uff0c\u5219\u610f\u5473\u7740\u6743\u91cd\u548c\u8ddd\u79bb\u6210\u53cd\u6bd4\uff0c\u5373\u8ddd\u79bb\u76ee\u6807\u70b9\u66f4\u8fd1\u7684\u70b9\u6709\u66f4\u9ad8\u7684\u6743\u91cd\uff1b\u53ef\u4ee5\u81ea\u5b9a\u5b9a\u4e49\u51fd\u6570\u81ea\u5b9a\u4e49\u6743\u91cd\uff0c\u8f93\u5165\u662f\u8ddd\u79bb\u503c\uff0c\u8f93\u51fa\u662f\u6743\u91cd\u503c\u3002\u9ed8\u8ba4\u4e3a&#8217;uniform&#8217;\u3002<\/li>\n<li><strong>algorithm\uff1a<\/strong>\u7b97\u6cd5\u5b9e\u73b0\uff0cstr\u7c7b\u578b\u3002\u5982\u679calgorithm=&#8217;brute&#8217;\uff0c\u5373\u6700\u539f\u59cb\u7684k\u8fd1\u90bb\u7b97\u6cd5\uff0c\u8ba1\u7b97\u51fa\u6240\u6709\u70b9\u4e0e\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1b\u5982\u679calgorithm=&#8217;kd_tree&#8217;\uff0c\u5373kd\u6811\u5b9e\u73b0\uff1b\u5982\u679calgorithm=&#8217;ball_tree&#8217;\uff0c\u5373\u7403\u6811\u5b9e\u73b0\uff1b\u5982\u679calgorithm=&#8217;auto&#8217;\uff0c\u5219\u6a21\u578b\u4f1a\u9009\u62e9\u4e00\u4e2a\u62df\u5408\u6700\u597d\u7684\u7b97\u6cd5\u3002\u5982\u679c\u6837\u672c\u7279\u5f81\u5c11\uff0c\u4f7f\u7528&#8217;auto&#8217;\u5373\u53ef\uff1b\u5982\u679c\u6570\u636e\u91cf\u5927\u6216\u8005\u6837\u672c\u7279\u5f81\u591a\uff0c\u63a8\u8350\u4f7f\u7528kd\u6811\u4e4b\u540e\u518d\u5c1d\u8bd5\u7403\u6811\uff0c\u5982\u6b64\u505a\u53ef\u4ee5\u63d0\u9ad8\u51c6\u786e\u5ea6\uff1b\u5982\u679c\u8f93\u5165\u7684\u6837\u672c\u7279\u5f81\u662f\u7a00\u758f\u7684\u65f6\u5019\uff0cscikit-learn\u59cb\u7ec8\u4f1a\u81ea\u884c\u9009\u62e9&#8217;brute&#8217;\u5b9e\u73b0\u3002\u9ed8\u8ba4\u4e3a&#8217;auto&#8217;\u3002<\/li>\n<li><strong>leaf_size\uff1a<\/strong>\u53f6\u5b50\u8282\u70b9\u9608\u503c\uff0cint\u7c7b\u578b\u3002\u53ea\u6709\u5f53algorithm={&#8216;kd_tree&#8217;,&#8217;ball_tree&#8217;}\u65f6\u8be5\u53c2\u6570\u624d\u751f\u6548\uff0c\u8fd9\u4e2a\u503c\u8d8a\u5c0f\uff0c\u5219\u751f\u6210\u7684kd\u6811\u6216\u7403\u6811\u5c42\u6570\u8d8a\u5927\uff0c\u5efa\u6811\u65f6\u95f4\u8d8a\u957f\uff0c\u6cdb\u6307\u5c42\u6570\u8d8a\u5c0f\uff0c\u5efa\u6811\u65f6\u95f4\u77ed\u3002\u5982\u679c\u6837\u672c\u6570\u91cf\u8fc7\u5927\uff0c\u5219\u5fc5\u987b\u5f97\u589e\u5927\u8be5\u503c\uff0c\u56e0\u4e3a\u6811\u7684\u5c42\u6570\u8d8a\u5927\uff0c\u5219\u6811\u8d8a\u5bb9\u6613\u8fc7\u62df\u5408\uff0c\u63a8\u8350\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u9009\u62e9\u4e00\u4e2a\u8f83\u4f18\u503c\u3002\u9ed8\u8ba4\u4e3a30\u3002<\/li>\n<li><strong>p\uff1a<\/strong>\u8ddd\u79bb\u5ea6\u91cf\u9644\u5c5e\u53c2\u6570\uff0cint\u7c7b\u578b\u3002\u53ea\u6709\u5f53metric=&#8217;minkowski&#8217;\u65f6\u8be5\u53c2\u6570\u624d\u751f\u6548\uff0cp=1\u65f6\u4e3a\u66fc\u54c8\u987f\u8ddd\u79bb\uff0cp=2\u65f6\u4e3a\u6b27\u6c0f\u8ddd\u79bb\u3002\u9ed8\u8ba4\u4e3a2\u3002<\/li>\n<li><strong>metric\uff1a<\/strong>\u8ddd\u79bb\u5ea6\u91cf\u7c7b\u578b\uff0cstr\u7c7b\u578b\u3002metric=&#8217;euclidean&#8217;\u4e3a\u6b27\u6c0f\u8ddd\u79bb\uff1bmetric=&#8217;manhattan&#8217;\u4e3a\u66fc\u54c8\u987f\u8ddd\u79bb\uff1bmetric=&#8217;chebyshev&#8217;\u4e3a\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\uff1bmetric=&#8217;minkowski&#8217;\u4e3a\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\uff1bmetric=&#8217;wminkowski&#8217;\u4e3a\u5e26\u6743\u91cd\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\uff1bmetric=&#8217;seuclidean&#8217;\u4e3a\u6807\u51c6\u5316\u6b27\u6c0f\u8ddd\u79bb\uff1bmetric=&#8217;mahalanobis&#8217;\u4e3a\u9a6c\u6c0f\u8ddd\u79bb\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u9ed8\u8ba4\u7684metric=&#8217;minkowski&#8217;+p=2\u5373\u6b27\u5f0f\u8ddd\u79bb\u5c31\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u4e1a\u52a1\u7684\u9700\u6c42\u3002\u9ed8\u8ba4\u4e3a&#8217;minkowski&#8217;\u3002<\/li>\n<li><strong>metric_params\uff1a<\/strong>\u8ddd\u79bb\u5ea6\u91cf\u9644\u5c5e\u53c2\u6570\uff0cdict\u7c7b\u578b\u3002\u5982\u5e26\u6743\u91cd\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\u7684\u53c2\u6570\uff0c\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002<\/li>\n<li><strong>n_jobs\uff1a<\/strong>\u5e76\u884c\u6570\uff0cint\u7c7b\u578b\u3002n_jobs=1\u4f7f\u75281\u4e2acpu\u8fd0\u884c\u7a0b\u5e8f\uff1bn_jobs=2\uff0c\u4f7f\u75282\u4e2acpu\u8fd0\u884c\u7a0b\u5e8f\uff1bn_jobs=-1\uff0c\u4f7f\u7528\u6240\u6709cpu\u8fd0\u884c\u7a0b\u5e8f\u3002\u9ed8\u8ba4\u4e3a1\u3002<\/li>\n<\/ul>\n<h2>\u65b9\u6cd5<\/h2>\n<p><\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"3040\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"3040\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"071c299487\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"1f98482c41\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>scikit-learn\u5e93\u4e4bk\u8fd1\u90bb\u7b97\u6cd5 &emsp;&emsp;\u7531\u4e8ek\u8fd1\u90bb\u53ef\u4ee5\u505a\u56de\u5f52\u53c8\u53ef\u4ee5\u505a\u5206\u7c7b\uff0c\u6240\u4ee5\u6700\u666e\u901a\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3036,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[276,288],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3040"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3040"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3040\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/3036"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}