{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\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":"2023-01-07T16:46:13.384639Z","iopub.execute_input":"2023-01-07T16:46:13.385279Z","iopub.status.idle":"2023-01-07T16:46:13.419164Z","shell.execute_reply.started":"2023-01-07T16:46:13.385139Z","shell.execute_reply":"2023-01-07T16:46:13.417807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:17.531714Z","iopub.execute_input":"2023-01-07T16:46:17.532221Z","iopub.status.idle":"2023-01-07T16:46:21.819086Z","shell.execute_reply.started":"2023-01-07T16:46:17.532183Z","shell.execute_reply":"2023-01-07T16:46:21.817613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:24.112406Z","iopub.execute_input":"2023-01-07T16:46:24.112931Z","iopub.status.idle":"2023-01-07T16:46:24.120798Z","shell.execute_reply.started":"2023-01-07T16:46:24.112891Z","shell.execute_reply":"2023-01-07T16:46:24.119243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-01-07T16:46:27.755146Z","iopub.execute_input":"2023-01-07T16:46:27.756346Z","iopub.status.idle":"2023-01-07T16:46:28.895329Z","shell.execute_reply.started":"2023-01-07T16:46:27.756271Z","shell.execute_reply":"2023-01-07T16:46:28.893897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:28.897550Z","iopub.execute_input":"2023-01-07T16:46:28.898008Z","iopub.status.idle":"2023-01-07T16:46:28.908426Z","shell.execute_reply.started":"2023-01-07T16:46:28.897968Z","shell.execute_reply":"2023-01-07T16:46:28.906806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:30.135611Z","iopub.execute_input":"2023-01-07T16:46:30.137098Z","iopub.status.idle":"2023-01-07T16:46:30.285816Z","shell.execute_reply.started":"2023-01-07T16:46:30.137031Z","shell.execute_reply":"2023-01-07T16:46:30.284300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:32.159962Z","iopub.execute_input":"2023-01-07T16:46:32.160456Z","iopub.status.idle":"2023-01-07T16:46:32.167029Z","shell.execute_reply.started":"2023-01-07T16:46:32.160418Z","shell.execute_reply":"2023-01-07T16:46:32.165451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:33.005373Z","iopub.execute_input":"2023-01-07T16:46:33.005876Z","iopub.status.idle":"2023-01-07T16:46:33.030619Z","shell.execute_reply.started":"2023-01-07T16:46:33.005840Z","shell.execute_reply":"2023-01-07T16:46:33.029092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:46:34.379474Z","iopub.execute_input":"2023-01-07T16:46:34.381164Z","iopub.status.idle":"2023-01-07T16:46:48.972250Z","shell.execute_reply.started":"2023-01-07T16:46:34.381101Z","shell.execute_reply":"2023-01-07T16:46:48.970853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:47:07.675754Z","iopub.execute_input":"2023-01-07T16:47:07.676241Z","iopub.status.idle":"2023-01-07T16:47:07.687313Z","shell.execute_reply.started":"2023-01-07T16:47:07.676206Z","shell.execute_reply":"2023-01-07T16:47:07.685819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:47:09.163726Z","iopub.execute_input":"2023-01-07T16:47:09.165423Z","iopub.status.idle":"2023-01-07T16:47:09.172169Z","shell.execute_reply.started":"2023-01-07T16:47:09.165338Z","shell.execute_reply":"2023-01-07T16:47:09.170484Z"},"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":"2023-01-07T16:47:11.106914Z","iopub.execute_input":"2023-01-07T16:47:11.107577Z","iopub.status.idle":"2023-01-07T16:47:11.817548Z","shell.execute_reply.started":"2023-01-07T16:47:11.107505Z","shell.execute_reply":"2023-01-07T16:47:11.816201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:47:12.724700Z","iopub.execute_input":"2023-01-07T16:47:12.725884Z","iopub.status.idle":"2023-01-07T16:47:12.731432Z","shell.execute_reply.started":"2023-01-07T16:47:12.725825Z","shell.execute_reply":"2023-01-07T16:47:12.730228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:00.291000Z","iopub.execute_input":"2023-01-07T16:48:00.291578Z","iopub.status.idle":"2023-01-07T16:48:09.063350Z","shell.execute_reply.started":"2023-01-07T16:48:00.291498Z","shell.execute_reply":"2023-01-07T16:48:09.061423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:12.199533Z","iopub.execute_input":"2023-01-07T16:48:12.200065Z","iopub.status.idle":"2023-01-07T16:48:12.207878Z","shell.execute_reply.started":"2023-01-07T16:48:12.200018Z","shell.execute_reply":"2023-01-07T16:48:12.206945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:12.732734Z","iopub.execute_input":"2023-01-07T16:48:12.733385Z","iopub.status.idle":"2023-01-07T16:48:12.740350Z","shell.execute_reply.started":"2023-01-07T16:48:12.733325Z","shell.execute_reply":"2023-01-07T16:48:12.738846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:13.290672Z","iopub.execute_input":"2023-01-07T16:48:13.291195Z","iopub.status.idle":"2023-01-07T16:48:13.317366Z","shell.execute_reply.started":"2023-01-07T16:48:13.291154Z","shell.execute_reply":"2023-01-07T16:48:13.315738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:14.006906Z","iopub.execute_input":"2023-01-07T16:48:14.007610Z","iopub.status.idle":"2023-01-07T16:48:23.328844Z","shell.execute_reply.started":"2023-01-07T16:48:14.007548Z","shell.execute_reply":"2023-01-07T16:48:23.327598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:23.330669Z","iopub.execute_input":"2023-01-07T16:48:23.331081Z","iopub.status.idle":"2023-01-07T16:48:23.340940Z","shell.execute_reply.started":"2023-01-07T16:48:23.331047Z","shell.execute_reply":"2023-01-07T16:48:23.339475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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))\n    ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:23.342857Z","iopub.execute_input":"2023-01-07T16:48:23.343498Z","iopub.status.idle":"2023-01-07T16:48:23.348652Z","shell.execute_reply.started":"2023-01-07T16:48:23.343457Z","shell.execute_reply":"2023-01-07T16:48:23.347485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:23.351053Z","iopub.execute_input":"2023-01-07T16:48:23.351431Z","iopub.status.idle":"2023-01-07T16:48:26.552033Z","shell.execute_reply.started":"2023-01-07T16:48:23.351398Z","shell.execute_reply":"2023-01-07T16:48:26.550169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:48:26.706772Z","iopub.execute_input":"2023-01-07T16:48:26.707334Z","iopub.status.idle":"2023-01-07T16:48:26.716416Z","shell.execute_reply.started":"2023-01-07T16:48:26.707288Z","shell.execute_reply":"2023-01-07T16:48:26.715026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:49:33.654086Z","iopub.execute_input":"2023-01-07T16:49:33.654646Z","iopub.status.idle":"2023-01-07T16:49:35.031459Z","shell.execute_reply.started":"2023-01-07T16:49:33.654608Z","shell.execute_reply":"2023-01-07T16:49:35.029650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-01-07T16:51:04.278443Z","iopub.execute_input":"2023-01-07T16:51:04.279004Z","iopub.status.idle":"2023-01-07T16:51:07.529906Z","shell.execute_reply.started":"2023-01-07T16:51:04.278964Z","shell.execute_reply":"2023-01-07T16:51:07.527866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:51:19.204072Z","iopub.execute_input":"2023-01-07T16:51:19.204596Z","iopub.status.idle":"2023-01-07T16:51:19.214923Z","shell.execute_reply.started":"2023-01-07T16:51:19.204555Z","shell.execute_reply":"2023-01-07T16:51:19.213047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-01-07T17:00:26.024564Z","iopub.execute_input":"2023-01-07T17:00:26.025130Z","iopub.status.idle":"2023-01-07T17:00:26.196235Z","shell.execute_reply.started":"2023-01-07T17:00:26.025092Z","shell.execute_reply":"2023-01-07T17:00:26.194608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:00:37.871919Z","iopub.execute_input":"2023-01-07T17:00:37.872393Z","iopub.status.idle":"2023-01-07T17:00:37.881769Z","shell.execute_reply.started":"2023-01-07T17:00:37.872355Z","shell.execute_reply":"2023-01-07T17:00:37.880275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:01:03.027277Z","iopub.execute_input":"2023-01-07T17:01:03.027979Z","iopub.status.idle":"2023-01-07T17:01:03.037414Z","shell.execute_reply.started":"2023-01-07T17:01:03.027917Z","shell.execute_reply":"2023-01-07T17:01:03.035937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:01:27.219328Z","iopub.execute_input":"2023-01-07T17:01:27.220406Z","iopub.status.idle":"2023-01-07T17:01:27.228187Z","shell.execute_reply.started":"2023-01-07T17:01:27.220355Z","shell.execute_reply":"2023-01-07T17:01:27.226770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-01-07T17:01:33.695336Z","iopub.execute_input":"2023-01-07T17:01:33.695854Z","iopub.status.idle":"2023-01-07T17:01:42.638813Z","shell.execute_reply.started":"2023-01-07T17:01:33.695814Z","shell.execute_reply":"2023-01-07T17:01:42.636766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape\npca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:01:48.890403Z","iopub.execute_input":"2023-01-07T17:01:48.891727Z","iopub.status.idle":"2023-01-07T17:01:48.901725Z","shell.execute_reply.started":"2023-01-07T17:01:48.891670Z","shell.execute_reply":"2023-01-07T17:01:48.900280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:01:52.417868Z","iopub.execute_input":"2023-01-07T17:01:52.418360Z","iopub.status.idle":"2023-01-07T17:01:52.438241Z","shell.execute_reply.started":"2023-01-07T17:01:52.418318Z","shell.execute_reply":"2023-01-07T17:01:52.436676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T17:02:27.152267Z","iopub.execute_input":"2023-01-07T17:02:27.153190Z","iopub.status.idle":"2023-01-07T17:02:27.445578Z","shell.execute_reply.started":"2023-01-07T17:02:27.153140Z","shell.execute_reply":"2023-01-07T17:02:27.444255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}