{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-26T19:11:48.553591Z","iopub.execute_input":"2021-06-26T19:11:48.554105Z","iopub.status.idle":"2021-06-26T19:11:48.572733Z","shell.execute_reply.started":"2021-06-26T19:11:48.553971Z","shell.execute_reply":"2021-06-26T19:11:48.571443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:48.574824Z","iopub.execute_input":"2021-06-26T19:11:48.575529Z","iopub.status.idle":"2021-06-26T19:11:52.862535Z","shell.execute_reply.started":"2021-06-26T19:11:48.575462Z","shell.execute_reply":"2021-06-26T19:11:52.858590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:52.873149Z","iopub.execute_input":"2021-06-26T19:11:52.876625Z","iopub.status.idle":"2021-06-26T19:11:54.787143Z","shell.execute_reply.started":"2021-06-26T19:11:52.876213Z","shell.execute_reply":"2021-06-26T19:11:54.786279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train.copy()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:54.788932Z","iopub.execute_input":"2021-06-26T19:11:54.789632Z","iopub.status.idle":"2021-06-26T19:11:54.926445Z","shell.execute_reply.started":"2021-06-26T19:11:54.789581Z","shell.execute_reply":"2021-06-26T19:11:54.925546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:54.927529Z","iopub.execute_input":"2021-06-26T19:11:54.927977Z","iopub.status.idle":"2021-06-26T19:11:54.946915Z","shell.execute_reply.started":"2021-06-26T19:11:54.927944Z","shell.execute_reply":"2021-06-26T19:11:54.945720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=X['label']\nX=X.drop(['label'],axis=1)\nX.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:54.948419Z","iopub.execute_input":"2021-06-26T19:11:54.948733Z","iopub.status.idle":"2021-06-26T19:11:55.103967Z","shell.execute_reply.started":"2021-06-26T19:11:54.948701Z","shell.execute_reply":"2021-06-26T19:11:55.103137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.105179Z","iopub.execute_input":"2021-06-26T19:11:55.105638Z","iopub.status.idle":"2021-06-26T19:11:55.112983Z","shell.execute_reply.started":"2021-06-26T19:11:55.105586Z","shell.execute_reply":"2021-06-26T19:11:55.112035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=y.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.114359Z","iopub.execute_input":"2021-06-26T19:11:55.114690Z","iopub.status.idle":"2021-06-26T19:11:55.129492Z","shell.execute_reply.started":"2021-06-26T19:11:55.114658Z","shell.execute_reply":"2021-06-26T19:11:55.128231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.132646Z","iopub.execute_input":"2021-06-26T19:11:55.133037Z","iopub.status.idle":"2021-06-26T19:11:55.145765Z","shell.execute_reply.started":"2021-06-26T19:11:55.133001Z","shell.execute_reply":"2021-06-26T19:11:55.143805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.148364Z","iopub.execute_input":"2021-06-26T19:11:55.148847Z","iopub.status.idle":"2021-06-26T19:11:55.163445Z","shell.execute_reply.started":"2021-06-26T19:11:55.148778Z","shell.execute_reply":"2021-06-26T19:11:55.162129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.165110Z","iopub.execute_input":"2021-06-26T19:11:55.165480Z","iopub.status.idle":"2021-06-26T19:11:55.178301Z","shell.execute_reply.started":"2021-06-26T19:11:55.165445Z","shell.execute_reply":"2021-06-26T19:11:55.176857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**the image representation****","metadata":{}},{"cell_type":"code","source":"\nplt.imshow(X[10].reshape(28,28),cmap = 'Greys')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.180072Z","iopub.execute_input":"2021-06-26T19:11:55.180551Z","iopub.status.idle":"2021-06-26T19:11:55.339467Z","shell.execute_reply.started":"2021-06-26T19:11:55.180500Z","shell.execute_reply":"2021-06-26T19:11:55.338558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X/255\nX","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.340659Z","iopub.execute_input":"2021-06-26T19:11:55.341086Z","iopub.status.idle":"2021-06-26T19:11:55.637134Z","shell.execute_reply.started":"2021-06-26T19:11:55.341050Z","shell.execute_reply":"2021-06-26T19:11:55.635918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size = 0.30,random_state=50)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:55.638835Z","iopub.execute_input":"2021-06-26T19:11:55.639260Z","iopub.status.idle":"2021-06-26T19:11:57.055101Z","shell.execute_reply.started":"2021-06-26T19:11:55.639214Z","shell.execute_reply":"2021-06-26T19:11:57.053683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:57.056828Z","iopub.execute_input":"2021-06-26T19:11:57.057262Z","iopub.status.idle":"2021-06-26T19:11:59.024349Z","shell.execute_reply.started":"2021-06-26T19:11:57.057216Z","shell.execute_reply":"2021-06-26T19:11:59.023362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:59.025714Z","iopub.execute_input":"2021-06-26T19:11:59.026182Z","iopub.status.idle":"2021-06-26T19:11:59.049783Z","shell.execute_reply.started":"2021-06-26T19:11:59.026134Z","shell.execute_reply":"2021-06-26T19:11:59.048611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:59.051265Z","iopub.execute_input":"2021-06-26T19:11:59.052207Z","iopub.status.idle":"2021-06-26T19:11:59.062126Z","shell.execute_reply.started":"2021-06-26T19:11:59.052151Z","shell.execute_reply":"2021-06-26T19:11:59.060799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:59.063830Z","iopub.execute_input":"2021-06-26T19:11:59.064299Z","iopub.status.idle":"2021-06-26T19:11:59.079166Z","shell.execute_reply.started":"2021-06-26T19:11:59.064246Z","shell.execute_reply":"2021-06-26T19:11:59.077777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"HYPERPARAMETER TUNING\n","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:59.081174Z","iopub.execute_input":"2021-06-26T19:11:59.082168Z","iopub.status.idle":"2021-06-26T19:11:59.090571Z","shell.execute_reply.started":"2021-06-26T19:11:59.082111Z","shell.execute_reply":"2021-06-26T19:11:59.089475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nk_range = range(1,10)\nk_scores = []\nfor k in k_range:\n    knn = KNeighborsClassifier(n_neighbors=k)\n    scores = cross_val_score(knn, X, y, cv=10, scoring='accuracy')\n    k_scores.append(scores.mean())\nprint(k_scores)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:11:59.092226Z","iopub.execute_input":"2021-06-26T19:11:59.092824Z","iopub.status.idle":"2021-06-26T19:21:48.560155Z","shell.execute_reply.started":"2021-06-26T19:11:59.092759Z","shell.execute_reply":"2021-06-26T19:21:48.559096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **BASED ON THE HYPERPARAMETER TUNING N_NEIGHBOR IS TAKEN AS 3 **","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nknn = KNeighborsClassifier(n_neighbors=3)\nknn.fit(X_train,y_train)\ny_pred = knn.predict(X_test)\nprint('accuracy score:',accuracy_score(y_test,y_pred))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:21:48.561515Z","iopub.execute_input":"2021-06-26T19:21:48.561850Z","iopub.status.idle":"2021-06-26T19:22:02.824944Z","shell.execute_reply.started":"2021-06-26T19:21:48.561815Z","shell.execute_reply":"2021-06-26T19:22:02.823662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = knn.predict(test1)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:02.826662Z","iopub.execute_input":"2021-06-26T19:22:02.827122Z","iopub.status.idle":"2021-06-26T19:22:36.696370Z","shell.execute_reply.started":"2021-06-26T19:22:02.827074Z","shell.execute_reply":"2021-06-26T19:22:36.695494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.697435Z","iopub.execute_input":"2021-06-26T19:22:36.697835Z","iopub.status.idle":"2021-06-26T19:22:36.704214Z","shell.execute_reply.started":"2021-06-26T19:22:36.697805Z","shell.execute_reply":"2021-06-26T19:22:36.703269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.705426Z","iopub.execute_input":"2021-06-26T19:22:36.705714Z","iopub.status.idle":"2021-06-26T19:22:36.719453Z","shell.execute_reply.started":"2021-06-26T19:22:36.705683Z","shell.execute_reply":"2021-06-26T19:22:36.718699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=test1/255\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.720879Z","iopub.execute_input":"2021-06-26T19:22:36.721187Z","iopub.status.idle":"2021-06-26T19:22:36.802345Z","shell.execute_reply.started":"2021-06-26T19:22:36.721157Z","shell.execute_reply":"2021-06-26T19:22:36.801386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.803670Z","iopub.execute_input":"2021-06-26T19:22:36.803987Z","iopub.status.idle":"2021-06-26T19:22:36.823459Z","shell.execute_reply.started":"2021-06-26T19:22:36.803957Z","shell.execute_reply":"2021-06-26T19:22:36.822091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submit = pd.DataFrame()\nSubmit['ImageId']=sample_sub['ImageId']                       # Copying it as it is from the sample data\nSubmit['label']=predictions\nSubmit.to_csv('Submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.827198Z","iopub.execute_input":"2021-06-26T19:22:36.827534Z","iopub.status.idle":"2021-06-26T19:22:36.890430Z","shell.execute_reply.started":"2021-06-26T19:22:36.827499Z","shell.execute_reply":"2021-06-26T19:22:36.889554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submit.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T19:22:36.891717Z","iopub.execute_input":"2021-06-26T19:22:36.892227Z","iopub.status.idle":"2021-06-26T19:22:36.901199Z","shell.execute_reply.started":"2021-06-26T19:22:36.892172Z","shell.execute_reply":"2021-06-26T19:22:36.900496Z"},"trusted":true},"execution_count":null,"outputs":[]}]}