{"id":2205,"date":"2020-07-30T11:50:21","date_gmt":"2020-07-30T03:50:21","guid":{"rendered":"http:\/\/39.97.231.10\/?p=2205"},"modified":"2022-03-27T01:07:14","modified_gmt":"2022-03-26T17:07:14","slug":"python%e4%b8%8e%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=2205","title":{"rendered":"Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"2205\" class=\"elementor elementor-2205\" data-elementor-settings=\"[]\">\n\t\t\t\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-76c21200 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"76c21200\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-20596b83\" data-id=\"20596b83\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-34bd2464 elementor-widget elementor-widget-text-editor\" data-id=\"34bd2464\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"\\&quot;post-header\" main-content-wrap=\"\" text-left\\\"=\"\">\n<div class=\"\\&quot;post-meta\\&quot;\"><span style=\"\\&quot;color:\" #1e73be;=\"\" font-size:=\"\" 1.53846rem;\\\"=\"\">1. \u57fa\u672c\u6982\u5ff5<\/span><\/div>\n<\/div>\n<div class=\"\\&quot;post-content\" markdown\\\"=\"\">\n<div class=\"\\&quot;main-content-wrap\\&quot;\">\n<h2 id=\"\\&quot;a-\u6570\u636e\u6316\u6398\\&quot;\">a. \u6570\u636e\u6316\u6398<\/h2>\n\u4ece\u6d77\u91cf\u6570\u636e\u627e\u5bfb\u6709\u7528\u4fe1\u606f\u3002\u4ece\u4e8b\u8be5\u7c7b\u5de5\u4f5c\u7684\u6280\u672f\u662fBI\uff08\u5546\u4e1a\u667a\u80fd\uff09\u3002\u7b80\u5355\u8bf4\uff0c\u5728Excel\u4e2d\u5206\u6790\u6570\u636e\uff0c\u627e\u5bfb\u6709\u7528\u4fe1\u606f\uff0c\u4f7f\u7528SQL\u7b5b\u9009\u6570\u636e\uff0c\u4ece\u800c\u6307\u5bfc\u4f01\u4e1a\u5de5\u4f5c\uff0c\u8fd9\u5c31\u662f\u6570\u636e\u6316\u6398\u3002\n<h2 id=\"\\&quot;b-\u673a\u5668\u5b66\u4e60\\&quot;\">b. \u673a\u5668\u5b66\u4e60<\/h2>\n\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u548c\u7edf\u8ba1\u5b66\u7684\u4ea4\u53c9\u5b66\u79d1\uff0c\u57fa\u672c\u76ee\u6807\u662f\u5b66\u4e60\u4e00\u4e2ax-&gt;y\u7684\u51fd\u6570\uff08\u6620\u5c04\uff09\uff0c\u6765\u505a\u5206\u7c7b\u6216\u8005\u56de\u5f52\u7684\u5de5\u4f5c\u3002\u800c\u76ee\u524d\uff0c\u76f8\u5bf9\u4e8e\u6df1\u5ea6\u5b66\u4e60\u6765\u8bf4\uff0c\u673a\u5668\u5b66\u4e60\u66f4\u591a\u6307\u4f20\u7edf\u4f20\u7edf\u6a21\u578b\u548cPipeline\u6a21\u578b\u3002\n<blockquote>Q: \u4ec0\u4e48\u662f\u5206\u7c7b\u6216\u8005\u56de\u5f52\u7684\u5de5\u4f5c\uff1f\nA: \u516c\u4ea4\u8f66\u4e0a\uff0c\u4e00\u4e2a\u957f\u5934\u53d1\u9752\u5e74\u7ed9\u5c0f\u670b\u53cb\u8ba9\u4e86\u5ea7\u4f4d\uff0c\u5c0f\u670b\u53cb\u8bf4\u8c22\u8c22\u59d0\u59d0\uff0c\u9752\u5e74\u8bf4\u4e0d\u662f\u59d0\u59d0\uff0c\u662f\u54e5\u54e5\uff0c\u5bf9\u5c0f\u670b\u53cb\u6765\u8bf4\uff0c\u8fd9\u4e2a\u5b66\u4e60\u8fc7\u7a0b\u5c31\u662f\u5206\u7c7b\u548c\u56de\u5f52\u3002\nQ: \u4e3a\u4ec0\u4e48\u6570\u636e\u6316\u6398\u603b\u548c\u673a\u5668\u5b66\u4e60\u4e00\u8d77\u88ab\u63d0\u5230\uff1f\nA: \u5f88\u591a\u6570\u636e\u6316\u6398\u5de5\u4f5c\u662f\u53ef\u4ee5\u901a\u8fc7\u673a\u5668\u5b66\u4e60\u63d0\u4f9b\u7684\u7b97\u6cd5\u5de5\u5177\u5b9e\u73b0\u3002<\/blockquote>\n<h2 id=\"\\&quot;c-\u6df1\u5ea6\u5b66\u4e60\\&quot;\">c. \u6df1\u5ea6\u5b66\u4e60<\/h2>\n\u662f\u673a\u5668\u5b66\u4e60\u4e2d\u795e\u7ecf\u7f51\u7edc\u7684\u884d\u751f\u3002\u5728\u56fe\u50cf\uff0c\u8bed\u97f3\u7b49\u5bcc\u5a92\u4f53\u7684\u5206\u7c7b\u548c\u8bc6\u522b\u4e0a\u53d6\u5f97\u4e86\u975e\u5e38\u597d\u7684\u6548\u679c\u3002\u4e5f\u662f\u6700\u8fd1\u4e00\u5e74\u8ddf\u968fAlpha Go\u7092\u5f97\u6700\u706b\u7684Topic\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/1.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n<strong>\u673a\u5668\u5b66\u4e60\u6846\u67b6\u548c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u95f4\u7684\u533a\u522b<\/strong>\n<ul>\n \t<li>\u673a\u5668\u5b66\u4e60\u6846\u67b6\u6db5\u76d6\u7528\u4e8e\u5206\u7c7b\uff0c\u56de\u5f52\uff0c\u805a\u7c7b\uff0c\u5f02\u5e38\u68c0\u6d4b\u548c\u6570\u636e\u51c6\u5907\u7684\u5404\u79cd\u5b66\u4e60\u65b9\u6cd5\uff0c\u5e76\u4e14\u5176\u53ef\u4ee5\u6216\u53ef\u4ee5\u4e0d\u5305\u62ec\u795e\u7ecf\u7f51\u7edc\u65b9\u6cd5\u3002<\/li>\n \t<li>\u6df1\u5ea6\u5b66\u4e60\u6216\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\uff08DNN\uff09\u6846\u67b6\u6db5\u76d6\u5177\u6709\u8bb8\u591a\u9690\u85cf\u5c42\u7684\u5404\u79cd\u795e\u7ecf\u7f51\u7edc\u62d3\u6251\u3002\u8fd9\u4e9b\u5c42\u5305\u62ec\u6a21\u5f0f\u8bc6\u522b\u7684\u591a\u6b65\u9aa4\u8fc7\u7a0b\u3002\u7f51\u7edc\u4e2d\u7684\u5c42\u8d8a\u591a\uff0c\u53ef\u4ee5\u63d0\u53d6\u7528\u4e8e\u805a\u7c7b\u548c\u5206\u7c7b\u7684\u7279\u5f81\u8d8a\u590d\u6742\u3002<\/li>\n \t<li>\u4e00\u822c\u6765\u8bf4\uff0c\u6df1\u5c42\u795e\u7ecf\u7f51\u7edc\u8ba1\u7b97\u5728GPU\uff08\u7279\u522b\u662fNvidia CUDA\u901a\u7528GPU\uff0c\u5927\u591a\u6570\u6846\u67b6\uff09\u4e0a\u8fd0\u884c\u7684\u901f\u5ea6\u8981\u6bd4CPU\u5feb\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u66f4\u7b80\u5355\u7684\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u4e0d\u9700\u8981GPU\u7684\u52a0\u901f\u3002<\/li>\n<\/ul>\n<h1 id=\"\\&quot;2-\u5e38\u89c1\u7684\u673a\u5668\u5b66\u4e60\u6846\u67b6\\&quot;\">2. \u5e38\u89c1\u7684\u673a\u5668\u5b66\u4e60\u6846\u67b6<\/h1>\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/2.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n<ul>\n \t<li>JAVA\u9635\u8425\uff1aSpark MLlib\u662fSpark\u7684\u5f00\u6e90\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u63d0\u4f9b\u4e86\u901a\u7528\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u5982\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u548c\u534f\u540c\u8fc7\u6ee4\uff08\u4f46\u4e0d\u5305\u62ecDNN\uff09\u4ee5\u53ca\u7279\u5f81\u63d0\u53d6\u3001\u8f6c\u6362\u3001\u7ef4\u6570\u964d\u4f4e\u5de5\u5177\uff0c\u4ee5\u53ca\u6784\u5efa\u3001\u8bc4\u4f30\u548c\u8c03\u6574\u673a\u5668\u5b66\u4e60\u7ba1\u9053\u9009\u62e9\u548c\u5de5\u5177\u3002<\/li>\n \t<li>Python\u9635\u8425\uff1aScikit-learn\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\uff0c\u6210\u719f\u7684\u673a\u5668\u5b66\u4e60Python\u5e93\u3002\u5305\u542b\u5404\u79cd\u5404\u6837\u6210\u719f\u7684\u7b97\u6cd5\u548c\u96c6\u6210\u56fe\u3002\u5b83\u76f8\u5bf9\u5bb9\u6613\u5b89\u88c5\u3001\u5b66\u4e60\u548c\u4f7f\u7528\uff0c\u5e26\u6709\u5f88\u597d\u7684\u4f8b\u5b50\u548c\u6559\u7a0b\u3002<\/li>\n<\/ul>\n<h1 id=\"\\&quot;3-\u5e38\u89c1\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\\&quot;\">3. \u5e38\u89c1\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6<\/h1>\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/3.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n<ul>\n \t<li>TensorFlow \u662fGoogle\u7b2c\u4e8c\u4ee3\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6<\/li>\n \t<li>Caffe\u6700\u521d\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5206\u7c7b\u6846\u67b6\uff0c\u4f46\u9879\u76ee\u51e0\u4e4e\u505c\u6ede\u3002\u6700\u65b0\u6d3b\u8dc3\u7248\u672c\u4e3aCaffe2<\/li>\n \t<li>MXNet\u7c7b\u4f3cTensorFlow<\/li>\n \t<li>Torch7\u4e3a\u6570\u4e0d\u591a\u652f\u6301lua\u7684\u6846\u67b6\uff0cPyTorch\u662f\u9762\u5411Python\u7684API<\/li>\n \t<li>Deeplearning4J\u652f\u6301java\u548cscala\uff0c\u662fSpark\u5728\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u7684\u6269\u5c55<\/li>\n<\/ul>\n<h1 id=\"\\&quot;4-\u673a\u5668\u5b66\u4e60\u4f7f\u7528\u4e0e\u7814\u7a76\\&quot;\">4. \u673a\u5668\u5b66\u4e60\u4f7f\u7528\u4e0e\u7814\u7a76<\/h1>\n<h2 id=\"\\&quot;a-\u4f7f\u7528\\&quot;\">a. \u4f7f\u7528<\/h2>\n\u5bf9\u4e8e\u4f7f\u7528\u8005\u6765\u8bf4\uff0c\u53ef\u4ee5\u4e0d\u7cbe\u901a\u673a\u5668\u5b66\u4e60\u7684\u6570\u5b66\u6a21\u578b\u4e0e\u5b9e\u73b0\u539f\u7406\uff0c\u53ea\u9700\u719f\u6089\u5728\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u578b\u5de5\u5177\u5373\u53ef\u3002\n<h2 id=\"\\&quot;b-\u7814\u7a76\\&quot;\">b. \u7814\u7a76<\/h2>\n\u7814\u7a76\u673a\u5668\u5b66\u4e60\uff0c\u662f\u5c06\u73b0\u5b9e\u9700\u6c42\u8f6c\u5316\u6210\u6570\u5b66\u6a21\u578b\uff0c\u5e76\u4e0d\u65ad\u4f18\u5316\u7684\u8fc7\u7a0b\u3002\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/4.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n<h1 id=\"\\&quot;5-Python\u4e0e\u673a\u5668\u5b66\u4e60\\&quot;\">5. Python\u4e0e\u673a\u5668\u5b66\u4e60<\/h1>\n<blockquote>Q: \u4e3a\u4ec0\u4e48\u5f88\u591a\u673a\u5668\u5b66\u4e60\u7684\u6846\u67b6\u90fd\u7528\u5230python?\nA: Matlab\u5e94\u8be5\u662f\u6700\u9002\u5408\u505a\u673a\u5668\u5b66\u4e60\u7814\u7a76\u7684\u5de5\u5177\uff0c\u4f46\u8003\u8651\u5f00\u53d1\u6548\u7387\uff0c\u4ef7\u683c\uff0c\u751f\u6001\u7b49\u56e0\u7d20\uff0c\u5f88\u591a\u6846\u67b6\u9009\u7528\u4e86python\u4f5c\u4e3a\u673a\u5668\u5b66\u4e60\u5f00\u53d1\u8bed\u8a00\u3002<\/blockquote>\n<h1 id=\"\\&quot;6-Scikit-Learn\u5e94\u7528\u793a\u4f8b\\&quot;\">6. Scikit-Learn\u5e94\u7528\u793a\u4f8b<\/h1>\n\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6846\u67b6\u7a76\u7adf\u80fd\u89e3\u51b3\u4ec0\u4e48\u6837\u7684\u95ee\u9898\uff0c\u5e26\u6765\u54ea\u4e9b\u4fbf\u5229\uff1f\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u56fe\u50cf\u8bc6\u522b\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u3002\n<h2 id=\"\\&quot;a-\u4e1a\u52a1\u9700\u6c42\\&quot;\">a. \u4e1a\u52a1\u9700\u6c42<\/h2>\n\u6211\u4eec\u60f3\u8ba9\u8ba1\u7b97\u673a\u8bc6\u522b\u4e00\u4e9b\u4eba\u5de5\u624b\u5199\u7684\u6570\u5b57\u56fe\u7247\uff0c\u8ba1\u7b97\u673a\u9700\u8981\u901a\u8fc7\u5b66\u4e60\uff0c\u5224\u65ad\u56fe\u7247\u4e2d\u7684\u5185\u5bb9\u662f\u5426\u662f\u4eba\u5de5\u624b\u5199\u7684\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u8bc6\u522b\u51fa\u6765\u3002\u53ef\u4ee5\u7406\u89e3\u6210\u8f66\u724c\u8bc6\u522b\u7684\u7b80\u5355\u7248\u3002\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/5.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n<h2 id=\"\\&quot;b-\u5b57\u8ff9\u8bc6\u522b\u4e0eMNIST\u6570\u636e\u96c6\\&quot;\">b. \u5b57\u8ff9\u8bc6\u522b\u4e0eMNIST\u6570\u636e\u96c6<\/h2>\n\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u7528\u5230\u4e00\u4e9b\u6570\u636e\uff0c\u7528\u4e8e\u63d0\u4f9b\u7ed9\u8ba1\u7b97\u673a\u5b66\u4e60\u548c\u6d4b\u8bd5\u6700\u7ec8\u7ed3\u679c\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u662fMNIST\u6570\u636e\u96c6\u3002\n<blockquote>MNIST\u6570\u636e\u96c6\u662f\u6df7\u5408\u7684\u56fd\u5bb6\u6807\u51c6\u548c\u6280\u672f (\u7b80\u79f0 MNIST) \u7531\u7ea2\u5916\u7814\u7a76\u5458\uff0c\u4f5c\u4e3a\u57fa\u51c6\u6765\u6bd4\u8f83\u4e0d\u540c\u7684\u7ea2\u5916\u7b97\u6cd5\u521b\u5efa\u6570\u636e\u96c6\u3002\n\u5176\u57fa\u672c\u601d\u60f3\u662f\u5982\u679c\u4f60\u6709\u4f60\u60f3\u8981\u6d4b\u8bd5\u7ea2\u5916\u7684\u7b97\u6cd5\u6216\u8f6f\u4ef6\u7684\u7cfb\u7edf\uff0c\u53ef\u4ee5\u8fd0\u884c\u60a8\u7684\u7b97\u6cd5\u6216\u7cfb\u7edf\u9488\u5bf9 MNIST \u7684\u6570\u636e\u96c6\u548c\u6bd4\u8f83\u60a8\u7684\u7ed3\u679c\u4e0e\u5176\u4ed6\u7cfb\u7edf\u4ee5\u524d\u53d1\u5e03\u6210\u679c\u3002\n\u6570\u636e\u96c6\u5305\u542b\u7684\u5171 70,000 \u56fe\u50cf\uff0c\u5176\u4e2d\u670960,000 \u4e2a\u8bad\u7ec3\u56fe\u50cf \uff08\u7528\u4e8e\u521b\u5efa\u7ea2\u5916\u6a21\u578b\uff09 \u548c 10,000 \u4e2a\u6d4b\u8bd5\u56fe\u50cf \uff08\u7528\u4e8e\u8bc4\u4f30\u6a21\u578b\u7684\u7cbe\u5ea6\uff09\u3002\n\u6bcf\u4e2a MNIST \u56fe\u50cf\u662f\u4e00\u4e2a\u5355\u4e00\u7684\u624b\u5199\u7684\u6570\u5b57\u5b57\u7b26\u7684\u6570\u5b57\u5316\u7684\u56fe\u7247\u3002\u6bcf\u4e2a\u56fe\u50cf\u662f 28 x 28 \u50cf\u7d20\u5927\u5c0f\u3002\u6bcf\u4e2a\u50cf\u7d20\u503c\u662f 0\uff0c\u8868\u793a\u767d\u8272\uff0c\u81f3 255\uff0c\u8868\u793a\u9ed1\u3002\u4e2d\u95f4\u50cf\u7d20\u503c\u8868\u793a\u7684\u7070\u5ea6\u7ea7\u3002<\/blockquote>\n\u6211\u4eec\u8fd9\u91cc\u7528\u5230\u4e00\u4e2a\u7b80\u5316\u7248\uff0c\u5b58\u50a8\u5728csv\u6587\u4ef6\u4e2d\uff0c\u5305\u542b42000\u6761\u6570\u636e\uff0c\u6bcf\u6761\u6570\u636e\u6709785\u5217\uff0c\u5176\u4e2d\u7b2c1\u5217\u4e3a\u8be5\u6570\u5b57\u56fe\u7247\u7684\u771f\u5b9e\u6570\u5b57\u7ed3\u679c\uff0c\u7b2c2\u81f3\u7b2c785\uff0828*28\u50cf\u7d20\uff09\u5217\u7528\u6765\u5b58\u8be5\u56fe\u7247\u7684\u7070\u5ea6\u503c\u3002\n<img src=\"\\&quot;https:\/\/wurang.net\/images\/2017\/python_machine_learning\/6.png\\&quot;\" alt=\"\\&quot;\\&quot;\">\n\u8be5\u6587\u4ef6\u53ef\u5728<a href=\"\/\/github.com\/sonicrang\/MNIST\/blob\/master\/train.csv\\&quot;\" target=\"\\&quot;_blank\\&quot;\" rel=\"\\&quot;external noopener\" noopener=\"\">\u6b64\u5904\u4e0b\u8f7d<\/a>\n<h2 id=\"\\&quot;c-\u524d\u671f\u51c6\u5907\\&quot;\">c. \u524d\u671f\u51c6\u5907<\/h2>\n\u6709\u4e86\u6570\u636e\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4f20\u7edf\u7684\u6570\u5b66\u6a21\u578b\u548c\u7528\u673a\u5668\u5b66\u4e60\u6846\u67b6\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u9700\u6c42\u3002\u7531\u4e8e\u7a0b\u5e8f\u5747\u4f7f\u7528Python\u6765\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u5b89\u88c5\u67d0\u4e9bPython\u5e93\uff0c\u5982<code>Numpy<\/code>\uff0c<code>Pandas<\/code>\u5305\u62ec\u540e\u9762\u6240\u9700\u7684<code>Scikit-Learn<\/code>\u3002\n\u5728windows\u4e0b\u7531\u4e8e\u7f3a\u5c11\u7f16\u8bd1\u73af\u5883\uff0c\u4e0d\u80fd\u76f4\u63a5\u7528<code>pip install<\/code>\u5b89\u88c5\u4e0a\u8ff0\u7684\u5e93\uff0c\u53ef\u4ee5\u5728<a href=\"\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#scikit-learn\\&quot;\" target=\"\\&quot;_blank\\&quot;\" rel=\"\\&quot;external noopener\" noopener=\"\">\u8fd9\u91cc\u4e0b\u8f7d<\/a>\u975e\u5b98\u65b9\u5df2\u9884\u7f16\u8bd1\u7684\u5e93\u8fdb\u884c\u5b89\u88c5\u3002\n<h2 id=\"\\&quot;d-\u4f20\u7edf\u6570\u5b66\u6a21\u578b\u89e3\u51b3\u65b9\u6848\\&quot;\">d. \u4f20\u7edf\u6570\u5b66\u6a21\u578b\u89e3\u51b3\u65b9\u6848<\/h2>\n\u5173\u4e8e\u6570\u5b66\u6a21\u578b\u5b9e\u73b0\u548c\u89e3\u91ca\u8bf4\u660e\u53ef\u4ee5\u53c2\u8003<a href=\"\\&quot;https:\/\/wurang.net\/python_machine_learning\/\\&quot;\">http:\/\/www.jianshu.com\/p\/4afc39897b3e<\/a>\n\u8fd9\u91cc\u4ec5\u5217\u51fa\u4f7f\u7528KNN\u7b97\u6cd5\u5b9e\u73b0\u7684\u4ee3\u7801\uff1a\n<figure class=\"\\&quot;highlight\" python\\\"=\"\">\n<table>\n<tbody>\n<tr>\n<td class=\"\\&quot;gutter\\&quot;\">\n<div class=\"\\&quot;line\\&quot;\">1<\/div>\n<div class=\"\\&quot;line\\&quot;\">2<\/div>\n<div class=\"\\&quot;line\\&quot;\">3<\/div>\n<div class=\"\\&quot;line\\&quot;\">4<\/div>\n<div class=\"\\&quot;line\\&quot;\">5<\/div>\n<div class=\"\\&quot;line\\&quot;\">6<\/div>\n<div class=\"\\&quot;line\\&quot;\">7<\/div>\n<div class=\"\\&quot;line\\&quot;\">8<\/div>\n<div class=\"\\&quot;line\\&quot;\">9<\/div>\n<div class=\"\\&quot;line\\&quot;\">10<\/div>\n<div class=\"\\&quot;line\\&quot;\">11<\/div>\n<div class=\"\\&quot;line\\&quot;\">12<\/div>\n<div class=\"\\&quot;line\\&quot;\">13<\/div>\n<div class=\"\\&quot;line\\&quot;\">14<\/div>\n<div class=\"\\&quot;line\\&quot;\">15<\/div>\n<div class=\"\\&quot;line\\&quot;\">16<\/div>\n<div class=\"\\&quot;line\\&quot;\">17<\/div>\n<div class=\"\\&quot;line\\&quot;\">18<\/div>\n<div class=\"\\&quot;line\\&quot;\">19<\/div>\n<div class=\"\\&quot;line\\&quot;\">20<\/div>\n<div class=\"\\&quot;line\\&quot;\">21<\/div>\n<div class=\"\\&quot;line\\&quot;\">22<\/div>\n<div class=\"\\&quot;line\\&quot;\">23<\/div>\n<div class=\"\\&quot;line\\&quot;\">24<\/div>\n<div class=\"\\&quot;line\\&quot;\">25<\/div>\n<div class=\"\\&quot;line\\&quot;\">26<\/div>\n<div class=\"\\&quot;line\\&quot;\">27<\/div>\n<div class=\"\\&quot;line\\&quot;\">28<\/div>\n<div class=\"\\&quot;line\\&quot;\">29<\/div>\n<div class=\"\\&quot;line\\&quot;\">30<\/div>\n<div class=\"\\&quot;line\\&quot;\">31<\/div>\n<div class=\"\\&quot;line\\&quot;\">32<\/div>\n<div class=\"\\&quot;line\\&quot;\">33<\/div>\n<div class=\"\\&quot;line\\&quot;\">34<\/div>\n<div class=\"\\&quot;line\\&quot;\">35<\/div>\n<div class=\"\\&quot;line\\&quot;\">36<\/div>\n<div class=\"\\&quot;line\\&quot;\">37<\/div>\n<div class=\"\\&quot;line\\&quot;\">38<\/div>\n<div class=\"\\&quot;line\\&quot;\">39<\/div>\n<div class=\"\\&quot;line\\&quot;\">40<\/div>\n<div class=\"\\&quot;line\\&quot;\">41<\/div>\n<div class=\"\\&quot;line\\&quot;\">42<\/div>\n<div class=\"\\&quot;line\\&quot;\">43<\/div>\n<div class=\"\\&quot;line\\&quot;\">44<\/div>\n<div class=\"\\&quot;line\\&quot;\">45<\/div>\n<div class=\"\\&quot;line\\&quot;\">46<\/div>\n<div class=\"\\&quot;line\\&quot;\">47<\/div>\n<div class=\"\\&quot;line\\&quot;\">48<\/div>\n<div class=\"\\&quot;line\\&quot;\">49<\/div>\n<div class=\"\\&quot;line\\&quot;\">50<\/div>\n<div class=\"\\&quot;line\\&quot;\">51<\/div>\n<div class=\"\\&quot;line\\&quot;\">52<\/div>\n<div class=\"\\&quot;line\\&quot;\">53<\/div>\n<div class=\"\\&quot;line\\&quot;\">54<\/div>\n<div class=\"\\&quot;line\\&quot;\">55<\/div>\n<div class=\"\\&quot;line\\&quot;\">56<\/div>\n<div class=\"\\&quot;line\\&quot;\">57<\/div><\/td>\n<td class=\"\\&quot;code\\&quot;\">\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">import<\/span> numpy <span class=\"\\&quot;keyword\\&quot;\">as<\/span> np<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">import<\/span> pandas <span class=\"\\&quot;keyword\\&quot;\">as<\/span> pd<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#import time<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#\u5f52\u4e00\u5316<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;function\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">def<\/span> <span class=\"\\&quot;title\\&quot;\">normalize<\/span><span class=\"\\&quot;params\\&quot;\">(x)<\/span>:<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">norms = np.apply_along_axis(np.linalg.norm,<span class=\"\\&quot;number\\&quot;\">1<\/span>,x)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">return<\/span> x \/ np.expand_dims(norms,<span class=\"\\&quot;number\\&quot;\">1<\/span>)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;function\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">def<\/span> <span class=\"\\&quot;title\\&quot;\">nearest_neighbor<\/span><span class=\"\\&quot;params\\&quot;\">(norm_func,train_data,train_label,test_data)<\/span>:<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">train_data = norm_func(train_data)<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_data = norm_func(test_data)<\/div>\n<div class=\"\\&quot;line\\&quot;\">cos = np.dot(train_data,np.transpose(test_data))<\/div>\n<div class=\"\\&quot;line\\&quot;\">max_cos = np.argmax(cos,axis=<span class=\"\\&quot;number\\&quot;\">0<\/span>)<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_pred = train_label[max_cos]<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">return<\/span> test_pred<\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\\&#8217;\\&#8217;\\&#8217;<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">train_label=np.array([[1],[2],[3]])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">b=np.array([1,2])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">a=train_label[b]<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u5f97\u5230\u7684\u7ed3\u679c\u662f<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">([[2],[3]])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u8bf4\u660e\u6570\u7ec4\u53ef\u4ee5\u4f5c\u4e3a\u8bfb\u53d6\u53e6\u4e00\u4e2a\u6570\u7ec4\u7684index<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\\&#8217;\\&#8217;\\&#8217;<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;function\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">def<\/span> <span class=\"\\&quot;title\\&quot;\">validate<\/span><span class=\"\\&quot;params\\&quot;\">(test_pred,test_label)<\/span>:<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">c=len(test_pred)<\/div>\n<div class=\"\\&quot;line\\&quot;\">correct=(test_pred == test_label).sum()<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">return<\/span> float(correct)\/c<span class=\"\\&quot;comment\\&quot;\">#\u5fc5\u987b\u8f6c\u53d8\u6210\u6d6e\u70b9\u6570\u518d\u505a\u9664\u6cd5\uff0c\u4e4b\u524d\u4f7f\u7528correct\/c\u5f97\u52300<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\\&#8217;\\&#8217;\\&#8217;<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">train_label=np.array([[1],[2],[3,2]])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">c = len(train_label)<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u7ed3\u679c\u4e3a3<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">train_label=np.array([1,3,2])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">c = len(train_label)<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u7ed3\u679c\u4e3a3<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">train_label=np.array([[1,3,2]])<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">c = len(train_label)<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u7ed3\u679c\u4e3a1<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\u8bf4\u660e\uff1a\u5728\u6570\u7ec4\u91cc\u9762\u5957\u6570\u7ec4\u7684\u65f6\u5019\uff0clen\u5f97\u5230\u7684\u662f\u5927\u6570\u7ec4\u91cc\u6570\u7ec4\u7684\u4e2a\u6570\uff0c\u5728\u53ea\u6709\u4e00\u5c42\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u5f97\u5230\u7684\u662f\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;string\\&quot;\">\\&#8217;\\&#8217;\\&#8217;<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">if<\/span> __name__ == <span class=\"\\&quot;string\\&quot;\">\\&#8217;__main__\\&#8217;<\/span>:<\/div>\n<div class=\"\\&quot;line\\&quot;\">train_num = <span class=\"\\&quot;number\\&quot;\">200<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">test_num = <span class=\"\\&quot;number\\&quot;\">300<\/span><span class=\"\\&quot;comment\\&quot;\">#\u6d4b\u8bd5\u6570\u636e\u8d77\u59cb\u662ftest_num-train_num<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">x = pd.read_csv(<span class=\"\\&quot;string\\&quot;\">\\&#8217;train.csv\\&#8217;<\/span>)<span class=\"\\&quot;comment\\&quot;\">#\u662f\u5c06\u5f97\u5230\u7684\u6240\u6709\u6570\u636e\u505a\u5f52\u4e00\u5316\u5904\u7406\u8fd8\u662f\u8981\u4f7f\u7528\u7684\u6570\u636e\u505a\u5f52\u4e00\u5316\u5904\u7406<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#x = normalize(r)#\u5c06\u6240\u6709\u6570\u636e\u505a\u5f52\u4e00\u5316\u5904\u7406<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">x_train = x.values[<span class=\"\\&quot;number\\&quot;\">0<\/span>:train_num,<span class=\"\\&quot;number\\&quot;\">1<\/span>:]<span class=\"\\&quot;comment\\&quot;\">#\u8bfb\u53d6pandas\u4e2d\u8bfb\u53d6\u51fa\u6765\u7684\u6570\u636e\uff0c\u9700\u8981\u7528data.train<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">x_train_label = x.values[<span class=\"\\&quot;number\\&quot;\">0<\/span>:train_num,<span class=\"\\&quot;number\\&quot;\">0<\/span>]<span class=\"\\&quot;comment\\&quot;\">#\u7b2c\u4e00\u5217\u662flabel,\u6bcf\u5e45\u56fe\u7684\u6570\u636e\u662f\u4e00\u884c<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">x_test = x.values[train_num:test_num,<span class=\"\\&quot;number\\&quot;\">1<\/span>:]<\/div>\n<div class=\"\\&quot;line\\&quot;\">x_test_label = x.values[train_num:test_num,<span class=\"\\&quot;number\\&quot;\">0<\/span>]<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_pred=nearest_neighbor(normalize,x_train,x_train_label,x_test)<\/div>\n<div class=\"\\&quot;line\\&quot;\">prec=validate(test_pred,x_test_label)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">print<\/span> <span class=\"\\&quot;string\\&quot;\">u\\&#8221;\u6b63\u786e\u7387\u4e3a%.2f\\&#8221;<\/span>%(prec)<span class=\"\\&quot;comment\\&quot;\">#\u6d6e\u70b9\u6570\u662f%f<\/span><\/div><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\u6761\u6761\u5927\u9053\u901a\u7f57\u9a6c\uff0c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7684\u6570\u5b66\u6a21\u578b\uff0c\u5bf9\u4e8e\u673a\u5668\u5b66\u4e60\u7814\u7a76\u4eba\u5458\u6765\u8bf4\uff0c\u5c31\u662f\u8981\u5c06\u66f4\u591a\u7684\u95ee\u9898\u6a21\u578b\u5316\uff0c\u5e76\u4e0d\u65ad\u4f18\u5316\u6a21\u578b\u548c\u65b9\u6cd5\u3002\n<h2 id=\"\\&quot;e-Scikit-Learn\u89e3\u51b3\u65b9\u6848\\&quot;\">e. Scikit-Learn\u89e3\u51b3\u65b9\u6848<\/h2>\n\u4e0a\u4e00\u5c0f\u7ed3\u4f7f\u7528\u4e86\u6700\u539f\u59cb\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u770b\u5230\u4ee3\u7801\u76f8\u5bf9\u8f83\u591a\u4e5f\u76f8\u5bf9\u590d\u6742\u3002\u540c\u65f6\u6211\u4eec\u8fd8\u9700\u8981\u53bb\u5efa\u7acb\u4e00\u4e2a\u6570\u5b66\u5173\u7cfb\u3002\u4f7f\u7528\u95e8\u69db\u592a\u9ad8\uff0c\u8fd9\u5927\u5927\u964d\u4f4e\u4e86\u5b9e\u7528\u6027\u3002\u6240\u4ee5\u673a\u5668\u5b66\u4e60\u6846\u67b6\u5c31\u662f\u5c06\u8fd9\u4e9b\u5e38\u7528\u6a21\u578b\u5c01\u88c5\u8d77\u6765\uff0c\u80fd\u8ba9\u5f00\u53d1\u8005\u66f4\u65b9\u4fbf\u7684\u8c03\u7528\u3002\n\u4e0b\u9762\u5c31\u662f\u4f7f\u7528Scikit-Learn\u5c01\u88c5\u7684KNN\u7b97\u6cd5\u5b9e\u73b0\u9700\u6c42\u7684\u4ee3\u7801\uff1a\n<figure class=\"\\&quot;highlight\" python\\\"=\"\">\n<table>\n<tbody>\n<tr>\n<td class=\"\\&quot;gutter\\&quot;\">\n<div class=\"\\&quot;line\\&quot;\">1<\/div>\n<div class=\"\\&quot;line\\&quot;\">2<\/div>\n<div class=\"\\&quot;line\\&quot;\">3<\/div>\n<div class=\"\\&quot;line\\&quot;\">4<\/div>\n<div class=\"\\&quot;line\\&quot;\">5<\/div>\n<div class=\"\\&quot;line\\&quot;\">6<\/div>\n<div class=\"\\&quot;line\\&quot;\">7<\/div>\n<div class=\"\\&quot;line\\&quot;\">8<\/div>\n<div class=\"\\&quot;line\\&quot;\">9<\/div>\n<div class=\"\\&quot;line\\&quot;\">10<\/div>\n<div class=\"\\&quot;line\\&quot;\">11<\/div>\n<div class=\"\\&quot;line\\&quot;\">12<\/div>\n<div class=\"\\&quot;line\\&quot;\">13<\/div>\n<div class=\"\\&quot;line\\&quot;\">14<\/div>\n<div class=\"\\&quot;line\\&quot;\">15<\/div>\n<div class=\"\\&quot;line\\&quot;\">16<\/div>\n<div class=\"\\&quot;line\\&quot;\">17<\/div>\n<div class=\"\\&quot;line\\&quot;\">18<\/div>\n<div class=\"\\&quot;line\\&quot;\">19<\/div>\n<div class=\"\\&quot;line\\&quot;\">20<\/div>\n<div class=\"\\&quot;line\\&quot;\">21<\/div>\n<div class=\"\\&quot;line\\&quot;\">22<\/div>\n<div class=\"\\&quot;line\\&quot;\">23<\/div>\n<div class=\"\\&quot;line\\&quot;\">24<\/div>\n<div class=\"\\&quot;line\\&quot;\">25<\/div>\n<div class=\"\\&quot;line\\&quot;\">26<\/div>\n<div class=\"\\&quot;line\\&quot;\">27<\/div>\n<div class=\"\\&quot;line\\&quot;\">28<\/div><\/td>\n<td class=\"\\&quot;code\\&quot;\">\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">import<\/span> pandas <span class=\"\\&quot;keyword\\&quot;\">as<\/span> pd<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">from<\/span> sklearn.decomposition <span class=\"\\&quot;keyword\\&quot;\">import<\/span> PCA<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">from<\/span> sklearn.neighbors <span class=\"\\&quot;keyword\\&quot;\">import<\/span> KNeighborsClassifier<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#\u5bfc\u5165Scikit learn\u5e93\u4e2d\u7684KNN\u7b97\u6cd5<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">import<\/span> time<\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">if<\/span> __name__ ==<span class=\"\\&quot;string\\&quot;\">\\&#8221;__main__\\&#8221;<\/span>:<\/div>\n<div class=\"\\&quot;line\\&quot;\">train_num = <span class=\"\\&quot;number\\&quot;\">20000<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">test_num = <span class=\"\\&quot;number\\&quot;\">30000<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">data = pd.read_csv(<span class=\"\\&quot;string\\&quot;\">\\&#8217;train.csv\\&#8217;<\/span>)<\/div>\n<div class=\"\\&quot;line\\&quot;\">train_data = data.values[<span class=\"\\&quot;number\\&quot;\">0<\/span>:train_num,<span class=\"\\&quot;number\\&quot;\">1<\/span>:]<\/div>\n<div class=\"\\&quot;line\\&quot;\">train_label = data.values[<span class=\"\\&quot;number\\&quot;\">0<\/span>:train_num,<span class=\"\\&quot;number\\&quot;\">0<\/span>]<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_data = data.values[train_num:test_num,<span class=\"\\&quot;number\\&quot;\">1<\/span>:]<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_label = data.values[train_num:test_num,<span class=\"\\&quot;number\\&quot;\">0<\/span>]<\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\">t = time.time()<\/div>\n<div class=\"\\&quot;line\\&quot;\">pca=PCA(n_components = <span class=\"\\&quot;number\\&quot;\">0.8<\/span>)<\/div>\n<div class=\"\\&quot;line\\&quot;\">train_x = pca.fit_transform(train_data)<\/div>\n<div class=\"\\&quot;line\\&quot;\">test_x = pca.transform(test_data)<\/div>\n<div class=\"\\&quot;line\\&quot;\">neighbors = KNeighborsClassifier(n_neighbors=<span class=\"\\&quot;number\\&quot;\">4<\/span>)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#\u627e\u5230\u4e00\u4e2a\u70b9\u7684K\u8fd1\u90bb\uff0cn_neighbors\u8fd1\u90bb\u7684\u6570\u76ee<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">neighbors.fit(train_x,train_label)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#\u5bf9\u8bad\u7ec3\u96c6\u7684\u8f93\u5165\u548c\u8f93\u51fa\u8fdb\u884c\u8bad\u7ec3<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\">pre= neighbors.predict(test_x)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;comment\\&quot;\">#\u5bf9\u6d4b\u8bd5\u96c6\u7684\u8f93\u5165\u8fdb\u884c\u9884\u6d4b\uff0c\u8fd4\u56de\u9884\u6d4b\u51fa\u7684\u6807\u7b7e<\/span><\/div>\n<div class=\"\\&quot;line\\&quot;\"><\/div>\n<div class=\"\\&quot;line\\&quot;\">acc = float((pre==test_label).sum())\/len(test_x)<\/div>\n<div class=\"\\&quot;line\\&quot;\"><span class=\"\\&quot;keyword\\&quot;\">print<\/span> <span class=\"\\&quot;string\\&quot;\">u\\&#8217;\u51c6\u786e\u7387\uff1a%f,\u82b1\u8d39\u65f6\u95f4\uff1a%.2fs\\&#8217;<\/span> %(acc,time.time()-t)<\/div><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\u901a\u8fc7\u4f7f\u7528Scikit-Learn\uff0c\u5927\u5927\u7b80\u5316\u4e86\u4ee3\u7801\u91cf\u548c\u5efa\u6a21\u5de5\u4f5c\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u66f4\u591a\u7684\u5173\u6ce8\u4f7f\u7528\u573a\u666f\uff0c\u89e3\u51b3\u5b9e\u9645\u9700\u6c42\u3002\n\n<\/div>\n<\/div>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>1. \u57fa\u672c\u6982\u5ff5 a. \u6570\u636e\u6316\u6398 \u4ece\u6d77\u91cf\u6570\u636e\u627e\u5bfb\u6709\u7528\u4fe1\u606f\u3002\u4ece\u4e8b\u8be5\u7c7b\u5de5\u4f5c\u7684\u6280\u672f\u662fBI\uff08\u5546\u4e1a\u667a\u80fd\uff09\u3002\u7b80\u5355\u8bf4\uff0c\u5728Exc &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=2205\"> <span class=\"screen-reader-text\">Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":5707,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[143],"tags":[],"uagb_featured_image_src":{"full":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"thumbnail":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-150x150.jpg",150,150,true],"medium":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-300x200.jpg",300,200,true],"medium_large":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"large":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"1536x1536":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"2048x2048":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"mailpoet_newsletter_max":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb.jpg",600,400,false],"woocommerce_thumbnail":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-50x50.jpg",50,50,true],"woocommerce_single":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-80x53.jpg",80,53,true],"woocommerce_gallery_thumbnail":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-100x100.jpg",100,100,true],"shop_catalog":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-50x50.jpg",50,50,true],"shop_single":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-80x53.jpg",80,53,true],"shop_thumbnail":["http:\/\/www.weijing.co\/wp-content\/uploads\/2020\/07\/Python\u4e0e\u673a\u5668\u5b66\u4e60\u7b80\u4ecb-100x100.jpg",100,100,true]},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"1. \u57fa\u672c\u6982\u5ff5 a. \u6570\u636e\u6316\u6398 \u4ece\u6d77\u91cf\u6570\u636e\u627e\u5bfb\u6709\u7528\u4fe1\u606f\u3002\u4ece\u4e8b\u8be5\u7c7b\u5de5\u4f5c\u7684\u6280\u672f\u662fBI\uff08\u5546\u4e1a\u667a\u80fd\uff09\u3002\u7b80\u5355\u8bf4\uff0c\u5728Exc&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2205"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2205"}],"version-history":[{"count":4,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2205\/revisions"}],"predecessor-version":[{"id":5727,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2205\/revisions\/5727"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/media\/5707"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2205"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}