{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31259,"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":"2026-01-21T15:02:00.255354Z","iopub.execute_input":"2026-01-21T15:02:00.255690Z","iopub.status.idle":"2026-01-21T15:02:00.268075Z","shell.execute_reply.started":"2026-01-21T15:02:00.255633Z","shell.execute_reply":"2026-01-21T15:02:00.267040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:00.269868Z","iopub.execute_input":"2026-01-21T15:02:00.270464Z","iopub.status.idle":"2026-01-21T15:02:00.286621Z","shell.execute_reply.started":"2026-01-21T15:02:00.270434Z","shell.execute_reply":"2026-01-21T15:02:00.285477Z"}},"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":"2026-01-21T15:02:00.292406Z","iopub.execute_input":"2026-01-21T15:02:00.292780Z","iopub.status.idle":"2026-01-21T15:02:03.552578Z","shell.execute_reply.started":"2026-01-21T15:02:00.292749Z","shell.execute_reply":"2026-01-21T15:02:03.551403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:03.554097Z","iopub.execute_input":"2026-01-21T15:02:03.554427Z","iopub.status.idle":"2026-01-21T15:02:03.572852Z","shell.execute_reply.started":"2026-01-21T15:02:03.554397Z","shell.execute_reply":"2026-01-21T15:02:03.571567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:03.575413Z","iopub.execute_input":"2026-01-21T15:02:03.575755Z","iopub.status.idle":"2026-01-21T15:02:03.594729Z","shell.execute_reply.started":"2026-01-21T15:02:03.575726Z","shell.execute_reply":"2026-01-21T15:02:03.593841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:03.595738Z","iopub.execute_input":"2026-01-21T15:02:03.596122Z","iopub.status.idle":"2026-01-21T15:02:03.624708Z","shell.execute_reply.started":"2026-01-21T15:02:03.596084Z","shell.execute_reply":"2026-01-21T15:02:03.623836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[25551,1:].values.reshape(28,28))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:03.626057Z","iopub.execute_input":"2026-01-21T15:02:03.626361Z","iopub.status.idle":"2026-01-21T15:02:03.764527Z","shell.execute_reply.started":"2026-01-21T15:02:03.626334Z","shell.execute_reply":"2026-01-21T15:02:03.763129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:03.766198Z","iopub.execute_input":"2026-01-21T15:02:03.766561Z","iopub.status.idle":"2026-01-21T15:02:03.773260Z","shell.execute_reply.started":"2026-01-21T15:02:03.766522Z","shell.execute_reply":"2026-01-21T15:02:03.772123Z"}},"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":"2026-01-21T15:02:03.774632Z","iopub.execute_input":"2026-01-21T15:02:03.774981Z","iopub.status.idle":"2026-01-21T15:02:04.422662Z","shell.execute_reply.started":"2026-01-21T15:02:03.774954Z","shell.execute_reply":"2026-01-21T15:02:04.421048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfrom sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:04.423878Z","iopub.execute_input":"2026-01-21T15:02:04.424151Z","iopub.status.idle":"2026-01-21T15:02:04.438191Z","shell.execute_reply.started":"2026-01-21T15:02:04.424125Z","shell.execute_reply":"2026-01-21T15:02:04.436758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)\ny_pred = knn.predict(x_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:04.439805Z","iopub.execute_input":"2026-01-21T15:02:04.440239Z","iopub.status.idle":"2026-01-21T15:02:18.757258Z","shell.execute_reply.started":"2026-01-21T15:02:04.440189Z","shell.execute_reply":"2026-01-21T15:02:18.755735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score \nprint(accuracy_score(y_test, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:18.758615Z","iopub.execute_input":"2026-01-21T15:02:18.759043Z","iopub.status.idle":"2026-01-21T15:02:18.770001Z","shell.execute_reply.started":"2026-01-21T15:02:18.759004Z","shell.execute_reply":"2026-01-21T15:02:18.768728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscale = StandardScaler()\nx_train_trf = scale.fit_transform(x_train)\nx_test_trf = scale.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:18.771172Z","iopub.execute_input":"2026-01-21T15:02:18.771484Z","iopub.status.idle":"2026-01-21T15:02:19.397635Z","shell.execute_reply.started":"2026-01-21T15:02:18.771457Z","shell.execute_reply":"2026-01-21T15:02:19.395344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=100)\nx_train_pca = pca.fit_transform(x_train_trf)\nx_test_pca = pca.transform(x_test_trf\n                          )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:19.404185Z","iopub.execute_input":"2026-01-21T15:02:19.405413Z","iopub.status.idle":"2026-01-21T15:02:20.476394Z","shell.execute_reply.started":"2026-01-21T15:02:19.405371Z","shell.execute_reply":"2026-01-21T15:02:20.475285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn_pca = KNeighborsClassifier()\nknn_pca.fit(x_train_pca,y_train)\ny_pred = knn_pca.predict(x_test_pca)\n\nprint(accuracy_score(y_test, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:20.477443Z","iopub.execute_input":"2026-01-21T15:02:20.477799Z","iopub.status.idle":"2026-01-21T15:02:22.492601Z","shell.execute_reply.started":"2026-01-21T15:02:20.477764Z","shell.execute_reply":"2026-01-21T15:02:22.490448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,784):\n    pca = PCA(n_components=i)\n    x_train_pca = pca.fit_transform(x_train_trf)\n    x_test_pca = pca.transform(x_test_trf\n                          )\n    knn_pca = KNeighborsClassifier()\n    knn_pca.fit(x_train_pca,y_train)\n    y_pred = knn_pca.predict(x_test_pca)\n    \n    print(i)\n    print(accuracy_score(y_test, y_pred))\n\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_2_components = PCA(n_components=2)\nx_train_trf_2_componenets = pca_2_components.fit_transform(x_train_trf)\nx_test_trf_2_componenets = pca_2_components.transform(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:53.846116Z","iopub.execute_input":"2026-01-21T15:02:53.846611Z","iopub.status.idle":"2026-01-21T15:02:54.612796Z","shell.execute_reply.started":"2026-01-21T15:02:53.846575Z","shell.execute_reply":"2026-01-21T15:02:54.612035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_2_componenets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:55.839102Z","iopub.execute_input":"2026-01-21T15:02:55.840430Z","iopub.status.idle":"2026-01-21T15:02:55.849604Z","shell.execute_reply.started":"2026-01-21T15:02:55.840391Z","shell.execute_reply":"2026-01-21T15:02:55.847929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = px.scatter(\n    x = x_train_trf_2_componenets[:, 0],\n    y = x_train_trf_2_componenets[:, 1],\n    color = y_train.astype(str),\n    color_discrete_sequence = px.colors.qualitative.G10\n)\n\nfig.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:56.484750Z","iopub.execute_input":"2026-01-21T15:02:56.485197Z","iopub.status.idle":"2026-01-21T15:02:56.600668Z","shell.execute_reply.started":"2026-01-21T15:02:56.485166Z","shell.execute_reply":"2026-01-21T15:02:56.599579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:56.979310Z","iopub.execute_input":"2026-01-21T15:02:56.979690Z","iopub.status.idle":"2026-01-21T15:02:56.988020Z","shell.execute_reply.started":"2026-01-21T15:02:56.979630Z","shell.execute_reply":"2026-01-21T15:02:56.986546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_3_components = PCA(n_components=3 )\nx_train_3_components = pca_3_components.fit_transform(x_train_trf)\nx_test_3_components = pca_3_components.transform(x_test_trf)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:57.389522Z","iopub.execute_input":"2026-01-21T15:02:57.389886Z","iopub.status.idle":"2026-01-21T15:02:58.141210Z","shell.execute_reply.started":"2026-01-21T15:02:57.389856Z","shell.execute_reply":"2026-01-21T15:02:58.140205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = px.scatter_3d(\n    x = x_train_3_components[:, 0],\n    y = x_train_3_components[:, 1],\n    z= x_train_3_components[:, 2],\n    color = y_train.astype(str),\n    color_discrete_sequence = px.colors.qualitative.G10\n)\nfig.update_layout(\n    margin = dict(l = 20 , r = 20 , t = 20,b = 20)\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:58.142536Z","iopub.execute_input":"2026-01-21T15:02:58.142893Z","iopub.status.idle":"2026-01-21T15:02:58.305573Z","shell.execute_reply.started":"2026-01-21T15:02:58.142844Z","shell.execute_reply":"2026-01-21T15:02:58.304526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_3_components.explained_variance_\n# Eigen Values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:58.306747Z","iopub.execute_input":"2026-01-21T15:02:58.307103Z","iopub.status.idle":"2026-01-21T15:02:58.313939Z","shell.execute_reply.started":"2026-01-21T15:02:58.307073Z","shell.execute_reply":"2026-01-21T15:02:58.312946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_3_components.components_.shape\n# Eigen vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:02:58.315830Z","iopub.execute_input":"2026-01-21T15:02:58.316220Z","iopub.status.idle":"2026-01-21T15:02:58.336529Z","shell.execute_reply.started":"2026-01-21T15:02:58.316189Z","shell.execute_reply":"2026-01-21T15:02:58.335055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_3_components.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:03:49.725304Z","iopub.execute_input":"2026-01-21T15:03:49.725949Z","iopub.status.idle":"2026-01-21T15:03:49.734418Z","shell.execute_reply.started":"2026-01-21T15:03:49.725913Z","shell.execute_reply":"2026-01-21T15:03:49.733215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components = None)\nx_train_trf = pca.fit_transform(x_train_trf)\nx_test_trf = pca.transform(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:05:54.606026Z","iopub.execute_input":"2026-01-21T15:05:54.606369Z","iopub.status.idle":"2026-01-21T15:05:56.317335Z","shell.execute_reply.started":"2026-01-21T15:05:54.606341Z","shell.execute_reply":"2026-01-21T15:05:56.316596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:06:56.755334Z","iopub.execute_input":"2026-01-21T15:06:56.755667Z","iopub.status.idle":"2026-01-21T15:06:56.761784Z","shell.execute_reply.started":"2026-01-21T15:06:56.755615Z","shell.execute_reply":"2026-01-21T15:06:56.760719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:07:36.233699Z","iopub.execute_input":"2026-01-21T15:07:36.234496Z","iopub.status.idle":"2026-01-21T15:07:36.240182Z","shell.execute_reply.started":"2026-01-21T15:07:36.234465Z","shell.execute_reply":"2026-01-21T15:07:36.238931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:08:37.400082Z","iopub.execute_input":"2026-01-21T15:08:37.400425Z","iopub.status.idle":"2026-01-21T15:08:37.412559Z","shell.execute_reply.started":"2026-01-21T15:08:37.400397Z","shell.execute_reply":"2026-01-21T15:08:37.411741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-21T15:09:42.874223Z","iopub.execute_input":"2026-01-21T15:09:42.874695Z","iopub.status.idle":"2026-01-21T15:09:43.003561Z","shell.execute_reply.started":"2026-01-21T15:09:42.874635Z","shell.execute_reply":"2026-01-21T15:09:43.002410Z"}},"outputs":[],"execution_count":null}]}