{"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":63087,"databundleVersionId":6874572,"sourceType":"competition"}],"dockerImageVersionId":30684,"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_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:42:44.924380Z","iopub.execute_input":"2024-04-10T03:42:44.924735Z","iopub.status.idle":"2024-04-10T03:42:47.388691Z","shell.execute_reply.started":"2024-04-10T03:42:44.924709Z","shell.execute_reply":"2024-04-10T03:42:47.387505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/copy-of-digit-recognizer-testing/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:43:52.539955Z","iopub.execute_input":"2024-04-10T03:43:52.540556Z","iopub.status.idle":"2024-04-10T03:43:56.565549Z","shell.execute_reply.started":"2024-04-10T03:43:52.540522Z","shell.execute_reply":"2024-04-10T03:43:56.564652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate features (pixels) and labels\nX = train_data.drop(\"label\", axis=1)\ny = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:44:15.620626Z","iopub.execute_input":"2024-04-10T03:44:15.620990Z","iopub.status.idle":"2024-04-10T03:44:15.732431Z","shell.execute_reply.started":"2024-04-10T03:44:15.620964Z","shell.execute_reply":"2024-04-10T03:44:15.731296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:44:18.378514Z","iopub.execute_input":"2024-04-10T03:44:18.379425Z","iopub.status.idle":"2024-04-10T03:44:18.633014Z","shell.execute_reply.started":"2024-04-10T03:44:18.379380Z","shell.execute_reply":"2024-04-10T03:44:18.631813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize and train the model\nmodel = RandomForestClassifier(random_state=42)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:44:29.459769Z","iopub.execute_input":"2024-04-10T03:44:29.460171Z","iopub.status.idle":"2024-04-10T03:44:55.622480Z","shell.execute_reply.started":"2024-04-10T03:44:29.460139Z","shell.execute_reply":"2024-04-10T03:44:55.621734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\ny_pred = model.predict(X_val)\naccuracy = accuracy_score(y_val, y_pred)\nprint(\"Validation Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:45:25.507906Z","iopub.execute_input":"2024-04-10T03:45:25.508928Z","iopub.status.idle":"2024-04-10T03:45:25.847309Z","shell.execute_reply.started":"2024-04-10T03:45:25.508896Z","shell.execute_reply":"2024-04-10T03:45:25.845967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the test data\ntest_data = pd.read_csv(\"/kaggle/input/copy-of-digit-recognizer-testing/test.csv\")\n\n# Make predictions on the test data\ntest_predictions = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:45:47.580332Z","iopub.execute_input":"2024-04-10T03:45:47.580811Z","iopub.status.idle":"2024-04-10T03:45:51.073322Z","shell.execute_reply.started":"2024-04-10T03:45:47.580777Z","shell.execute_reply":"2024-04-10T03:45:51.071922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare submission file\nsubmission_df = pd.DataFrame({\n    \"ImageId\": range(1, len(test_predictions) + 1),\n    \"Label\": test_predictions\n})\n\n# Save submission file\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T03:46:03.962820Z","iopub.execute_input":"2024-04-10T03:46:03.963190Z","iopub.status.idle":"2024-04-10T03:46:04.008975Z","shell.execute_reply.started":"2024-04-10T03:46:03.963161Z","shell.execute_reply":"2024-04-10T03:46:04.007736Z"},"trusted":true},"execution_count":null,"outputs":[]}]}