{"cells":[{"metadata":{"_uuid":"ebb70d7332fcafed13c1c963652c48285b531b76","_cell_guid":"504c1194-6980-45aa-92f1-caf55b44dbb0"},"cell_type":"markdown","source":"Kaggle: https://www.kaggle.com/c/digit-recognizer"},{"metadata":{"_uuid":"26283408c3c54401ae8c237dded11a5d54141159","_cell_guid":"0604f6b3-046f-4b65-a1a9-3a5f8e824752","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"11ab637213812ce08688490c3f6cef097d603ff2","_cell_guid":"f206c2ba-7c09-4f26-ac95-e8a24dfb9ae7","trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../input/train.csv'\nTEST_DIR = '../input/test.csv'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ae5b578dffd73ed55fcba8b4c011334d46f55027","_cell_guid":"61d6082e-6ea1-4f92-9329-0848647a17c3"},"cell_type":"markdown","source":"# Explantory Data Analyisis"},{"metadata":{"_uuid":"8541a8ca7e77c1b6885d56f8b7b932b13f5f67c8","_cell_guid":"cc5d4fe8-a0a6-4640-973c-94bfe7639635","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"761de08af4ee6c975fe406ed66f90f0bc88fe8ff","_cell_guid":"c2c6ec59-ebc1-4d4e-ae89-d5a9ef240835","trusted":true},"cell_type":"code","source":"def show_example(X, y, row):\n    print(y[row])\n    plt.imshow(X[row].reshape((28, 28)))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9f3273d2f90dc599f82c63f3bc829b779e282eb5","_cell_guid":"ff788363-fa17-4748-bba8-6a4ae2487181","trusted":true},"cell_type":"code","source":"def show_examples(X, y, show_how_many=4):\n    classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n    for col, cls in enumerate(classes):\n        idxs = np.nonzero([i == cls for i in y]) # Note: nonzero means i==cls is true.\n        idxs = np.random.choice(idxs[0], show_how_many)\n        for i , idx in enumerate(idxs):\n            plt_idx = i * len(classes) + col + 1\n            plt.subplot(show_how_many, len(classes), plt_idx)\n            plt.imshow(X[idx].reshape((28, 28)))\n            plt.axis(\"off\")\n            if i == 0:\n                plt.title(cls)      \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2d8a667aa10291fe03b528c4ab5f55eb7dcf9dd0","_cell_guid":"93aec800-e499-4de7-a218-9d3231043b32","collapsed":true,"trusted":false},"cell_type":"markdown","source":"1.Take a Glance at the Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(TRAIN_DIR, sep=',', header=0)\ntest = pd.read_csv(TEST_DIR, sep=',', header=0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1862c48c0d9ccba622f7876d5baad0191bf32796","_cell_guid":"67390da3-6710-4bce-b0f1-8f364e5944e1","trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d791d62060f3b6deb5215c89eb6e813b95bd536","_cell_guid":"87c41cfe-4863-4e7f-a38b-dabdf14b24a0","trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6890a00551cdf3ec76ee99a60965a670ed20c239","_cell_guid":"02616849-0f6c-422f-82b1-f1b7a31c87c5"},"cell_type":"markdown","source":"2. Show Some Examples"},{"metadata":{"_uuid":"f5456e57ecb856612c2be0e7b3089a5e4a34dd5a","_cell_guid":"6d774516-3d31-417a-b1eb-d7a8ffa6242e","trusted":true},"cell_type":"code","source":"show_example(train.values[:, 1:], train.values[:, 0], 100)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b467e130ce5d8f5656062c7eadf63621d171671","_cell_guid":"a7c7dd6d-6d89-4ea9-9d37-41a6d3ecf048","trusted":true},"cell_type":"code","source":"show_examples(train.values[:300, 1:], train.values[:300, 0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(TRAIN_DIR, sep=',', header=0)\ntest = pd.read_csv(TEST_DIR, sep=',', header=0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"1. Training Set, Cross Validation Set, Test Set"},{"metadata":{"trusted":true},"cell_type":"code","source":"X, y = train.values[:, 1:], train.values[:, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=0, test_size=0.2)\nprint(f'Training Set - X train shape: {X_train.shape}, y train shape: {y_train.shape}')\nprint(f'Validation Set - X val shape: {X_val.shape}, y train shape: {y_val.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = test.values\nprint(f'Test Set - X test shape: {X_test.shape}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Hyperparameter Tuning"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score, classification_report","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"KRANGE = range(1, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def find_optimal_K(X_train, y_train, X_val, y_val):   \n    scores = []\n    for K in KRANGE:\n        knn = KNeighborsClassifier(n_neighbors=K)\n        knn.fit(X_train, y_train)\n    \n        y_pred = knn.predict(X_val)\n        accuracy = accuracy_score(y_val,y_pred)\n        scores.append(accuracy)\n    \n        print(f\"k = {K}: accuracy = {accuracy}\")\n        print('classification_report')\n        print(classification_report(y_val, y_pred))\n        # print('confusion_matrix')\n        # print(confusion_matrix(y_val, y_pred))\n    \n    plt.plot(KRANGE, scores)\n    plt.xlabel('Value of K')\n    plt.ylabel('Testing accuracy')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"find_optimal_K(X_train, y_train, X_val, y_val)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 1: KNN"},{"metadata":{"trusted":true},"cell_type":"code","source":"K = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=K)\nknn.fit(X, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = knn.predict(X_test)\nshow_example(X_test, y_pred, 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.DataFrame({\"ImageId\": list(range(1,len(y_pred)+1)),\\\n              \"Label\": y_pred}).to_csv('submission.csv', index=False,header=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 2: KNN (No Sklearn)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class KNN():\n    def __init__(self, K=3):\n        self.K = K\n        \n    def fit(self, X_train, y_train):\n        self.X_train, self.y_train = X_train, y_train\n                \n    def predict(self, X_test_multiple):\n        num_samples = X_test_multiple.shape[0]\n        y_pred = np.zeros(num_samples)\n        for i in range(num_samples):\n            X_test = X_test_multiple[i]\n            dist = self.__calc_dist(X_test)\n            y_pred[i] = self.__find_the_closet(dist)\n        return y_pred\n    \n    def __calc_dist(self, X_test):\n        \"\"\"\n        Description: Calculate the discription for one test sample.\n        \"\"\"\n        X_train, y_train = self.X_train, self.y_train\n        diff = X_test - X_train\n        squared_diff = diff ** 2\n        sum_squared_diff = np.sum(squared_diff, axis=1)\n        dist = sum_squared_diff ** 0.5\n        return dist\n    \n    def __find_the_closet(self, dist):\n        y_train, K = self.y_train, self.K\n        sort = dist.argsort()\n        \n        class_count = {}\n        for i in range(K):\n            vote = y_train[sort[i]]\n            class_count[vote] = class_count.get(vote, 0) + 1\n            \n        max_class, max_count = -1, -1\n        for cls, count in class_count.items():\n            if count > max_count:\n                max_class, max_count = cls, count\n        return max_class","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"K = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"knn = KNN(K)\nknn.fit(X, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = knn.predict(X_test)\nshow_example(X_test, y_pred, 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.DataFrame({\"ImageId\": list(range(1,len(y_pred)+1)),\\\n              \"Label\": y_pred}).to_csv('submission.csv', index=False,header=True)","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}