{"id":657,"date":"2017-06-25T14:19:19","date_gmt":"2017-06-25T06:19:19","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=657"},"modified":"2023-03-05T19:23:13","modified_gmt":"2023-03-05T11:23:13","slug":"python%e5%9f%ba%e4%ba%8e%e9%bc%a0%e6%a0%87%e8%bd%a8%e8%bf%b9%e7%9a%84%e4%ba%ba%e6%9c%ba%e9%aa%8c%e8%af%81%e5%88%86%e7%b1%bb%e9%97%ae%e9%a2%98%e8%b5%9b%e6%b0%aa-%e5%a4%a7%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=657","title":{"rendered":"[python]\u57fa\u4e8e\u9f20\u6807\u8f68\u8ff9\u7684\u4eba\u673a\u9a8c\u8bc1\u5206\u7c7b\u95ee\u9898(\u8d5b\u6c2a\u2014\u2014\u5927\u6570\u636e\u6311\u6218\u8d5b)baseline"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,391<\/span><span class='epvc-label'> Views<\/span><\/div><h3>\u524d\u8a00<\/h3>\n<p>\u4e00\u4e2a\u6708\u7684mis\u7cfb\u7edf\u5f00\u53d1\u5f04\u5f97\u5934\u6655\u773c\u82b1\uff0c\u803d\u8bef\u4e86\u4e0d\u5c11\u65f6\u95f4\uff0c\u597d\u5728\u8fd9\u4e2a\u6bd4\u8d5b\u65f6\u95f4\u5468\u671f\u5f88\u957f\uff0c\u800c\u4e14\u9898\u76ee\u4e0d\u7b97\u592a\u96be\uff0c\u4e2a\u4eba\u611f\u89c9\u5bf9\u65b0\u4eba\u6bd4\u8f83\u53cb\u597d\uff08\u9664\u4e86\u6bcf\u5929\u53ea\u80fd\u4e0a\u4f20\u4e00\u6b21\u4ee5\u5916\uff09\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u505a\u5f00\u53d1\u7684\u65f6\u5019\u5fd9\u91cc\u5077\u95f2\u63d0\u4e86\u4e00\u4e9b\u7279\u5f81\uff0c\u53ea\u7528\u7ecf\u5178\u7684\u51b3\u7b56\u6811\u548c4\u4e2a\u7279\u5f81\u5c31\u8fbe\u5230\u4e86\u5c06\u8fd160\u5206\uff0c\u987f\u65f6\u4fe1\u5fc3\u7206\u68da\uff08\u867d\u7136\u8bf4\u4e8c\u5206\u7c7b\u95ee\u9898\u778e\u8499\u53ef\u80fd\u5c31\u662f50\u5206\u3002\u3002\u3002\/\uff09\u3002<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-658\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike1.png\" alt=\"\" width=\"807\" height=\"42\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike1.png 807w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike1-300x16.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike1-768x40.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/a><br \/>\n\u53d7\u5230\u4e00\u4e9bdalao\u7684\u542f\u53d1\uff0c\u8003\u8651\u5728\u4e0d\u63d0\u7279\u5f81\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u5206\u7c7b\uff0c\u628a\u8fd9\u4e2a\u5f53\u505abaseline\u987a\u4fbf\u4e3a\u4ee5\u540e\u505a\u9884\u6d4b\u589e\u52a0\u53ef\u80fd\u6027\u4e5f\u662f\u6781\u597d\u7684\u3002<\/p>\n<h4><strong>\u3010\u672c\u6587\u9002\u5408\u6709\u4e00\u4e9b\u6570\u636e\u6316\u6398\u57fa\u7840\u7684\u5c0f\u767d\u89c2\u770b\uff0cdalao\u8bf7\u65e0\u89c6\u3011<\/strong><\/h4>\n<h3>\u6bd4\u8d5b\u6570\u636e<\/h3>\n<p>\u672c\u9898\u76ee\u6570\u636e\u6765\u6e90\u4e8e\u67d0\u4eba\u673a\u9a8c\u8bc1\u4ea7\u54c1\u91c7\u96c6\u7684\u9f20\u6807\u8f68\u8ff9\uff0c\u7ecf\u8fc7\u8131\u654f\u5904\u7406\uff0c\u6570\u636e\u5206\u4e3a3\u90e8\u5206\uff08\u6570\u636e\u91cf\u5206\u522b\u4e3a3000\u6761\uff0c10\u4e07\uff0c200\u4e07\uff09\u3002<\/p>\n<p>\u8bad\u7ec3\u6570\u636e\u8868\u540d\u79f0\uff1adsjtzs_txfz_training<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u5b57\u6bb5<\/td>\n<td>\u7c7b\u578b<\/td>\n<td>\u89e3\u91ca<\/td>\n<\/tr>\n<tr>\n<td>a1<\/td>\n<td>bigint<\/td>\n<td>\u7f16\u53f7id<\/td>\n<\/tr>\n<tr>\n<td>a2<\/td>\n<td>string<\/td>\n<td>\u9f20\u6807\u79fb\u52a8\u8f68\u8ff9(x,y,t)<\/td>\n<\/tr>\n<tr>\n<td>a3<\/td>\n<td>string<\/td>\n<td>\u76ee\u6807\u5750\u6807(x,y)<\/td>\n<\/tr>\n<tr>\n<td>label<\/td>\n<td>string<\/td>\n<td>\u7c7b\u522b\u6807\u7b7e\uff1a1-\u6b63\u5e38\u8f68\u8ff9\uff0c0-\u673a\u5668\u8f68\u8ff9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2dt\u4e3a\u91c7\u6837\u65f6\u90a3\u4e00\u65f6\u523b\u7684\u65f6\u95f4\uff08\u8131\u654f\uff09\u3002\u8bc4\u5206\u65b9\u5f0f\u5c31\u4e0d\u5217\u4e86\uff0c\u56e0\u4e3a\u6700\u540e\u6bd4\u8d5b\u4f1a\u66f4\u6539\u79ef\u5206\u89c4\u5219\uff0c\u4f46\u662f\u6709\u5fc3\u4eba\u53ef\u4ee5\u6839\u636e\u73b0\u6709\u7684\u8bc4\u5206\u89c4\u5219\u63a8\u6d4b\u51fa\u81ea\u5df1\u63d0\u4ea4\u7ed3\u679c\u7684\u6b63\u786e\u6570\u76ee\u263a<\/p>\n<h3>\u6570\u636e\u5206\u6790<\/h3>\n<p>\u4e0b\u8f7d\u5b8c\u6570\u636e\u96c6\u540e\uff0c\u6253\u5f00txt\u3002\u8bad\u7ec3\u96c6\u7684\u6570\u636e\u683c\u5f0f\u4e3a[id \u82e5\u5e72\u4e2ax,y,t\u5750\u6807 target\u5750\u6807 label]\uff0c\u6d4b\u8bd5\u96c6\u4ec5\u4ec5\u5c11\u4e86\u4e00\u4e2alabel<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-659\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike2.png\" alt=\"\" width=\"791\" height=\"250\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike2.png 791w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike2-300x95.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike2-768x243.png 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/a><\/p>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<br \/>\n\u4f7f\u7528pandas\u8bfb\u53d6\u6587\u4ef6\u5f88\u65b9\u4fbf\uff0c\u76f4\u63a5\u8bfb\u53d6\u540e\u4fdd\u5b58\u5728dataframe\u4e2d<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n# -*- coding: utf-8 -*-\nimport numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom matplotlib.pyplot import plot,savefig\n\nTRAIN_DATA=&#039;..\/data\/dsjtzs_txfz_training.txt&#039;\nTEST_DATA=&#039;..\/data\/dsjtzs_txfz_test1.txt&#039;\nTRAIN=&#039;..\/data\/train.txt&#039;\nTEST=&#039;..\/data\/test.txt&#039;\nRESULT=&#039;..\/tmp\/res&#039;\n\n#\u8bfb\u53d6\u6587\u4ef6\ndef read_file(filepath):\n    data = pd.read_table(filepath,header=None,encoding=&#039;utf-8&#039;,delim_whitespace=True)\n    return data\n<\/code><\/pre>\n<p>\u73b0\u5728dataframe\u4e2d\u6570\u636e\u7684\u683c\u5f0f\u4e3a<br \/>\nid x,y,t;x,y,t;&#8230;x,y,t target_X,target_y label<br \/>\n\u7b2c\u4e8c\u5217\u7531\u4e8ex,y,t&#8230;\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e14\u7531\u5206\u53f7\u9694\u5f00\uff0c\u4e0d\u65b9\u4fbf\u5728dataframe\u7ee7\u7eed\u64cd\u4f5c\uff0c\u6240\u4ee5\u8fd9\u91cc\u76f4\u63a5\u904d\u5386dataframe\u53d6\u51fax,y,t\u5206\u522b\u5b58\u5728\u4e09\u4e2alist<br \/>\n\u501f\u52a9\u4e4b\u524d\u7684read_file\u51fd\u6570\uff0c\u65b0\u5efa\u4e00\u4e2a\u521b\u5efa\u6807\u51c6\u6570\u636e\u96c6\u7684\u51fd\u6570loadDataSets:<br \/>\n(\u8003\u8651\u8fd9\u4e2a\u51fd\u6570\u8981\u5904\u7406\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u800c\u6d4b\u8bd5\u96c6\u6ca1\u6709\u6807\u7b7e\uff0c\u6240\u4ee5\u7ed9\u6d4b\u8bd5\u96c6\u9ed8\u8ba4\u8d4b\u6807\u7b7e\u4e3a1)<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u521b\u5efa\u6837\u672c\u96c6\ndef loadDataSets(filepath,outpath):\n    data=read_file(filepath)\n    for ix,row in data:\n        trail_x,trail_y,trail_t=get_trail(row)\n        target=str(row[2]).split(&#039;,&#039;)\n        label=str(row[3]) if len(row)==4 else -1#\u9ed8\u8ba4\u6d4b\u8bd5\u96c6\u6807\u7b7e\u4e3a-1\n<\/code><\/pre>\n<p>\u518d\u8865\u4e0aget_trail\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u83b7\u53d6\u79fb\u52a8\u8f68\u8ff9\u5750\u6807\u548c\u65f6\u95f4\ndef get_trail(row):\n    trail_x,trail_y,trail_t=[],[],[]\n    loc=str(row[1]).split(&#039;;&#039;)\n    for x_y_t in loc:\n        x_y=x_y_t.split(&#039;,&#039;)\n        if len(x_y) &gt;=3:\n            trail_x.append(x_y[0])\n            trail_y.append(x_y[1])\n            trail_t.append(x_y[2])\n    return trail_x,trail_y,trail_t\n<\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u6837\u5f53\u7136\u4e0d\u7b97\u5b8c\uff0c\u53ea\u662f\u83b7\u53d6\u4e86\u8f68\u8ff9\u7684x,y,t\u4e09\u4e2alist\uff0c\u5982\u4f55\u80fd\u4f5c\u4e3a\u8bad\u7ec3\u6570\u636e\uff1f<\/p>\n<p>\u5728\u4e0d\u63d0\u53d6\u7279\u5f81\u7684\u60c5\u51b5\u4e0b\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u628a\u6240\u6709\u7684x\u5750\u6807\u5f53\u505a\u7279\u5f81\uff0c\u7528\u8fd9\u4e9b\u7279\u5f81\u4ea7\u751f\u8bad\u7ec3\u6a21\u578b(\u4e0d\u7528t\u662f\u56e0\u4e3at\u7684\u7ef4\u5ea6\u548cx\u4e0d\u4e00\u6837\uff0c\u4e00\u4e2a\u662f\u65f6\u95f4\uff0c\u4e00\u4e2a\u662f\u5750\u6807\u70b9\uff0c\u540e\u9762\u518d\u8003\u8651\u628ay,t\u53d8\u6210\u7279\u5f81\u52a0\u5165\uff0c\u4e0d\u8fc7\u8981\u505a\u5f52\u4e00\u5316\uff0c\u770b\u770b\u600e\u4e48\u53d6\u6548\u679c\u66f4\u597d)<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u5e76\u975e\u6240\u6709\u6837\u672c\u7684\u5750\u6807\u6570\u90fd\u4e00\u6837\uff0c\u7b80\u5355\u7edf\u8ba1\u540e\u53d1\u73b0\u6240\u6709\u6837\u672c\u7684\u5750\u6807\u6570\u4e0d\u8d85\u8fc7300\u4e2a\uff0c\u6240\u4ee5\u8fd9\u91cc\u9ed8\u8ba4\u7ed9\u5750\u6807\u6570\u91cf\u4e0d\u591f\u7684\u6837\u672c\u5168\u90e8\u586b\u51450\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4e3a\u4e86\u65b9\u4fbf\u540e\u7eed\u5904\u7406\uff0c\u628a\u4ea7\u751f\u7684\u7279\u5f81\u653e\u5165dataframe\u4e2d\u3002<br \/>\n\u66f4\u6539loadDataSets\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u521b\u5efa\u6837\u672c\u96c6\ndef loadDataSets(filepath,outpath):\n    train_data=read_file(filepath)\n    col_name=[&#039;id&#039;]#col_name\u662fdataframe\u6bcf\u4e00\u5217\u7684\u540d\u79f0\n    all_data=[]\n    for i in range(300):\n        col_name.append(&#039;x&#039;+str(i))\n        col_name.append(&#039;target_x&#039;)\n        col_name.append(&#039;label&#039;)\n    for ix, row in train_data.iterrows():\n        each_row=[]\n        each_row.append(row[0])#id\n        trail_x,trail_y,trail_t=get_trail(row)\n        target=str(row[2]).split(&#039;,&#039;)\n        label=str(row[3]) if len(row)==4 else 1#\u9ed8\u8ba4\u6d4b\u8bd5\u96c6\u6807\u7b7e\u4e3a1\n        loc_len=len(trail_x)\n        for i in range(300-loc_len):#\u524d\u4f4d\u5168\u90e8\u586b\u51450\n            each_row.append(0)\n        for i in range(loc_len):#\u540e\u4f4d\u586b\u5145\u771f\u5b9e\u6570\u636e\n            each_row.append(trail_x[i])\n        each_row.append(target[0])#\u7ec8\u70b9x\u5750\u6807\n        each_row.append(label)#\u6807\u7b7e\n        all_data.append(each_row)#\u4e00\u4e2a\u6837\u672c\u7684\u7279\u5f81list\u6dfb\u52a0\u5230\u603b\u6837\u672c\u7279\u5f81list\n    dataSet=pd.DataFrame(all_data)\n    dataSet.columns=col_name\n    return dataSet\n<\/code><\/pre>\n<p>\u83b7\u53d6\u4e86\u7279\u5f81\u4ee5\u540e\uff0c\u65b0\u5efa\u4e00\u4e2amain\u51fd\u6570\u6d4b\u8bd5\u8f93\u51fa\u4e00\u4e0b\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u4e3b\u51fd\u6570\nif __name__ == &quot;__main__&quot;:\n    dataSet=loadDataSets(TRAIN_DATA)\n    print dataSet\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike3.png\" alt=\"\" width=\"544\" height=\"526\" class=\"alignnone size-full wp-image-663\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike3.png 544w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike3-300x290.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike4.png\" alt=\"\" width=\"273\" height=\"510\" class=\"alignnone size-full wp-image-664\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike4.png 273w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike4-161x300.png 161w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/a><\/p>\n<p>\u7279\u5f81\u662f\u5f97\u5230\u4e86\uff0c\u4ea7\u751f\u7279\u5f81\u96c6\u7684\u8fc7\u7a0b\u5f88\u6162\uff0c\u5c24\u5176\u662f\u5bf9\u6d4b\u8bd5\u96c610\u4e07\u6570\u636e\u7684\u5904\u7406\uff0c\u6240\u4ee5\u628a\u6bcf\u6b21\u4ee3\u7801\u8fd0\u884c\u65f6\u6ca1\u6709\u53d8\u5316\u7684\u5e9e\u5927\u6570\u636e\u53ca\u65f6\u5b58\u6863\u5c24\u4e3a\u91cd\u8981\u3002\u505a\u8d77\u6765\u4e5f\u5f88\u7b80\u5355\uff0c\u5728loadDataSets\u51fd\u6570\u7684return\u4e4b\u95f4\u6dfb\u52a0\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\ndataSet.to_csv(outpath,index=False)\n<\/code><\/pre>\n<p>\u4e4b\u540e\u518d\u8fd0\u884c\u4e00\u6b21\u4ee3\u7801\uff0c\u4ea7\u751f\u4e86train.txt\u540e\u5c31\u53ef\u4ee5\u6ce8\u91ca\u6389main\u4e2d\u7684loadDataSets\u4e86<\/p>\n<p>\u73b0\u5728\u8fd8\u9700\u8981\u505a\u7684\u662f\uff0c\u521b\u5efa\u4ea4\u53c9\u9a8c\u8bc1\u96c6\uff08\u9884\u4f30\u5206\u7c7b\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff0c\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u4ea4\u53c9\u9a8c\u8bc1\u7684\u7ed3\u679c\u8c03\u6574\u5206\u7c7b\u5668\u7684\u53c2\u6570\uff09\u3002\u540c\u65f6\u4e3a\u4e86\u8ba9\u6574\u4e2a\u6d41\u7a0b\u4ee3\u7801\u4e0d\u8981\u8fc7\u4e8e\u81c3\u80bf\uff0c\u628a\u5bf9\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u7684\u64cd\u4f5c\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u521b\u5efacreate_train_and_test:<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u521b\u5efa\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\ndef create_train_and_test(model=&#039;train&#039;):\n    train_data=pd.read_csv(TRAIN)\n    train=train_data.drop([&#039;id&#039;,&#039;label&#039;],axis=1)#\u7279\u5f81\u96c6\u4e0d\u80fd\u6709id\u548clabel\n    label=train_data[&#039;label&#039;]#\u6807\u7b7e\u96c6\n    if model==&#039;train&#039;:\n        from sklearn.model_selection import train_test_split\n        x_train, x_test, y_train, y_test = train_test_split(train, label, test_size = 0.4)\n    elif model==&#039;test&#039;:\n        x_train=train\n        y_train=label\n        test_data=pd.read_csv(TEST)\n        x_test=test_data.drop([&#039;id&#039;,&#039;label&#039;],axis=1)\n        y_test=test_data[&#039;label&#039;]\n    return x_train,y_train,x_test,y_test\n<\/code><\/pre>\n<p>\u4f7f\u7528sklearn\u63d0\u4f9b\u7684\u5206\u7c7b\u5668\u5206\u7c7b\uff0c\u8fd9\u91cc\u5148\u5c1d\u8bd5\u4f7f\u7528knn\uff0c\u521b\u5efaalgorithm\u5e76\u4fee\u6539main\u51fd\u6570:<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u5206\u7c7b\u5e76\u9884\u6d4b\ndef algorithm(x_train,y_train,x_test,y_test):\n    from sklearn import neighbors\n    clf = neighbors.KNeighborsClassifier()\n    clf.fit(x_train,y_train)\n    y_pred = clf.predict(x_test)\n    from sklearn.metrics import classification_report\n    print classification_report(y_test, y_pred)\n\n#\u4e3b\u51fd\u6570\nif __name__ == &quot;__main__&quot;:\n    #train=loadDataSets(TRAIN_DATA)\n    x_train,y_train,x_test,y_test=create_train_and_test(&#039;train&#039;)\n    algorithm(x_train,y_train,x_test,y_test)\n<\/code><\/pre>\n<p>\u4ea4\u53c9\u9a8c\u8bc1\u7684\u7ed3\u679c\uff1a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike5.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike5.png\" alt=\"\" width=\"372\" height=\"93\" class=\"alignnone size-full wp-image-666\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike5.png 372w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike5-300x75.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><br \/>\nsklearn\u7684\u7b97\u5206\u65b9\u5f0f\u548c\u5b98\u65b9\u4e0d\u540c\uff0c\u4f46\u662f\u5206\u7c7b0\u7684\u51c6\u786e\u7387\u548c\u53ec\u56de\u7387\u662f\u53ef\u4ee5\u7528\u7684\u3002\u6309\u7167\u5b98\u65b9\u7684\u65b9\u5f0f\u518d\u7b97\u4e00\u904d\uff0c\u5f97\u5206\u4e3a\uff1a<br \/>\n5PR\/(2P+3R)*100=5*0.80*0.68\/(2*0.80+3*0.68)*100=2.72\/3.64*100=74.72%<\/p>\n<p>\u6700\u540e\uff0c\u5bf9\u6d4b\u8bd5\u96c6\u505a\u76f8\u540c\u7684\u5904\u7406\uff0c\u5e76\u628a\u7ed3\u679c\u5bfc\u51fa\u5230\u6587\u4ef6\uff0c\u6dfb\u52a0write_to_result\uff0c\u5e76\u6539\u5199main\u548calgorithm\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#\u5199\u6587\u4ef6\ndef write_to_result(y_pred,model=&#039;train&#039;):\n    res=[i+1 for i in range(len(y_pred)) if y_pred[i]==0]\n    res=[str(int(i)) for i in res]\n    import datetime\n    import time\n    i = str(datetime.datetime.now())\n    i=i.replace(&#039;:&#039;,&#039;-&#039;)\n    i=i.replace(&#039; &#039;,&#039;_&#039;)\n    i=i.replace(&#039;.&#039;,&#039;-&#039;)\n    f=open(RESULT+&#039;_&#039;+model+&#039;_&#039;+i+&#039;.txt&#039;,&#039;w&#039;)\n    f.write(&#039;\\n&#039;.join(res))\n    f.close()\n#\u5206\u7c7b\u5e76\u9884\u6d4b\ndef algorithm(x_train,y_train,x_test,y_test,model=&#039;train&#039;):\n    from sklearn import neighbors\n    clf = neighbors.KNeighborsClassifier()\n    clf.fit(x_train,y_train)\n    y_pred = clf.predict(x_test)\n    write_to_result(y_pred,model)\n    from sklearn.metrics import classification_report\n    print classification_report(y_test, y_pred)\n\nif __name__ == &quot;__main__&quot;:\n    train=loadDataSets(TRAIN_DATA)\n    test=loadDataSets(TEST_DATA)\n    model=&#039;test&#039;\n    x_train,y_train,x_test,y_test=create_train_and_test(model)\n    algorithm(x_train,y_train,x_test,y_test,model)\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike6.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike6.png\" alt=\"\" width=\"379\" height=\"117\" class=\"alignnone size-full wp-image-669\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike6.png 379w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike6-300x93.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/a><br \/>\n\u56e0\u4e3a\u6d4b\u8bd5\u96c6\u4e0d\u80fd\u5224\u65ad\u6b63\u786e\u7387\uff0c\u6240\u4ee5\u53ea\u8981\u6253\u5f00\u76f8\u5173\u7684\u7ed3\u679c\u6587\u4ef6res_test&#8230;..txt\u5927\u81f4\u770b\u4e00\u770b\u9884\u6d4b\u6570\u91cf\u6709\u591a\u5c11\u5c31\u884c\u4e86\uff0c\u8f83\u597d\u7684\u7ed3\u679c\u572820000\u4e2a\u5de6\u53f3\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u5206\u7c7b\u548c\u9884\u6d4b\u90fd\u5df2\u7ecf\u5b8c\u6210\uff0c\u540e\u7eed\u5de5\u4f5c\u9700\u8981\u6162\u6162\u8003\u8651\u4e86\u3002<br \/>\n\u5f97\u5206\uff1a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7.png\" alt=\"\" width=\"847\" height=\"46\" class=\"alignnone size-full wp-image-675\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7.png 847w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7-300x16.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7-768x42.png 768w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/06\/saike7-825x46.png 825w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/a><br \/>\nTo be continue&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,391 Views\u524d\u8a00 \u4e00\u4e2a\u6708\u7684mis\u7cfb\u7edf\u5f00\u53d1\u5f04\u5f97\u5934\u6655\u773c\u82b1\uff0c\u803d\u8bef\u4e86\u4e0d\u5c11\u65f6\u95f4\uff0c\u597d\u5728\u8fd9\u4e2a\u6bd4\u8d5b\u65f6\u95f4\u5468\u671f\u5f88\u957f\uff0c\u800c\u4e14 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-data-mining"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=657"}],"version-history":[{"count":14,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":679,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions\/679"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}