{"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":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-18T12:49:28.046437Z","iopub.execute_input":"2024-07-18T12:49:28.046857Z","iopub.status.idle":"2024-07-18T12:49:29.691020Z","shell.execute_reply.started":"2024-07-18T12:49:28.046822Z","shell.execute_reply":"2024-07-18T12:49:29.689525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T12:30:35.423509Z","iopub.execute_input":"2024-07-18T12:30:35.423915Z","iopub.status.idle":"2024-07-18T12:30:38.066549Z","shell.execute_reply.started":"2024-07-18T12:30:35.423876Z","shell.execute_reply":"2024-07-18T12:30:38.065494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T12:30:23.824247Z","iopub.execute_input":"2024-07-18T12:30:23.824662Z","iopub.status.idle":"2024-07-18T12:30:25.925908Z","shell.execute_reply.started":"2024-07-18T12:30:23.824628Z","shell.execute_reply":"2024-07-18T12:30:25.924549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum().unique() # verify if there is null values ","metadata":{"execution":{"iopub.status.busy":"2024-07-18T12:50:20.312234Z","iopub.execute_input":"2024-07-18T12:50:20.312650Z","iopub.status.idle":"2024-07-18T12:50:20.356507Z","shell.execute_reply.started":"2024-07-18T12:50:20.312614Z","shell.execute_reply":"2024-07-18T12:50:20.355333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_label(row):\n    \n    # give the row index \n    # the number appears to verify it with label\n    \n    pixels = train_df.loc[row].drop('label').values\n\n    pixels = pixels.reshape(28, 28)\n\n    plt.imshow(pixels, cmap='gray')\n    plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T12:45:37.890834Z","iopub.execute_input":"2024-07-18T12:45:37.891401Z","iopub.status.idle":"2024-07-18T12:45:37.900761Z","shell.execute_reply.started":"2024-07-18T12:45:37.891338Z","shell.execute_reply":"2024-07-18T12:45:37.899312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_label(3)\ntrain_df.label.loc[3]","metadata":{"execution":{"iopub.status.busy":"2024-07-18T12:56:24.588142Z","iopub.execute_input":"2024-07-18T12:56:24.588591Z","iopub.status.idle":"2024-07-18T12:56:24.664139Z","shell.execute_reply.started":"2024-07-18T12:56:24.588554Z","shell.execute_reply":"2024-07-18T12:56:24.662800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(\"label\", axis=1) / 255.0\ny = train_df[\"label\"]\n\ntest_df = test_df  / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-07-18T13:20:47.339921Z","iopub.execute_input":"2024-07-18T13:20:47.340557Z","iopub.status.idle":"2024-07-18T13:20:47.673593Z","shell.execute_reply.started":"2024-07-18T13:20:47.340519Z","shell.execute_reply":"2024-07-18T13:20:47.672172Z"},"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-18T13:25:18.806397Z","iopub.execute_input":"2024-07-18T13:25:18.806794Z","iopub.status.idle":"2024-07-18T13:25:19.047272Z","shell.execute_reply.started":"2024-07-18T13:25:18.806765Z","shell.execute_reply":"2024-07-18T13:25:19.046201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = RandomForestClassifier(n_estimators=100, random_state=42)\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T13:27:00.062867Z","iopub.execute_input":"2024-07-18T13:27:00.063492Z","iopub.status.idle":"2024-07-18T13:27:27.298220Z","shell.execute_reply.started":"2024-07-18T13:27:00.063453Z","shell.execute_reply":"2024-07-18T13:27:27.297020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test)\nacc = accuracy_score(y_test, y_pred)\nacc","metadata":{"execution":{"iopub.status.busy":"2024-07-18T13:28:23.289078Z","iopub.execute_input":"2024-07-18T13:28:23.289483Z","iopub.status.idle":"2024-07-18T13:28:23.745231Z","shell.execute_reply.started":"2024-07-18T13:28:23.289454Z","shell.execute_reply":"2024-07-18T13:28:23.743974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = clf.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T13:29:46.085821Z","iopub.execute_input":"2024-07-18T13:29:46.086261Z","iopub.status.idle":"2024-07-18T13:29:47.224994Z","shell.execute_reply.started":"2024-07-18T13:29:46.086227Z","shell.execute_reply":"2024-07-18T13:29:47.223967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\" : range(1, len(test_pred)+1),\n    \"Label\" : test_pred\n})\n\nsubmission.to_csv(\"submission_digit_rec.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T13:36:39.929946Z","iopub.execute_input":"2024-07-18T13:36:39.930382Z","iopub.status.idle":"2024-07-18T13:36:39.965480Z","shell.execute_reply.started":"2024-07-18T13:36:39.930348Z","shell.execute_reply":"2024-07-18T13:36:39.964182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}