{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","execution":{"iopub.status.busy":"2024-04-04T10:50:29.436475Z","iopub.execute_input":"2024-04-04T10:50:29.437233Z","iopub.status.idle":"2024-04-04T10:50:29.444625Z","shell.execute_reply.started":"2024-04-04T10:50:29.437192Z","shell.execute_reply":"2024-04-04T10:50:29.443523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:29.446402Z","iopub.execute_input":"2024-04-04T10:50:29.447182Z","iopub.status.idle":"2024-04-04T10:50:31.720539Z","shell.execute_reply.started":"2024-04-04T10:50:29.447146Z","shell.execute_reply":"2024-04-04T10:50:31.719572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:31.722370Z","iopub.execute_input":"2024-04-04T10:50:31.722732Z","iopub.status.idle":"2024-04-04T10:50:31.739382Z","shell.execute_reply.started":"2024-04-04T10:50:31.722673Z","shell.execute_reply":"2024-04-04T10:50:31.738469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.imshow(df.sample().values[0][1:].reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:31.740499Z","iopub.execute_input":"2024-04-04T10:50:31.741082Z","iopub.status.idle":"2024-04-04T10:50:31.998837Z","shell.execute_reply.started":"2024-04-04T10:50:31.741049Z","shell.execute_reply":"2024-04-04T10:50:31.997861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= df.iloc[:, 1:]\ny= df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.000902Z","iopub.execute_input":"2024-04-04T10:50:32.001177Z","iopub.status.idle":"2024-04-04T10:50:32.005671Z","shell.execute_reply.started":"2024-04-04T10:50:32.001153Z","shell.execute_reply":"2024-04-04T10:50:32.004756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.unique()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.006783Z","iopub.execute_input":"2024-04-04T10:50:32.007047Z","iopub.status.idle":"2024-04-04T10:50:32.032436Z","shell.execute_reply.started":"2024-04-04T10:50:32.007025Z","shell.execute_reply":"2024-04-04T10:50:32.031561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.2, random_state=101)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.033422Z","iopub.execute_input":"2024-04-04T10:50:32.033762Z","iopub.status.idle":"2024-04-04T10:50:32.250507Z","shell.execute_reply.started":"2024-04-04T10:50:32.033729Z","shell.execute_reply":"2024-04-04T10:50:32.249721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.251874Z","iopub.execute_input":"2024-04-04T10:50:32.252301Z","iopub.status.idle":"2024-04-04T10:50:32.259344Z","shell.execute_reply.started":"2024-04-04T10:50:32.252266Z","shell.execute_reply":"2024-04-04T10:50:32.258436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nknn= KNeighborsClassifier()\nknn.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.260660Z","iopub.execute_input":"2024-04-04T10:50:32.261344Z","iopub.status.idle":"2024-04-04T10:50:32.384056Z","shell.execute_reply.started":"2024-04-04T10:50:32.261312Z","shell.execute_reply":"2024-04-04T10:50:32.383043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nstart= time.time()\ny_pred= knn.predict(x_test)\nprint(time.time()- start)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:32.385253Z","iopub.execute_input":"2024-04-04T10:50:32.385538Z","iopub.status.idle":"2024-04-04T10:50:41.344341Z","shell.execute_reply.started":"2024-04-04T10:50:32.385512Z","shell.execute_reply":"2024-04-04T10:50:41.343374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:41.347565Z","iopub.execute_input":"2024-04-04T10:50:41.347864Z","iopub.status.idle":"2024-04-04T10:50:41.355357Z","shell.execute_reply.started":"2024-04-04T10:50:41.347838Z","shell.execute_reply":"2024-04-04T10:50:41.354477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler= StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:41.356493Z","iopub.execute_input":"2024-04-04T10:50:41.356842Z","iopub.status.idle":"2024-04-04T10:50:41.363244Z","shell.execute_reply.started":"2024-04-04T10:50:41.356807Z","shell.execute_reply":"2024-04-04T10:50:41.362310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train= scaler.fit_transform(x_train)\nx_test= scaler.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:41.364245Z","iopub.execute_input":"2024-04-04T10:50:41.364505Z","iopub.status.idle":"2024-04-04T10:50:41.810279Z","shell.execute_reply.started":"2024-04-04T10:50:41.364483Z","shell.execute_reply":"2024-04-04T10:50:41.809501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca= PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:56.889840Z","iopub.execute_input":"2024-04-04T10:50:56.890191Z","iopub.status.idle":"2024-04-04T10:50:56.894801Z","shell.execute_reply.started":"2024-04-04T10:50:56.890163Z","shell.execute_reply":"2024-04-04T10:50:56.893752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_pca= pca.fit_transform(x_train)\nx_test_pca= pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:50:58.896831Z","iopub.execute_input":"2024-04-04T10:50:58.897965Z","iopub.status.idle":"2024-04-04T10:51:02.711437Z","shell.execute_reply.started":"2024-04-04T10:50:58.897930Z","shell.execute_reply":"2024-04-04T10:51:02.710094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_pca.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:51:03.733961Z","iopub.execute_input":"2024-04-04T10:51:03.734558Z","iopub.status.idle":"2024-04-04T10:51:03.740312Z","shell.execute_reply.started":"2024-04-04T10:51:03.734527Z","shell.execute_reply":"2024-04-04T10:51:03.739274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn= KNeighborsClassifier()\nknn.fit(x_train_pca, y_train)\n\nstart= time.time()\ny_pred_pca= knn.predict(x_test_pca)\nprint(time.time()- start)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:51:04.747469Z","iopub.execute_input":"2024-04-04T10:51:04.748121Z","iopub.status.idle":"2024-04-04T10:51:06.721501Z","shell.execute_reply.started":"2024-04-04T10:51:04.748087Z","shell.execute_reply":"2024-04-04T10:51:06.720542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred_pca)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:51:06.722851Z","iopub.execute_input":"2024-04-04T10:51:06.723123Z","iopub.status.idle":"2024-04-04T10:51:06.730676Z","shell.execute_reply.started":"2024-04-04T10:51:06.723098Z","shell.execute_reply":"2024-04-04T10:51:06.729815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{}},{"cell_type":"code","source":"pca= PCA(n_components=2)\nx_train_pca= pca.fit_transform(x_train)\nx_test_pca= pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:55:46.796374Z","iopub.execute_input":"2024-04-04T10:55:46.797060Z","iopub.status.idle":"2024-04-04T10:55:48.205363Z","shell.execute_reply.started":"2024-04-04T10:55:46.797024Z","shell.execute_reply":"2024-04-04T10:55:48.203925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\ny_train_trf= y_train.astype(str)\nfig= px.scatter(x= x_train_pca[:,0],y= x_train_pca[:,1], color= y_train_trf, color_discrete_sequence= px.colors.qualitative.G10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:59:37.077907Z","iopub.execute_input":"2024-04-04T10:59:37.078283Z","iopub.status.idle":"2024-04-04T10:59:37.213672Z","shell.execute_reply.started":"2024-04-04T10:59:37.078251Z","shell.execute_reply":"2024-04-04T10:59:37.212824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca= PCA(n_components=3)\nx_train_pca= pca.fit_transform(x_train)\nx_test_pca= pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:00:05.957272Z","iopub.execute_input":"2024-04-04T11:00:05.957629Z","iopub.status.idle":"2024-04-04T11:00:07.516716Z","shell.execute_reply.started":"2024-04-04T11:00:05.957599Z","shell.execute_reply":"2024-04-04T11:00:07.515377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_trf= y_train.astype(str)\nfig= px.scatter_3d(x= x_train_pca[:,0],y= x_train_pca[:,1], z= x_train_pca[:,2], color= y_train_trf, color_discrete_sequence= px.colors.qualitative.G10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:01:12.862115Z","iopub.execute_input":"2024-04-04T11:01:12.862849Z","iopub.status.idle":"2024-04-04T11:01:13.045052Z","shell.execute_reply.started":"2024-04-04T11:01:12.862818Z","shell.execute_reply":"2024-04-04T11:01:13.043637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:06:55.430613Z","iopub.execute_input":"2024-04-04T11:06:55.431341Z","iopub.status.idle":"2024-04-04T11:06:55.438494Z","shell.execute_reply.started":"2024-04-04T11:06:55.431305Z","shell.execute_reply":"2024-04-04T11:06:55.437192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca= PCA(n_components=None)\nx_train_pca= pca.fit_transform(x_train)\nx_test_pca= pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:18:45.102863Z","iopub.execute_input":"2024-04-04T11:18:45.103252Z","iopub.status.idle":"2024-04-04T11:18:48.205165Z","shell.execute_reply.started":"2024-04-04T11:18:45.103223Z","shell.execute_reply":"2024-04-04T11:18:48.203634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:19:12.264956Z","iopub.execute_input":"2024-04-04T11:19:12.265798Z","iopub.status.idle":"2024-04-04T11:19:12.281825Z","shell.execute_reply.started":"2024-04-04T11:19:12.265764Z","shell.execute_reply":"2024-04-04T11:19:12.280870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:19:28.957889Z","iopub.execute_input":"2024-04-04T11:19:28.958238Z","iopub.status.idle":"2024-04-04T11:19:28.976045Z","shell.execute_reply.started":"2024-04-04T11:19:28.958209Z","shell.execute_reply":"2024-04-04T11:19:28.975142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T11:19:43.706997Z","iopub.execute_input":"2024-04-04T11:19:43.707591Z","iopub.status.idle":"2024-04-04T11:19:43.963372Z","shell.execute_reply.started":"2024-04-04T11:19:43.707562Z","shell.execute_reply":"2024-04-04T11:19:43.962623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}