{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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":[{"id":"2f5952b4bb77862f","cell_type":"markdown","source":"## **Digit Recognizer - CNN + RFC Baseline**\n\n\n### *Goal*:\n#### Classify images of handwritten digits (0-9) from 28x28 grayscale images using CNN and Random Forest.\n\n### *Approach*:\n#### 1. Load data\n#### 2. Preprocess (normalize, reshape)\n#### 3. Train a CNN for high accuracy\n#### 4. Train a Random Forest for baseline comparison\n#### 5. Generate predictions and save two submission files\n","metadata":{}},{"id":"initial_id","cell_type":"code","source":"# Import Libraries\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.utils import to_categorical","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:17.726009Z","start_time":"2025-08-01T07:34:17.701811Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:10.466680Z","iopub.execute_input":"2025-08-01T07:40:10.467021Z","iopub.status.idle":"2025-08-01T07:40:10.471757Z","shell.execute_reply.started":"2025-08-01T07:40:10.466996Z","shell.execute_reply":"2025-08-01T07:40:10.470950Z"}},"outputs":[],"execution_count":null},{"id":"c8477ecb-f624-4092-b6fc-faea00f1d1dd","cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:12.052839Z","iopub.execute_input":"2025-08-01T07:40:12.053380Z","iopub.status.idle":"2025-08-01T07:40:12.062030Z","shell.execute_reply.started":"2025-08-01T07:40:12.053343Z","shell.execute_reply":"2025-08-01T07:40:12.061063Z"}},"outputs":[],"execution_count":null},{"id":"cf06066248cddec1","cell_type":"code","source":"# Load data\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:22.405098Z","start_time":"2025-08-01T07:34:18.532526Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:31.556806Z","iopub.execute_input":"2025-08-01T07:40:31.557503Z","iopub.status.idle":"2025-08-01T07:40:36.558190Z","shell.execute_reply.started":"2025-08-01T07:40:31.557464Z","shell.execute_reply":"2025-08-01T07:40:36.557514Z"}},"outputs":[],"execution_count":null},{"id":"d506be95c1dbea80","cell_type":"code","source":"# Preprocessing for CNN\n# Split features and labels\nX_train = train_df.drop('label', axis =1).values\ny_train = train_df['label'].values","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:23.337831Z","start_time":"2025-08-01T07:34:23.097704Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:40.038212Z","iopub.execute_input":"2025-08-01T07:40:40.038793Z","iopub.status.idle":"2025-08-01T07:40:40.157559Z","shell.execute_reply.started":"2025-08-01T07:40:40.038765Z","shell.execute_reply":"2025-08-01T07:40:40.156939Z"}},"outputs":[],"execution_count":null},{"id":"3ede19e4f873141f","cell_type":"code","source":"# Normalize pixel values to [0, 1]\nX_train = X_train / 255.0\ntest_df = test_df / 255.0","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:24.460792Z","start_time":"2025-08-01T07:34:23.972497Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:41.528810Z","iopub.execute_input":"2025-08-01T07:40:41.529445Z","iopub.status.idle":"2025-08-01T07:40:41.716714Z","shell.execute_reply.started":"2025-08-01T07:40:41.529419Z","shell.execute_reply":"2025-08-01T07:40:41.715994Z"}},"outputs":[],"execution_count":null},{"id":"ed0537a6b05ef3a4","cell_type":"code","source":"# Reshape for CNN (28x28x1)\nX_train = X_train.reshape(-1, 28, 28, 1)\ntest_df = test_df.values.reshape(-1, 28, 28, 1)","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:24.924233Z","start_time":"2025-08-01T07:34:24.921094Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:43.900482Z","iopub.execute_input":"2025-08-01T07:40:43.901124Z","iopub.status.idle":"2025-08-01T07:40:43.905001Z","shell.execute_reply.started":"2025-08-01T07:40:43.901094Z","shell.execute_reply":"2025-08-01T07:40:43.904217Z"}},"outputs":[],"execution_count":null},{"id":"f620646f712537ef","cell_type":"code","source":"# One-hot encode labels for CNN\ny_train = to_categorical(y_train, num_classes=10)","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:25.616913Z","start_time":"2025-08-01T07:34:25.609753Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:44.821991Z","iopub.execute_input":"2025-08-01T07:40:44.822278Z","iopub.status.idle":"2025-08-01T07:40:44.827334Z","shell.execute_reply.started":"2025-08-01T07:40:44.822256Z","shell.execute_reply":"2025-08-01T07:40:44.826627Z"}},"outputs":[],"execution_count":null},{"id":"ce5b01c0c8d8ca0f","cell_type":"code","source":"# Build CNN\nmodel = Sequential([\n    Input(shape=(28, 28, 1)),\n    Conv2D(32, kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Conv2D(64, kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train\nmodel.fit(X_train, y_train, epochs=10, batch_size=64, validation_split=0.1)\n\n# Predict\npred = model.predict(test_df)\npred_labels = np.argmax(pred, axis=1)\n\n# Create submission\nsubmission = pd.DataFrame({'ImageId': range(1, len(pred_labels)+1), 'Label': pred_labels})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"ExecuteTime":{"end_time":"2025-08-01T07:34:37.214088Z","start_time":"2025-08-01T07:34:26.235515Z"},"trusted":true,"execution":{"iopub.status.busy":"2025-08-01T07:40:48.462424Z","iopub.execute_input":"2025-08-01T07:40:48.462712Z","iopub.status.idle":"2025-08-01T07:43:13.695724Z","shell.execute_reply.started":"2025-08-01T07:40:48.462691Z","shell.execute_reply":"2025-08-01T07:43:13.694876Z"}},"outputs":[],"execution_count":null},{"id":"21b475cf1d347cfd","cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}