{"cells":[{"metadata":{"_cell_guid":"6deaa4c7-a3f9-4bbf-88d8-d63d0c776b29","_uuid":"713adf438ef4a53b7d0eab579ce7165aeafffa5e","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"#data_dir = \"../input/digit-recognizer/\"\ndata_dir = '../input/Kannada-MNIST/'\ntrain_row = 60000 #max = 60000\nmax_k = 10\nrun_find_k = train_row <= 5000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(data_dir + \"train.csv\")\ntrain #(60000, 785)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(data_dir + \"test.csv\")\ntest #(5000, 785)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"168d9f23-4270-4f38-afab-19696af78ed5","_uuid":"7dd973e6ba26a736a0e717408e9dab866e210fa7","trusted":true},"cell_type":"code","source":"Origin_X_train = train.values[0:train_row,1:] \nOrigin_y_train = train.values[0:train_row,0] \nOrigin_y_test = test.values[:,1:]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"838cd2e2-49db-4bcf-abc8-615223d7d0f8","_uuid":"48f88dd50f29cdbbf8b322c3d5f4b4f3a3f3dca6","trusted":true},"cell_type":"code","source":"print(Origin_X_train.shape, Origin_y_train.shape, Origin_y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fdaf0001-2d4e-4826-a9a8-027ae989e74e","_uuid":"7657d65ad6239e284f136fd5ce26bd8783411cd6"},"cell_type":"markdown","source":"# Display Samples"},{"metadata":{"_cell_guid":"5cd554f6-f83b-4d4b-bb14-4b027ca415ba","_uuid":"d290065f23d2a5802a39f14c592a2c7336d434ae","scrolled":true,"trusted":true},"cell_type":"code","source":"import matplotlib\nimport matplotlib.pyplot as plt\nrow = 6\n\nprint ('Label is: ' + str(Origin_y_train[row]))\n\nplt.imshow(Origin_X_train[row].reshape((28, 28)))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5a8d2ab8-217f-4ca6-8aca-dfc26db58679","_uuid":"240c54ff4db9ec7f1525afbf09e82c6e91e76d4b","trusted":true},"cell_type":"code","source":"classes = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\nrows = 6\n\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":"eaace04d-2bfd-47d3-bc1d-74d13c3a64d0","_uuid":"61c45cb2767da8f26bade9c010ba7ae44eff0ec5","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)\n\nprint(X_train.shape, X_vali.shape, y_train.shape, y_vali.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7d4c0fcd1e4eae2d8ddcaaf95cba8f00ace98b3"},"cell_type":"code","source":"class knn():\n    def __init__(self):\n        pass\n\n    def train(self, X, y):\n        self.X_train = X\n        self.y_train = y\n\n    def predict(self, X, k=3):\n        dataSet = self.X_train\n        labels = self.y_train\n      \n        dataSetSize = dataSet.shape[0]\n        diffMat = np.tile(X,(dataSetSize,1)) - dataSet\n        sqDiffMat = diffMat**2\n        sumDiffMat = sqDiffMat.sum(axis=1)\n        distances = sumDiffMat**0.5\n        sortedDistances = distances.argsort()\n        \n        classCount = {}\n        \n        for i in range(k):\n            vote = labels[sortedDistances[i]]\n            classCount[vote] = classCount.get(vote,0) + 1\n        max = 0\n        ans = 0\n        for k,v in classCount.items():\n            if v > max:\n                ans = k\n                max = v\n        return ans","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9e4ec226-4902-47e4-92ad-54485b0c37a8","_uuid":"5c01492bc2442e255f55bc9a79f6b6b852dad933","trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nclassifier = knn()\nclassifier.train(X_train, y_train)\n\ndef find_k():\n    max = 0\n    ans_k = 0\n\n    for k in range(1, max_k):\n        print ('when k = ' + str(k) + ', start training')\n        predictions = np.zeros(len(y_vali))\n        for i in range(X_vali.shape[0]):\n            if i % 500 == 0:\n                print(\"Computing  \" + str(i+1) + \"/\" + str(int(len(X_vali))) + \"...\")\n            output = classifier.predict(X_vali[i], k)\n            predictions[i] = output\n        accuracy = accuracy_score(y_vali, predictions)\n        print ('k = '+ str(k) , ' accuracy =' + str(accuracy))\n        if max < accuracy:\n            ans_k = k\n            max = accuracy\n    print(\"best k =\" + str(ans_k))\n    return ans_k","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fa7e9dd1-6fe5-4ba4-bdcb-f1be5caacdbe","_uuid":"dec77fd602cafaeccef13034b1749edf83abfcf2","trusted":true},"cell_type":"code","source":"k = find_k() if run_find_k else 3\npredictions = np.zeros(Origin_y_test.shape[0])\nfor i in range(Origin_y_test.shape[0]):\n    if i % 100 ==0:\n        print(\"Computing  \" + str(i+1) + \"/\" + str(int(len(Origin_y_test))) + \"...\")\n    predictions[i] = classifier.predict(Origin_y_test[i], k)\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9d09f5db-1bf8-48e8-a730-7c926f5b9bab","_uuid":"7304aab0deb7183c93b78f078b4ae2a35e7ea17c","trusted":true},"cell_type":"code","source":"print (predictions[4905])\nplt.imshow(Origin_y_test[4905].reshape((28, 28)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for y, cls in enumerate(classes):\n    idxs = np.nonzero([i == y for i in predictions])\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_y_test[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":{"trusted":true},"cell_type":"code","source":"sample_submisison = pd.read_csv(data_dir + \"sample_submission.csv\")\nsample_submisison #(60000, 785)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5a85ba45-6728-43a4-a70d-b695a8e8363a","_uuid":"39a4e43702e2988f98a88d700bb47d253a312ff2","trusted":true},"cell_type":"code","source":"print(len(predictions))\nout_file = open(\"submission.csv\", \"w\")\nout_file.write(\"id,label\\n\")\nfor i in range(len(predictions)):\n    out_file.write(str(i) + \",\" + str(int(predictions[i])) + \"\\n\")\nout_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91d8533a1369f9dd36b07620ab6da8bb7bb0316d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}