{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30804,"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":"2024-12-10T16:24:51.245389Z","iopub.execute_input":"2024-12-10T16:24:51.246391Z","iopub.status.idle":"2024-12-10T16:24:51.696489Z","shell.execute_reply.started":"2024-12-10T16:24:51.246350Z","shell.execute_reply":"2024-12-10T16:24:51.695268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:24:53.363429Z","iopub.execute_input":"2024-12-10T16:24:53.364023Z","iopub.status.idle":"2024-12-10T16:24:56.794312Z","shell.execute_reply.started":"2024-12-10T16:24:53.363986Z","shell.execute_reply":"2024-12-10T16:24:56.793296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:24:57.760892Z","iopub.execute_input":"2024-12-10T16:24:57.761264Z","iopub.status.idle":"2024-12-10T16:24:57.768282Z","shell.execute_reply.started":"2024-12-10T16:24:57.761234Z","shell.execute_reply":"2024-12-10T16:24:57.767048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:00.057976Z","iopub.execute_input":"2024-12-10T16:25:00.058350Z","iopub.status.idle":"2024-12-10T16:25:00.063335Z","shell.execute_reply.started":"2024-12-10T16:25:00.058319Z","shell.execute_reply":"2024-12-10T16:25:00.062141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:02.094092Z","iopub.execute_input":"2024-12-10T16:25:02.094462Z","iopub.status.idle":"2024-12-10T16:25:02.325499Z","shell.execute_reply.started":"2024-12-10T16:25:02.094433Z","shell.execute_reply":"2024-12-10T16:25:02.324336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx=df.iloc[:,1:]\ny=df.iloc[:,0]\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:04.989224Z","iopub.execute_input":"2024-12-10T16:25:04.989662Z","iopub.status.idle":"2024-12-10T16:25:05.385275Z","shell.execute_reply.started":"2024-12-10T16:25:04.989615Z","shell.execute_reply":"2024-12-10T16:25:05.384077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:07.838008Z","iopub.execute_input":"2024-12-10T16:25:07.838400Z","iopub.status.idle":"2024-12-10T16:25:07.846123Z","shell.execute_reply.started":"2024-12-10T16:25:07.838368Z","shell.execute_reply":"2024-12-10T16:25:07.844745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()\nknn.fit(x_train,y_train)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:09.935628Z","iopub.execute_input":"2024-12-10T16:25:09.936005Z","iopub.status.idle":"2024-12-10T16:25:10.251306Z","shell.execute_reply.started":"2024-12-10T16:25:09.935975Z","shell.execute_reply":"2024-12-10T16:25:10.250258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(x_test)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:25:11.979433Z","iopub.execute_input":"2024-12-10T16:25:11.979848Z","iopub.status.idle":"2024-12-10T16:25:25.237405Z","shell.execute_reply.started":"2024-12-10T16:25:11.979812Z","shell.execute_reply":"2024-12-10T16:25:25.236160Z"}},"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":"2024-12-10T16:25:25.239035Z","iopub.execute_input":"2024-12-10T16:25:25.239373Z","iopub.status.idle":"2024-12-10T16:25:25.248500Z","shell.execute_reply.started":"2024-12-10T16:25:25.239340Z","shell.execute_reply":"2024-12-10T16:25:25.247356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\nx_train=scaler.fit_transform(x_train)\nx_test=scaler.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:26:26.850481Z","iopub.execute_input":"2024-12-10T16:26:26.854968Z","iopub.status.idle":"2024-12-10T16:26:27.358923Z","shell.execute_reply.started":"2024-12-10T16:26:26.854901Z","shell.execute_reply":"2024-12-10T16:26:27.357815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=100)\nx_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)\nx_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:31:45.895186Z","iopub.execute_input":"2024-12-10T16:31:45.895571Z","iopub.status.idle":"2024-12-10T16:31:46.186489Z","shell.execute_reply.started":"2024-12-10T16:31:45.895533Z","shell.execute_reply":"2024-12-10T16:31:46.182316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:31:50.213563Z","iopub.execute_input":"2024-12-10T16:31:50.214880Z","iopub.status.idle":"2024-12-10T16:31:50.222571Z","shell.execute_reply.started":"2024-12-10T16:31:50.214797Z","shell.execute_reply":"2024-12-10T16:31:50.221370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:31:56.630102Z","iopub.execute_input":"2024-12-10T16:31:56.630488Z","iopub.status.idle":"2024-12-10T16:31:56.636011Z","shell.execute_reply.started":"2024-12-10T16:31:56.630455Z","shell.execute_reply":"2024-12-10T16:31:56.634330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:32:22.263768Z","iopub.execute_input":"2024-12-10T16:32:22.264267Z","iopub.status.idle":"2024-12-10T16:32:22.291985Z","shell.execute_reply.started":"2024-12-10T16:32:22.264223Z","shell.execute_reply":"2024-12-10T16:32:22.290758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:32:53.874511Z","iopub.execute_input":"2024-12-10T16:32:53.874947Z","iopub.status.idle":"2024-12-10T16:32:56.089400Z","shell.execute_reply.started":"2024-12-10T16:32:53.874908Z","shell.execute_reply":"2024-12-10T16:32:56.088161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:33:08.200027Z","iopub.execute_input":"2024-12-10T16:33:08.200437Z","iopub.status.idle":"2024-12-10T16:33:08.210182Z","shell.execute_reply.started":"2024-12-10T16:33:08.200401Z","shell.execute_reply":"2024-12-10T16:33:08.208885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,785):\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    accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:34:54.628081Z","iopub.execute_input":"2024-12-10T16:34:54.628487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}