{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# import libraries\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_dir = \"\"\ntrain_data = \"../input/train.csv\"\n\n# load csv files to numpy arrays\ndef load_data(data_dir, input_file):\n    data = pd.read_csv(data_dir + input_file)\n    \n#    print(train.shape)\n    return(data)\n   \ntrain_data = load_data(data_dir, train_data)\nX_train = train_data.values[:,1:] \ny_train = train_data.values[:,0] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Check the data\nprint(train_data.shape)\nprint(train_data[0:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape, y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Show the image of a single data\nimport matplotlib\nimport matplotlib.pyplot as plt\nrow = 4\n\nprint (y_train[row])\n\nplt.imshow(X_train[row].reshape((28, 28)))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Show the images of multiple rows data\nclasses = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\nrows = 5\n\nprint(classes)\nfor y, cls in enumerate(classes):\n    idxs = np.nonzero([i == y for i in 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(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":{"trusted":true},"cell_type":"code","source":"# Split Training Dataset into two sets: train and validation\nfrom sklearn.model_selection import train_test_split\n\nX_train,X_vali, y_train, y_vali = train_test_split(X_train,\n                                                   y_train,\n                                                   test_size = 0.2,\n                                                   random_state = 0)\n# If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.\n\nprint(X_train.shape, X_vali.shape, y_train.shape, y_vali.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Build KNN model and run the train set and predict Y for the validation set\nimport 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\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_vali)\n    accuracy = accuracy_score(y_vali,y_pred)\n    scores.append(accuracy)\n    end = time.time()\n    print(classification_report(y_vali, y_pred))  \n    print(confusion_matrix(y_vali, y_pred))  \n    \n    print(\"Complete time: \" + str(end-start) + \" Secs.\")","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"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":{"trusted":true},"cell_type":"code","source":"\nfor k, score in zip(k_range, scores):\n    if score == min(scores):\n        k_min=k\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Input Test Dataset\ntest_data = pd.read_csv(\"../input/test.csv\").values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import time\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nfrom sklearn.neighbors import KNeighborsClassifier\n\nk = k_min\n\nstart = time.time()\nknn = KNeighborsClassifier(n_neighbors=k)\nknn.fit(X_train,y_train)\ny_pred = knn.predict(test_data)\nend = time.time()\n\nprint(\"Complete time: \" + str(end-start) + \" Secs.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test the prediction results\nprint (y_pred[200])\n\nplt.imshow(test_data[200].reshape((28, 28)))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Output csv\n\npd.DataFrame({\"ImageId\": list(range(1,len(y_pred)+1)),\"Label\": y_pred}).to_csv('Digit_Recogniser_Result.csv', index=False,header=True)","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}