{"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":31328,"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-03-22T11:00:08.035617Z","iopub.execute_input":"2026-03-22T11:00:08.035979Z","iopub.status.idle":"2026-03-22T11:00:09.311234Z","shell.execute_reply.started":"2026-03-22T11:00:08.035936Z","shell.execute_reply":"2026-03-22T11:00:09.310192Z"}},"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-03-22T12:04:01.333654Z","iopub.execute_input":"2026-03-22T12:04:01.333954Z","iopub.status.idle":"2026-03-22T12:04:03.378475Z","shell.execute_reply.started":"2026-03-22T12:04:01.333928Z","shell.execute_reply":"2026-03-22T12:04:03.377582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.to_csv('mnist.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:07.733821Z","iopub.execute_input":"2026-03-22T12:04:07.734487Z","iopub.status.idle":"2026-03-22T12:04:14.904668Z","shell.execute_reply.started":"2026-03-22T12:04:07.734452Z","shell.execute_reply":"2026-03-22T12:04:14.903090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:18.920779Z","iopub.execute_input":"2026-03-22T12:04:18.921560Z","iopub.status.idle":"2026-03-22T12:04:18.938255Z","shell.execute_reply.started":"2026-03-22T12:04:18.921524Z","shell.execute_reply":"2026-03-22T12:04:18.937122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:24.052498Z","iopub.execute_input":"2026-03-22T12:04:24.053444Z","iopub.status.idle":"2026-03-22T12:04:24.059155Z","shell.execute_reply.started":"2026-03-22T12:04:24.053357Z","shell.execute_reply":"2026-03-22T12:04:24.057913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:26.925143Z","iopub.execute_input":"2026-03-22T12:04:26.926017Z","iopub.status.idle":"2026-03-22T12:04:26.941462Z","shell.execute_reply.started":"2026-03-22T12:04:26.925982Z","shell.execute_reply":"2026-03-22T12:04:26.940469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:29.644081Z","iopub.execute_input":"2026-03-22T12:04:29.644590Z","iopub.status.idle":"2026-03-22T12:04:29.648993Z","shell.execute_reply.started":"2026-03-22T12:04:29.644555Z","shell.execute_reply":"2026-03-22T12:04:29.647953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[990,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:30.411977Z","iopub.execute_input":"2026-03-22T12:04:30.412362Z","iopub.status.idle":"2026-03-22T12:04:30.553985Z","shell.execute_reply.started":"2026-03-22T12:04:30.412329Z","shell.execute_reply":"2026-03-22T12:04:30.553238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:32.867981Z","iopub.execute_input":"2026-03-22T12:04:32.868757Z","iopub.status.idle":"2026-03-22T12:04:32.873645Z","shell.execute_reply.started":"2026-03-22T12:04:32.868719Z","shell.execute_reply":"2026-03-22T12:04:32.872729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:34.659429Z","iopub.execute_input":"2026-03-22T12:04:34.659823Z","iopub.status.idle":"2026-03-22T12:04:34.665948Z","shell.execute_reply.started":"2026-03-22T12:04:34.659789Z","shell.execute_reply":"2026-03-22T12:04:34.664721Z"}},"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-03-22T12:04:35.765934Z","iopub.execute_input":"2026-03-22T12:04:35.766447Z","iopub.status.idle":"2026-03-22T12:04:36.044206Z","shell.execute_reply.started":"2026-03-22T12:04:35.766324Z","shell.execute_reply":"2026-03-22T12:04:36.043283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:37.032583Z","iopub.execute_input":"2026-03-22T12:04:37.033059Z","iopub.status.idle":"2026-03-22T12:04:37.038996Z","shell.execute_reply.started":"2026-03-22T12:04:37.033024Z","shell.execute_reply":"2026-03-22T12:04:37.037926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:39.849222Z","iopub.execute_input":"2026-03-22T12:04:39.849839Z","iopub.status.idle":"2026-03-22T12:04:39.854004Z","shell.execute_reply.started":"2026-03-22T12:04:39.849806Z","shell.execute_reply":"2026-03-22T12:04:39.852973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:40.881437Z","iopub.execute_input":"2026-03-22T12:04:40.882411Z","iopub.status.idle":"2026-03-22T12:04:40.887018Z","shell.execute_reply.started":"2026-03-22T12:04:40.882373Z","shell.execute_reply":"2026-03-22T12:04:40.885988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:43.191529Z","iopub.execute_input":"2026-03-22T12:04:43.192313Z","iopub.status.idle":"2026-03-22T12:04:43.336748Z","shell.execute_reply.started":"2026-03-22T12:04:43.192274Z","shell.execute_reply":"2026-03-22T12:04:43.335801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred = knn.predict(X_test)\n\nprint(time.time() - start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:04:45.502004Z","iopub.execute_input":"2026-03-22T12:04:45.503043Z","iopub.status.idle":"2026-03-22T12:04:55.946268Z","shell.execute_reply.started":"2026-03-22T12:04:45.503006Z","shell.execute_reply":"2026-03-22T12:04:55.945297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:05:02.473611Z","iopub.execute_input":"2026-03-22T12:05:02.474840Z","iopub.status.idle":"2026-03-22T12:05:02.479579Z","shell.execute_reply.started":"2026-03-22T12:05:02.474787Z","shell.execute_reply":"2026-03-22T12:05:02.478511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:05:05.097618Z","iopub.execute_input":"2026-03-22T12:05:05.097935Z","iopub.status.idle":"2026-03-22T12:05:05.107004Z","shell.execute_reply.started":"2026-03-22T12:05:05.097906Z","shell.execute_reply":"2026-03-22T12:05:05.106086Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**PCA**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:05:11.706044Z","iopub.execute_input":"2026-03-22T12:05:11.707387Z","iopub.status.idle":"2026-03-22T12:05:11.712816Z","shell.execute_reply.started":"2026-03-22T12:05:11.707224Z","shell.execute_reply":"2026-03-22T12:05:11.711322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf1 = scaler.fit_transform(X_train)\nX_test_trf1 = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:10.095007Z","iopub.execute_input":"2026-03-22T12:14:10.095373Z","iopub.status.idle":"2026-03-22T12:14:10.660006Z","shell.execute_reply.started":"2026-03-22T12:14:10.095341Z","shell.execute_reply":"2026-03-22T12:14:10.658976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:11.143029Z","iopub.execute_input":"2026-03-22T12:14:11.143352Z","iopub.status.idle":"2026-03-22T12:14:11.148603Z","shell.execute_reply.started":"2026-03-22T12:14:11.143322Z","shell.execute_reply":"2026-03-22T12:14:11.147755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf2 = pca.fit_transform(X_train_trf1)\nX_test_trf2 = pca.transform(X_test_trf1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:19.889653Z","iopub.execute_input":"2026-03-22T12:14:19.890065Z","iopub.status.idle":"2026-03-22T12:14:21.296820Z","shell.execute_reply.started":"2026-03-22T12:14:19.890025Z","shell.execute_reply":"2026-03-22T12:14:21.295975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:24.361590Z","iopub.execute_input":"2026-03-22T12:14:24.362469Z","iopub.status.idle":"2026-03-22T12:14:24.368332Z","shell.execute_reply.started":"2026-03-22T12:14:24.362433Z","shell.execute_reply":"2026-03-22T12:14:24.367338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train_trf2,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:26.416008Z","iopub.execute_input":"2026-03-22T12:14:26.416393Z","iopub.status.idle":"2026-03-22T12:14:26.431190Z","shell.execute_reply.started":"2026-03-22T12:14:26.416360Z","shell.execute_reply":"2026-03-22T12:14:26.430064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred2 = knn.predict(X_test_trf2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:28.944358Z","iopub.execute_input":"2026-03-22T12:14:28.945285Z","iopub.status.idle":"2026-03-22T12:14:31.526447Z","shell.execute_reply.started":"2026-03-22T12:14:28.945236Z","shell.execute_reply":"2026-03-22T12:14:31.525151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:31.822329Z","iopub.execute_input":"2026-03-22T12:14:31.822946Z","iopub.status.idle":"2026-03-22T12:14:31.830403Z","shell.execute_reply.started":"2026-03-22T12:14:31.822910Z","shell.execute_reply":"2026-03-22T12:14:31.829475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,785):\n    pca = PCA(n_components=i)\n    X_train_trf2 = pca.fit_transform(X_train_trf1)\n    X_test_trf2 = pca.transform(X_test_trf1)\n    knn = KNeighborsClassifier()\n    knn.fit(X_train_trf2,y_train)\n    y_pred2 = knn.predict(X_test_trf2)\n    print(accuracy_score(y_test,y_pred2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:14:48.006122Z","iopub.execute_input":"2026-03-22T12:14:48.006488Z","iopub.status.idle":"2026-03-22T12:17:06.986048Z","shell.execute_reply.started":"2026-03-22T12:14:48.006458Z","shell.execute_reply":"2026-03-22T12:17:06.984541Z"}},"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-03-22T12:17:17.063491Z","iopub.execute_input":"2026-03-22T12:17:17.063825Z","iopub.status.idle":"2026-03-22T12:17:18.207945Z","shell.execute_reply.started":"2026-03-22T12:17:17.063785Z","shell.execute_reply":"2026-03-22T12:17:18.206847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:17:20.734516Z","iopub.execute_input":"2026-03-22T12:17:20.735365Z","iopub.status.idle":"2026-03-22T12:17:20.741624Z","shell.execute_reply.started":"2026-03-22T12:17:20.735327Z","shell.execute_reply":"2026-03-22T12:17:20.740588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:21:07.267076Z","iopub.execute_input":"2026-03-22T12:21:07.267467Z","iopub.status.idle":"2026-03-22T12:21:08.513115Z","shell.execute_reply.started":"2026-03-22T12:21:07.267428Z","shell.execute_reply":"2026-03-22T12:21:08.512380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf = y_train.astype(str)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:21:08.655779Z","iopub.execute_input":"2026-03-22T12:21:08.656078Z","iopub.status.idle":"2026-03-22T12:21:08.670511Z","shell.execute_reply.started":"2026-03-22T12:21:08.656052Z","shell.execute_reply":"2026-03-22T12:21:08.669362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = 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-03-22T12:22:52.825641Z","iopub.execute_input":"2026-03-22T12:22:52.826600Z","iopub.status.idle":"2026-03-22T12:22:52.908307Z","shell.execute_reply.started":"2026-03-22T12:22:52.826566Z","shell.execute_reply":"2026-03-22T12:22:52.907157Z"}},"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)\n\nimport plotly.express as px\n\ny_train_trf = y_train.astype(str)\n\nfig = px.scatter_3d(x=X_train_trf[:,0],\n                y= X_train_trf[:,1],\n                z= X_train_trf[:,2],\n                color=y_train_trf,\n                color_discrete_sequence=px.colors.qualitative.G10)\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:27:05.015887Z","iopub.execute_input":"2026-03-22T12:27:05.016801Z","iopub.status.idle":"2026-03-22T12:27:06.339194Z","shell.execute_reply.started":"2026-03-22T12:27:05.016755Z","shell.execute_reply":"2026-03-22T12:27:06.337963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_\n# explained variance are eigevalues","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:31:34.433008Z","iopub.execute_input":"2026-03-22T12:31:34.433894Z","iopub.status.idle":"2026-03-22T12:31:34.440228Z","shell.execute_reply.started":"2026-03-22T12:31:34.433851Z","shell.execute_reply":"2026-03-22T12:31:34.439208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_\n# eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:33:26.466792Z","iopub.execute_input":"2026-03-22T12:33:26.467547Z","iopub.status.idle":"2026-03-22T12:33:26.475209Z","shell.execute_reply.started":"2026-03-22T12:33:26.467511Z","shell.execute_reply":"2026-03-22T12:33:26.474135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:34:04.729711Z","iopub.execute_input":"2026-03-22T12:34:04.730355Z","iopub.status.idle":"2026-03-22T12:34:04.735867Z","shell.execute_reply.started":"2026-03-22T12:34:04.730319Z","shell.execute_reply":"2026-03-22T12:34:04.735131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:41:05.121805Z","iopub.execute_input":"2026-03-22T12:41:05.122594Z","iopub.status.idle":"2026-03-22T12:41:05.130767Z","shell.execute_reply.started":"2026-03-22T12:41:05.122552Z","shell.execute_reply":"2026-03-22T12:41:05.129279Z"}},"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-03-22T12:42:02.558616Z","iopub.execute_input":"2026-03-22T12:42:02.559572Z","iopub.status.idle":"2026-03-22T12:42:05.378860Z","shell.execute_reply.started":"2026-03-22T12:42:02.559537Z","shell.execute_reply":"2026-03-22T12:42:05.377688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:42:08.781790Z","iopub.execute_input":"2026-03-22T12:42:08.782575Z","iopub.status.idle":"2026-03-22T12:42:08.795138Z","shell.execute_reply.started":"2026-03-22T12:42:08.782533Z","shell.execute_reply":"2026-03-22T12:42:08.794264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:42:41.274305Z","iopub.execute_input":"2026-03-22T12:42:41.275017Z","iopub.status.idle":"2026-03-22T12:42:41.287032Z","shell.execute_reply.started":"2026-03-22T12:42:41.274978Z","shell.execute_reply":"2026-03-22T12:42:41.285854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T12:43:42.842257Z","iopub.execute_input":"2026-03-22T12:43:42.842625Z","iopub.status.idle":"2026-03-22T12:43:42.975978Z","shell.execute_reply.started":"2026-03-22T12:43:42.842593Z","shell.execute_reply":"2026-03-22T12:43:42.975132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}