{"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":"2023-02-28T13:39:09.558952Z","iopub.execute_input":"2023-02-28T13:39:09.560017Z","iopub.status.idle":"2023-02-28T13:39:09.599477Z","shell.execute_reply.started":"2023-02-28T13:39:09.559966Z","shell.execute_reply":"2023-02-28T13:39:09.598059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:39:45.831713Z","iopub.execute_input":"2023-02-28T13:39:45.832978Z","iopub.status.idle":"2023-02-28T13:39:50.174532Z","shell.execute_reply.started":"2023-02-28T13:39:45.832900Z","shell.execute_reply":"2023-02-28T13:39:50.170733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:40:41.350370Z","iopub.execute_input":"2023-02-28T13:40:41.350838Z","iopub.status.idle":"2023-02-28T13:40:41.377042Z","shell.execute_reply.started":"2023-02-28T13:40:41.350796Z","shell.execute_reply":"2023-02-28T13:40:41.375945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:42:17.951903Z","iopub.execute_input":"2023-02-28T13:42:17.952477Z","iopub.status.idle":"2023-02-28T13:42:17.958345Z","shell.execute_reply.started":"2023-02-28T13:42:17.952424Z","shell.execute_reply":"2023-02-28T13:42:17.956817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[34540,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:52:44.191324Z","iopub.execute_input":"2023-02-28T13:52:44.191827Z","iopub.status.idle":"2023-02-28T13:52:44.453311Z","shell.execute_reply.started":"2023-02-28T13:52:44.191782Z","shell.execute_reply":"2023-02-28T13:52:44.451523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:54:26.585609Z","iopub.execute_input":"2023-02-28T13:54:26.586061Z","iopub.status.idle":"2023-02-28T13:54:26.592491Z","shell.execute_reply.started":"2023-02-28T13:54:26.586022Z","shell.execute_reply":"2023-02-28T13:54:26.591038Z"},"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.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:56:02.231854Z","iopub.execute_input":"2023-02-28T13:56:02.232272Z","iopub.status.idle":"2023-02-28T13:56:02.513453Z","shell.execute_reply.started":"2023-02-28T13:56:02.232237Z","shell.execute_reply":"2023-02-28T13:56:02.512056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:03:52.768984Z","iopub.execute_input":"2023-02-28T14:03:52.770367Z","iopub.status.idle":"2023-02-28T14:03:52.778018Z","shell.execute_reply.started":"2023-02-28T14:03:52.770316Z","shell.execute_reply":"2023-02-28T14:03:52.776504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:21:56.634901Z","iopub.execute_input":"2023-02-28T14:21:56.635300Z","iopub.status.idle":"2023-02-28T14:21:56.870602Z","shell.execute_reply.started":"2023-02-28T14:21:56.635266Z","shell.execute_reply":"2023-02-28T14:21:56.869257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:22:28.690260Z","iopub.execute_input":"2023-02-28T14:22:28.690725Z","iopub.status.idle":"2023-02-28T14:22:28.696829Z","shell.execute_reply.started":"2023-02-28T14:22:28.690685Z","shell.execute_reply":"2023-02-28T14:22:28.695550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:22:59.497972Z","iopub.execute_input":"2023-02-28T14:22:59.498366Z","iopub.status.idle":"2023-02-28T14:22:59.522227Z","shell.execute_reply.started":"2023-02-28T14:22:59.498332Z","shell.execute_reply":"2023-02-28T14:22:59.521018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:25:02.430894Z","iopub.execute_input":"2023-02-28T14:25:02.431320Z","iopub.status.idle":"2023-02-28T14:25:15.665514Z","shell.execute_reply.started":"2023-02-28T14:25:02.431276Z","shell.execute_reply":"2023-02-28T14:25:15.663990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(Y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:26:04.840890Z","iopub.execute_input":"2023-02-28T14:26:04.841359Z","iopub.status.idle":"2023-02-28T14:26:04.852051Z","shell.execute_reply.started":"2023-02-28T14:26:04.841321Z","shell.execute_reply":"2023-02-28T14:26:04.850290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:26:51.865369Z","iopub.execute_input":"2023-02-28T14:26:51.865938Z","iopub.status.idle":"2023-02-28T14:26:51.871657Z","shell.execute_reply.started":"2023-02-28T14:26:51.865886Z","shell.execute_reply":"2023-02-28T14:26:51.870204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)\nX_test=scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:28:12.285632Z","iopub.execute_input":"2023-02-28T14:28:12.286035Z","iopub.status.idle":"2023-02-28T14:28:12.791788Z","shell.execute_reply.started":"2023-02-28T14:28:12.286001Z","shell.execute_reply":"2023-02-28T14:28:12.790190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=100)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:30:25.273093Z","iopub.execute_input":"2023-02-28T14:30:25.273637Z","iopub.status.idle":"2023-02-28T14:30:25.280808Z","shell.execute_reply.started":"2023-02-28T14:30:25.273580Z","shell.execute_reply":"2023-02-28T14:30:25.278902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf=pca.fit_transform(X_train)\nX_test_trf=pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:31:15.826796Z","iopub.execute_input":"2023-02-28T14:31:15.827292Z","iopub.status.idle":"2023-02-28T14:31:16.177507Z","shell.execute_reply.started":"2023-02-28T14:31:15.827233Z","shell.execute_reply":"2023-02-28T14:31:16.175568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:31:42.597159Z","iopub.execute_input":"2023-02-28T14:31:42.598374Z","iopub.status.idle":"2023-02-28T14:31:42.617146Z","shell.execute_reply.started":"2023-02-28T14:31:42.598285Z","shell.execute_reply":"2023-02-28T14:31:42.614212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred=knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:32:15.583908Z","iopub.execute_input":"2023-02-28T14:32:15.584372Z","iopub.status.idle":"2023-02-28T14:32:22.383393Z","shell.execute_reply.started":"2023-02-28T14:32:15.584334Z","shell.execute_reply":"2023-02-28T14:32:22.382011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(Y_test,Y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T14:33:09.731837Z","iopub.execute_input":"2023-02-28T14:33:09.732260Z","iopub.status.idle":"2023-02-28T14:33:09.743301Z","shell.execute_reply.started":"2023-02-28T14:33:09.732227Z","shell.execute_reply":"2023-02-28T14:33:09.741715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}