{"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":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div style=\"border-radius: 0px 40px; border: 2px solid #ffea00; padding: 10px; background-color: #6082B6; text-align: center; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">Digit Recognizer using PCA</h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"# MNIST Digit Recognizer using principal component analysis(PCA)\n![MNIST Dataset](https://miro.medium.com/max/1400/1*26W2Yk3cu2uz_R8BuSb_SA.png)\n\n\n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius: 0 10px; border: 2px solid #ffea00; padding: 5px; background-color: #6082B6; text-align: start; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">Attribute\tDetails</h1>\n</div>\n\n**Hi Everyone! Welcome to my notebook.**\n\n<div style=\"text-align: left;\">\n    <table>\n        <tr>\n            <th><b>Website</b></th>\n            <th><b>Links</b></th>\n        </tr>\n        <tr>\n        </tr>\n        <tr>\n            <td>GitHub</td>\n            <td><a href=\"https://github.com/abubakarsaddique13?tab=repositories\"><img src=\"https://img.shields.io/badge/GitHub-Profile-blue?style=for-the-badge&logo=github\" alt=\"GitHub\"/></a></td>\n        </tr>\n        <tr>\n            <td>LinkedIn</td>\n            <td><a href=\"https://www.linkedin.com/in/abubakar-saddique-5060a931a/\"><img src=\"https://img.shields.io/badge/LinkedIn-Profile-blue?style=for-the-badge&logo=linkedin\" alt=\"LinkedIn\"/></a></td>\n        </tr>\n        <tr>\n        </tr>\n        <tr>\n            <td>kaggle</td>\n            <td><a href=\"https://www.kaggle.com/abubakarabid\"><img src=\"https://img.shields.io/badge/kaggle-Profile-blue?style=for-the-badge&logo=kaggle\" alt=\"kaggle\"/></a></td>\n        </tr>\n    </table>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius: 0px 40px; border: 2px solid #ffea00; padding: 10px; background-color: #6082B6; text-align: center; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">Import Libaries</h1>\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt \nimport seaborn as sns \nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-25T09:26:18.311448Z","iopub.execute_input":"2024-07-25T09:26:18.311854Z","iopub.status.idle":"2024-07-25T09:26:18.323811Z","shell.execute_reply.started":"2024-07-25T09:26:18.311822Z","shell.execute_reply":"2024-07-25T09:26:18.322653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius: 0px 40px; border: 2px solid #ffea00; padding: 10px; background-color: #6082B6; text-align: center; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">Read Dataset</h1>\n</div>","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:13:04.909344Z","iopub.execute_input":"2024-07-25T09:13:04.909750Z","iopub.status.idle":"2024-07-25T09:13:08.137572Z","shell.execute_reply.started":"2024-07-25T09:13:04.909721Z","shell.execute_reply":"2024-07-25T09:13:08.136402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:10:21.776829Z","iopub.execute_input":"2024-07-25T09:10:21.777190Z","iopub.status.idle":"2024-07-25T09:10:21.800877Z","shell.execute_reply.started":"2024-07-25T09:10:21.777159Z","shell.execute_reply":"2024-07-25T09:10:21.799760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:10:35.659288Z","iopub.execute_input":"2024-07-25T09:10:35.659808Z","iopub.status.idle":"2024-07-25T09:10:35.668024Z","shell.execute_reply.started":"2024-07-25T09:10:35.659767Z","shell.execute_reply":"2024-07-25T09:10:35.666728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[3,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:15:56.691296Z","iopub.execute_input":"2024-07-25T09:15:56.691718Z","iopub.status.idle":"2024-07-25T09:15:56.970838Z","shell.execute_reply.started":"2024-07-25T09:15:56.691687Z","shell.execute_reply":"2024-07-25T09:15:56.969723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data \nx=df.drop('label',axis=1)\ny=df['label']","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:17:23.841530Z","iopub.execute_input":"2024-07-25T09:17:23.841932Z","iopub.status.idle":"2024-07-25T09:17:23.953880Z","shell.execute_reply.started":"2024-07-25T09:17:23.841901Z","shell.execute_reply":"2024-07-25T09:17:23.952527Z"},"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-07-25T09:18:02.976266Z","iopub.execute_input":"2024-07-25T09:18:02.976730Z","iopub.status.idle":"2024-07-25T09:18:03.252340Z","shell.execute_reply.started":"2024-07-25T09:18:02.976694Z","shell.execute_reply":"2024-07-25T09:18:03.251253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:18:15.857268Z","iopub.execute_input":"2024-07-25T09:18:15.857706Z","iopub.status.idle":"2024-07-25T09:18:15.865101Z","shell.execute_reply.started":"2024-07-25T09:18:15.857675Z","shell.execute_reply":"2024-07-25T09:18:15.863801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler=StandardScaler()\nx_train=scaler.fit_transform(x_train)\nx_test=scaler.transform(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:21:44.540651Z","iopub.execute_input":"2024-07-25T09:21:44.541038Z","iopub.status.idle":"2024-07-25T09:21:45.095994Z","shell.execute_reply.started":"2024-07-25T09:21:44.541008Z","shell.execute_reply":"2024-07-25T09:21:45.094799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=100)\nx_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:23:40.736012Z","iopub.execute_input":"2024-07-25T09:23:40.736453Z","iopub.status.idle":"2024-07-25T09:23:44.209940Z","shell.execute_reply.started":"2024-07-25T09:23:40.736418Z","shell.execute_reply":"2024-07-25T09:23:44.208379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:23:46.517572Z","iopub.execute_input":"2024-07-25T09:23:46.517978Z","iopub.status.idle":"2024-07-25T09:23:46.524584Z","shell.execute_reply.started":"2024-07-25T09:23:46.517946Z","shell.execute_reply":"2024-07-25T09:23:46.523399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:24:03.175661Z","iopub.execute_input":"2024-07-25T09:24:03.176068Z","iopub.status.idle":"2024-07-25T09:24:03.181578Z","shell.execute_reply.started":"2024-07-25T09:24:03.176036Z","shell.execute_reply":"2024-07-25T09:24:03.180327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:24:05.354686Z","iopub.execute_input":"2024-07-25T09:24:05.355123Z","iopub.status.idle":"2024-07-25T09:24:05.380488Z","shell.execute_reply.started":"2024-07-25T09:24:05.355089Z","shell.execute_reply":"2024-07-25T09:24:05.379381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=knn.predict(x_test_trf)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:24:25.184259Z","iopub.execute_input":"2024-07-25T09:24:25.184701Z","iopub.status.idle":"2024-07-25T09:24:27.073921Z","shell.execute_reply.started":"2024-07-25T09:24:25.184670Z","shell.execute_reply":"2024-07-25T09:24:27.072563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('accuracy_score : ',accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:26:21.590451Z","iopub.execute_input":"2024-07-25T09:26:21.590931Z","iopub.status.idle":"2024-07-25T09:26:21.599823Z","shell.execute_reply.started":"2024-07-25T09:26:21.590895Z","shell.execute_reply":"2024-07-25T09:26:21.598515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius: 0px 40px; border: 2px solid #ffea00; padding: 10px; background-color: #6082B6; text-align: center; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">3D visualization</h1>\n</div>","metadata":{}},{"cell_type":"code","source":"pca=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-07-25T09:29:56.234832Z","iopub.execute_input":"2024-07-25T09:29:56.235257Z","iopub.status.idle":"2024-07-25T09:29:57.349333Z","shell.execute_reply.started":"2024-07-25T09:29:56.235226Z","shell.execute_reply":"2024-07-25T09:29:57.347811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:30:04.131280Z","iopub.execute_input":"2024-07-25T09:30:04.131702Z","iopub.status.idle":"2024-07-25T09:30:04.140393Z","shell.execute_reply.started":"2024-07-25T09:30:04.131672Z","shell.execute_reply":"2024-07-25T09:30:04.139127Z"},"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(margin=dict(l=20, r=20, b=20, t=20))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:32:42.205495Z","iopub.execute_input":"2024-07-25T09:32:42.205934Z","iopub.status.idle":"2024-07-25T09:32:42.392606Z","shell.execute_reply.started":"2024-07-25T09:32:42.205891Z","shell.execute_reply":"2024-07-25T09:32:42.391345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius: 0 40px; border: 2px solid #ffea00; padding: 10px; background-color: #6082B6; text-align: center; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\">\n    <h1 style=\"color: #ffff00; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); font-weight: bold; margin-bottom: 5px; font-size: 24px;\">Eigen value & Eigen vector</h1>\n</div>","metadata":{}},{"cell_type":"code","source":"# eigen vector\npca.components_","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:32:58.374735Z","iopub.execute_input":"2024-07-25T09:32:58.375140Z","iopub.status.idle":"2024-07-25T09:32:58.385837Z","shell.execute_reply.started":"2024-07-25T09:32:58.375109Z","shell.execute_reply":"2024-07-25T09:32:58.384538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eigen value \npca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-07-25T09:33:09.803743Z","iopub.execute_input":"2024-07-25T09:33:09.804139Z","iopub.status.idle":"2024-07-25T09:33:09.814426Z","shell.execute_reply.started":"2024-07-25T09:33:09.804109Z","shell.execute_reply":"2024-07-25T09:33:09.813209Z"},"trusted":true},"execution_count":null,"outputs":[]}]}