{"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-08-29T11:54:30.419660Z","iopub.execute_input":"2026-08-29T11:54:30.419931Z","iopub.status.idle":"2026-08-29T11:54:32.361228Z","shell.execute_reply.started":"2026-08-29T11:54:30.419894Z","shell.execute_reply":"2026-08-29T11:54:32.360409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndf=pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:02:30.536606Z","iopub.execute_input":"2026-08-29T12:02:30.537485Z","iopub.status.idle":"2026-08-29T12:02:32.714233Z","shell.execute_reply.started":"2026-08-29T12:02:30.537446Z","shell.execute_reply":"2026-08-29T12:02:32.713561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:02:33.872272Z","iopub.execute_input":"2026-08-29T12:02:33.872585Z","iopub.status.idle":"2026-08-29T12:02:33.885518Z","shell.execute_reply.started":"2026-08-29T12:02:33.872555Z","shell.execute_reply":"2026-08-29T12:02:33.884750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:02:34.721591Z","iopub.execute_input":"2026-08-29T12:02:34.722025Z","iopub.status.idle":"2026-08-29T12:02:34.727210Z","shell.execute_reply.started":"2026-08-29T12:02:34.721995Z","shell.execute_reply":"2026-08-29T12:02:34.726496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(random_state=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:02:54.551726Z","iopub.execute_input":"2026-08-29T12:02:54.552563Z","iopub.status.idle":"2026-08-29T12:02:54.567853Z","shell.execute_reply.started":"2026-08-29T12:02:54.552528Z","shell.execute_reply":"2026-08-29T12:02:54.567055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[38732,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:04:48.436922Z","iopub.execute_input":"2026-08-29T12:04:48.437278Z","iopub.status.idle":"2026-08-29T12:04:48.566118Z","shell.execute_reply.started":"2026-08-29T12:04:48.437237Z","shell.execute_reply":"2026-08-29T12:04:48.565256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:29:40.121392Z","iopub.execute_input":"2026-08-29T12:29:40.122208Z","iopub.status.idle":"2026-08-29T12:29:40.126519Z","shell.execute_reply.started":"2026-08-29T12:29:40.122171Z","shell.execute_reply":"2026-08-29T12:29:40.125819Z"}},"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-08-29T12:29:40.983208Z","iopub.execute_input":"2026-08-29T12:29:40.983533Z","iopub.status.idle":"2026-08-29T12:29:41.204697Z","shell.execute_reply.started":"2026-08-29T12:29:40.983502Z","shell.execute_reply":"2026-08-29T12:29:41.203865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:10:24.969635Z","iopub.execute_input":"2026-08-29T12:10:24.970171Z","iopub.status.idle":"2026-08-29T12:10:24.976548Z","shell.execute_reply.started":"2026-08-29T12:10:24.970138Z","shell.execute_reply":"2026-08-29T12:10:24.975837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:11:43.421382Z","iopub.execute_input":"2026-08-29T12:11:43.422259Z","iopub.status.idle":"2026-08-29T12:11:43.657776Z","shell.execute_reply.started":"2026-08-29T12:11:43.422223Z","shell.execute_reply":"2026-08-29T12:11:43.657141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:12:17.721426Z","iopub.execute_input":"2026-08-29T12:12:17.722249Z","iopub.status.idle":"2026-08-29T12:12:17.726256Z","shell.execute_reply.started":"2026-08-29T12:12:17.722215Z","shell.execute_reply":"2026-08-29T12:12:17.725329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:13:45.200858Z","iopub.execute_input":"2026-08-29T12:13:45.201493Z","iopub.status.idle":"2026-08-29T12:13:45.324089Z","shell.execute_reply.started":"2026-08-29T12:13:45.201452Z","shell.execute_reply":"2026-08-29T12:13:45.323316Z"}},"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-08-29T12:18:16.199733Z","iopub.execute_input":"2026-08-29T12:18:16.200116Z","iopub.status.idle":"2026-08-29T12:18:25.615671Z","shell.execute_reply.started":"2026-08-29T12:18:16.200081Z","shell.execute_reply":"2026-08-29T12:18:25.614737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint('accuracy: ',accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:18:34.510702Z","iopub.execute_input":"2026-08-29T12:18:34.511058Z","iopub.status.idle":"2026-08-29T12:18:34.518324Z","shell.execute_reply.started":"2026-08-29T12:18:34.511027Z","shell.execute_reply":"2026-08-29T12:18:34.517390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:20:08.704690Z","iopub.execute_input":"2026-08-29T12:20:08.705486Z","iopub.status.idle":"2026-08-29T12:20:08.709181Z","shell.execute_reply.started":"2026-08-29T12:20:08.705449Z","shell.execute_reply":"2026-08-29T12:20:08.708461Z"}},"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-08-29T12:21:33.601230Z","iopub.execute_input":"2026-08-29T12:21:33.602158Z","iopub.status.idle":"2026-08-29T12:21:34.050141Z","shell.execute_reply.started":"2026-08-29T12:21:33.602121Z","shell.execute_reply":"2026-08-29T12:21:34.049402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca=PCA(n_components=100)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:05.554695Z","iopub.execute_input":"2026-08-29T12:47:05.555568Z","iopub.status.idle":"2026-08-29T12:47:05.559470Z","shell.execute_reply.started":"2026-08-29T12:47:05.555531Z","shell.execute_reply":"2026-08-29T12:47:05.558684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#in this step pca will find eigen values and eign vecgtors and project our data to new principal components\nx_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:06.233075Z","iopub.execute_input":"2026-08-29T12:47:06.233951Z","iopub.status.idle":"2026-08-29T12:47:07.412817Z","shell.execute_reply.started":"2026-08-29T12:47:06.233913Z","shell.execute_reply":"2026-08-29T12:47:07.411953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:07.414152Z","iopub.execute_input":"2026-08-29T12:47:07.414428Z","iopub.status.idle":"2026-08-29T12:47:07.419624Z","shell.execute_reply.started":"2026-08-29T12:47:07.414402Z","shell.execute_reply":"2026-08-29T12:47:07.419002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:09.269751Z","iopub.execute_input":"2026-08-29T12:47:09.270655Z","iopub.status.idle":"2026-08-29T12:47:09.283839Z","shell.execute_reply.started":"2026-08-29T12:47:09.270612Z","shell.execute_reply":"2026-08-29T12:47:09.283091Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:09.897957Z","iopub.execute_input":"2026-08-29T12:47:09.898760Z","iopub.status.idle":"2026-08-29T12:47:11.342152Z","shell.execute_reply.started":"2026-08-29T12:47:09.898723Z","shell.execute_reply":"2026-08-29T12:47:11.341345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('accuracy score after performing pca: ',accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:47:11.343693Z","iopub.execute_input":"2026-08-29T12:47:11.344336Z","iopub.status.idle":"2026-08-29T12:47:11.351066Z","shell.execute_reply.started":"2026-08-29T12:47:11.344304Z","shell.execute_reply":"2026-08-29T12:47:11.350252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,30):\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(accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:39:44.560331Z","iopub.execute_input":"2026-08-29T12:39:44.561055Z","iopub.status.idle":"2026-08-29T12:40:38.456175Z","shell.execute_reply.started":"2026-08-29T12:39:44.561020Z","shell.execute_reply":"2026-08-29T12:40:38.455364Z"}},"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-08-29T12:48:04.766783Z","iopub.execute_input":"2026-08-29T12:48:04.767526Z","iopub.status.idle":"2026-08-29T12:48:05.925025Z","shell.execute_reply.started":"2026-08-29T12:48:04.767490Z","shell.execute_reply":"2026-08-29T12:48:05.923666Z"}},"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-08-29T12:49:26.299711Z","iopub.execute_input":"2026-08-29T12:49:26.300171Z","iopub.status.idle":"2026-08-29T12:49:26.399870Z","shell.execute_reply.started":"2026-08-29T12:49:26.300130Z","shell.execute_reply":"2026-08-29T12:49:26.398663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming in 3D\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-08-29T13:00:03.343887Z","iopub.execute_input":"2026-08-29T13:00:03.344322Z","iopub.status.idle":"2026-08-29T13:00:04.612120Z","shell.execute_reply.started":"2026-08-29T13:00:03.344286Z","shell.execute_reply":"2026-08-29T13:00:04.611260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport plotly.express as px\ny_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    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:00:07.940609Z","iopub.execute_input":"2026-08-29T13:00:07.941055Z","iopub.status.idle":"2026-08-29T13:00:08.058144Z","shell.execute_reply.started":"2026-08-29T13:00:07.941004Z","shell.execute_reply":"2026-08-29T13:00:08.056931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#pca has attributes in scikit-learn\npca.explained_variance_\n# Eigen values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:59:00.445437Z","iopub.execute_input":"2026-08-29T12:59:00.445855Z","iopub.status.idle":"2026-08-29T12:59:00.452222Z","shell.execute_reply.started":"2026-08-29T12:59:00.445821Z","shell.execute_reply":"2026-08-29T12:59:00.451454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T12:59:26.748472Z","iopub.execute_input":"2026-08-29T12:59:26.748877Z","iopub.status.idle":"2026-08-29T12:59:26.754524Z","shell.execute_reply.started":"2026-08-29T12:59:26.748836Z","shell.execute_reply":"2026-08-29T12:59:26.753700Z"}},"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-08-29T13:10:02.367173Z","iopub.execute_input":"2026-08-29T13:10:02.367532Z","iopub.status.idle":"2026-08-29T13:10:05.212826Z","shell.execute_reply.started":"2026-08-29T13:10:02.367497Z","shell.execute_reply":"2026-08-29T13:10:05.212192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:11:38.646578Z","iopub.execute_input":"2026-08-29T13:11:38.647034Z","iopub.status.idle":"2026-08-29T13:11:38.657419Z","shell.execute_reply.started":"2026-08-29T13:11:38.646997Z","shell.execute_reply":"2026-08-29T13:11:38.656557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:12:08.935494Z","iopub.execute_input":"2026-08-29T13:12:08.936056Z","iopub.status.idle":"2026-08-29T13:12:08.941167Z","shell.execute_reply.started":"2026-08-29T13:12:08.936020Z","shell.execute_reply":"2026-08-29T13:12:08.940471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:12:11.431420Z","iopub.execute_input":"2026-08-29T13:12:11.431771Z","iopub.status.idle":"2026-08-29T13:12:11.437273Z","shell.execute_reply.started":"2026-08-29T13:12:11.431715Z","shell.execute_reply":"2026-08-29T13:12:11.436481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:15:01.623037Z","iopub.execute_input":"2026-08-29T13:15:01.623368Z","iopub.status.idle":"2026-08-29T13:15:01.633544Z","shell.execute_reply.started":"2026-08-29T13:15:01.623336Z","shell.execute_reply":"2026-08-29T13:15:01.632768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-29T13:16:41.205195Z","iopub.execute_input":"2026-08-29T13:16:41.205532Z","iopub.status.idle":"2026-08-29T13:16:41.339584Z","shell.execute_reply.started":"2026-08-29T13:16:41.205504Z","shell.execute_reply":"2026-08-29T13:16:41.338883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}