{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-03T13:43:47.189168Z","iopub.execute_input":"2024-08-03T13:43:47.189638Z","iopub.status.idle":"2024-08-03T13:43:47.664293Z","shell.execute_reply.started":"2024-08-03T13:43:47.189605Z","shell.execute_reply":"2024-08-03T13:43:47.663134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:47.666994Z","iopub.execute_input":"2024-08-03T13:43:47.667591Z","iopub.status.idle":"2024-08-03T13:43:51.885111Z","shell.execute_reply.started":"2024-08-03T13:43:47.667551Z","shell.execute_reply":"2024-08-03T13:43:51.883962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:51.886640Z","iopub.execute_input":"2024-08-03T13:43:51.887083Z","iopub.status.idle":"2024-08-03T13:43:51.919201Z","shell.execute_reply.started":"2024-08-03T13:43:51.887045Z","shell.execute_reply":"2024-08-03T13:43:51.917974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:51.920893Z","iopub.execute_input":"2024-08-03T13:43:51.921289Z","iopub.status.idle":"2024-08-03T13:43:51.930575Z","shell.execute_reply.started":"2024-08-03T13:43:51.921258Z","shell.execute_reply":"2024-08-03T13:43:51.929291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:51.934855Z","iopub.execute_input":"2024-08-03T13:43:51.935256Z","iopub.status.idle":"2024-08-03T13:43:51.941947Z","shell.execute_reply.started":"2024-08-03T13:43:51.935224Z","shell.execute_reply":"2024-08-03T13:43:51.940794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:51.943251Z","iopub.execute_input":"2024-08-03T13:43:51.943607Z","iopub.status.idle":"2024-08-03T13:43:51.972705Z","shell.execute_reply.started":"2024-08-03T13:43:51.943579Z","shell.execute_reply":"2024-08-03T13:43:51.971327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[7018,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:51.974313Z","iopub.execute_input":"2024-08-03T13:43:51.974659Z","iopub.status.idle":"2024-08-03T13:43:52.288949Z","shell.execute_reply.started":"2024-08-03T13:43:51.974628Z","shell.execute_reply":"2024-08-03T13:43:52.287800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:44:40.426610Z","iopub.execute_input":"2024-08-03T13:44:40.427476Z","iopub.status.idle":"2024-08-03T13:44:40.433034Z","shell.execute_reply.started":"2024-08-03T13:44:40.427435Z","shell.execute_reply":"2024-08-03T13:44:40.431720Z"},"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":"2024-08-03T13:44:54.530514Z","iopub.execute_input":"2024-08-03T13:44:54.530938Z","iopub.status.idle":"2024-08-03T13:44:54.762975Z","shell.execute_reply.started":"2024-08-03T13:44:54.530905Z","shell.execute_reply":"2024-08-03T13:44:54.761751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:53.273075Z","iopub.execute_input":"2024-08-03T13:43:53.273560Z","iopub.status.idle":"2024-08-03T13:43:53.300374Z","shell.execute_reply.started":"2024-08-03T13:43:53.273520Z","shell.execute_reply":"2024-08-03T13:43:53.299296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:53.302187Z","iopub.execute_input":"2024-08-03T13:43:53.302647Z","iopub.status.idle":"2024-08-03T13:43:53.328419Z","shell.execute_reply.started":"2024-08-03T13:43:53.302604Z","shell.execute_reply":"2024-08-03T13:43:53.327204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:53.329676Z","iopub.execute_input":"2024-08-03T13:43:53.330133Z","iopub.status.idle":"2024-08-03T13:43:53.489065Z","shell.execute_reply.started":"2024-08-03T13:43:53.330093Z","shell.execute_reply":"2024-08-03T13:43:53.487849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:53.490432Z","iopub.execute_input":"2024-08-03T13:43:53.490881Z","iopub.status.idle":"2024-08-03T13:43:53.496454Z","shell.execute_reply.started":"2024-08-03T13:43:53.490841Z","shell.execute_reply":"2024-08-03T13:43:53.495036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:43:53.503478Z","iopub.execute_input":"2024-08-03T13:43:53.503943Z","iopub.status.idle":"2024-08-03T13:43:53.664840Z","shell.execute_reply.started":"2024-08-03T13:43:53.503909Z","shell.execute_reply":"2024-08-03T13:43:53.663540Z"},"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":"2024-08-03T13:45:13.509754Z","iopub.execute_input":"2024-08-03T13:45:13.510196Z","iopub.status.idle":"2024-08-03T13:45:26.564378Z","shell.execute_reply.started":"2024-08-03T13:45:13.510162Z","shell.execute_reply":"2024-08-03T13:45:26.563117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_pred,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:45:33.820938Z","iopub.execute_input":"2024-08-03T13:45:33.821333Z","iopub.status.idle":"2024-08-03T13:45:33.832021Z","shell.execute_reply.started":"2024-08-03T13:45:33.821304Z","shell.execute_reply":"2024-08-03T13:45:33.830861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:45:45.702067Z","iopub.execute_input":"2024-08-03T13:45:45.702480Z","iopub.status.idle":"2024-08-03T13:45:45.707784Z","shell.execute_reply.started":"2024-08-03T13:45:45.702447Z","shell.execute_reply":"2024-08-03T13:45:45.706595Z"},"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-08-03T13:46:00.776727Z","iopub.execute_input":"2024-08-03T13:46:00.777192Z","iopub.status.idle":"2024-08-03T13:46:01.278395Z","shell.execute_reply.started":"2024-08-03T13:46:00.777160Z","shell.execute_reply":"2024-08-03T13:46:01.277109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:08.102039Z","iopub.execute_input":"2024-08-03T13:46:08.102448Z","iopub.status.idle":"2024-08-03T13:46:08.108365Z","shell.execute_reply.started":"2024-08-03T13:46:08.102416Z","shell.execute_reply":"2024-08-03T13:46:08.106813Z"},"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":"2024-08-03T13:46:13.593016Z","iopub.execute_input":"2024-08-03T13:46:13.593412Z","iopub.status.idle":"2024-08-03T13:46:18.557001Z","shell.execute_reply.started":"2024-08-03T13:46:13.593383Z","shell.execute_reply":"2024-08-03T13:46:18.555632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:25.466472Z","iopub.execute_input":"2024-08-03T13:46:25.467474Z","iopub.status.idle":"2024-08-03T13:46:25.474469Z","shell.execute_reply.started":"2024-08-03T13:46:25.467437Z","shell.execute_reply":"2024-08-03T13:46:25.473194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:32.133092Z","iopub.execute_input":"2024-08-03T13:46:32.133502Z","iopub.status.idle":"2024-08-03T13:46:32.138668Z","shell.execute_reply.started":"2024-08-03T13:46:32.133472Z","shell.execute_reply":"2024-08-03T13:46:32.137568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:35.800095Z","iopub.execute_input":"2024-08-03T13:46:35.800521Z","iopub.status.idle":"2024-08-03T13:46:35.836822Z","shell.execute_reply.started":"2024-08-03T13:46:35.800489Z","shell.execute_reply":"2024-08-03T13:46:35.835570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:39.605362Z","iopub.execute_input":"2024-08-03T13:46:39.606516Z","iopub.status.idle":"2024-08-03T13:46:42.400993Z","shell.execute_reply.started":"2024-08-03T13:46:39.606478Z","shell.execute_reply":"2024-08-03T13:46:42.399819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:48.090642Z","iopub.execute_input":"2024-08-03T13:46:48.091077Z","iopub.status.idle":"2024-08-03T13:46:48.100894Z","shell.execute_reply.started":"2024-08-03T13:46:48.091043Z","shell.execute_reply":"2024-08-03T13:46:48.099798Z"},"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    knn = KNeighborsClassifier()\n    knn.fit(X_train_trf,y_train)\n    y_pred = knn.predict(X_test_trf)\n    print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:44:00.352974Z","iopub.status.idle":"2024-08-03T13:44:00.353497Z","shell.execute_reply.started":"2024-08-03T13:44:00.353227Z","shell.execute_reply":"2024-08-03T13:44:00.353250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming 2d cordinate system\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:53.329361Z","iopub.execute_input":"2024-08-03T13:46:53.329812Z","iopub.status.idle":"2024-08-03T13:46:54.321203Z","shell.execute_reply.started":"2024-08-03T13:46:53.329760Z","shell.execute_reply":"2024-08-03T13:46:54.319619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:58.554195Z","iopub.execute_input":"2024-08-03T13:46:58.554587Z","iopub.status.idle":"2024-08-03T13:46:58.562325Z","shell.execute_reply.started":"2024-08-03T13:46:58.554560Z","shell.execute_reply":"2024-08-03T13:46:58.561043Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:47:32.785194Z","iopub.execute_input":"2024-08-03T13:47:32.785582Z","iopub.status.idle":"2024-08-03T13:47:32.945009Z","shell.execute_reply.started":"2024-08-03T13:47:32.785553Z","shell.execute_reply":"2024-08-03T13:47:32.943755Z"},"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":"2024-08-03T13:47:45.600652Z","iopub.execute_input":"2024-08-03T13:47:45.601092Z","iopub.status.idle":"2024-08-03T13:47:46.684887Z","shell.execute_reply.started":"2024-08-03T13:47:45.601060Z","shell.execute_reply":"2024-08-03T13:47:46.683280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:47:52.045042Z","iopub.execute_input":"2024-08-03T13:47:52.045454Z","iopub.status.idle":"2024-08-03T13:47:52.053314Z","shell.execute_reply.started":"2024-08-03T13:47:52.045422Z","shell.execute_reply":"2024-08-03T13:47:52.052220Z"},"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],color=y_train_trf)\n\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":"2024-08-03T13:48:32.261054Z","iopub.execute_input":"2024-08-03T13:48:32.261491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape\n# Eigen values","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:48:45.953953Z","iopub.execute_input":"2024-08-03T13:48:45.954362Z","iopub.status.idle":"2024-08-03T13:48:45.961483Z","shell.execute_reply.started":"2024-08-03T13:48:45.954330Z","shell.execute_reply":"2024-08-03T13:48:45.960271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:48:50.151364Z","iopub.execute_input":"2024-08-03T13:48:50.151810Z","iopub.status.idle":"2024-08-03T13:48:50.159544Z","shell.execute_reply.started":"2024-08-03T13:48:50.151753Z","shell.execute_reply":"2024-08-03T13:48:50.158136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:48:53.040219Z","iopub.execute_input":"2024-08-03T13:48:53.040639Z","iopub.status.idle":"2024-08-03T13:48:53.048244Z","shell.execute_reply.started":"2024-08-03T13:48:53.040605Z","shell.execute_reply":"2024-08-03T13:48:53.046953Z"},"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":"2024-08-03T13:48:55.504228Z","iopub.execute_input":"2024-08-03T13:48:55.504660Z","iopub.status.idle":"2024-08-03T13:48:59.782191Z","shell.execute_reply.started":"2024-08-03T13:48:55.504627Z","shell.execute_reply":"2024-08-03T13:48:59.780511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:49:05.503475Z","iopub.execute_input":"2024-08-03T13:49:05.504007Z","iopub.status.idle":"2024-08-03T13:49:05.512623Z","shell.execute_reply.started":"2024-08-03T13:49:05.503953Z","shell.execute_reply":"2024-08-03T13:49:05.511226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:49:08.829611Z","iopub.execute_input":"2024-08-03T13:49:08.830322Z","iopub.status.idle":"2024-08-03T13:49:08.837519Z","shell.execute_reply.started":"2024-08-03T13:49:08.830288Z","shell.execute_reply":"2024-08-03T13:49:08.836086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:49:12.124191Z","iopub.execute_input":"2024-08-03T13:49:12.125132Z","iopub.status.idle":"2024-08-03T13:49:12.143474Z","shell.execute_reply.started":"2024-08-03T13:49:12.125090Z","shell.execute_reply":"2024-08-03T13:49:12.142219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:49:18.964146Z","iopub.execute_input":"2024-08-03T13:49:18.964549Z","iopub.status.idle":"2024-08-03T13:49:19.254814Z","shell.execute_reply.started":"2024-08-03T13:49:18.964521Z","shell.execute_reply":"2024-08-03T13:49:19.253517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}