{"cells":[{"metadata":{"_uuid":"f091e3732cce8a4c5160fd0b1710ffbd77263252"},"cell_type":"markdown","source":"# Nearest neighbor for handwritten digit recognition\nIn this notebook we will build a classifier that takes an image of a handwritten digit and outputs a label 0-9 using **nearest neighbor classifier**"},{"metadata":{"_uuid":"5e3a5386747419ecddfde30ee8d9a990b822c8f5"},"cell_type":"markdown","source":"## 1. The MNIST dataset\n`MNIST` is a dataset, consisting of 28x28 gray-scale images handwritten digits."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom numpy import genfromtxt\nfrom sklearn.neighbors import KDTree\n\nimport numpy as np \nimport pandas as pd \n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode('utf8'))\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"data_sample = \"../input/sample_submission.csv\"\ntest_sample = \"../input/test.csv\"\ntrain_sample = \"../input/train.csv\"\n\ntrain = genfromtxt(train_sample, delimiter=',')\ntest_data = genfromtxt(test_sample, delimiter=',')\nsubmssion = genfromtxt(data_sample, delimiter=',')\nfile = pd.read_csv(data_sample)\ndata = pd.read_csv(train_sample)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d61d6b1091fd3e6192ca19bb8cd8015739a1fd75"},"cell_type":"markdown","source":"## 2. Understanding and pre-processing data\nThe **training data-set** consists of **42001** and the **test data-set** consists of **28001**  images spread as a row of **784 pixels** "},{"metadata":{"trusted":true,"_uuid":"8b344d4285c3ec2b54200c532abcc79835437e08","collapsed":true},"cell_type":"code","source":"print('Shape of train: ', np.shape(train))\nprint('Shape of test_data: ', np.shape(test_data))\nprint('Shape of submission: ', np.shape(submssion))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7dc03978871fdb023e79a0f878abedfd6571a7d5"},"cell_type":"code","source":"train_data = train[1:,1:]\ntrain_labels = train[1:,0:1]\ntest_data = test_data[1:,:] ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"61841a0ab64833ba9dae38864c251b9840835ed4"},"cell_type":"markdown","source":"## 3. Building a classifier\nHere we use a Kd tree data structure for fast computation of K Nearest neighbour algorithm."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"52e016226fb89b3c64929399ba94a77fcf87bf01"},"cell_type":"code","source":"kd_tree = KDTree(train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"fc9018fd00b97425ebec9c5f72a5e0828bf63b66"},"cell_type":"code","source":"test_neighbors = np.squeeze(kd_tree.query(test_data, k=1, return_distance=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3c8c35c4ed12f700019fa810d9814261852adde1"},"cell_type":"code","source":"kd_tree_predictions = train_labels[test_neighbors]\nkd_tree_predictions = kd_tree_predictions.astype(int)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"645556f4a9cfdded30270591547ec0056f58329f"},"cell_type":"markdown","source":"## 4. Extraction of output file"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3d9bf693b5ca65c21d5f10c7155da20e1324d1aa"},"cell_type":"code","source":"del file['Label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b00843f983ded2ab78e129a1506934673a73e791"},"cell_type":"code","source":"file.insert(1, 'Label', kd_tree_predictions )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4c841fb6b2bcbb3fa3f763b15d8bdb64660c05a8"},"cell_type":"code","source":"file.to_csv('Kaggle.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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"}},"nbformat":4,"nbformat_minor":1}