{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31328,"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":"2026-05-06T22:55:56.194705Z","iopub.execute_input":"2026-05-06T22:55:56.195040Z","iopub.status.idle":"2026-05-06T22:55:57.676712Z","shell.execute_reply.started":"2026-05-06T22:55:56.195001Z","shell.execute_reply":"2026-05-06T22:55:57.675560Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T22:57:25.506894Z","iopub.execute_input":"2026-05-06T22:57:25.507785Z","iopub.status.idle":"2026-05-06T22:57:26.968447Z","shell.execute_reply.started":"2026-05-06T22:57:25.507745Z","shell.execute_reply":"2026-05-06T22:57:26.967462Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load the datasets","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T22:59:39.545028Z","iopub.execute_input":"2026-05-06T22:59:39.546006Z","iopub.status.idle":"2026-05-06T22:59:43.175072Z","shell.execute_reply.started":"2026-05-06T22:59:39.545963Z","shell.execute_reply":"2026-05-06T22:59:43.174150Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Inspecting the dataset","metadata":{}},{"cell_type":"code","source":"## Checking first few rows\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:00:54.324505Z","iopub.execute_input":"2026-05-06T23:00:54.325847Z","iopub.status.idle":"2026-05-06T23:00:54.373085Z","shell.execute_reply.started":"2026-05-06T23:00:54.325758Z","shell.execute_reply":"2026-05-06T23:00:54.371805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Checking for dimensions\nprint(train.shape) # It has labels\nprint(test.shape) # It doesn't have labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:01:30.897516Z","iopub.execute_input":"2026-05-06T23:01:30.897835Z","iopub.status.idle":"2026-05-06T23:01:30.904044Z","shell.execute_reply.started":"2026-05-06T23:01:30.897808Z","shell.execute_reply":"2026-05-06T23:01:30.902876Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Separate Features and Labels","metadata":{}},{"cell_type":"code","source":"X = train.drop(\"label\", axis=1)\ny = train[\"label\"]\n\n## Here, x is the pixel data and y is the actual digit","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:04:24.886100Z","iopub.execute_input":"2026-05-06T23:04:24.886498Z","iopub.status.idle":"2026-05-06T23:04:25.005224Z","shell.execute_reply.started":"2026-05-06T23:04:24.886454Z","shell.execute_reply":"2026-05-06T23:04:25.003871Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Split Training and validation data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X,y,test_size = 0.2, random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:05:29.693253Z","iopub.execute_input":"2026-05-06T23:05:29.693602Z","iopub.status.idle":"2026-05-06T23:05:29.919260Z","shell.execute_reply.started":"2026-05-06T23:05:29.693573Z","shell.execute_reply":"2026-05-06T23:05:29.918316Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training the first model --> ****Random Forest****","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:06:24.715314Z","iopub.execute_input":"2026-05-06T23:06:24.715663Z","iopub.status.idle":"2026-05-06T23:06:24.721430Z","shell.execute_reply.started":"2026-05-06T23:06:24.715631Z","shell.execute_reply":"2026-05-06T23:06:24.720230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestClassifier()\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:06:57.497941Z","iopub.execute_input":"2026-05-06T23:06:57.498309Z","iopub.status.idle":"2026-05-06T23:07:24.484208Z","shell.execute_reply.started":"2026-05-06T23:06:57.498277Z","shell.execute_reply":"2026-05-06T23:07:24.483143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Cheking the accuracy\naccuracy = model.score(X_val, y_val)\nprint(\"Accuracy:\", np.round(accuracy*100,2),\"%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:08:22.470922Z","iopub.execute_input":"2026-05-06T23:08:22.471311Z","iopub.status.idle":"2026-05-06T23:08:22.829787Z","shell.execute_reply.started":"2026-05-06T23:08:22.471273Z","shell.execute_reply":"2026-05-06T23:08:22.828851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Predict test dataset\npredictions = model.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:08:56.547019Z","iopub.execute_input":"2026-05-06T23:08:56.547396Z","iopub.status.idle":"2026-05-06T23:08:57.519576Z","shell.execute_reply.started":"2026-05-06T23:08:56.547363Z","shell.execute_reply":"2026-05-06T23:08:57.518557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Create submission file\nsubmission = pd.DataFrame(\n    {\n        \"ImageId\": range(1,len(predictions)+1),\n        \"Label\": predictions\n    }\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:09:58.012156Z","iopub.execute_input":"2026-05-06T23:09:58.012620Z","iopub.status.idle":"2026-05-06T23:09:58.018460Z","shell.execute_reply.started":"2026-05-06T23:09:58.012588Z","shell.execute_reply":"2026-05-06T23:09:58.017399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Save as CSV file\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-06T23:10:29.154122Z","iopub.execute_input":"2026-05-06T23:10:29.154540Z","iopub.status.idle":"2026-05-06T23:10:29.188299Z","shell.execute_reply.started":"2026-05-06T23:10:29.154491Z","shell.execute_reply":"2026-05-06T23:10:29.187407Z"}},"outputs":[],"execution_count":null}]}