{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# --- 1. Data Loading & Preprocessing ---\n\n# Load the data\ntrain_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# Separate features (pixels) and labels (the digit)\ny_train = train_df[\"label\"]\nX_train = train_df.drop(labels = [\"label\"], axis = 1)\n\n# Normalize the data: Pixel values range from 0 to 255.\n# Dividing by 255 scales them to a 0 to 1 range, which helps training.\nX_train = X_train / 255.0\ntest_df = test_df / 255.0\n\n# Reshape the data: The images are 28x28 pixels, but are given as a flat 784-pixel list.\n# We need to reshape them into (28, 28, 1) to be used by the CNN.\n# The '1' signifies that it's a grayscale image.\nX_train = X_train.values.reshape(-1, 28, 28, 1)\ntest = test_df.values.reshape(-1, 28, 28, 1)\n\n# One-Hot Encode the labels: Convert the label (e.g., '7') into a vector where the 7th element is 1 and others are 0.\n# Example: 2 -> [0,0,1,0,0,0,0,0,0,0]\ny_train = to_categorical(y_train, num_classes = 10)\n\n# Create a validation set from the training data\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=2)\n\n\n# --- 2. Model Building (Creating the CNN) ---\n\nmodel = Sequential([\n    # First convolutional block\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    # Second convolutional block\n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    # Flatten the 2D arrays to a 1D vector to feed into the dense layers\n    Flatten(),\n    # Dense (fully connected) layers for classification\n    Dense(256, activation='relu'),\n    Dropout(0.5), # Dropout helps prevent overfitting by randomly setting neurons to 0\n    Dense(10, activation='softmax') # Softmax gives a probability distribution over the 10 classes (digits)\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n\n# --- 3. Model Training ---\n\n# Set up a learning rate scheduler to reduce the learning rate if validation accuracy plateaus\nlearning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='val_accuracy',\n    patience=3,\n    verbose=1,\n    factor=0.5,\n    min_lr=0.00001\n)\n\n# Train the model\nhistory = model.fit(X_train, y_train,\n                    batch_size=86,\n                    epochs=30, # For a good score, 20-30 epochs are usually sufficient\n                    validation_data=(X_val, y_val),\n                    callbacks=[learning_rate_reduction])\n\n\n# --- 4. Prediction and Submission ---\n\n# Predict the values from the test dataset\nresults = model.predict(test)\n\n# Select the index with the maximum probability (this is the predicted digit)\nresults = np.argmax(results, axis=1)\n\n# Create the submission file\nsubmission = pd.DataFrame({\n    \"ImageId\": list(range(1, len(results) + 1)),\n    \"Label\": results\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)\n\nprint(\"Submission file created successfully!\")\nprint(\"A preview of the submission file:\")\nprint(submission.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-19T23:24:10.617378Z","iopub.execute_input":"2025-07-19T23:24:10.617983Z","iopub.status.idle":"2025-07-19T23:25:21.914499Z","shell.execute_reply.started":"2025-07-19T23:24:10.617958Z","shell.execute_reply":"2025-07-19T23:25:21.913806Z"}},"outputs":[],"execution_count":null}]}