{"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":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\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# Display the first few rows of the training data\nprint(\"Training Data Head:\")\nprint(train_df.head())\n\n# Display the shape of the dataframes\nprint(f\"\\nTraining data shape: {train_df.shape}\")\nprint(f\"Test data shape: {test_df.shape}\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:54:49.083558Z","iopub.execute_input":"2025-10-20T19:54:49.084439Z","iopub.status.idle":"2025-10-20T19:54:54.894997Z","shell.execute_reply.started":"2025-10-20T19:54:49.084404Z","shell.execute_reply":"2025-10-20T19:54:54.893953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# 1. Separate features (pixels) and labels (digits)\nX_train_full = train_df.drop(columns=['label'])\ny_train_full = train_df['label']\n\n# 2. Visualize some of the digits\nplt.figure(figsize=(10, 6))\nfor i in range(10):\n    plt.subplot(2, 5, i + 1)\n    # Reshape the 784 pixels into a 28x28 image\n    image = X_train_full.iloc[i].values.reshape(28, 28)\n    plt.imshow(image, cmap='gray')\n    plt.title(f\"Label: {y_train_full.iloc[i]}\")\n    plt.axis('off')\nplt.suptitle(\"Sample Digits from the Training Set\", fontsize=16)\nplt.show()\n\n# 3. Check the distribution of the labels\nplt.figure(figsize=(10, 5))\nsns.countplot(x=y_train_full)\nplt.title(\"Distribution of Digit Labels in Training Data\")\nplt.xlabel(\"Digit\")\nplt.ylabel(\"Count\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:54:54.896509Z","iopub.execute_input":"2025-10-20T19:54:54.896871Z","iopub.status.idle":"2025-10-20T19:54:56.211008Z","shell.execute_reply.started":"2025-10-20T19:54:54.896840Z","shell.execute_reply":"2025-10-20T19:54:56.210166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# 1. Normalize the pixel values to be between 0 and 1\nX_train_full = X_train_full / 255.0\n# Also normalize the test data, which we will use later\ntest_images = test_df / 255.0\n\n# 2. Reshape the data for the CNN\n# The shape should be (number_of_images, height, width, color_channels)\nX_train_full = X_train_full.values.reshape(-1, 28, 28, 1)\ntest_images = test_images.values.reshape(-1, 28, 28, 1)\n\n# 3. One-hot encode the labels\ny_train_full_cat = to_categorical(y_train_full, num_classes=10)\n\n# Print the new shapes to verify\nprint(f\"Training images shape: {X_train_full.shape}\")\nprint(f\"Test images shape: {test_images.shape}\")\nprint(f\"Categorical training labels shape: {y_train_full_cat.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:54:56.211837Z","iopub.execute_input":"2025-10-20T19:54:56.212291Z","iopub.status.idle":"2025-10-20T19:55:12.178331Z","shell.execute_reply.started":"2025-10-20T19:54:56.212266Z","shell.execute_reply":"2025-10-20T19:55:12.177555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\n# Define the model architecture\nmodel = Sequential([\n    # First convolutional block\n    Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n\n    # Second convolutional block\n    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n\n    # Flatten the feature maps to feed into dense layers\n    Flatten(),\n\n    # Dense layer for classification\n    Dense(128, activation='relu'),\n    \n    # Dropout layer to prevent overfitting\n    Dropout(0.5),\n\n    # Output layer with 10 units (for 10 digits) and softmax activation\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Display the model's architecture\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:55:12.179774Z","iopub.execute_input":"2025-10-20T19:55:12.180289Z","iopub.status.idle":"2025-10-20T19:55:12.313134Z","shell.execute_reply.started":"2025-10-20T19:55:12.180268Z","shell.execute_reply":"2025-10-20T19:55:12.312439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# We'll use 90% for training and 10% for validation\nX_train, X_val, y_train, y_val = train_test_split(\n    X_train_full, y_train_full_cat, test_size=0.1, random_state=42\n)\n\n# Set training parameters\nepochs = 15\nbatch_size = 64\n\n# Train the model\nhistory = model.fit(\n    X_train, y_train,\n    batch_size=batch_size,\n    epochs=epochs,\n    validation_data=(X_val, y_val)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:55:12.313962Z","iopub.execute_input":"2025-10-20T19:55:12.314216Z","iopub.status.idle":"2025-10-20T19:58:45.887791Z","shell.execute_reply.started":"2025-10-20T19:55:12.314196Z","shell.execute_reply":"2025-10-20T19:58:45.887013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Visualize the training history\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))\n\n# Plotting accuracy\nax1.plot(history.history['accuracy'], label='Training Accuracy')\nax1.plot(history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_title('Model Accuracy')\nax1.set_xlabel('Epoch')\nax1.set_ylabel('Accuracy')\nax1.legend()\n\n# Plotting loss\nax2.plot(history.history['loss'], label='Training Loss')\nax2.plot(history.history['val_loss'], label='Validation Loss')\nax2.set_title('Model Loss')\nax2.set_xlabel('Epoch')\nax2.set_ylabel('Loss')\nax2.legend()\n\nplt.show()\n\n# Make predictions on the test data\npredictions = model.predict(test_images)\n\n# The model outputs probabilities for each class. We want the class with the highest probability.\npredicted_classes = np.argmax(predictions, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T20:02:54.584671Z","iopub.execute_input":"2025-10-20T20:02:54.585558Z","iopub.status.idle":"2025-10-20T20:02:59.599366Z","shell.execute_reply.started":"2025-10-20T20:02:54.585495Z","shell.execute_reply":"2025-10-20T20:02:59.598403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create the submission file\nsubmission_df = pd.DataFrame({\n    'ImageId': range(1, len(predicted_classes) + 1),\n    'Label': predicted_classes\n})\n\n# Save the submission file\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\n\nprint(\"Submission file created successfully!\")\nprint(submission_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T20:03:10.009146Z","iopub.execute_input":"2025-10-20T20:03:10.009461Z","iopub.status.idle":"2025-10-20T20:03:10.044416Z","shell.execute_reply.started":"2025-10-20T20:03:10.009431Z","shell.execute_reply":"2025-10-20T20:03:10.043588Z"}},"outputs":[],"execution_count":null}]}