{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:20:18.483199Z","iopub.execute_input":"2026-07-18T06:20:18.483523Z","iopub.status.idle":"2026-07-18T06:20:20.945804Z","shell.execute_reply.started":"2026-07-18T06:20:18.483493Z","shell.execute_reply":"2026-07-18T06:20:20.944641Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:28:31.763121Z","iopub.execute_input":"2026-07-18T06:28:31.763500Z","iopub.status.idle":"2026-07-18T06:28:34.640521Z","shell.execute_reply.started":"2026-07-18T06:28:31.763467Z","shell.execute_reply":"2026-07-18T06:28:34.639475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:28:40.078538Z","iopub.execute_input":"2026-07-18T06:28:40.078993Z","iopub.status.idle":"2026-07-18T06:28:40.126812Z","shell.execute_reply.started":"2026-07-18T06:28:40.078953Z","shell.execute_reply":"2026-07-18T06:28:40.125877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:29:51.285066Z","iopub.execute_input":"2026-07-18T06:29:51.285413Z","iopub.status.idle":"2026-07-18T06:29:51.290281Z","shell.execute_reply.started":"2026-07-18T06:29:51.285384Z","shell.execute_reply":"2026-07-18T06:29:51.289094Z"}},"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":"2026-07-18T06:31:15.192904Z","iopub.execute_input":"2026-07-18T06:31:15.193279Z","iopub.status.idle":"2026-07-18T06:31:15.420463Z","shell.execute_reply.started":"2026-07-18T06:31:15.193249Z","shell.execute_reply":"2026-07-18T06:31:15.419525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:49:35.856734Z","iopub.execute_input":"2026-07-18T06:49:35.857113Z","iopub.status.idle":"2026-07-18T06:49:35.862773Z","shell.execute_reply.started":"2026-07-18T06:49:35.857082Z","shell.execute_reply":"2026-07-18T06:49:35.861881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:49:37.423629Z","iopub.execute_input":"2026-07-18T06:49:37.423985Z","iopub.status.idle":"2026-07-18T06:49:37.429486Z","shell.execute_reply.started":"2026-07-18T06:49:37.423954Z","shell.execute_reply":"2026-07-18T06:49:37.428324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_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":"2026-07-18T06:49:38.676901Z","iopub.execute_input":"2026-07-18T06:49:38.677257Z","iopub.status.idle":"2026-07-18T06:49:38.849345Z","shell.execute_reply.started":"2026-07-18T06:49:38.677226Z","shell.execute_reply":"2026-07-18T06:49:38.848179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:47:28.717586Z","iopub.execute_input":"2026-07-18T06:47:28.718038Z","iopub.status.idle":"2026-07-18T06:47:28.724388Z","shell.execute_reply.started":"2026-07-18T06:47:28.718006Z","shell.execute_reply":"2026-07-18T06:47:28.723346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:34:52.784876Z","iopub.execute_input":"2026-07-18T06:34:52.785551Z","iopub.status.idle":"2026-07-18T06:34:53.065862Z","shell.execute_reply.started":"2026-07-18T06:34:52.785497Z","shell.execute_reply":"2026-07-18T06:34:53.064896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:35:13.782259Z","iopub.execute_input":"2026-07-18T06:35:13.782849Z","iopub.status.idle":"2026-07-18T06:35:13.787475Z","shell.execute_reply.started":"2026-07-18T06:35:13.782786Z","shell.execute_reply":"2026-07-18T06:35:13.786438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:49:42.106433Z","iopub.execute_input":"2026-07-18T06:49:42.106779Z","iopub.status.idle":"2026-07-18T06:49:42.245388Z","shell.execute_reply.started":"2026-07-18T06:49:42.106750Z","shell.execute_reply":"2026-07-18T06:49:42.244224Z"}},"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":"2026-07-18T06:37:34.126213Z","iopub.execute_input":"2026-07-18T06:37:34.126552Z","iopub.status.idle":"2026-07-18T06:37:45.599190Z","shell.execute_reply.started":"2026-07-18T06:37:34.126511Z","shell.execute_reply":"2026-07-18T06:37:45.598260Z"}},"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":"2026-07-18T06:38:20.733517Z","iopub.execute_input":"2026-07-18T06:38:20.733915Z","iopub.status.idle":"2026-07-18T06:38:20.742569Z","shell.execute_reply.started":"2026-07-18T06:38:20.733879Z","shell.execute_reply":"2026-07-18T06:38:20.741631Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Using PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:47:40.762450Z","iopub.execute_input":"2026-07-18T06:47:40.762867Z","iopub.status.idle":"2026-07-18T06:47:40.767773Z","shell.execute_reply.started":"2026-07-18T06:47:40.762802Z","shell.execute_reply":"2026-07-18T06:47:40.766659Z"}},"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":"2026-07-18T06:49:48.558656Z","iopub.execute_input":"2026-07-18T06:49:48.559254Z","iopub.status.idle":"2026-07-18T06:49:49.063630Z","shell.execute_reply.started":"2026-07-18T06:49:48.559202Z","shell.execute_reply":"2026-07-18T06:49:49.062458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:01.067960Z","iopub.execute_input":"2026-07-18T06:51:01.068302Z","iopub.status.idle":"2026-07-18T06:51:01.073554Z","shell.execute_reply.started":"2026-07-18T06:51:01.068272Z","shell.execute_reply":"2026-07-18T06:51:01.072746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:22.939731Z","iopub.execute_input":"2026-07-18T06:51:22.940175Z","iopub.status.idle":"2026-07-18T06:51:24.550385Z","shell.execute_reply.started":"2026-07-18T06:51:22.940142Z","shell.execute_reply":"2026-07-18T06:51:24.546390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:26.614021Z","iopub.execute_input":"2026-07-18T06:51:26.614357Z","iopub.status.idle":"2026-07-18T06:51:26.620988Z","shell.execute_reply.started":"2026-07-18T06:51:26.614328Z","shell.execute_reply":"2026-07-18T06:51:26.619988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:28.185648Z","iopub.execute_input":"2026-07-18T06:51:28.186020Z","iopub.status.idle":"2026-07-18T06:51:28.190743Z","shell.execute_reply.started":"2026-07-18T06:51:28.185988Z","shell.execute_reply":"2026-07-18T06:51:28.189743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:31.195881Z","iopub.execute_input":"2026-07-18T06:51:31.196213Z","iopub.status.idle":"2026-07-18T06:51:31.209111Z","shell.execute_reply.started":"2026-07-18T06:51:31.196183Z","shell.execute_reply":"2026-07-18T06:51:31.207798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:32.525387Z","iopub.execute_input":"2026-07-18T06:51:32.525727Z","iopub.status.idle":"2026-07-18T06:51:34.246733Z","shell.execute_reply.started":"2026-07-18T06:51:32.525699Z","shell.execute_reply":"2026-07-18T06:51:34.245806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:51:34.248201Z","iopub.execute_input":"2026-07-18T06:51:34.249274Z","iopub.status.idle":"2026-07-18T06:51:34.257844Z","shell.execute_reply.started":"2026-07-18T06:51:34.249233Z","shell.execute_reply":"2026-07-18T06:51:34.256500Z"}},"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\n    knn.fit(x_train_trf,y_train)\n    y_pred = knn.predict(x_test_trf)\n\n    print(accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-18T06:53:51.085479Z","iopub.execute_input":"2026-07-18T06:53:51.085911Z","iopub.status.idle":"2026-07-18T06:54:41.515496Z","shell.execute_reply.started":"2026-07-18T06:53:51.085873Z","shell.execute_reply":"2026-07-18T06:54:41.514200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}