{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:46:51.330071Z","iopub.execute_input":"2025-01-22T13:46:51.330412Z","iopub.status.idle":"2025-01-22T13:46:51.338887Z","shell.execute_reply.started":"2025-01-22T13:46:51.330387Z","shell.execute_reply":"2025-01-22T13:46:51.33771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:46:51.340406Z","iopub.execute_input":"2025-01-22T13:46:51.340751Z","iopub.status.idle":"2025-01-22T13:46:53.518371Z","shell.execute_reply.started":"2025-01-22T13:46:51.340727Z","shell.execute_reply":"2025-01-22T13:46:53.517556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:46:53.52014Z","iopub.execute_input":"2025-01-22T13:46:53.520527Z","iopub.status.idle":"2025-01-22T13:46:53.535443Z","shell.execute_reply.started":"2025-01-22T13:46:53.520491Z","shell.execute_reply":"2025-01-22T13:46:53.53445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:46:53.536641Z","iopub.execute_input":"2025-01-22T13:46:53.537037Z","iopub.status.idle":"2025-01-22T13:46:53.554739Z","shell.execute_reply.started":"2025-01-22T13:46:53.536993Z","shell.execute_reply":"2025-01-22T13:46:53.553583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:49:19.753436Z","iopub.execute_input":"2025-01-22T13:49:19.753942Z","iopub.status.idle":"2025-01-22T13:49:19.759861Z","shell.execute_reply.started":"2025-01-22T13:49:19.753899Z","shell.execute_reply":"2025-01-22T13:49:19.758237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[18306,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:49:22.679601Z","iopub.execute_input":"2025-01-22T13:49:22.679923Z","iopub.status.idle":"2025-01-22T13:49:22.958687Z","shell.execute_reply.started":"2025-01-22T13:49:22.679898Z","shell.execute_reply":"2025-01-22T13:49:22.957596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:51:28.40076Z","iopub.execute_input":"2025-01-22T13:51:28.401125Z","iopub.status.idle":"2025-01-22T13:51:28.406107Z","shell.execute_reply.started":"2025-01-22T13:51:28.401095Z","shell.execute_reply":"2025-01-22T13:51:28.405208Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:56:59.287889Z","iopub.execute_input":"2025-01-22T13:56:59.288277Z","iopub.status.idle":"2025-01-22T13:56:59.540571Z","shell.execute_reply.started":"2025-01-22T13:56:59.28825Z","shell.execute_reply":"2025-01-22T13:56:59.539804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T13:57:16.644017Z","iopub.execute_input":"2025-01-22T13:57:16.64432Z","iopub.status.idle":"2025-01-22T13:57:16.649791Z","shell.execute_reply.started":"2025-01-22T13:57:16.644297Z","shell.execute_reply":"2025-01-22T13:57:16.648813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:00:50.396639Z","iopub.execute_input":"2025-01-22T14:00:50.396999Z","iopub.status.idle":"2025-01-22T14:00:50.624699Z","shell.execute_reply.started":"2025-01-22T14:00:50.396973Z","shell.execute_reply":"2025-01-22T14:00:50.6237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:01:33.543519Z","iopub.execute_input":"2025-01-22T14:01:33.543841Z","iopub.status.idle":"2025-01-22T14:01:33.691335Z","shell.execute_reply.started":"2025-01-22T14:01:33.543817Z","shell.execute_reply":"2025-01-22T14:01:33.69049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train,y_train)\nimport time \nstart= time.time()\ny_pred = knn.predict(x_test)\nprint(time.time())-start","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:09:11.554976Z","iopub.execute_input":"2025-01-22T14:09:11.555349Z","iopub.status.idle":"2025-01-22T14:09:24.746181Z","shell.execute_reply.started":"2025-01-22T14:09:11.555318Z","shell.execute_reply":"2025-01-22T14:09:24.7449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:12:45.688823Z","iopub.execute_input":"2025-01-22T14:12:45.689213Z","iopub.status.idle":"2025-01-22T14:12:45.697959Z","shell.execute_reply.started":"2025-01-22T14:12:45.689182Z","shell.execute_reply":"2025-01-22T14:12:45.696875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:14:24.211285Z","iopub.execute_input":"2025-01-22T14:14:24.211668Z","iopub.status.idle":"2025-01-22T14:14:24.215669Z","shell.execute_reply.started":"2025-01-22T14:14:24.211635Z","shell.execute_reply":"2025-01-22T14:14:24.2147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train= scaler.fit_transform(x_train)\nx_test=scaler.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:15:21.916613Z","iopub.execute_input":"2025-01-22T14:15:21.916962Z","iopub.status.idle":"2025-01-22T14:15:22.461512Z","shell.execute_reply.started":"2025-01-22T14:15:21.916938Z","shell.execute_reply":"2025-01-22T14:15:22.460503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#pca\nfrom sklearn.decomposition import PCA\npca= PCA(n_components=100)\nx_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:24:38.577063Z","iopub.execute_input":"2025-01-22T14:24:38.577404Z","iopub.status.idle":"2025-01-22T14:24:38.736058Z","shell.execute_reply.started":"2025-01-22T14:24:38.57738Z","shell.execute_reply":"2025-01-22T14:24:38.735154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:24:46.497068Z","iopub.execute_input":"2025-01-22T14:24:46.497396Z","iopub.status.idle":"2025-01-22T14:24:46.503163Z","shell.execute_reply.started":"2025-01-22T14:24:46.497367Z","shell.execute_reply":"2025-01-22T14:24:46.502158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train_trf,y_train)\ny_pred=knn.predict(x_test_trf)\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:31:21.755414Z","iopub.execute_input":"2025-01-22T14:31:21.755735Z","iopub.status.idle":"2025-01-22T14:31:23.265703Z","shell.execute_reply.started":"2025-01-22T14:31:21.755713Z","shell.execute_reply":"2025-01-22T14:31:23.264967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,7):\n    pca=PCA(n_components=i)\n    x_train_trf=pca.fit_transform(x_train)\n    x_test_trf=pca.transform(x_test)\n    knn= KNeighborsClassifier()\n    knn.fit(x_train_trf,y_train)\n    y_pred= knn.predict(x_test_trf)\n    print(accuracy_score(y_test,y_pred))\n    \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:40:14.383972Z","iopub.execute_input":"2025-01-22T14:40:14.384271Z","iopub.status.idle":"2025-01-22T14:40:18.87986Z","shell.execute_reply.started":"2025-01-22T14:40:14.384249Z","shell.execute_reply":"2025-01-22T14:40:18.878949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#transform to a 2d coordinate\npca=PCA(n_components=2)\nx_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)\nx_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:43:13.438846Z","iopub.execute_input":"2025-01-22T14:43:13.439377Z","iopub.status.idle":"2025-01-22T14:43:13.606816Z","shell.execute_reply.started":"2025-01-22T14:43:13.439327Z","shell.execute_reply":"2025-01-22T14:43:13.605943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf=y_train.astype(str)\nfig=px.scatter(x=x_train_trf[:,0],\n               y=x_train_trf[:,1],\n              color=y_train_trf,\n              color_discrete_sequence=px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-22T14:48:21.379823Z","iopub.execute_input":"2025-01-22T14:48:21.380147Z","iopub.status.idle":"2025-01-22T14:48:21.474788Z","shell.execute_reply.started":"2025-01-22T14:48:21.380122Z","shell.execute_reply":"2025-01-22T14:48:21.473908Z"}},"outputs":[],"execution_count":null}]}