{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31012,"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)\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\n\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-04-13T09:30:42.713374Z","iopub.execute_input":"2025-04-13T09:30:42.713666Z","iopub.status.idle":"2025-04-13T09:30:42.720764Z","shell.execute_reply.started":"2025-04-13T09:30:42.713647Z","shell.execute_reply":"2025-04-13T09:30:42.719901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nx=train.drop(\"label\",axis=1)\ny=train[\"label\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:31:56.102362Z","iopub.execute_input":"2025-04-13T09:31:56.102673Z","iopub.status.idle":"2025-04-13T09:31:59.950012Z","shell.execute_reply.started":"2025-04-13T09:31:56.102651Z","shell.execute_reply":"2025-04-13T09:31:59.949098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_valid,y_train,y_valid =  train_test_split(x,y,test_size=0.2,random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:33:52.313123Z","iopub.execute_input":"2025-04-13T09:33:52.313717Z","iopub.status.idle":"2025-04-13T09:33:52.527547Z","shell.execute_reply.started":"2025-04-13T09:33:52.313691Z","shell.execute_reply":"2025-04-13T09:33:52.526731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:34:09.863823Z","iopub.execute_input":"2025-04-13T09:34:09.864387Z","iopub.status.idle":"2025-04-13T09:34:09.893003Z","shell.execute_reply.started":"2025-04-13T09:34:09.864361Z","shell.execute_reply":"2025-04-13T09:34:09.892297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:34:22.768493Z","iopub.execute_input":"2025-04-13T09:34:22.768790Z","iopub.status.idle":"2025-04-13T09:34:22.776001Z","shell.execute_reply.started":"2025-04-13T09:34:22.768770Z","shell.execute_reply":"2025-04-13T09:34:22.775031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=100,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:35:22.253719Z","iopub.execute_input":"2025-04-13T09:35:22.254305Z","iopub.status.idle":"2025-04-13T09:35:22.258133Z","shell.execute_reply.started":"2025-04-13T09:35:22.254281Z","shell.execute_reply":"2025-04-13T09:35:22.257332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:35:40.074268Z","iopub.execute_input":"2025-04-13T09:35:40.074604Z","iopub.status.idle":"2025-04-13T09:36:02.869918Z","shell.execute_reply.started":"2025-04-13T09:35:40.074575Z","shell.execute_reply":"2025-04-13T09:36:02.869212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = model.predict(X_valid)\npreds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:36:42.632768Z","iopub.execute_input":"2025-04-13T09:36:42.633391Z","iopub.status.idle":"2025-04-13T09:36:42.935602Z","shell.execute_reply.started":"2025-04-13T09:36:42.633366Z","shell.execute_reply":"2025-04-13T09:36:42.934922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_valid,preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:37:09.063530Z","iopub.execute_input":"2025-04-13T09:37:09.063800Z","iopub.status.idle":"2025-04-13T09:37:09.070643Z","shell.execute_reply.started":"2025-04-13T09:37:09.063783Z","shell.execute_reply":"2025-04-13T09:37:09.069894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = model.predict(test)\ntest_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:38:05.579120Z","iopub.execute_input":"2025-04-13T09:38:05.579920Z","iopub.status.idle":"2025-04-13T09:38:06.675489Z","shell.execute_reply.started":"2025-04-13T09:38:05.579888Z","shell.execute_reply":"2025-04-13T09:38:06.674790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission =pd.DataFrame({\n    \"ImageId\":range(1,len(test_preds)+1),\n    \"Label\": test_preds\n})\nsubmission.to_csv(\"submission.csv\",index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:40:03.472534Z","iopub.execute_input":"2025-04-13T09:40:03.472795Z","iopub.status.idle":"2025-04-13T09:40:03.504128Z","shell.execute_reply.started":"2025-04-13T09:40:03.472778Z","shell.execute_reply":"2025-04-13T09:40:03.503417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-13T09:40:19.457618Z","iopub.execute_input":"2025-04-13T09:40:19.458429Z","iopub.status.idle":"2025-04-13T09:40:19.466868Z","shell.execute_reply.started":"2025-04-13T09:40:19.458403Z","shell.execute_reply":"2025-04-13T09:40:19.465907Z"}},"outputs":[],"execution_count":null}]}