{"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":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:05:05.728781Z","iopub.execute_input":"2025-11-10T19:05:05.729103Z","iopub.status.idle":"2025-11-10T19:05:07.270366Z","shell.execute_reply.started":"2025-11-10T19:05:05.729078Z","shell.execute_reply":"2025-11-10T19:05:07.269466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:07:02.534143Z","iopub.execute_input":"2025-11-10T19:07:02.534655Z","iopub.status.idle":"2025-11-10T19:07:06.165602Z","shell.execute_reply.started":"2025-11-10T19:07:02.534629Z","shell.execute_reply":"2025-11-10T19:07:06.164810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_data.drop('label', axis = 1).values\ny = train_data['label'].values\nprint(f\"Training data shape: {X.shape}\")\nprint(f\"Labels shape: {y.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:08:09.994215Z","iopub.execute_input":"2025-11-10T19:08:09.995727Z","iopub.status.idle":"2025-11-10T19:08:10.137434Z","shell.execute_reply.started":"2025-11-10T19:08:09.995683Z","shell.execute_reply":"2025-11-10T19:08:10.136227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X / 255.0 # Normalize pixel values\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size = 0.2, random_state = 42, stratify = y\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:09:49.722538Z","iopub.execute_input":"2025-11-10T19:09:49.726306Z","iopub.status.idle":"2025-11-10T19:09:50.305597Z","shell.execute_reply.started":"2025-11-10T19:09:49.726196Z","shell.execute_reply":"2025-11-10T19:09:50.304412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"\\nTraining set size: {X_train.shape[0]}\")\nprint(f\"Validation set size: {X_val.shape[0]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:10:02.789190Z","iopub.execute_input":"2025-11-10T19:10:02.789648Z","iopub.status.idle":"2025-11-10T19:10:02.796683Z","shell.execute_reply.started":"2025-11-10T19:10:02.789619Z","shell.execute_reply":"2025-11-10T19:10:02.795663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Random Forest Classifier\nrf_model = RandomForestClassifier(\n    n_estimators = 100,\n    max_depth = 20,\n    random_state = 42,\n    n_jobs = -1,\n    verbose = 1\n)\nrf_model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:11:24.074945Z","iopub.execute_input":"2025-11-10T19:11:24.075597Z","iopub.status.idle":"2025-11-10T19:11:33.057557Z","shell.execute_reply.started":"2025-11-10T19:11:24.075564Z","shell.execute_reply":"2025-11-10T19:11:33.056655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = rf_model.predict(X_val)\naccuracy = accuracy_score(y_val, y_pred)\nprint(f\"Validation Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:13:16.273714Z","iopub.execute_input":"2025-11-10T19:13:16.274061Z","iopub.status.idle":"2025-11-10T19:13:16.488813Z","shell.execute_reply.started":"2025-11-10T19:13:16.274037Z","shell.execute_reply":"2025-11-10T19:13:16.487740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = test_data.values / 255.0\nprint(f\"Test data shape: {X_test.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:14:26.022208Z","iopub.execute_input":"2025-11-10T19:14:26.022650Z","iopub.status.idle":"2025-11-10T19:14:27.641759Z","shell.execute_reply.started":"2025-11-10T19:14:26.022626Z","shell.execute_reply":"2025-11-10T19:14:27.640700Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = rf_model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:15:48.169798Z","iopub.execute_input":"2025-11-10T19:15:48.170112Z","iopub.status.idle":"2025-11-10T19:15:48.637721Z","shell.execute_reply.started":"2025-11-10T19:15:48.170089Z","shell.execute_reply":"2025-11-10T19:15:48.636648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': range(1, len(test_predictions) + 1),\n    'Label': test_predictions\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:16:17.908747Z","iopub.execute_input":"2025-11-10T19:16:17.909088Z","iopub.status.idle":"2025-11-10T19:16:17.915004Z","shell.execute_reply.started":"2025-11-10T19:16:17.909056Z","shell.execute_reply":"2025-11-10T19:16:17.913855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index = False)\nprint(f\"Total predictions: {len(test_predictions)}\")\nprint(\"\\nFirst 10 predictions:\")\nprint(submission.head(10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:17:33.478947Z","iopub.execute_input":"2025-11-10T19:17:33.479288Z","iopub.status.idle":"2025-11-10T19:17:33.520643Z","shell.execute_reply.started":"2025-11-10T19:17:33.479262Z","shell.execute_reply":"2025-11-10T19:17:33.519698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\nDistribution of predicted labels:\")\nprint(submission['Label'].value_counts().sort_index())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T19:17:42.913109Z","iopub.execute_input":"2025-11-10T19:17:42.913480Z","iopub.status.idle":"2025-11-10T19:17:42.928536Z","shell.execute_reply.started":"2025-11-10T19:17:42.913443Z","shell.execute_reply":"2025-11-10T19:17:42.927629Z"}},"outputs":[],"execution_count":null}]}