{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"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-05-30T09:22:21.706966Z","iopub.execute_input":"2025-05-30T09:22:21.708069Z","iopub.status.idle":"2025-05-30T09:22:21.715479Z","shell.execute_reply.started":"2025-05-30T09:22:21.708029Z","shell.execute_reply":"2025-05-30T09:22:21.714616Z"}},"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-05-30T09:22:21.716936Z","iopub.execute_input":"2025-05-30T09:22:21.717248Z","iopub.status.idle":"2025-05-30T09:22:23.912921Z","shell.execute_reply.started":"2025-05-30T09:22:21.717225Z","shell.execute_reply":"2025-05-30T09:22:23.912024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:23.913796Z","iopub.execute_input":"2025-05-30T09:22:23.914043Z","iopub.status.idle":"2025-05-30T09:22:23.919667Z","shell.execute_reply.started":"2025-05-30T09:22:23.914022Z","shell.execute_reply":"2025-05-30T09:22:23.918722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:23.921768Z","iopub.execute_input":"2025-05-30T09:22:23.922045Z","iopub.status.idle":"2025-05-30T09:22:23.946546Z","shell.execute_reply.started":"2025-05-30T09:22:23.922026Z","shell.execute_reply":"2025-05-30T09:22:23.945518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:23.947438Z","iopub.execute_input":"2025-05-30T09:22:23.947814Z","iopub.status.idle":"2025-05-30T09:22:23.972936Z","shell.execute_reply.started":"2025-05-30T09:22:23.947790Z","shell.execute_reply":"2025-05-30T09:22:23.972126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:23.974062Z","iopub.execute_input":"2025-05-30T09:22:23.974415Z","iopub.status.idle":"2025-05-30T09:22:23.990407Z","shell.execute_reply.started":"2025-05-30T09:22:23.974392Z","shell.execute_reply":"2025-05-30T09:22:23.989447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[37870,1:].values.reshape(28,28)) # image show = imshow()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:23.991301Z","iopub.execute_input":"2025-05-30T09:22:23.991584Z","iopub.status.idle":"2025-05-30T09:22:24.139525Z","shell.execute_reply.started":"2025-05-30T09:22:23.991553Z","shell.execute_reply":"2025-05-30T09:22:24.138629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:24.140483Z","iopub.execute_input":"2025-05-30T09:22:24.140734Z","iopub.status.idle":"2025-05-30T09:22:24.145921Z","shell.execute_reply.started":"2025-05-30T09:22:24.140713Z","shell.execute_reply":"2025-05-30T09:22:24.145143Z"}},"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-05-30T09:22:24.146812Z","iopub.execute_input":"2025-05-30T09:22:24.147262Z","iopub.status.idle":"2025-05-30T09:22:24.391190Z","shell.execute_reply.started":"2025-05-30T09:22:24.147238Z","shell.execute_reply":"2025-05-30T09:22:24.390214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:24.394161Z","iopub.execute_input":"2025-05-30T09:22:24.394442Z","iopub.status.idle":"2025-05-30T09:22:24.400052Z","shell.execute_reply.started":"2025-05-30T09:22:24.394420Z","shell.execute_reply":"2025-05-30T09:22:24.399226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nknn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:24.401133Z","iopub.execute_input":"2025-05-30T09:22:24.401795Z","iopub.status.idle":"2025-05-30T09:22:24.415698Z","shell.execute_reply.started":"2025-05-30T09:22:24.401772Z","shell.execute_reply":"2025-05-30T09:22:24.414718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:24.416827Z","iopub.execute_input":"2025-05-30T09:22:24.417212Z","iopub.status.idle":"2025-05-30T09:22:24.562991Z","shell.execute_reply.started":"2025-05-30T09:22:24.417188Z","shell.execute_reply":"2025-05-30T09:22:24.562294Z"}},"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-05-30T09:22:24.564001Z","iopub.execute_input":"2025-05-30T09:22:24.564304Z","iopub.status.idle":"2025-05-30T09:22:43.386411Z","shell.execute_reply.started":"2025-05-30T09:22:24.564282Z","shell.execute_reply":"2025-05-30T09:22:43.385564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:43.387321Z","iopub.execute_input":"2025-05-30T09:22:43.387624Z","iopub.status.idle":"2025-05-30T09:22:43.396345Z","shell.execute_reply.started":"2025-05-30T09:22:43.387597Z","shell.execute_reply":"2025-05-30T09:22:43.395335Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Now we will use PCA to do the same things","metadata":{}},{"cell_type":"code","source":"# step 1 - Standard scalling\n\nfrom sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:43.397333Z","iopub.execute_input":"2025-05-30T09:22:43.397598Z","iopub.status.idle":"2025-05-30T09:22:43.410659Z","shell.execute_reply.started":"2025-05-30T09:22:43.397578Z","shell.execute_reply":"2025-05-30T09:22:43.409733Z"}},"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-05-30T09:22:43.411642Z","iopub.execute_input":"2025-05-30T09:22:43.411957Z","iopub.status.idle":"2025-05-30T09:22:44.052549Z","shell.execute_reply.started":"2025-05-30T09:22:43.411929Z","shell.execute_reply":"2025-05-30T09:22:44.051510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA \n\nfrom sklearn.decomposition import PCA\npca = PCA(n_components = 100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:44.053515Z","iopub.execute_input":"2025-05-30T09:22:44.053779Z","iopub.status.idle":"2025-05-30T09:22:44.059001Z","shell.execute_reply.started":"2025-05-30T09:22:44.053760Z","shell.execute_reply":"2025-05-30T09:22:44.057671Z"}},"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-05-30T09:22:44.060043Z","iopub.execute_input":"2025-05-30T09:22:44.060399Z","iopub.status.idle":"2025-05-30T09:22:47.274615Z","shell.execute_reply.started":"2025-05-30T09:22:44.060361Z","shell.execute_reply":"2025-05-30T09:22:47.273805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:47.275558Z","iopub.execute_input":"2025-05-30T09:22:47.275834Z","iopub.status.idle":"2025-05-30T09:22:47.281893Z","shell.execute_reply.started":"2025-05-30T09:22:47.275812Z","shell.execute_reply":"2025-05-30T09:22:47.281055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:47.282887Z","iopub.execute_input":"2025-05-30T09:22:47.283230Z","iopub.status.idle":"2025-05-30T09:22:47.364281Z","shell.execute_reply.started":"2025-05-30T09:22:47.283208Z","shell.execute_reply":"2025-05-30T09:22:47.363405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:47.365178Z","iopub.execute_input":"2025-05-30T09:22:47.365401Z","iopub.status.idle":"2025-05-30T09:22:48.906422Z","shell.execute_reply.started":"2025-05-30T09:22:47.365383Z","shell.execute_reply":"2025-05-30T09:22:48.905630Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:48.907122Z","iopub.execute_input":"2025-05-30T09:22:48.907364Z","iopub.status.idle":"2025-05-30T09:22:48.914452Z","shell.execute_reply.started":"2025-05-30T09:22:48.907345Z","shell.execute_reply":"2025-05-30T09:22:48.913649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  # doing all these things in a loop just to assume how accuracy increases after adding dimensions\n\n# for i in range(1,784):\n#     pca = PCA(n_components = i)\n    \n#     X_train_trf = pca.fit_transform(X_train)\n#     X_test_trf = pca.transform(X_test)\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-05-30T09:22:48.915344Z","iopub.execute_input":"2025-05-30T09:22:48.915597Z","iopub.status.idle":"2025-05-30T09:22:48.928607Z","shell.execute_reply.started":"2025-05-30T09:22:48.915575Z","shell.execute_reply":"2025-05-30T09:22:48.927614Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":" - Now we will visualize the data in 2D and 3D","metadata":{}},{"cell_type":"code","source":"# transforming in 2D\npca = PCA(n_components = 2)\n\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:48.929575Z","iopub.execute_input":"2025-05-30T09:22:48.929984Z","iopub.status.idle":"2025-05-30T09:22:50.110533Z","shell.execute_reply.started":"2025-05-30T09:22:48.929955Z","shell.execute_reply":"2025-05-30T09:22:50.109605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:50.111490Z","iopub.execute_input":"2025-05-30T09:22:50.111730Z","iopub.status.idle":"2025-05-30T09:22:50.117519Z","shell.execute_reply.started":"2025-05-30T09:22:50.111711Z","shell.execute_reply":"2025-05-30T09:22:50.116723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\ny_train_trf = y_train.astype(str)\n\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                )\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:50.118438Z","iopub.execute_input":"2025-05-30T09:22:50.118713Z","iopub.status.idle":"2025-05-30T09:22:50.239909Z","shell.execute_reply.started":"2025-05-30T09:22:50.118686Z","shell.execute_reply":"2025-05-30T09:22:50.238621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# transforming in 3D\n\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-05-30T09:22:50.241257Z","iopub.execute_input":"2025-05-30T09:22:50.241684Z","iopub.status.idle":"2025-05-30T09:22:51.544490Z","shell.execute_reply.started":"2025-05-30T09:22:50.241653Z","shell.execute_reply":"2025-05-30T09:22:51.543675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:51.548689Z","iopub.execute_input":"2025-05-30T09:22:51.549044Z","iopub.status.idle":"2025-05-30T09:22:51.554694Z","shell.execute_reply.started":"2025-05-30T09:22:51.549023Z","shell.execute_reply":"2025-05-30T09:22:51.553858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = px.scatter_3d(df, x = X_train_trf[:,0], y = X_train_trf[:,1], z = X_train_trf[:,2],\n                   color = y_train.astype(str),\n                   )\n\nfig.update_layout (\n                    margin = dict\n                    (\n                        l = 20,\n                        r = 20,\n                        t = 20, \n                        b = 20\n                    )\n                  )\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:51.555651Z","iopub.execute_input":"2025-05-30T09:22:51.556259Z","iopub.status.idle":"2025-05-30T09:22:51.673864Z","shell.execute_reply.started":"2025-05-30T09:22:51.556230Z","shell.execute_reply":"2025-05-30T09:22:51.672580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_\n\n# Eigen values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:51.675048Z","iopub.execute_input":"2025-05-30T09:22:51.675456Z","iopub.status.idle":"2025-05-30T09:22:51.682194Z","shell.execute_reply.started":"2025-05-30T09:22:51.675423Z","shell.execute_reply":"2025-05-30T09:22:51.681342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_\n\n# Eigen vectors","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:51.683282Z","iopub.execute_input":"2025-05-30T09:22:51.683561Z","iopub.status.idle":"2025-05-30T09:22:51.703906Z","shell.execute_reply.started":"2025-05-30T09:22:51.683542Z","shell.execute_reply":"2025-05-30T09:22:51.702899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:51.704920Z","iopub.execute_input":"2025-05-30T09:22:51.705294Z","iopub.status.idle":"2025-05-30T09:22:51.715479Z","shell.execute_reply.started":"2025-05-30T09:22:51.705270Z","shell.execute_reply":"2025-05-30T09:22:51.714529Z"}},"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-05-30T09:22:51.716437Z","iopub.execute_input":"2025-05-30T09:22:51.716742Z","iopub.status.idle":"2025-05-30T09:22:58.355696Z","shell.execute_reply.started":"2025-05-30T09:22:51.716713Z","shell.execute_reply":"2025-05-30T09:22:58.354832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:58.356826Z","iopub.execute_input":"2025-05-30T09:22:58.357513Z","iopub.status.idle":"2025-05-30T09:22:58.362539Z","shell.execute_reply.started":"2025-05-30T09:22:58.357490Z","shell.execute_reply":"2025-05-30T09:22:58.361795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:58.363376Z","iopub.execute_input":"2025-05-30T09:22:58.363633Z","iopub.status.idle":"2025-05-30T09:22:58.380126Z","shell.execute_reply.started":"2025-05-30T09:22:58.363611Z","shell.execute_reply":"2025-05-30T09:22:58.379339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:58.381002Z","iopub.execute_input":"2025-05-30T09:22:58.381347Z","iopub.status.idle":"2025-05-30T09:22:58.405152Z","shell.execute_reply.started":"2025-05-30T09:22:58.381328Z","shell.execute_reply":"2025-05-30T09:22:58.404353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T09:22:58.405984Z","iopub.execute_input":"2025-05-30T09:22:58.406283Z","iopub.status.idle":"2025-05-30T09:22:58.579298Z","shell.execute_reply.started":"2025-05-30T09:22:58.406262Z","shell.execute_reply":"2025-05-30T09:22:58.578139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}