{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":31089,"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","trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:31:44.421034Z","iopub.execute_input":"2025-08-31T14:31:44.421297Z","iopub.status.idle":"2025-08-31T14:31:45.909360Z","shell.execute_reply.started":"2025-08-31T14:31:44.421260Z","shell.execute_reply":"2025-08-31T14:31:45.908497Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:33:05.606671Z","iopub.execute_input":"2025-08-31T14:33:05.607563Z","iopub.status.idle":"2025-08-31T14:33:09.249205Z","shell.execute_reply.started":"2025-08-31T14:33:05.607526Z","shell.execute_reply":"2025-08-31T14:33:09.248274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:33:55.693587Z","iopub.execute_input":"2025-08-31T14:33:55.693954Z","iopub.status.idle":"2025-08-31T14:33:55.699472Z","shell.execute_reply.started":"2025-08-31T14:33:55.693929Z","shell.execute_reply":"2025-08-31T14:33:55.698276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:34:27.239882Z","iopub.execute_input":"2025-08-31T14:34:27.240220Z","iopub.status.idle":"2025-08-31T14:34:27.271613Z","shell.execute_reply.started":"2025-08-31T14:34:27.240196Z","shell.execute_reply":"2025-08-31T14:34:27.270782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.iloc[:, 1:]\ny = train.iloc[:, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:37:33.146706Z","iopub.execute_input":"2025-08-31T14:37:33.147092Z","iopub.status.idle":"2025-08-31T14:37:33.152744Z","shell.execute_reply.started":"2025-08-31T14:37:33.147067Z","shell.execute_reply":"2025-08-31T14:37:33.151741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef print_image(row, df):\n    val = df.iloc[row, :].values\n    val = val.reshape(28,28).astype('uint8')\n    plt.imshow(val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:41:33.248501Z","iopub.execute_input":"2025-08-31T14:41:33.248788Z","iopub.status.idle":"2025-08-31T14:41:33.254622Z","shell.execute_reply.started":"2025-08-31T14:41:33.248769Z","shell.execute_reply":"2025-08-31T14:41:33.253229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_image(5000, X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:42:27.059647Z","iopub.execute_input":"2025-08-31T14:42:27.059984Z","iopub.status.idle":"2025-08-31T14:42:27.228100Z","shell.execute_reply.started":"2025-08-31T14:42:27.059963Z","shell.execute_reply":"2025-08-31T14:42:27.227165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:45:16.151678Z","iopub.execute_input":"2025-08-31T14:45:16.152089Z","iopub.status.idle":"2025-08-31T14:45:17.084427Z","shell.execute_reply.started":"2025-08-31T14:45:16.152064Z","shell.execute_reply":"2025-08-31T14:45:17.083522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:45:50.245085Z","iopub.execute_input":"2025-08-31T14:45:50.246098Z","iopub.status.idle":"2025-08-31T14:45:50.251401Z","shell.execute_reply.started":"2025-08-31T14:45:50.246067Z","shell.execute_reply":"2025-08-31T14:45:50.250250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:47:27.230630Z","iopub.execute_input":"2025-08-31T14:47:27.230989Z","iopub.status.idle":"2025-08-31T14:47:27.786539Z","shell.execute_reply.started":"2025-08-31T14:47:27.230967Z","shell.execute_reply":"2025-08-31T14:47:27.785444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"forest = RandomForestClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:47:46.729516Z","iopub.execute_input":"2025-08-31T14:47:46.729862Z","iopub.status.idle":"2025-08-31T14:47:46.734532Z","shell.execute_reply.started":"2025-08-31T14:47:46.729836Z","shell.execute_reply":"2025-08-31T14:47:46.733586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"forest.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:49:13.583072Z","iopub.execute_input":"2025-08-31T14:49:13.583378Z","iopub.status.idle":"2025-08-31T14:49:40.317448Z","shell.execute_reply.started":"2025-08-31T14:49:13.583357Z","shell.execute_reply":"2025-08-31T14:49:40.316357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = forest.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:50:13.066455Z","iopub.execute_input":"2025-08-31T14:50:13.066780Z","iopub.status.idle":"2025-08-31T14:50:13.581270Z","shell.execute_reply.started":"2025-08-31T14:50:13.066757Z","shell.execute_reply":"2025-08-31T14:50:13.580187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:51:19.121737Z","iopub.execute_input":"2025-08-31T14:51:19.122169Z","iopub.status.idle":"2025-08-31T14:51:19.127065Z","shell.execute_reply.started":"2025-08-31T14:51:19.122141Z","shell.execute_reply":"2025-08-31T14:51:19.125930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Classification Report\")\n\nprint(classification_report(y_test, pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:51:46.936607Z","iopub.execute_input":"2025-08-31T14:51:46.936949Z","iopub.status.idle":"2025-08-31T14:51:46.967309Z","shell.execute_reply.started":"2025-08-31T14:51:46.936925Z","shell.execute_reply":"2025-08-31T14:51:46.966157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Confusion Matrix\")\nprint(confusion_matrix(y_test, pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T14:54:09.552941Z","iopub.execute_input":"2025-08-31T14:54:09.553294Z","iopub.status.idle":"2025-08-31T14:54:09.563356Z","shell.execute_reply.started":"2025-08-31T14:54:09.553272Z","shell.execute_reply":"2025-08-31T14:54:09.562455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perdiction = forest.predict(test)\n\nprediction = pd.Series(perdiction, name = \"Label\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T15:05:23.084687Z","iopub.execute_input":"2025-08-31T15:05:23.085443Z","iopub.status.idle":"2025-08-31T15:05:24.561175Z","shell.execute_reply.started":"2025-08-31T15:05:23.085404Z","shell.execute_reply":"2025-08-31T15:05:24.559800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001), name = \"ImageId\"), prediction], axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T15:08:38.657038Z","iopub.execute_input":"2025-08-31T15:08:38.658130Z","iopub.status.idle":"2025-08-31T15:08:38.664695Z","shell.execute_reply.started":"2025-08-31T15:08:38.658096Z","shell.execute_reply":"2025-08-31T15:08:38.663793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-31T15:13:18.423247Z","iopub.execute_input":"2025-08-31T15:13:18.423601Z","iopub.status.idle":"2025-08-31T15:13:18.455456Z","shell.execute_reply.started":"2025-08-31T15:13:18.423580Z","shell.execute_reply":"2025-08-31T15:13:18.454250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}