{"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"},{"sourceId":9212956,"sourceType":"datasetVersion","datasetId":5570851}],"dockerImageVersionId":30746,"isInternetEnabled":true,"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-20T18:10:07.970848Z","iopub.execute_input":"2024-08-20T18:10:07.971521Z","iopub.status.idle":"2024-08-20T18:10:09.279545Z","shell.execute_reply.started":"2024-08-20T18:10:07.971474Z","shell.execute_reply":"2024-08-20T18:10:09.278191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the train and test datasets\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:14:54.751290Z","iopub.execute_input":"2024-08-20T18:14:54.752655Z","iopub.status.idle":"2024-08-20T18:15:01.754980Z","shell.execute_reply.started":"2024-08-20T18:14:54.752602Z","shell.execute_reply":"2024-08-20T18:15:01.753666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:15:17.300667Z","iopub.execute_input":"2024-08-20T18:15:17.301107Z","iopub.status.idle":"2024-08-20T18:15:17.330735Z","shell.execute_reply.started":"2024-08-20T18:15:17.301073Z","shell.execute_reply":"2024-08-20T18:15:17.329233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Select features (X) and target (y) for training\nX = train_data.drop(columns='label')\ny = train_data['label']\n","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:25:11.192335Z","iopub.execute_input":"2024-08-20T18:25:11.192896Z","iopub.status.idle":"2024-08-20T18:25:11.337753Z","shell.execute_reply.started":"2024-08-20T18:25:11.192859Z","shell.execute_reply":"2024-08-20T18:25:11.336490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:25:17.283407Z","iopub.execute_input":"2024-08-20T18:25:17.283886Z","iopub.status.idle":"2024-08-20T18:25:17.292559Z","shell.execute_reply.started":"2024-08-20T18:25:17.283853Z","shell.execute_reply":"2024-08-20T18:25:17.291010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:25:22.975823Z","iopub.execute_input":"2024-08-20T18:25:22.976310Z","iopub.status.idle":"2024-08-20T18:25:22.985038Z","shell.execute_reply.started":"2024-08-20T18:25:22.976275Z","shell.execute_reply":"2024-08-20T18:25:22.983423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nsome_digit = X.iloc[4]\nsome_digit_image = some_digit.values.reshape(28,28)\n\nplt.imshow(some_digit_image, cmap='binary')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:25:30.049653Z","iopub.execute_input":"2024-08-20T18:25:30.050747Z","iopub.status.idle":"2024-08-20T18:25:30.147412Z","shell.execute_reply.started":"2024-08-20T18:25:30.050701Z","shell.execute_reply":"2024-08-20T18:25:30.145449Z"},"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-20T18:25:48.630163Z","iopub.execute_input":"2024-08-20T18:25:48.630667Z","iopub.status.idle":"2024-08-20T18:25:48.976762Z","shell.execute_reply.started":"2024-08-20T18:25:48.630632Z","shell.execute_reply":"2024-08-20T18:25:48.975007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using RandomForestClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier(random_state=42)\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:27:52.727827Z","iopub.execute_input":"2024-08-20T18:27:52.729314Z","iopub.status.idle":"2024-08-20T18:28:24.783471Z","shell.execute_reply.started":"2024-08-20T18:27:52.729260Z","shell.execute_reply":"2024-08-20T18:28:24.781885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:28:24.785704Z","iopub.execute_input":"2024-08-20T18:28:24.786220Z","iopub.status.idle":"2024-08-20T18:28:25.326213Z","shell.execute_reply.started":"2024-08-20T18:28:24.786179Z","shell.execute_reply":"2024-08-20T18:28:25.324688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix\n#accuracy \nprint(\"Accuracy of this model is:\",accuracy_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:29:29.156922Z","iopub.execute_input":"2024-08-20T18:29:29.157384Z","iopub.status.idle":"2024-08-20T18:29:29.169468Z","shell.execute_reply.started":"2024-08-20T18:29:29.157350Z","shell.execute_reply":"2024-08-20T18:29:29.167990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ncm = confusion_matrix(y_test, y_pred)\nplt.matshow(cm)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:30:09.842442Z","iopub.execute_input":"2024-08-20T18:30:09.842859Z","iopub.status.idle":"2024-08-20T18:30:10.231548Z","shell.execute_reply.started":"2024-08-20T18:30:09.842828Z","shell.execute_reply":"2024-08-20T18:30:10.229444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit = X.iloc[222]\ndigit_image = digit.values.reshape(28,28)\n\nplt.imshow(digit_image, cmap='binary')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:31:16.254165Z","iopub.execute_input":"2024-08-20T18:31:16.254679Z","iopub.status.idle":"2024-08-20T18:31:16.344462Z","shell.execute_reply.started":"2024-08-20T18:31:16.254641Z","shell.execute_reply":"2024-08-20T18:31:16.343138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.predict([digit])","metadata":{"execution":{"iopub.status.busy":"2024-08-20T18:31:34.860756Z","iopub.execute_input":"2024-08-20T18:31:34.861244Z","iopub.status.idle":"2024-08-20T18:31:34.886526Z","shell.execute_reply.started":"2024-08-20T18:31:34.861210Z","shell.execute_reply":"2024-08-20T18:31:34.885112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}