{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":30918,"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":"2025-02-22T08:38:34.189408Z","iopub.execute_input":"2025-02-22T08:38:34.189714Z","iopub.status.idle":"2025-02-22T08:38:34.942992Z","shell.execute_reply.started":"2025-02-22T08:38:34.189673Z","shell.execute_reply":"2025-02-22T08:38:34.941847Z"}},"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":"2025-02-22T08:38:48.722728Z","iopub.execute_input":"2025-02-22T08:38:48.723076Z","iopub.status.idle":"2025-02-22T08:38:52.148628Z","shell.execute_reply.started":"2025-02-22T08:38:48.723051Z","shell.execute_reply":"2025-02-22T08:38:52.147594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:38:52.149920Z","iopub.execute_input":"2025-02-22T08:38:52.150313Z","iopub.status.idle":"2025-02-22T08:38:52.157321Z","shell.execute_reply.started":"2025-02-22T08:38:52.150278Z","shell.execute_reply":"2025-02-22T08:38:52.156432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:38:52.487171Z","iopub.execute_input":"2025-02-22T08:38:52.487644Z","iopub.status.idle":"2025-02-22T08:38:52.531612Z","shell.execute_reply.started":"2025-02-22T08:38:52.487587Z","shell.execute_reply":"2025-02-22T08:38:52.530595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:38:54.655965Z","iopub.execute_input":"2025-02-22T08:38:54.656413Z","iopub.status.idle":"2025-02-22T08:38:54.662021Z","shell.execute_reply.started":"2025-02-22T08:38:54.656375Z","shell.execute_reply":"2025-02-22T08:38:54.660989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[40584,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:38:57.723641Z","iopub.execute_input":"2025-02-22T08:38:57.724032Z","iopub.status.idle":"2025-02-22T08:38:58.027810Z","shell.execute_reply.started":"2025-02-22T08:38:57.724002Z","shell.execute_reply":"2025-02-22T08:38:58.026668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[19093,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:01.644647Z","iopub.execute_input":"2025-02-22T08:39:01.645034Z","iopub.status.idle":"2025-02-22T08:39:01.831300Z","shell.execute_reply.started":"2025-02-22T08:39:01.645005Z","shell.execute_reply":"2025-02-22T08:39:01.830260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:04.824703Z","iopub.execute_input":"2025-02-22T08:39:04.825093Z","iopub.status.idle":"2025-02-22T08:39:04.830049Z","shell.execute_reply.started":"2025-02-22T08:39:04.825060Z","shell.execute_reply":"2025-02-22T08:39:04.828904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:18.747553Z","iopub.execute_input":"2025-02-22T08:39:18.747929Z","iopub.status.idle":"2025-02-22T08:39:18.764321Z","shell.execute_reply.started":"2025-02-22T08:39:18.747898Z","shell.execute_reply":"2025-02-22T08:39:18.763205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:21.541561Z","iopub.execute_input":"2025-02-22T08:39:21.542089Z","iopub.status.idle":"2025-02-22T08:39:21.549261Z","shell.execute_reply.started":"2025-02-22T08:39:21.542052Z","shell.execute_reply":"2025-02-22T08:39:21.548176Z"}},"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":"2025-02-22T08:39:24.511359Z","iopub.execute_input":"2025-02-22T08:39:24.511795Z","iopub.status.idle":"2025-02-22T08:39:25.467791Z","shell.execute_reply.started":"2025-02-22T08:39:24.511733Z","shell.execute_reply":"2025-02-22T08:39:25.466648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:26.090882Z","iopub.execute_input":"2025-02-22T08:39:26.091516Z","iopub.status.idle":"2025-02-22T08:39:26.097581Z","shell.execute_reply.started":"2025-02-22T08:39:26.091479Z","shell.execute_reply":"2025-02-22T08:39:26.096240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:28.412824Z","iopub.execute_input":"2025-02-22T08:39:28.413152Z","iopub.status.idle":"2025-02-22T08:39:28.650807Z","shell.execute_reply.started":"2025-02-22T08:39:28.413129Z","shell.execute_reply":"2025-02-22T08:39:28.649701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:30.121620Z","iopub.execute_input":"2025-02-22T08:39:30.121973Z","iopub.status.idle":"2025-02-22T08:39:30.126416Z","shell.execute_reply.started":"2025-02-22T08:39:30.121944Z","shell.execute_reply":"2025-02-22T08:39:30.125397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:32.646507Z","iopub.execute_input":"2025-02-22T08:39:32.646873Z","iopub.status.idle":"2025-02-22T08:39:32.797656Z","shell.execute_reply.started":"2025-02-22T08:39:32.646844Z","shell.execute_reply":"2025-02-22T08:39:32.796606Z"}},"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":"2025-02-22T08:39:34.090203Z","iopub.execute_input":"2025-02-22T08:39:34.090696Z","iopub.status.idle":"2025-02-22T08:39:47.641280Z","shell.execute_reply.started":"2025-02-22T08:39:34.090641Z","shell.execute_reply":"2025-02-22T08:39:47.640096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:47.642832Z","iopub.execute_input":"2025-02-22T08:39:47.643209Z","iopub.status.idle":"2025-02-22T08:39:47.649074Z","shell.execute_reply.started":"2025-02-22T08:39:47.643171Z","shell.execute_reply":"2025-02-22T08:39:47.648042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_pred,y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:47.651058Z","iopub.execute_input":"2025-02-22T08:39:47.651470Z","iopub.status.idle":"2025-02-22T08:39:47.670650Z","shell.execute_reply.started":"2025-02-22T08:39:47.651376Z","shell.execute_reply":"2025-02-22T08:39:47.669549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:47.671913Z","iopub.execute_input":"2025-02-22T08:39:47.672288Z","iopub.status.idle":"2025-02-22T08:39:47.683101Z","shell.execute_reply.started":"2025-02-22T08:39:47.672254Z","shell.execute_reply":"2025-02-22T08:39:47.682098Z"}},"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":"2025-02-22T06:36:00.306222Z","iopub.execute_input":"2025-02-22T06:36:00.306515Z","iopub.status.idle":"2025-02-22T06:36:00.606976Z","shell.execute_reply.started":"2025-02-22T06:36:00.306492Z","shell.execute_reply":"2025-02-22T06:36:00.605822Z"}},"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":"2025-02-22T08:39:49.878662Z","iopub.execute_input":"2025-02-22T08:39:49.879080Z","iopub.status.idle":"2025-02-22T08:39:49.883848Z","shell.execute_reply.started":"2025-02-22T08:39:49.879047Z","shell.execute_reply":"2025-02-22T08:39:49.882502Z"}},"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":"2025-02-22T08:39:55.135244Z","iopub.execute_input":"2025-02-22T08:39:55.135575Z","iopub.status.idle":"2025-02-22T08:39:57.232130Z","shell.execute_reply.started":"2025-02-22T08:39:55.135550Z","shell.execute_reply":"2025-02-22T08:39:57.230796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:57.478719Z","iopub.execute_input":"2025-02-22T08:39:57.479175Z","iopub.status.idle":"2025-02-22T08:39:57.486137Z","shell.execute_reply.started":"2025-02-22T08:39:57.479133Z","shell.execute_reply":"2025-02-22T08:39:57.485120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:39:59.858556Z","iopub.execute_input":"2025-02-22T08:39:59.858992Z","iopub.status.idle":"2025-02-22T08:39:59.863528Z","shell.execute_reply.started":"2025-02-22T08:39:59.858946Z","shell.execute_reply":"2025-02-22T08:39:59.862223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:40:02.595378Z","iopub.execute_input":"2025-02-22T08:40:02.595738Z","iopub.status.idle":"2025-02-22T08:40:02.615467Z","shell.execute_reply.started":"2025-02-22T08:40:02.595708Z","shell.execute_reply":"2025-02-22T08:40:02.614443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:40:04.660986Z","iopub.execute_input":"2025-02-22T08:40:04.661307Z","iopub.status.idle":"2025-02-22T08:40:06.296921Z","shell.execute_reply.started":"2025-02-22T08:40:04.661281Z","shell.execute_reply":"2025-02-22T08:40:06.295853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:40:06.298146Z","iopub.execute_input":"2025-02-22T08:40:06.298460Z","iopub.status.idle":"2025-02-22T08:40:06.305673Z","shell.execute_reply.started":"2025-02-22T08:40:06.298428Z","shell.execute_reply":"2025-02-22T08:40:06.304682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,785):\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":"2025-02-22T08:45:46.963533Z","iopub.execute_input":"2025-02-22T08:45:46.963970Z","iopub.status.idle":"2025-02-22T08:49:32.158857Z","shell.execute_reply.started":"2025-02-22T08:45:46.963938Z","shell.execute_reply":"2025-02-22T08:49:32.156854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Transforming to a 2D Cordinate 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":"2025-02-22T08:49:56.167579Z","iopub.execute_input":"2025-02-22T08:49:56.167990Z","iopub.status.idle":"2025-02-22T08:49:57.339675Z","shell.execute_reply.started":"2025-02-22T08:49:56.167960Z","shell.execute_reply":"2025-02-22T08:49:57.338526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T08:50:00.486608Z","iopub.execute_input":"2025-02-22T08:50:00.486998Z","iopub.status.idle":"2025-02-22T08:50:00.493360Z","shell.execute_reply.started":"2025-02-22T08:50:00.486970Z","shell.execute_reply":"2025-02-22T08:50:00.492239Z"}},"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":"2025-02-22T08:59:44.311650Z","iopub.execute_input":"2025-02-22T08:59:44.312094Z","iopub.status.idle":"2025-02-22T08:59:44.421986Z","shell.execute_reply.started":"2025-02-22T08:59:44.312060Z","shell.execute_reply":"2025-02-22T08:59:44.420543Z"}},"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":"2025-02-22T09:01:08.372076Z","iopub.execute_input":"2025-02-22T09:01:08.372495Z","iopub.status.idle":"2025-02-22T09:01:09.621087Z","shell.execute_reply.started":"2025-02-22T09:01:08.372463Z","shell.execute_reply":"2025-02-22T09:01:09.619917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:01:22.231627Z","iopub.execute_input":"2025-02-22T09:01:22.232022Z","iopub.status.idle":"2025-02-22T09:01:22.238229Z","shell.execute_reply.started":"2025-02-22T09:01:22.231992Z","shell.execute_reply":"2025-02-22T09:01:22.237161Z"}},"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],\n                    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":"2025-02-22T09:02:42.731503Z","iopub.execute_input":"2025-02-22T09:02:42.731887Z","iopub.status.idle":"2025-02-22T09:02:42.914558Z","shell.execute_reply.started":"2025-02-22T09:02:42.731857Z","shell.execute_reply":"2025-02-22T09:02:42.913245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen Values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:06:24.826605Z","iopub.execute_input":"2025-02-22T09:06:24.826995Z","iopub.status.idle":"2025-02-22T09:06:24.833156Z","shell.execute_reply.started":"2025-02-22T09:06:24.826964Z","shell.execute_reply":"2025-02-22T09:06:24.832188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:06:50.181490Z","iopub.execute_input":"2025-02-22T09:06:50.181885Z","iopub.status.idle":"2025-02-22T09:06:50.187622Z","shell.execute_reply.started":"2025-02-22T09:06:50.181851Z","shell.execute_reply":"2025-02-22T09:06:50.186545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:10:21.578992Z","iopub.execute_input":"2025-02-22T09:10:21.579317Z","iopub.status.idle":"2025-02-22T09:10:21.585506Z","shell.execute_reply.started":"2025-02-22T09:10:21.579293Z","shell.execute_reply":"2025-02-22T09:10:21.584565Z"}},"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":"2025-02-22T09:12:34.104167Z","iopub.execute_input":"2025-02-22T09:12:34.104526Z","iopub.status.idle":"2025-02-22T09:12:41.083912Z","shell.execute_reply.started":"2025-02-22T09:12:34.104493Z","shell.execute_reply":"2025-02-22T09:12:41.082995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:12:32.698891Z","iopub.execute_input":"2025-02-22T09:12:32.699216Z","iopub.status.idle":"2025-02-22T09:12:32.705051Z","shell.execute_reply.started":"2025-02-22T09:12:32.699192Z","shell.execute_reply":"2025-02-22T09:12:32.703836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:13:09.113480Z","iopub.execute_input":"2025-02-22T09:13:09.113939Z","iopub.status.idle":"2025-02-22T09:13:09.119917Z","shell.execute_reply.started":"2025-02-22T09:13:09.113895Z","shell.execute_reply":"2025-02-22T09:13:09.118881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:13:29.909721Z","iopub.execute_input":"2025-02-22T09:13:29.910097Z","iopub.status.idle":"2025-02-22T09:13:29.923182Z","shell.execute_reply.started":"2025-02-22T09:13:29.910064Z","shell.execute_reply":"2025-02-22T09:13:29.922083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:13:50.667598Z","iopub.execute_input":"2025-02-22T09:13:50.667968Z","iopub.status.idle":"2025-02-22T09:13:50.870595Z","shell.execute_reply.started":"2025-02-22T09:13:50.667939Z","shell.execute_reply":"2025-02-22T09:13:50.869125Z"}},"outputs":[],"execution_count":null}]}