{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import necessary libraries for data manipulation, visualization and deep learning\nimport pandas as pd  # For data manipulation and analysis\nimport numpy as np   # For numerical operations\nimport keras as ke   # Deep learning framework\nfrom sklearn.model_selection import train_test_split  # For splitting data into train and test sets\nfrom tensorflow.keras.utils import to_categorical  # For one-hot encoding of labels\nfrom keras.models import Sequential  # For creating sequential neural network model\nfrom keras.layers import Dense, Flatten, Conv2D, MaxPool2D, BatchNormalization  # Neural network layers\n\n# from keras.callbacks import LearningRateScheduler  # For adjusting learning rate during training\nimport matplotlib.pyplot as plt  # For visualization","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:00.393775Z","iopub.execute_input":"2025-05-30T13:09:00.393994Z","iopub.status.idle":"2025-05-30T13:09:16.440527Z","shell.execute_reply.started":"2025-05-30T13:09:00.393966Z","shell.execute_reply":"2025-05-30T13:09:16.439975Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  Load the MNIST dataset from CSV files\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv') #  Load training data with labels\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv') # Load test data without labels\nsubmission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv') # Load submission format file","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:16.441650Z","iopub.execute_input":"2025-05-30T13:09:16.442120Z","iopub.status.idle":"2025-05-30T13:09:20.681030Z","shell.execute_reply.started":"2025-05-30T13:09:16.442101Z","shell.execute_reply":"2025-05-30T13:09:20.680421Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare data for training\nY_train = train[\"label\"]  # Extract labels from training data\nX_train = train.drop(\"label\", axis=1)  # Extract features from training data\n\nX_train = X_train/255.0  # Normalize pixel values to range [0,1]\nX_test = test/255.0      # Normalize test data similarly\n\nX_train = X_train.values.reshape(-1,28,28,1)  # Reshape to 28x28 images with 1 channel\nX_test = X_test.values.reshape(-1,28,28,1)    # Reshape test data similarly\n\nY_train = to_categorical(Y_train, num_classes = 10)  # Convert labels to one-hot encoding (0-9 digits)","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:20.681734Z","iopub.execute_input":"2025-05-30T13:09:20.681939Z","iopub.status.idle":"2025-05-30T13:09:20.861006Z","shell.execute_reply.started":"2025-05-30T13:09:20.681924Z","shell.execute_reply":"2025-05-30T13:09:20.860446Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Data augmentation code (commented out)\n# This would create additional training samples by applying transformations \n#---------------------------------------------\n\n# datagen = ImageDataGenerator(\n# rotation_range=40,\n# width_shift_range=0.2,\n# height_shift_range=0.2,\n# shear_range=0.2,\n# zoom_range=0.2,\n# horizontal_flip=True,\n# fill_mode='nearest')\n\n\n# Genrate_train = datagen.flow(X_train , Y_train , batch_size=32)\n# # Genrate_train = Genrate_train.reshape(28,28,1)\n# augmented_image = []\n# augmented_label = []\n# for i in range ((100)):\n#     img , lab = next (Genrate_train)\n#     augmented_image.append(img)\n#     augmented_label.append(lab)   \n\n# augmented_image = np.array(augmented_image)\n# augmented_label = np.array(augmented_label)\n\n# augmented_image = augmented_image.reshape(-1, 28, 28, 1)\n# augmented_label = augmented_label.reshape(-1, 10)\n\n\n# X_train = np.concatenate([X_train,augmented_image] )\n# Y_train = np.concatenate([Y_train, augmented_label])\n","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:20.861692Z","iopub.execute_input":"2025-05-30T13:09:20.861915Z","iopub.status.idle":"2025-05-30T13:09:20.865714Z","shell.execute_reply.started":"2025-05-30T13:09:20.861895Z","shell.execute_reply":"2025-05-30T13:09:20.864978Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split training data into training and validation sets\nX_tr, X_te, Y_tr, Y_te = train_test_split(X_train, Y_train, test_size=0.2, random_state=42, shuffle=True)\n# test_size=0.2: 20% of data used for validation\n# random_state=42: For reproducible results\n# shuffle=True: Randomly shuffle data before splitting","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:20.867860Z","iopub.execute_input":"2025-05-30T13:09:20.868241Z","iopub.status.idle":"2025-05-30T13:09:21.241693Z","shell.execute_reply.started":"2025-05-30T13:09:20.868223Z","shell.execute_reply":"2025-05-30T13:09:21.241124Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize sample images from training data\nplt.figure(figsize=(15,4.5))  # Set figure size\nfor i in range(5):  # Display 5 sample images\n    plt.subplot(3, 10, i+1)  # Create subplot grid\n    plt.imshow(X_tr[i].reshape((28,28)), cmap=plt.cm.binary)  # Display image in binary colormap\n    plt.axis('off')  # Hide axes\nplt.subplots_adjust(wspace=-0.1, hspace=-0.1)  # Adjust spacing between subplots\nplt.show()  # Display the figure","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:21.242349Z","iopub.execute_input":"2025-05-30T13:09:21.242576Z","iopub.status.idle":"2025-05-30T13:09:21.411227Z","shell.execute_reply.started":"2025-05-30T13:09:21.242559Z","shell.execute_reply":"2025-05-30T13:09:21.410706Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define CNN model architecture using Sequential API\nmodel = Sequential([\n    # First convolutional block\n    Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=(28,28,1)),  # 32 filters, 3x3 kernel\n    MaxPool2D((2,2)),  # Reduce spatial dimensions by half\n    \n    # Second convolutional block\n    Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu'),  # 64 filters, 3x3 kernel\n    MaxPool2D((2,2)),  # Further reduce dimensions\n    \n    # Third convolutional block\n    Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu'),  # 128 filters, 3x3 kernel\n    \n    # Flatten and dense layers\n    Flatten(),  # Convert 3D feature maps to 1D feature vector\n    Dense(units=128, activation='relu'),  # Fully connected layer with 128 neurons\n    Dense(units=10, activation='softmax')  # Output layer with 10 neurons (one per digit)\n])\n#-------------------------------------------\n\n# Compile and train the model\nmodel.compile(\n    optimizer=ke.optimizers.Adam(learning_rate=0.0001),  # Adam optimizer with low learning rate\n    loss='categorical_crossentropy',  # Loss function for multi-class classification\n    metrics=['accuracy']  # Track accuracy during training\n)","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:21.411857Z","iopub.execute_input":"2025-05-30T13:09:21.412094Z","iopub.status.idle":"2025-05-30T13:09:24.023982Z","shell.execute_reply.started":"2025-05-30T13:09:21.412077Z","shell.execute_reply":"2025-05-30T13:09:24.023412Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display model summary showing layers and parameters\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T13:09:24.024631Z","iopub.execute_input":"2025-05-30T13:09:24.024873Z","iopub.status.idle":"2025-05-30T13:09:24.042455Z","shell.execute_reply.started":"2025-05-30T13:09:24.024849Z","shell.execute_reply":"2025-05-30T13:09:24.041787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    x=X_tr,  # Training features\n    y=Y_tr,  # Training labels\n    validation_data=(X_te, Y_te),  # Validation data\n    epochs=7,  # Number of training cycles\n    verbose=2  # Output level (2 = one line per epoch)\n)","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:24.043255Z","iopub.execute_input":"2025-05-30T13:09:24.043473Z","iopub.status.idle":"2025-05-30T13:09:53.763883Z","shell.execute_reply.started":"2025-05-30T13:09:24.043453Z","shell.execute_reply":"2025-05-30T13:09:53.763199Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training and validation accuracy over epochs\nplt.figure(figsize=(12, 4))\n\n# Plot accuracy\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')  # Training accuracy curve\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')  # Validation accuracy curve\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\n\n# Plot loss\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Training Loss')  # Training loss curve\nplt.plot(history.history['val_loss'], label='Validation Loss')  # Validation loss curve\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.tight_layout()  # Adjust subplot spacing\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:53.764869Z","iopub.execute_input":"2025-05-30T13:09:53.765117Z","iopub.status.idle":"2025-05-30T13:09:54.163332Z","shell.execute_reply.started":"2025-05-30T13:09:53.765093Z","shell.execute_reply":"2025-05-30T13:09:54.162467Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on test data\npredictions = model.predict(X_test)  # Get probability distributions\npredictions = np.argmax(predictions, axis=1)  # Convert to class indices (0-9)\n","metadata":{"execution":{"iopub.status.busy":"2025-05-30T13:09:54.164128Z","iopub.execute_input":"2025-05-30T13:09:54.164370Z","iopub.status.idle":"2025-05-30T13:09:56.538810Z","shell.execute_reply.started":"2025-05-30T13:09:54.164351Z","shell.execute_reply":"2025-05-30T13:09:56.537947Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"FainlRuslt = pd.DataFrame( {\"ImageId\":test.index+1 , \"Label\": predictions} ) #! creat a Taple for Ruslt \nFainlRuslt.to_csv('/kaggle/working/submission.csv' , index= False)\nFainlRuslt.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T13:09:56.539705Z","iopub.execute_input":"2025-05-30T13:09:56.539976Z","iopub.status.idle":"2025-05-30T13:09:56.579679Z","shell.execute_reply.started":"2025-05-30T13:09:56.539958Z","shell.execute_reply":"2025-05-30T13:09:56.579092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize some test predictions\nplt.figure(figsize=(15, 7))\nfor i in range(10):  # Show 15 test examples\n    plt.subplot(3, 5, i+1)\n    plt.imshow(X_test[i].reshape(28, 28), cmap='gray')  # Display image\n    plt.title(f'Prediction: {predictions[i]}')  # Show predicted digit\n    plt.axis('off')  # Hide axes\nplt.tight_layout()\nplt.show ()\nprint (\"###### Check the first 10 image and the predicted Values #######\")\nprint (FainlRuslt[:10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T13:09:56.580392Z","iopub.execute_input":"2025-05-30T13:09:56.580678Z","iopub.status.idle":"2025-05-30T13:09:57.095053Z","shell.execute_reply.started":"2025-05-30T13:09:56.580662Z","shell.execute_reply":"2025-05-30T13:09:57.094429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate model on validation set\nval_loss, val_accuracy = model.evaluate(X_te, Y_te)\nprint(f'Validation accuracy: {val_accuracy:.4f}')  # Display validation accuracy\nprint(f'Validation loss: {val_loss:.4f}')  # Display validation loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-30T13:09:57.097083Z","iopub.execute_input":"2025-05-30T13:09:57.097391Z","iopub.status.idle":"2025-05-30T13:09:57.759788Z","shell.execute_reply.started":"2025-05-30T13:09:57.097371Z","shell.execute_reply":"2025-05-30T13:09:57.759087Z"}},"outputs":[],"execution_count":null}]}