{"cells":[{"metadata":{"_cell_guid":"9c450404-15b7-440a-ac77-0a10335f4bb8","_uuid":"de4c1378d5ffa1faeb45bb0b66d58f9334cccf63"},"cell_type":"markdown","source":"# KNN - Digit Recognizer\nhttps://www.kaggle.com/c/digit-recognizer"},{"metadata":{"_cell_guid":"8ea01555-1caf-4a5f-ae83-fa7ff1c8406c","_uuid":"b58275e430559b81714f698ada2bbf3606d9b269","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":"c4dee561-4756-4a47-a4b8-456966dcbcf3","_uuid":"ae97bd8227ec697fd6ac901f6f2871b60927536a"},"cell_type":"markdown","source":"Import dataset ( numpy arrays) from `../train.csv` and `../test.csv, will take about 10sec.\n\nand print out their shape to check data size."},{"metadata":{"_cell_guid":"c0cbbd24-d6bd-4376-8c89-5feffe486a95","_uuid":"4007e394ca30a228ee98791fe36d69fe6a1bb821","trusted":true},"cell_type":"code","source":"\ndata_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#     print(train.head())\n#     print(train.shape)\n#     print(train.head())\n#     print(train[0:])\n    X_train = train.values[0:train_row,1:] # 取下标为1-784的列（pixel0 - pixel783）\n    y_train = train.values[0:train_row,0] # 取下标为0的列 (label)\n    \n    \n    Pred_test = pd.read_csv(data_dir + \"test.csv\").values  # 解释 value\n#     print(Pred_test.shape)\n#     print(pd.read_csv(data_dir + \"test.csv\").head())\n    return X_train, y_train, Pred_test\n\ntrain_row = 42000    # partially take the data, the max = 42000\nOrigin_X_train, Origin_y_train, Origin_y_test = load_data(data_dir, train_row)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aaff4eed20ab363f86d113812c2ff5ae1402378d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ace2c824-f92d-459d-b666-1d8c385a43d8","_uuid":"3b6486700b5cd46339559211f603fbbaefad30d9","trusted":true},"cell_type":"code","source":"print(Origin_X_train.shape, Origin_y_train.shape, Origin_y_test.shape)\nprint(Origin_X_train)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6ee268c8-2d12-4692-a036-193710bd2c75","_uuid":"4c97800b5663eaf06433f299ceb6cf4d9b2b068d"},"cell_type":"markdown","source":"### Show digit image"},{"metadata":{"_cell_guid":"bb658c09-2fab-4d7f-8010-574337721f77","scrolled":true,"_uuid":"04c3fbf7b93eae9ee6780d74d2b0fbc5ef37f4ff","trusted":true},"cell_type":"code","source":"import matplotlib\nimport matplotlib.pyplot as plt\nrow = 3\n\n# print (X_train[row].reshape((28, 28)))\n\nprint (Origin_y_train[row])\n\nplt.imshow(Origin_X_train[row].reshape((28, 28)))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"24ab085a-5039-4840-ace1-10d46e214808","_uuid":"87ffb664a20eb64050f44ead2173511106142804","trusted":true},"cell_type":"code","source":"classes = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\nrows = 4\n\nprint(classes)\nfor y, cls in enumerate(classes):\n    idxs = np.nonzero([i == y for i in Origin_y_train])\n    idxs = np.random.choice(idxs[0], rows)\n    for i , idx in enumerate(idxs):\n        plt_idx = i * len(classes) + y + 1\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)\n        \n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"440013a9-153e-47e6-8255-d663ba85fee4","_uuid":"30aea10d79c1d43d4bdc383d340abffc7d60ffca"},"cell_type":"markdown","source":"Divide the imported dataset into 20% training set（8400），80% test set（33600）"},{"metadata":{"_cell_guid":"e4263dbb-7c0c-4452-bad5-0c3a3093b5ea","_uuid":"9945c86722870e2ba5dba14f07f20c08603a1255","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train,X_test, y_train, y_test = train_test_split(Origin_X_train,\n                                                   Origin_y_train,\n                                                   test_size = 0.2,\n                                                   random_state = 0)\n# 解释一下random 那个随机函数随机种子是什么。 随机种子一样，那么结果是一样的。\n\nprint(X_train.shape, X_test.shape, y_train.shape, y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0d298ec1-7661-483d-a278-a7388720f53a","_uuid":"4fae022c3182bcdfad51d7d40564eb0ce9a246f3"},"cell_type":"markdown","source":"\nTo train the KNN model, it may take 1.5hr for completed dataset."},{"metadata":{"_cell_guid":"e26f77ca-387b-4368-8eda-03f9a5649ba7","scrolled":false,"_uuid":"186f1aacb710d5dd71e71a690ac0aba5081447c6","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\n\nk_range = range(1,8)\nscores = []\n\n# for each K, check its accuracy score\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)\n    y_pred = knn.predict(X_test)\n    accuracy = accuracy_score(y_test,y_pred)\n    scores.append(accuracy)\n    end = time.time()\n    print(classification_report(y_test, y_pred))  \n    print(confusion_matrix(y_test, y_pred))  \n    \n    print(\"Complete time: \" + str(end-start) + \" Secs.\")\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"dde901a9-bfc9-46ea-9378-5b9bd5ce4e28","_uuid":"a6730619ea4de386d690eaf905ab54142dfb9224"},"cell_type":"markdown","source":"### Find out the best result, print out its Accuracy"},{"metadata":{"_cell_guid":"f859c144-4f29-4670-bd7d-ce1a2f190557","_uuid":"cdf60fdb66a13fa58f2282062a70f6ef1cbfe5a9","trusted":true},"cell_type":"code","source":"\nprint (scores)\nplt.plot(k_range,scores)\nplt.xlabel('Value of K')\nplt.ylabel('Testing accuracy')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"db0d6a7a-155f-4bd6-b219-5d72c02515fa","_uuid":"d14e8c5b74750eb6d04bc036c23c2190028adee8"},"cell_type":"markdown","source":"### Predict on test dataset\n"},{"metadata":{"_cell_guid":"d72804f2-d2a6-4e8e-8c91-884fd6eb5a78","_uuid":"914ef29e209cd5464b0e469cd9f21214fd7f2ae8","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_y_test)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"180878cf-f145-4720-9348-763cdbdca4f5","_uuid":"553cfd24a3b3c009c0d0db31d52cbabb7ffbe8f2","trusted":true},"cell_type":"code","source":"print (y_pred[200])\nplt.imshow(Origin_y_test[200].reshape((28, 28)))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d2cdf65b-3b59-4cb5-9a17-3dc0df02323d","_uuid":"e57b771a20ce1180ad1619035c87c537821f9d9f"},"cell_type":"markdown","source":"### After done prediction, export result into Digit_Recogniser_Result.csv, with following format \n\n* ImageId\tLabel\n* 1\t2\n* 2\t0\n* 3\t9\n* 4\t9"},{"metadata":{"_cell_guid":"7c47dc75-1e84-4e6d-a836-80a9e623f68f","_uuid":"c833491642816637467711cdc2c0b299b7b3ed18","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)),\"Label\": y_pred}).to_csv('Digit_Recogniser_Result.csv', index=False,header=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cc1b70fb8bf5db70df4337a5f60d497f3a627970"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccb76cd25a717d15b8415a8f8bce07d12a19ee3c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00d284572b334ad5a449677e9833b8cf3b7ecb3d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}