{"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":"markdown","source":"# **Importing Libraries**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport seaborn as sns \nimport tensorflow as tf \nfrom sklearn.decomposition import PCA\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score , classification_report , ConfusionMatrixDisplay\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-26T20:10:18.167107Z","iopub.execute_input":"2023-07-26T20:10:18.167653Z","iopub.status.idle":"2023-07-26T20:10:30.628482Z","shell.execute_reply.started":"2023-07-26T20:10:18.167607Z","shell.execute_reply":"2023-07-26T20:10:30.627331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:10:30.630496Z","iopub.execute_input":"2023-07-26T20:10:30.631606Z","iopub.status.idle":"2023-07-26T20:10:37.405945Z","shell.execute_reply.started":"2023-07-26T20:10:30.631568Z","shell.execute_reply":"2023-07-26T20:10:37.404688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-26T21:02:37.888964Z","iopub.execute_input":"2023-07-26T21:02:37.890471Z","iopub.status.idle":"2023-07-26T21:02:37.900147Z","shell.execute_reply.started":"2023-07-26T21:02:37.890408Z","shell.execute_reply":"2023-07-26T21:02:37.898644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:10:37.419100Z","iopub.execute_input":"2023-07-26T20:10:37.419479Z","iopub.status.idle":"2023-07-26T20:10:37.434066Z","shell.execute_reply.started":"2023-07-26T20:10:37.419441Z","shell.execute_reply":"2023-07-26T20:10:37.432916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('label' , axis = 1).values\ny = train['label'].values","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:28:23.530777Z","iopub.execute_input":"2023-07-26T20:28:23.531123Z","iopub.status.idle":"2023-07-26T20:28:24.009715Z","shell.execute_reply.started":"2023-07-26T20:28:23.531094Z","shell.execute_reply":"2023-07-26T20:28:24.008201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Spliting dataset**","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:23.478672Z","iopub.execute_input":"2023-07-26T20:56:23.479122Z","iopub.status.idle":"2023-07-26T20:56:24.315177Z","shell.execute_reply.started":"2023-07-26T20:56:23.479088Z","shell.execute_reply":"2023-07-26T20:56:24.313813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:24.317810Z","iopub.execute_input":"2023-07-26T20:56:24.318415Z","iopub.status.idle":"2023-07-26T20:56:24.326641Z","shell.execute_reply.started":"2023-07-26T20:56:24.318360Z","shell.execute_reply":"2023-07-26T20:56:24.325400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Dimensions Reduction**","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components=90 , random_state=42)\npca.fit(X_train)\n\nX_train = pca.transform(X_train)\nX_val = pca.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:24.328242Z","iopub.execute_input":"2023-07-26T20:56:24.329062Z","iopub.status.idle":"2023-07-26T20:56:27.589630Z","shell.execute_reply.started":"2023-07-26T20:56:24.329017Z","shell.execute_reply":"2023-07-26T20:56:27.587958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **KNN model building**","metadata":{}},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=6 , weights='distance' , p = 2 )\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:27.593056Z","iopub.execute_input":"2023-07-26T20:56:27.594085Z","iopub.status.idle":"2023-07-26T20:56:27.601284Z","shell.execute_reply.started":"2023-07-26T20:56:27.594021Z","shell.execute_reply":"2023-07-26T20:56:27.599837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train , y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:27.603553Z","iopub.execute_input":"2023-07-26T20:56:27.604477Z","iopub.status.idle":"2023-07-26T20:56:27.641083Z","shell.execute_reply.started":"2023-07-26T20:56:27.604416Z","shell.execute_reply":"2023-07-26T20:56:27.639583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = knn.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:27.643722Z","iopub.execute_input":"2023-07-26T20:56:27.644804Z","iopub.status.idle":"2023-07-26T20:56:29.520073Z","shell.execute_reply.started":"2023-07-26T20:56:27.644741Z","shell.execute_reply":"2023-07-26T20:56:29.519060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Evaluation model**","metadata":{}},{"cell_type":"code","source":"acc = accuracy_score(y_val , y_val_pred)\nacc","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:29.522546Z","iopub.execute_input":"2023-07-26T20:56:29.523538Z","iopub.status.idle":"2023-07-26T20:56:29.531819Z","shell.execute_reply.started":"2023-07-26T20:56:29.523500Z","shell.execute_reply":"2023-07-26T20:56:29.530570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val , y_val_pred ))","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:29.533134Z","iopub.execute_input":"2023-07-26T20:56:29.533701Z","iopub.status.idle":"2023-07-26T20:56:29.591404Z","shell.execute_reply.started":"2023-07-26T20:56:29.533660Z","shell.execute_reply":"2023-07-26T20:56:29.589991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConfusionMatrixDisplay.from_predictions(y_val , y_val_pred)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T20:56:29.592985Z","iopub.execute_input":"2023-07-26T20:56:29.593623Z","iopub.status.idle":"2023-07-26T20:56:30.155843Z","shell.execute_reply.started":"2023-07-26T20:56:29.593577Z","shell.execute_reply":"2023-07-26T20:56:30.154570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pca.transform(test)\ny_test_pred = knn.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T21:00:18.320703Z","iopub.execute_input":"2023-07-26T21:00:18.321140Z","iopub.status.idle":"2023-07-26T21:00:22.207210Z","shell.execute_reply.started":"2023-07-26T21:00:18.321106Z","shell.execute_reply":"2023-07-26T21:00:22.205911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(y_test_pred)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-26T21:04:42.788333Z","iopub.execute_input":"2023-07-26T21:04:42.788740Z","iopub.status.idle":"2023-07-26T21:04:42.794309Z","shell.execute_reply.started":"2023-07-26T21:04:42.788710Z","shell.execute_reply":"2023-07-26T21:04:42.793034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_root = \"/kaggle/working/\"\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-26T21:07:22.266403Z","iopub.execute_input":"2023-07-26T21:07:22.266863Z","iopub.status.idle":"2023-07-26T21:07:22.272331Z","shell.execute_reply.started":"2023-07-26T21:07:22.266829Z","shell.execute_reply":"2023-07-26T21:07:22.271061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"ImageId\": test_df.index + 1, \"Label\": y_test_pred})\nsubmission.to_csv(f\"{output_root}submission.csv\", index=False)\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T21:16:23.972787Z","iopub.execute_input":"2023-07-26T21:16:23.973269Z","iopub.status.idle":"2023-07-26T21:16:24.054341Z","shell.execute_reply.started":"2023-07-26T21:16:23.973203Z","shell.execute_reply":"2023-07-26T21:16:24.053278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}