{"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":30732,"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-06-09T05:54:15.839887Z","iopub.execute_input":"2024-06-09T05:54:15.840327Z","iopub.status.idle":"2024-06-09T05:54:15.850874Z","shell.execute_reply.started":"2024-06-09T05:54:15.840292Z","shell.execute_reply":"2024-06-09T05:54:15.849612Z"},"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-06-09T05:54:15.863680Z","iopub.execute_input":"2024-06-09T05:54:15.864117Z","iopub.status.idle":"2024-06-09T05:54:19.017057Z","shell.execute_reply.started":"2024-06-09T05:54:15.864086Z","shell.execute_reply":"2024-06-09T05:54:19.015853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.019306Z","iopub.execute_input":"2024-06-09T05:54:19.019697Z","iopub.status.idle":"2024-06-09T05:54:19.046411Z","shell.execute_reply.started":"2024-06-09T05:54:19.019664Z","shell.execute_reply":"2024-06-09T05:54:19.045322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape\n# means here there are 28(in one direction) * 28(in other direction) pixels means each column is for single pixel\n# and threre are 42000 images\n# and 785 because 1 column is label","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.047948Z","iopub.execute_input":"2024-06-09T05:54:19.048296Z","iopub.status.idle":"2024-06-09T05:54:19.055356Z","shell.execute_reply.started":"2024-06-09T05:54:19.048261Z","shell.execute_reply":"2024-06-09T05:54:19.054037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.058702Z","iopub.execute_input":"2024-06-09T05:54:19.059313Z","iopub.status.idle":"2024-06-09T05:54:19.066026Z","shell.execute_reply.started":"2024-06-09T05:54:19.059272Z","shell.execute_reply":"2024-06-09T05:54:19.064827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[18006,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.067490Z","iopub.execute_input":"2024-06-09T05:54:19.067919Z","iopub.status.idle":"2024-06-09T05:54:19.360270Z","shell.execute_reply.started":"2024-06-09T05:54:19.067890Z","shell.execute_reply":"2024-06-09T05:54:19.358922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.361840Z","iopub.execute_input":"2024-06-09T05:54:19.362207Z","iopub.status.idle":"2024-06-09T05:54:19.644889Z","shell.execute_reply.started":"2024-06-09T05:54:19.362173Z","shell.execute_reply":"2024-06-09T05:54:19.643393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.drop(columns=['label'])","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.646400Z","iopub.execute_input":"2024-06-09T05:54:19.646800Z","iopub.status.idle":"2024-06-09T05:54:19.919896Z","shell.execute_reply.started":"2024-06-09T05:54:19.646769Z","shell.execute_reply":"2024-06-09T05:54:19.918744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=df['label']","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.921278Z","iopub.execute_input":"2024-06-09T05:54:19.921646Z","iopub.status.idle":"2024-06-09T05:54:19.926667Z","shell.execute_reply.started":"2024-06-09T05:54:19.921611Z","shell.execute_reply":"2024-06-09T05:54:19.925635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:19.928087Z","iopub.execute_input":"2024-06-09T05:54:19.928427Z","iopub.status.idle":"2024-06-09T05:54:19.938183Z","shell.execute_reply.started":"2024-06-09T05:54:19.928398Z","shell.execute_reply":"2024-06-09T05:54:19.937048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_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-06-09T05:54:19.942383Z","iopub.execute_input":"2024-06-09T05:54:19.942783Z","iopub.status.idle":"2024-06-09T05:54:20.311174Z","shell.execute_reply.started":"2024-06-09T05:54:19.942749Z","shell.execute_reply":"2024-06-09T05:54:20.310047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.312708Z","iopub.execute_input":"2024-06-09T05:54:20.313118Z","iopub.status.idle":"2024-06-09T05:54:20.321331Z","shell.execute_reply.started":"2024-06-09T05:54:20.313080Z","shell.execute_reply":"2024-06-09T05:54:20.320166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.322839Z","iopub.execute_input":"2024-06-09T05:54:20.323187Z","iopub.status.idle":"2024-06-09T05:54:20.335894Z","shell.execute_reply.started":"2024-06-09T05:54:20.323157Z","shell.execute_reply":"2024-06-09T05:54:20.334470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.337502Z","iopub.execute_input":"2024-06-09T05:54:20.338050Z","iopub.status.idle":"2024-06-09T05:54:20.346382Z","shell.execute_reply.started":"2024-06-09T05:54:20.338014Z","shell.execute_reply":"2024-06-09T05:54:20.345216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.347859Z","iopub.execute_input":"2024-06-09T05:54:20.348198Z","iopub.status.idle":"2024-06-09T05:54:20.358835Z","shell.execute_reply.started":"2024-06-09T05:54:20.348169Z","shell.execute_reply":"2024-06-09T05:54:20.357749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.360161Z","iopub.execute_input":"2024-06-09T05:54:20.360554Z","iopub.status.idle":"2024-06-09T05:54:20.878620Z","shell.execute_reply.started":"2024-06-09T05:54:20.360522Z","shell.execute_reply":"2024-06-09T05:54:20.877492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=clf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:20.880213Z","iopub.execute_input":"2024-06-09T05:54:20.880677Z","iopub.status.idle":"2024-06-09T05:54:35.558240Z","shell.execute_reply.started":"2024-06-09T05:54:20.880638Z","shell.execute_reply":"2024-06-09T05:54:35.557035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:35.559650Z","iopub.execute_input":"2024-06-09T05:54:35.559994Z","iopub.status.idle":"2024-06-09T05:54:35.565511Z","shell.execute_reply.started":"2024-06-09T05:54:35.559965Z","shell.execute_reply":"2024-06-09T05:54:35.564076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:35.567150Z","iopub.execute_input":"2024-06-09T05:54:35.567500Z","iopub.status.idle":"2024-06-09T05:54:35.584631Z","shell.execute_reply.started":"2024-06-09T05:54:35.567471Z","shell.execute_reply":"2024-06-09T05:54:35.583413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****By_Using_PCA****","metadata":{}},{"cell_type":"markdown","source":"**Step-1 : Standardize Data(Mean_Centering)**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:35.586447Z","iopub.execute_input":"2024-06-09T05:54:35.586868Z","iopub.status.idle":"2024-06-09T05:54:35.592165Z","shell.execute_reply.started":"2024-06-09T05:54:35.586837Z","shell.execute_reply":"2024-06-09T05:54:35.591072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:35.593715Z","iopub.execute_input":"2024-06-09T05:54:35.594062Z","iopub.status.idle":"2024-06-09T05:54:35.605391Z","shell.execute_reply.started":"2024-06-09T05:54:35.594034Z","shell.execute_reply":"2024-06-09T05:54:35.604137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=ss.fit_transform(x_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:35.607027Z","iopub.execute_input":"2024-06-09T05:54:35.607882Z","iopub.status.idle":"2024-06-09T05:54:36.066474Z","shell.execute_reply.started":"2024-06-09T05:54:35.607824Z","shell.execute_reply":"2024-06-09T05:54:36.065347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=ss.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:36.068025Z","iopub.execute_input":"2024-06-09T05:54:36.068406Z","iopub.status.idle":"2024-06-09T05:54:36.117517Z","shell.execute_reply.started":"2024-06-09T05:54:36.068372Z","shell.execute_reply":"2024-06-09T05:54:36.116075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:36.119335Z","iopub.execute_input":"2024-06-09T05:54:36.119822Z","iopub.status.idle":"2024-06-09T05:54:36.127924Z","shell.execute_reply.started":"2024-06-09T05:54:36.119781Z","shell.execute_reply":"2024-06-09T05:54:36.126745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Step-2 : import PCA**","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:36.129413Z","iopub.execute_input":"2024-06-09T05:54:36.129793Z","iopub.status.idle":"2024-06-09T05:54:36.139138Z","shell.execute_reply.started":"2024-06-09T05:54:36.129761Z","shell.execute_reply":"2024-06-09T05:54:36.138000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# by passing n_components = None,I'll get the components same as No. of columns in the dataset\n# by passing as n_components, it will take top 100 means better 100 principal components\npca=PCA(n_components=2)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:36.140638Z","iopub.execute_input":"2024-06-09T05:54:36.141142Z","iopub.status.idle":"2024-06-09T05:54:36.152171Z","shell.execute_reply.started":"2024-06-09T05:54:36.141075Z","shell.execute_reply":"2024-06-09T05:54:36.150897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf=pca.fit_transform(x_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:36.153695Z","iopub.execute_input":"2024-06-09T05:54:36.154134Z","iopub.status.idle":"2024-06-09T05:54:37.180284Z","shell.execute_reply.started":"2024-06-09T05:54:36.154093Z","shell.execute_reply":"2024-06-09T05:54:37.179043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_trf=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.181754Z","iopub.execute_input":"2024-06-09T05:54:37.182086Z","iopub.status.idle":"2024-06-09T05:54:37.216442Z","shell.execute_reply.started":"2024-06-09T05:54:37.182058Z","shell.execute_reply":"2024-06-09T05:54:37.214618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.226226Z","iopub.execute_input":"2024-06-09T05:54:37.231373Z","iopub.status.idle":"2024-06-09T05:54:37.248990Z","shell.execute_reply.started":"2024-06-09T05:54:37.231295Z","shell.execute_reply":"2024-06-09T05:54:37.247123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.251246Z","iopub.execute_input":"2024-06-09T05:54:37.251961Z","iopub.status.idle":"2024-06-09T05:54:37.259258Z","shell.execute_reply.started":"2024-06-09T05:54:37.251917Z","shell.execute_reply":"2024-06-09T05:54:37.258048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.261615Z","iopub.execute_input":"2024-06-09T05:54:37.262391Z","iopub.status.idle":"2024-06-09T05:54:37.271361Z","shell.execute_reply.started":"2024-06-09T05:54:37.262350Z","shell.execute_reply":"2024-06-09T05:54:37.269993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.273597Z","iopub.execute_input":"2024-06-09T05:54:37.274362Z","iopub.status.idle":"2024-06-09T05:54:37.327391Z","shell.execute_reply.started":"2024-06-09T05:54:37.274322Z","shell.execute_reply":"2024-06-09T05:54:37.326292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1=clf.predict(x_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.329069Z","iopub.execute_input":"2024-06-09T05:54:37.329740Z","iopub.status.idle":"2024-06-09T05:54:37.860616Z","shell.execute_reply.started":"2024-06-09T05:54:37.329698Z","shell.execute_reply":"2024-06-09T05:54:37.859611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.862056Z","iopub.execute_input":"2024-06-09T05:54:37.862605Z","iopub.status.idle":"2024-06-09T05:54:37.872164Z","shell.execute_reply.started":"2024-06-09T05:54:37.862544Z","shell.execute_reply":"2024-06-09T05:54:37.871053Z"},"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   # clf=KNeighborsClassifier()\n    \n   # clf.fit(x_train_trf,y_train)\n    #y_pred1=clf.predict(x_test_trf)\n    #print(accuracy_score(y_test,y_pred1))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.873389Z","iopub.execute_input":"2024-06-09T05:54:37.873764Z","iopub.status.idle":"2024-06-09T05:54:37.881719Z","shell.execute_reply.started":"2024-06-09T05:54:37.873722Z","shell.execute_reply":"2024-06-09T05:54:37.880523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:54:37.883033Z","iopub.execute_input":"2024-06-09T05:54:37.883445Z","iopub.status.idle":"2024-06-09T05:54:37.896803Z","shell.execute_reply.started":"2024-06-09T05:54:37.883415Z","shell.execute_reply":"2024-06-09T05:54:37.895558Z"},"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-06-09T05:54:37.898278Z","iopub.execute_input":"2024-06-09T05:54:37.898742Z"},"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-06-09T05:55:57.369635Z","iopub.execute_input":"2024-06-09T05:55:57.370481Z","iopub.status.idle":"2024-06-09T05:55:57.528714Z","shell.execute_reply.started":"2024-06-09T05:55:57.370425Z","shell.execute_reply":"2024-06-09T05:55:57.527305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=3)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:57:38.053763Z","iopub.execute_input":"2024-06-09T05:57:38.054841Z","iopub.status.idle":"2024-06-09T05:57:38.059929Z","shell.execute_reply.started":"2024-06-09T05:57:38.054803Z","shell.execute_reply":"2024-06-09T05:57:38.058635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf=pca.fit_transform(x_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:58:07.043358Z","iopub.execute_input":"2024-06-09T05:58:07.043811Z","iopub.status.idle":"2024-06-09T05:58:08.690843Z","shell.execute_reply.started":"2024-06-09T05:58:07.043776Z","shell.execute_reply":"2024-06-09T05:58:08.689312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_trf=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:58:22.548988Z","iopub.execute_input":"2024-06-09T05:58:22.549483Z","iopub.status.idle":"2024-06-09T05:58:22.585237Z","shell.execute_reply.started":"2024-06-09T05:58:22.549449Z","shell.execute_reply":"2024-06-09T05:58:22.583540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:58:40.370024Z","iopub.execute_input":"2024-06-09T05:58:40.370473Z","iopub.status.idle":"2024-06-09T05:58:40.405455Z","shell.execute_reply.started":"2024-06-09T05:58:40.370438Z","shell.execute_reply":"2024-06-09T05:58:40.404334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1=clf.predict(x_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:58:56.767784Z","iopub.execute_input":"2024-06-09T05:58:56.768217Z","iopub.status.idle":"2024-06-09T05:58:57.327729Z","shell.execute_reply.started":"2024-06-09T05:58:56.768183Z","shell.execute_reply":"2024-06-09T05:58:57.326660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:59:19.248278Z","iopub.execute_input":"2024-06-09T05:59:19.248727Z","iopub.status.idle":"2024-06-09T05:59:19.259422Z","shell.execute_reply.started":"2024-06-09T05:59:19.248692Z","shell.execute_reply":"2024-06-09T05:59:19.258170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-06-09T05:59:32.253651Z","iopub.execute_input":"2024-06-09T05:59:32.254050Z","iopub.status.idle":"2024-06-09T05:59:32.262633Z","shell.execute_reply.started":"2024-06-09T05:59:32.254021Z","shell.execute_reply":"2024-06-09T05:59:32.261395Z"},"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(x=x_train_trf[:,0],\n              y=x_train_trf[:,1],\n              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)\n              \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:02:22.943516Z","iopub.execute_input":"2024-06-09T06:02:22.943999Z","iopub.status.idle":"2024-06-09T06:02:23.204937Z","shell.execute_reply.started":"2024-06-09T06:02:22.943968Z","shell.execute_reply":"2024-06-09T06:02:23.203056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# these are eignen_values - we chose top 3 ","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:15:27.919882Z","iopub.execute_input":"2024-06-09T06:15:27.920305Z","iopub.status.idle":"2024-06-09T06:15:27.928291Z","shell.execute_reply.started":"2024-06-09T06:15:27.920273Z","shell.execute_reply":"2024-06-09T06:15:27.927030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# there are three vecctors and there are 784 components in each vector because it is 3 vectors in 784 dimensions space or these are vectors of 784 dimensions","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:19:30.933055Z","iopub.execute_input":"2024-06-09T06:19:30.933787Z","iopub.status.idle":"2024-06-09T06:19:30.941912Z","shell.execute_reply.started":"2024-06-09T06:19:30.933746Z","shell.execute_reply":"2024-06-09T06:19:30.940341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eigen values tell that how much variance of the original data can be explained by a particular PC so, that's why they are called as explained_variance_\n# and we have to find how many percent of varience can be explained by each eigen vector \npca.explained_variance_ratio_\n# means these top 3 eigen_vectors explain 12% varience of original_data","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:52:34.479506Z","iopub.execute_input":"2024-06-09T06:52:34.480632Z","iopub.status.idle":"2024-06-09T06:52:34.488720Z","shell.execute_reply.started":"2024-06-09T06:52:34.480587Z","shell.execute_reply":"2024-06-09T06:52:34.487518Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:58:53.544444Z","iopub.execute_input":"2024-06-09T06:58:53.545267Z","iopub.status.idle":"2024-06-09T06:58:58.996102Z","shell.execute_reply.started":"2024-06-09T06:58:53.545231Z","shell.execute_reply":"2024-06-09T06:58:58.994503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(x_train_trf,y_train)\ny_pred1=clf.predict(x_test_trf)\naccuracy_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:59:01.884717Z","iopub.execute_input":"2024-06-09T06:59:01.885135Z","iopub.status.idle":"2024-06-09T06:59:10.429622Z","shell.execute_reply.started":"2024-06-09T06:59:01.885103Z","shell.execute_reply":"2024-06-09T06:59:10.428529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-06-09T06:59:52.688975Z","iopub.execute_input":"2024-06-09T06:59:52.689434Z","iopub.status.idle":"2024-06-09T06:59:52.696915Z","shell.execute_reply.started":"2024-06-09T06:59:52.689390Z","shell.execute_reply":"2024-06-09T06:59:52.695626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-06-09T07:01:10.989226Z","iopub.execute_input":"2024-06-09T07:01:10.989677Z","iopub.status.idle":"2024-06-09T07:01:11.010067Z","shell.execute_reply.started":"2024-06-09T07:01:10.989643Z","shell.execute_reply":"2024-06-09T07:01:11.008690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T07:00:27.182021Z","iopub.execute_input":"2024-06-09T07:00:27.182468Z","iopub.status.idle":"2024-06-09T07:00:27.190202Z","shell.execute_reply.started":"2024-06-09T07:00:27.182433Z","shell.execute_reply":"2024-06-09T07:00:27.188781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T07:01:49.450485Z","iopub.execute_input":"2024-06-09T07:01:49.451004Z","iopub.status.idle":"2024-06-09T07:01:49.472239Z","shell.execute_reply.started":"2024-06-09T07:01:49.450968Z","shell.execute_reply":"2024-06-09T07:01:49.470840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T07:02:49.444314Z","iopub.execute_input":"2024-06-09T07:02:49.444776Z","iopub.status.idle":"2024-06-09T07:02:49.736403Z","shell.execute_reply.started":"2024-06-09T07:02:49.444743Z","shell.execute_reply":"2024-06-09T07:02:49.735053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}