{"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-17T06:07:41.191496Z","iopub.execute_input":"2026-07-17T06:07:41.191843Z","iopub.status.idle":"2026-07-17T06:07:41.212869Z","shell.execute_reply.started":"2026-07-17T06:07:41.191817Z","shell.execute_reply":"2026-07-17T06:07:41.211922Z"}},"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-17T06:07:43.614723Z","iopub.execute_input":"2026-07-17T06:07:43.615085Z","iopub.status.idle":"2026-07-17T06:07:46.033527Z","shell.execute_reply.started":"2026-07-17T06:07:43.615055Z","shell.execute_reply":"2026-07-17T06:07:46.032537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T04:51:03.070650Z","iopub.execute_input":"2026-07-17T04:51:03.071576Z","iopub.status.idle":"2026-07-17T04:51:03.078078Z","shell.execute_reply.started":"2026-07-17T04:51:03.071538Z","shell.execute_reply":"2026-07-17T04:51:03.077017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T04:51:19.529101Z","iopub.execute_input":"2026-07-17T04:51:19.529566Z","iopub.status.idle":"2026-07-17T04:51:19.558843Z","shell.execute_reply.started":"2026-07-17T04:51:19.529536Z","shell.execute_reply":"2026-07-17T04:51:19.557714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T04:51:44.028131Z","iopub.execute_input":"2026-07-17T04:51:44.029201Z","iopub.status.idle":"2026-07-17T04:51:44.033507Z","shell.execute_reply.started":"2026-07-17T04:51:44.029167Z","shell.execute_reply":"2026-07-17T04:51:44.032582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[12590,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T04:56:36.267162Z","iopub.execute_input":"2026-07-17T04:56:36.267627Z","iopub.status.idle":"2026-07-17T04:56:36.504113Z","shell.execute_reply.started":"2026-07-17T04:56:36.267598Z","shell.execute_reply":"2026-07-17T04:56:36.502501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop(columns=['label'])\ny = df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:07:51.411337Z","iopub.execute_input":"2026-07-17T06:07:51.411651Z","iopub.status.idle":"2026-07-17T06:07:51.520278Z","shell.execute_reply.started":"2026-07-17T06:07:51.411627Z","shell.execute_reply":"2026-07-17T06:07:51.519168Z"}},"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-07-17T06:07:52.715619Z","iopub.execute_input":"2026-07-17T06:07:52.716683Z","iopub.status.idle":"2026-07-17T06:07:53.597886Z","shell.execute_reply.started":"2026-07-17T06:07:52.716648Z","shell.execute_reply":"2026-07-17T06:07:53.596811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:01:02.768853Z","iopub.execute_input":"2026-07-17T05:01:02.769838Z","iopub.status.idle":"2026-07-17T05:01:02.775139Z","shell.execute_reply.started":"2026-07-17T05:01:02.769805Z","shell.execute_reply":"2026-07-17T05:01:02.774357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:07:56.753195Z","iopub.execute_input":"2026-07-17T06:07:56.754301Z","iopub.status.idle":"2026-07-17T06:07:56.758676Z","shell.execute_reply.started":"2026-07-17T06:07:56.754265Z","shell.execute_reply":"2026-07-17T06:07:56.757701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:07:58.475935Z","iopub.execute_input":"2026-07-17T06:07:58.476386Z","iopub.status.idle":"2026-07-17T06:07:58.481565Z","shell.execute_reply.started":"2026-07-17T06:07:58.476349Z","shell.execute_reply":"2026-07-17T06:07:58.480500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:08:00.772950Z","iopub.execute_input":"2026-07-17T06:08:00.773889Z","iopub.status.idle":"2026-07-17T06:08:01.043461Z","shell.execute_reply.started":"2026-07-17T06:08:00.773847Z","shell.execute_reply":"2026-07-17T06:08:01.042303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:08:25.424013Z","iopub.execute_input":"2026-07-17T06:08:25.424385Z","iopub.status.idle":"2026-07-17T06:08:42.512610Z","shell.execute_reply.started":"2026-07-17T06:08:25.424346Z","shell.execute_reply":"2026-07-17T06:08:42.511505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)\n#Accuracy score while doing it normally without using PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:04:38.986059Z","iopub.execute_input":"2026-07-17T05:04:38.986388Z","iopub.status.idle":"2026-07-17T05:04:38.995368Z","shell.execute_reply.started":"2026-07-17T05:04:38.986362Z","shell.execute_reply":"2026-07-17T05:04:38.994459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Using scalerscaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:04:52.207982Z","iopub.execute_input":"2026-07-17T05:04:52.208916Z","iopub.status.idle":"2026-07-17T05:04:52.213087Z","shell.execute_reply.started":"2026-07-17T05:04:52.208878Z","shell.execute_reply":"2026-07-17T05:04:52.212002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:05:25.037374Z","iopub.execute_input":"2026-07-17T05:05:25.037688Z","iopub.status.idle":"2026-07-17T05:05:25.042683Z","shell.execute_reply.started":"2026-07-17T05:05:25.037663Z","shell.execute_reply":"2026-07-17T05:05:25.041665Z"}},"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-17T06:08:55.469790Z","iopub.execute_input":"2026-07-17T06:08:55.470465Z","iopub.status.idle":"2026-07-17T06:08:55.948587Z","shell.execute_reply.started":"2026-07-17T06:08:55.470429Z","shell.execute_reply":"2026-07-17T06:08:55.947602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Now applying PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=250)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:08:57.073371Z","iopub.execute_input":"2026-07-17T06:08:57.073675Z","iopub.status.idle":"2026-07-17T06:08:57.078555Z","shell.execute_reply.started":"2026-07-17T06:08:57.073651Z","shell.execute_reply":"2026-07-17T06:08:57.077551Z"}},"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-17T06:08:58.840928Z","iopub.execute_input":"2026-07-17T06:08:58.841325Z","iopub.status.idle":"2026-07-17T06:09:00.391748Z","shell.execute_reply.started":"2026-07-17T06:08:58.841289Z","shell.execute_reply":"2026-07-17T06:09:00.390654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:09:02.393576Z","iopub.execute_input":"2026-07-17T06:09:02.394560Z","iopub.status.idle":"2026-07-17T06:09:02.401363Z","shell.execute_reply.started":"2026-07-17T06:09:02.394510Z","shell.execute_reply":"2026-07-17T06:09:02.400253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn2 = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:09:09.394298Z","iopub.execute_input":"2026-07-17T06:09:09.394613Z","iopub.status.idle":"2026-07-17T06:09:09.399210Z","shell.execute_reply.started":"2026-07-17T06:09:09.394588Z","shell.execute_reply":"2026-07-17T06:09:09.398121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn2.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:09:10.800329Z","iopub.execute_input":"2026-07-17T06:09:10.800709Z","iopub.status.idle":"2026-07-17T06:09:10.817215Z","shell.execute_reply.started":"2026-07-17T06:09:10.800679Z","shell.execute_reply":"2026-07-17T06:09:10.816248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn2.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:09:13.751214Z","iopub.execute_input":"2026-07-17T06:09:13.751538Z","iopub.status.idle":"2026-07-17T06:09:16.831133Z","shell.execute_reply.started":"2026-07-17T06:09:13.751512Z","shell.execute_reply":"2026-07-17T06:09:16.829987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:09:18.111468Z","iopub.execute_input":"2026-07-17T06:09:18.112430Z","iopub.status.idle":"2026-07-17T06:09:18.122308Z","shell.execute_reply.started":"2026-07-17T06:09:18.112385Z","shell.execute_reply":"2026-07-17T06:09:18.121116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Now I will see how this works at different value of n from 0 to 785\nfor i in range(1,785):\n    pca = PCA(n_components=i)\n    knn3 = KNeighborsClassifier()\n    knn3.fit(X_train_trf,y_train)\n    y_pred = knn3.predict(X_test_trf)\n    print(accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:13:27.665467Z","iopub.execute_input":"2026-07-17T05:13:27.665862Z","iopub.status.idle":"2026-07-17T05:17:19.237423Z","shell.execute_reply.started":"2026-07-17T05:13:27.665831Z","shell.execute_reply":"2026-07-17T05:17:19.236185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import 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-07-17T06:09:22.287539Z","iopub.execute_input":"2026-07-17T06:09:22.288664Z","iopub.status.idle":"2026-07-17T06:09:22.401979Z","shell.execute_reply.started":"2026-07-17T06:09:22.288600Z","shell.execute_reply":"2026-07-17T06:09:22.400795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = 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-07-17T05:54:10.114677Z","iopub.execute_input":"2026-07-17T05:54:10.115720Z","iopub.status.idle":"2026-07-17T05:54:11.162346Z","shell.execute_reply.started":"2026-07-17T05:54:10.115684Z","shell.execute_reply":"2026-07-17T05:54:11.161282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#These are eigen valus\npca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:54:12.907300Z","iopub.execute_input":"2026-07-17T05:54:12.907745Z","iopub.status.idle":"2026-07-17T05:54:12.914773Z","shell.execute_reply.started":"2026-07-17T05:54:12.907715Z","shell.execute_reply":"2026-07-17T05:54:12.913846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#These are eigen vectors\npca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T05:56:40.235280Z","iopub.execute_input":"2026-07-17T05:56:40.237140Z","iopub.status.idle":"2026-07-17T05:56:40.245103Z","shell.execute_reply.started":"2026-07-17T05:56:40.237068Z","shell.execute_reply":"2026-07-17T05:56:40.244097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#This is how much variance is explaned by each principal component\npca.explained_variance_ratio_","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:06:48.227765Z","iopub.execute_input":"2026-07-17T06:06:48.228361Z","iopub.status.idle":"2026-07-17T06:06:48.236191Z","shell.execute_reply.started":"2026-07-17T06:06:48.228328Z","shell.execute_reply":"2026-07-17T06:06:48.235197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T06:07:20.821838Z","iopub.execute_input":"2026-07-17T06:07:20.822285Z","iopub.status.idle":"2026-07-17T06:07:20.966810Z","shell.execute_reply.started":"2026-07-17T06:07:20.822256Z","shell.execute_reply":"2026-07-17T06:07:20.965854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}