{"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":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31234,"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-09T19:30:32.071060Z","iopub.execute_input":"2026-01-09T19:30:32.071414Z","iopub.status.idle":"2026-01-09T19:30:32.478289Z","shell.execute_reply.started":"2026-01-09T19:30:32.071383Z","shell.execute_reply":"2026-01-09T19:30:32.476955Z"}},"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-09T19:30:32.480620Z","iopub.execute_input":"2026-01-09T19:30:32.481212Z","iopub.status.idle":"2026-01-09T19:30:35.512916Z","shell.execute_reply.started":"2026-01-09T19:30:32.481178Z","shell.execute_reply":"2026-01-09T19:30:35.511798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.514690Z","iopub.execute_input":"2026-01-09T19:30:35.515064Z","iopub.status.idle":"2026-01-09T19:30:35.553127Z","shell.execute_reply.started":"2026-01-09T19:30:35.515028Z","shell.execute_reply":"2026-01-09T19:30:35.552261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.554202Z","iopub.execute_input":"2026-01-09T19:30:35.554499Z","iopub.status.idle":"2026-01-09T19:30:35.560192Z","shell.execute_reply.started":"2026-01-09T19:30:35.554470Z","shell.execute_reply":"2026-01-09T19:30:35.559292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.562298Z","iopub.execute_input":"2026-01-09T19:30:35.562569Z","iopub.status.idle":"2026-01-09T19:30:35.587504Z","shell.execute_reply.started":"2026-01-09T19:30:35.562545Z","shell.execute_reply":"2026-01-09T19:30:35.586658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.588571Z","iopub.execute_input":"2026-01-09T19:30:35.588882Z","iopub.status.idle":"2026-01-09T19:30:35.601433Z","shell.execute_reply.started":"2026-01-09T19:30:35.588838Z","shell.execute_reply":"2026-01-09T19:30:35.600488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[26808,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.602684Z","iopub.execute_input":"2026-01-09T19:30:35.603012Z","iopub.status.idle":"2026-01-09T19:30:35.818500Z","shell.execute_reply.started":"2026-01-09T19:30:35.602985Z","shell.execute_reply":"2026-01-09T19:30:35.817496Z"}},"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-01-09T19:30:35.819647Z","iopub.execute_input":"2026-01-09T19:30:35.819942Z","iopub.status.idle":"2026-01-09T19:30:35.954552Z","shell.execute_reply.started":"2026-01-09T19:30:35.819916Z","shell.execute_reply":"2026-01-09T19:30:35.953551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = df['label']\nX = df.drop(columns=['label'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:35.955814Z","iopub.execute_input":"2026-01-09T19:30:35.956146Z","iopub.status.idle":"2026-01-09T19:30:36.069268Z","shell.execute_reply.started":"2026-01-09T19:30:35.956118Z","shell.execute_reply":"2026-01-09T19:30:36.068375Z"}},"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-09T19:30:36.071512Z","iopub.execute_input":"2026-01-09T19:30:36.071817Z","iopub.status.idle":"2026-01-09T19:30:37.199832Z","shell.execute_reply.started":"2026-01-09T19:30:36.071791Z","shell.execute_reply":"2026-01-09T19:30:37.198914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:37.201030Z","iopub.execute_input":"2026-01-09T19:30:37.201502Z","iopub.status.idle":"2026-01-09T19:30:37.207756Z","shell.execute_reply.started":"2026-01-09T19:30:37.201473Z","shell.execute_reply":"2026-01-09T19:30:37.207047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:37.208905Z","iopub.execute_input":"2026-01-09T19:30:37.209261Z","iopub.status.idle":"2026-01-09T19:30:37.505549Z","shell.execute_reply.started":"2026-01-09T19:30:37.209226Z","shell.execute_reply":"2026-01-09T19:30:37.504607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:37.507268Z","iopub.execute_input":"2026-01-09T19:30:37.507734Z","iopub.status.idle":"2026-01-09T19:30:37.511889Z","shell.execute_reply.started":"2026-01-09T19:30:37.507705Z","shell.execute_reply":"2026-01-09T19:30:37.511068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:37.513071Z","iopub.execute_input":"2026-01-09T19:30:37.513395Z","iopub.status.idle":"2026-01-09T19:30:37.681564Z","shell.execute_reply.started":"2026-01-09T19:30:37.513359Z","shell.execute_reply":"2026-01-09T19:30:37.680689Z"}},"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-01-09T19:30:37.682995Z","iopub.execute_input":"2026-01-09T19:30:37.683379Z","iopub.status.idle":"2026-01-09T19:30:47.737504Z","shell.execute_reply.started":"2026-01-09T19:30:37.683352Z","shell.execute_reply":"2026-01-09T19:30:47.736268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:47.739278Z","iopub.execute_input":"2026-01-09T19:30:47.739614Z","iopub.status.idle":"2026-01-09T19:30:47.743952Z","shell.execute_reply.started":"2026-01-09T19:30:47.739577Z","shell.execute_reply":"2026-01-09T19:30:47.742957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:47.745215Z","iopub.execute_input":"2026-01-09T19:30:47.745477Z","iopub.status.idle":"2026-01-09T19:30:47.770920Z","shell.execute_reply.started":"2026-01-09T19:30:47.745454Z","shell.execute_reply":"2026-01-09T19:30:47.769949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:47.772831Z","iopub.execute_input":"2026-01-09T19:30:47.773247Z","iopub.status.idle":"2026-01-09T19:30:47.783860Z","shell.execute_reply.started":"2026-01-09T19:30:47.773214Z","shell.execute_reply":"2026-01-09T19:30:47.782697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:47.785399Z","iopub.execute_input":"2026-01-09T19:30:47.785728Z","iopub.status.idle":"2026-01-09T19:30:47.801994Z","shell.execute_reply.started":"2026-01-09T19:30:47.785690Z","shell.execute_reply":"2026-01-09T19:30:47.800816Z"}},"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-01-09T19:30:47.803399Z","iopub.execute_input":"2026-01-09T19:30:47.803751Z","iopub.status.idle":"2026-01-09T19:30:48.287461Z","shell.execute_reply.started":"2026-01-09T19:30:47.803709Z","shell.execute_reply":"2026-01-09T19:30:48.286477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:48.288717Z","iopub.execute_input":"2026-01-09T19:30:48.289088Z","iopub.status.idle":"2026-01-09T19:30:48.293243Z","shell.execute_reply.started":"2026-01-09T19:30:48.289032Z","shell.execute_reply":"2026-01-09T19:30:48.292369Z"}},"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-01-09T19:30:48.294357Z","iopub.execute_input":"2026-01-09T19:30:48.294736Z","iopub.status.idle":"2026-01-09T19:30:49.028835Z","shell.execute_reply.started":"2026-01-09T19:30:48.294700Z","shell.execute_reply":"2026-01-09T19:30:49.028091Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:49.029689Z","iopub.execute_input":"2026-01-09T19:30:49.029992Z","iopub.status.idle":"2026-01-09T19:30:49.039238Z","shell.execute_reply.started":"2026-01-09T19:30:49.029962Z","shell.execute_reply":"2026-01-09T19:30:49.038210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:49.042344Z","iopub.execute_input":"2026-01-09T19:30:49.042929Z","iopub.status.idle":"2026-01-09T19:30:49.069889Z","shell.execute_reply.started":"2026-01-09T19:30:49.042894Z","shell.execute_reply":"2026-01-09T19:30:49.069194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:49.070857Z","iopub.execute_input":"2026-01-09T19:30:49.071178Z","iopub.status.idle":"2026-01-09T19:30:50.618086Z","shell.execute_reply.started":"2026-01-09T19:30:49.071149Z","shell.execute_reply":"2026-01-09T19:30:50.617144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:50.619280Z","iopub.execute_input":"2026-01-09T19:30:50.619698Z","iopub.status.idle":"2026-01-09T19:30:50.628258Z","shell.execute_reply.started":"2026-01-09T19:30:50.619661Z","shell.execute_reply":"2026-01-09T19:30:50.627345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1, 785 ,40):\n    pca = PCA(n_components = i)\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf = pca.transform(X_test)\n\n    knn = KNeighborsClassifier()\n    \n    knn.fit(X_train_trf,y_train)\n\n    y_pred = knn.predict(X_test_trf)\n\n    print(i ,\"=\",accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:30:50.629668Z","iopub.execute_input":"2026-01-09T19:30:50.630151Z","iopub.status.idle":"2026-01-09T19:32:30.349781Z","shell.execute_reply.started":"2026-01-09T19:30:50.630113Z","shell.execute_reply":"2026-01-09T19:32:30.348661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:30.351136Z","iopub.execute_input":"2026-01-09T19:32:30.351479Z","iopub.status.idle":"2026-01-09T19:32:30.917856Z","shell.execute_reply.started":"2026-01-09T19:32:30.351450Z","shell.execute_reply":"2026-01-09T19:32:30.917088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:30.918857Z","iopub.execute_input":"2026-01-09T19:32:30.919517Z","iopub.status.idle":"2026-01-09T19:32:30.925167Z","shell.execute_reply.started":"2026-01-09T19:32:30.919483Z","shell.execute_reply":"2026-01-09T19:32:30.924376Z"}},"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\n                 )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:30.927414Z","iopub.execute_input":"2026-01-09T19:32:30.927717Z","iopub.status.idle":"2026-01-09T19:32:35.958676Z","shell.execute_reply.started":"2026-01-09T19:32:30.927682Z","shell.execute_reply":"2026-01-09T19:32:35.957449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming to a 3D coordinate system\npca = PCA(n_components=3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:35.960331Z","iopub.execute_input":"2026-01-09T19:32:35.960824Z","iopub.status.idle":"2026-01-09T19:32:36.527076Z","shell.execute_reply.started":"2026-01-09T19:32:35.960783Z","shell.execute_reply":"2026-01-09T19:32:36.526040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:36.528021Z","iopub.execute_input":"2026-01-09T19:32:36.528340Z","iopub.status.idle":"2026-01-09T19:32:36.534136Z","shell.execute_reply.started":"2026-01-09T19:32:36.528300Z","shell.execute_reply":"2026-01-09T19:32:36.533391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df, x=X_train_trf[:,0], y=X_train_trf[:,1], z=X_train_trf[:,2],\n                    color=y_train_trf)\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-09T19:32:36.535599Z","iopub.execute_input":"2026-01-09T19:32:36.536134Z","iopub.status.idle":"2026-01-09T19:32:36.898651Z","shell.execute_reply.started":"2026-01-09T19:32:36.536099Z","shell.execute_reply":"2026-01-09T19:32:36.897399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:36.900104Z","iopub.execute_input":"2026-01-09T19:32:36.900413Z","iopub.status.idle":"2026-01-09T19:32:36.906803Z","shell.execute_reply.started":"2026-01-09T19:32:36.900377Z","shell.execute_reply":"2026-01-09T19:32:36.905707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape\n#eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:32:36.908115Z","iopub.execute_input":"2026-01-09T19:32:36.908580Z","iopub.status.idle":"2026-01-09T19:32:36.926894Z","shell.execute_reply.started":"2026-01-09T19:32:36.908540Z","shell.execute_reply":"2026-01-09T19:32:36.925886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:41:01.345467Z","iopub.execute_input":"2026-01-09T19:41:01.345942Z","iopub.status.idle":"2026-01-09T19:41:01.352030Z","shell.execute_reply.started":"2026-01-09T19:41:01.345899Z","shell.execute_reply":"2026-01-09T19:41:01.350809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:38:24.301463Z","iopub.execute_input":"2026-01-09T19:38:24.301821Z","iopub.status.idle":"2026-01-09T19:38:25.636644Z","shell.execute_reply.started":"2026-01-09T19:38:24.301794Z","shell.execute_reply":"2026-01-09T19:38:25.635880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:39:43.320977Z","iopub.execute_input":"2026-01-09T19:39:43.321920Z","iopub.status.idle":"2026-01-09T19:39:43.328216Z","shell.execute_reply.started":"2026-01-09T19:39:43.321850Z","shell.execute_reply":"2026-01-09T19:39:43.326819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:40:03.526058Z","iopub.execute_input":"2026-01-09T19:40:03.526383Z","iopub.status.idle":"2026-01-09T19:40:03.532524Z","shell.execute_reply.started":"2026-01-09T19:40:03.526357Z","shell.execute_reply":"2026-01-09T19:40:03.531542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:50:01.936398Z","iopub.execute_input":"2026-01-09T19:50:01.936765Z","iopub.status.idle":"2026-01-09T19:50:01.949104Z","shell.execute_reply.started":"2026-01-09T19:50:01.936738Z","shell.execute_reply":"2026-01-09T19:50:01.947798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-09T19:50:22.408611Z","iopub.execute_input":"2026-01-09T19:50:22.409222Z","iopub.status.idle":"2026-01-09T19:50:22.589658Z","shell.execute_reply.started":"2026-01-09T19:50:22.409178Z","shell.execute_reply":"2026-01-09T19:50:22.588618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}