{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Upload libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:11.029487Z","iopub.execute_input":"2025-02-24T12:23:11.029815Z","iopub.status.idle":"2025-02-24T12:23:11.366985Z","shell.execute_reply.started":"2025-02-24T12:23:11.029789Z","shell.execute_reply":"2025-02-24T12:23:11.366140Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nimport seaborn as sns\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:24:01.884900Z","iopub.execute_input":"2025-02-24T12:24:01.885236Z","iopub.status.idle":"2025-02-24T12:24:02.195312Z","shell.execute_reply.started":"2025-02-24T12:24:01.885209Z","shell.execute_reply":"2025-02-24T12:24:02.194388Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load the data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:24.277232Z","iopub.execute_input":"2025-02-24T12:23:24.277754Z","iopub.status.idle":"2025-02-24T12:23:28.964445Z","shell.execute_reply.started":"2025-02-24T12:23:24.277732Z","shell.execute_reply":"2025-02-24T12:23:28.963513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:28.965672Z","iopub.execute_input":"2025-02-24T12:23:28.965904Z","iopub.status.idle":"2025-02-24T12:23:28.992521Z","shell.execute_reply.started":"2025-02-24T12:23:28.965885Z","shell.execute_reply":"2025-02-24T12:23:28.991463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:28.993275Z","iopub.execute_input":"2025-02-24T12:23:28.993548Z","iopub.status.idle":"2025-02-24T12:23:29.005291Z","shell.execute_reply.started":"2025-02-24T12:23:28.993527Z","shell.execute_reply":"2025-02-24T12:23:29.004444Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualize first 5 images from training set\n","metadata":{}},{"cell_type":"code","source":"def show_sample_images(df, num_images=5):\n    fig, axes = plt.subplots(1, num_images, figsize=(10, 2))\n    pixel_cols = [col for col in df.columns if col != 'label']\n    for i, ax in enumerate(axes):\n        # get one image (first num_images rows)\n        image = df.iloc[i][pixel_cols].values.astype(np.float32)\n        # Normalize for visualization (if not already 0-255)\n        image = image.reshape(28, 28)\n        ax.imshow(image, cmap='gray')\n        ax.axis('off')\n        if 'label' in df.columns:\n            ax.set_title(f\"Label: {df.iloc[i]['label']}\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.005866Z","iopub.execute_input":"2025-02-24T12:23:29.006185Z","iopub.status.idle":"2025-02-24T12:23:29.021354Z","shell.execute_reply.started":"2025-02-24T12:23:29.006121Z","shell.execute_reply":"2025-02-24T12:23:29.020451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize first 5 images from training set\nshow_sample_images(train, num_images=5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.022025Z","iopub.execute_input":"2025-02-24T12:23:29.022253Z","iopub.status.idle":"2025-02-24T12:23:29.364745Z","shell.execute_reply.started":"2025-02-24T12:23:29.022236Z","shell.execute_reply":"2025-02-24T12:23:29.363745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.set_theme(style=\"darkgrid\", palette=\"muted\")\nplt.figure(figsize=(10, 5))\nsns.countplot(x=train[\"label\"], palette=\"viridis\")\nplt.title(\"Class Distribution in Training Data\", fontsize=14)\nplt.xlabel(\"Label\")\nplt.ylabel(\"Count\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:24:05.227693Z","iopub.execute_input":"2025-02-24T12:24:05.228280Z","iopub.status.idle":"2025-02-24T12:24:05.456053Z","shell.execute_reply.started":"2025-02-24T12:24:05.228248Z","shell.execute_reply":"2025-02-24T12:24:05.455077Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# --- Preprocessing ---","metadata":{}},{"cell_type":"code","source":"# --- 3. Preprocessing ---\n# Separate features and labels\nX = train.drop(\"label\", axis=1).values.astype(np.float32)\ny = train[\"label\"].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.640881Z","iopub.status.idle":"2025-02-24T12:23:29.641224Z","shell.execute_reply":"2025-02-24T12:23:29.641105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize pixel values (assuming they are in the range 0-255)\nX = X / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.641826Z","iopub.status.idle":"2025-02-24T12:23:29.642120Z","shell.execute_reply":"2025-02-24T12:23:29.641983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# For test set\nX_test = test.values.astype(np.float32) / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.642888Z","iopub.status.idle":"2025-02-24T12:23:29.643209Z","shell.execute_reply":"2025-02-24T12:23:29.643094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshape to image dimensions.\n# If your image is 28x28 and grayscale, reshape accordingly.\n# Note: If you have a different image size, update (28,28) below.\nX = X.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.644124Z","iopub.status.idle":"2025-02-24T12:23:29.644401Z","shell.execute_reply":"2025-02-24T12:23:29.644289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert labels to one-hot encoding (for multi-class classification)\nnum_classes = len(np.unique(y))\ny_cat = to_categorical(y, num_classes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.645586Z","iopub.status.idle":"2025-02-24T12:23:29.645903Z","shell.execute_reply":"2025-02-24T12:23:29.645791Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#  ---Train/Validation Split ---\n","metadata":{}},{"cell_type":"code","source":"\nX_train, X_val, y_train, y_val = train_test_split(X, y_cat, test_size=0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.647207Z","iopub.status.idle":"2025-02-24T12:23:29.647673Z","shell.execute_reply":"2025-02-24T12:23:29.647424Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# --- Build and Train the Model ---\n","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(num_classes, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.648666Z","iopub.status.idle":"2025-02-24T12:23:29.649081Z","shell.execute_reply":"2025-02-24T12:23:29.648895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.649951Z","iopub.status.idle":"2025-02-24T12:23:29.650240Z","shell.execute_reply":"2025-02-24T12:23:29.650130Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train the model\n","metadata":{}},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val),\n                    epochs=10, batch_size=64)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.651210Z","iopub.status.idle":"2025-02-24T12:23:29.651588Z","shell.execute_reply":"2025-02-24T12:23:29.651422Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ---Visualize Training Accuracy and Loss ---\n","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.652375Z","iopub.status.idle":"2025-02-24T12:23:29.652729Z","shell.execute_reply":"2025-02-24T12:23:29.652575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot accuracy\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title(\"Accuracy over Epochs\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.653722Z","iopub.status.idle":"2025-02-24T12:23:29.654021Z","shell.execute_reply":"2025-02-24T12:23:29.653892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot loss\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title(\"Loss over Epochs\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.654991Z","iopub.status.idle":"2025-02-24T12:23:29.655306Z","shell.execute_reply":"2025-02-24T12:23:29.655201Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# --- Prediction on Test Data ---\n","metadata":{}},{"cell_type":"code","source":"# Predict probabilities and then choose the class with highest probability.\npredictions_prob = model.predict(X_test)\npredictions = np.argmax(predictions_prob, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.655975Z","iopub.status.idle":"2025-02-24T12:23:29.656310Z","shell.execute_reply":"2025-02-24T12:23:29.656159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- Save the Output to an Excel File ---\n# Create a DataFrame with 'ImageId' and 'Prediction'\nsubmission_df = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(predictions) + 1),\n    \"Prediction\": predictions\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.657349Z","iopub.status.idle":"2025-02-24T12:23:29.657731Z","shell.execute_reply":"2025-02-24T12:23:29.657560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save to Excel file (requires openpyxl or xlsxwriter)\nsubmission_df.to_csv(\"submissionp.csv\", index=False)\nprint(\"Submission saved to submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-24T12:23:29.658409Z","iopub.status.idle":"2025-02-24T12:23:29.658788Z","shell.execute_reply":"2025-02-24T12:23:29.658622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}