{"cells":[{"metadata":{"_cell_guid":"504c1194-6980-45aa-92f1-caf55b44dbb0","_uuid":"ebb70d7332fcafed13c1c963652c48285b531b76"},"cell_type":"markdown","source":"# 九章 KNN 教程 \n\nhttps://www.kaggle.com/c/digit-recognizer"},{"metadata":{"_cell_guid":"0604f6b3-046f-4b65-a1a9-3a5f8e824752","_uuid":"26283408c3c54401ae8c237dded11a5d54141159"},"cell_type":"markdown","source":"# 导入数据"},{"metadata":{"_cell_guid":"f206c2ba-7c09-4f26-ac95-e8a24dfb9ae7","_uuid":"11ab637213812ce08688490c3f6cef097d603ff2","trusted":true},"cell_type":"code","source":"import numpy as np # 数组常用库\nimport pandas as pd # 读入csv常用库\nimport matplotlib.pyplot as plt # 画图常用库","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"61d6082e-6ea1-4f92-9329-0848647a17c3","_uuid":"ae5b578dffd73ed55fcba8b4c011334d46f55027"},"cell_type":"markdown","source":"从 `../train.csv` and `../test.csv` 文件里面读入 numpy arrays.\n\n导入数据后打印shape查看是否数据是否完整\n\n导入数据差不多10秒"},{"metadata":{"_cell_guid":"cc5d4fe8-a0a6-4640-973c-94bfe7639635","_uuid":"8541a8ca7e77c1b6885d56f8b7b932b13f5f67c8","scrolled":true,"trusted":true},"cell_type":"code","source":"data_dir = \"../input/\"\n\n# load csv files to numpy arrays\ndef load_data(data_dir, train_row):\n    train = pd.read_csv(data_dir + \"train.csv\")\n    print(train.shape)\n    X_train = train.values[0:train_row, 1:] # pixels\n    y_train = train.values[0:train_row, 0] # labels\n    Pred_test = pd.read_csv(data_dir + \"test.csv\").values # data as a matrix, i.e. list of lists; data contains pixels withotu labels\n    \n    return X_train, y_train, Pred_test\n\ntrain_row = 5000\nOrigin_X_train, Origin_y_train, Origin_X_test = load_data(data_dir, train_row)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4c2b43d7-5930-4e98-af99-d74049771acc","_uuid":"df3d5ab5655dc2f05367d6c147f7423effcf1d68"},"cell_type":"markdown","source":"# 用图像展示一下数据集"},{"metadata":{"trusted":true,"_uuid":"3151467d7c5239e92ddb6ba638cf44a54e787891"},"cell_type":"code","source":"row = 3\nprint(Origin_y_train[row]) # 4\n# print(\"-----\")\n# print(Origin_X_train[row])# pixels of 4\n# print(\"-----\")\n# print(Origin_X_train[row].reshape(28, 28))# pixels of 4 in 28*28 matrix\nplt.imshow(Origin_X_train[row].reshape(28, 28)) # returns an image object, imshow() generates an image without showing it\nplt.show() # print the image","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5c654f1a-4c8c-4b7d-b1e8-5fae2ff048c5","_uuid":"4eb06a693881cc878ccd9fc9647af48b2743b0ce"},"cell_type":"markdown","source":"展示部分数据看看图片是什么样子的。（有兴趣的同学可以自己看）"},{"metadata":{"_cell_guid":"c2c6ec59-ebc1-4d4e-ae89-d5a9ef240835","_uuid":"761de08af4ee6c975fe406ed66f90f0bc88fe8ff","trusted":true},"cell_type":"code","source":"classes = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\nrows = 4\nprint(Origin_y_train)\n\nfor y, cls in enumerate(classes):\n    idxs = np.nonzero([i == y for i in Origin_y_train]) # record index of each class in Origin_Y_train\n    idxs = np.random.choice(idxs[0], rows) # idxs is an array with a list of index\n    for i, idx in enumerate(idxs):\n        plt_idx = i * len(classes) + y + 1 # plt_idx starts at 1, increments across rows first and has a maximum of nrows * ncols.\n        plt.subplot(rows, len(classes), plt_idx)\n        plt.imshow(Origin_X_train[idx].reshape(28, 28))\n        plt.axis(\"off\")\n        if i == 0:\n            plt.title(cls)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"16e64324-48af-492d-8f42-c6ac6e7a35c0","_uuid":"31a53b52bea59d427d33194aafd6103ad46cf601"},"cell_type":"markdown","source":"把原本的训练数据拆分成为20%测试集（8400个），80%训练集（33600个）\n\n怎么样2/8分"},{"metadata":{"_cell_guid":"ff788363-fa17-4748-bba8-6a4ae2487181","_uuid":"9f3273d2f90dc599f82c63f3bc829b779e282eb5","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_vali, y_train, y_vali = train_test_split(Origin_X_train,\n                                                    Origin_y_train,\n                                                    test_size = 0.2,\n                                                    random_state = 0) # random_state is the seed used by the random number generator (default None)\nprint(X_train.shape, X_vali.shape, y_train.shape, y_vali.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"68bc3069-1241-4dc1-a28c-46ca0d22da5d","_uuid":"d1d5b8c1013bbb069b93fc79791ddaa2d1607277"},"cell_type":"markdown","source":"\n下面进行KNN模型训练\n如果是大数据，需要1.5小时跑完"},{"metadata":{"_cell_guid":"93aec800-e499-4de7-a218-9d3231043b32","_uuid":"2d8a667aa10291fe03b528c4ab5f55eb7dcf9dd0","trusted":true},"cell_type":"code","source":"import time\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.neighbors import KNeighborsClassifier\n\nans_k = 0\nk_range = range(1, 8)\nscores = []\nfor k in k_range:\n    print(\"k = \" + str(k) + \" begin \")\n    start = time.time()\n    knn = KNeighborsClassifier(n_neighbors=k)\n    knn.fit(X_train, y_train) # training\n    y_pred = knn.predict(X_vali) # test\n    accuracy = accuracy_score(y_vali, y_pred) # accuracy\n    scores.append(accuracy)\n    end = time.time()\n    print(classification_report(y_vali, y_pred)) # precision, recall, f1-score, support; support means # of each class\n    print(confusion_matrix(y_vali, y_pred))\n    print(\"Complete time: \" + str(end - start) + \"Secs.\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"85741aa6-14e1-43e3-b12c-f00180ea9db8","_uuid":"e11013543394219528ba34e266c9134e9b340ebf"},"cell_type":"markdown","source":"# 通过训练我们可以找到最好的测试数据集，打印出Accuracy的图"},{"metadata":{"_cell_guid":"67390da3-6710-4bce-b0f1-8f364e5944e1","_uuid":"1862c48c0d9ccba622f7876d5baad0191bf32796","trusted":true},"cell_type":"code","source":"print(scores)\nplt.plot(k_range, scores)\nplt.xlabel(\"Value of K\")\nplt.ylabel(\"Testing accuracy\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0fc75b9d-87e5-4147-b74f-724d7bc93fcd","_uuid":"fdbf617c65d78718fe7793e7fc2e55ecadaaa04e"},"cell_type":"markdown","source":"# 预测测试数据集的结果\n"},{"metadata":{"_cell_guid":"87c41cfe-4863-4e7f-a38b-dabdf14b24a0","_uuid":"1d791d62060f3b6deb5215c89eb6e813b95bd536","trusted":true},"cell_type":"code","source":"k = 3\n\nknn = KNeighborsClassifier(n_neighbors=k)\nknn.fit(Origin_X_train, Origin_y_train)\ny_pred = knn.predict(Origin_X_test[:300]) # change to Origin_X_test for full test","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"02616849-0f6c-422f-82b1-f1b7a31c87c5","_uuid":"6890a00551cdf3ec76ee99a60965a670ed20c239"},"cell_type":"markdown","source":"# 验证一下结果"},{"metadata":{"_cell_guid":"6d774516-3d31-417a-b1eb-d7a8ffa6242e","_uuid":"f5456e57ecb856612c2be0e7b3089a5e4a34dd5a","trusted":true},"cell_type":"code","source":"print(y_pred[200])\nplt.imshow(Origin_X_test[200].reshape(28, 28))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"eadc71f0-386e-4318-8261-d3a0da05c0bd","_uuid":"401c578da3e3c6ea4180242bc5843d7a8cb00b65"},"cell_type":"markdown","source":"# 预测之后，我们把预测好的数据打印到文件Digit_Recogniser_Result.csv里面。\n# 打印的格式是如下\n\n* ImageId\tLabel\n* 1\t2\n* 2\t0\n* 3\t9\n* 4\t9"},{"metadata":{"_cell_guid":"a7c7dd6d-6d89-4ea9-9d37-41a6d3ecf048","_uuid":"6b467e130ce5d8f5656062c7eadf63621d171671","trusted":true},"cell_type":"code","source":"print(len(y_pred))\n\n# save submission to csv\npd.DataFrame({\"ImageId\": list(range(1, len(y_pred) + 1)),\n            \"Label\": y_pred}).to_csv(\"Digit_Recogniser_Result.csv\", index=False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"68bc74a9f47e83f03801843fd9dab6c736f87bb8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.0"}},"nbformat":4,"nbformat_minor":1}