{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import necessary libraries\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-30T09:31:51.664898Z","iopub.execute_input":"2024-05-30T09:31:51.666657Z","iopub.status.idle":"2024-05-30T09:31:51.672817Z","shell.execute_reply.started":"2024-05-30T09:31:51.666520Z","shell.execute_reply":"2024-05-30T09:31:51.671644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the MNIST dataset from CSV files\ndataset_path_train = '/kaggle/input/digit-recognizer/train.csv'\ndataset_path_test = '/kaggle/input/digit-recognizer/test.csv'\n\n# Read training and test datasets\ntrain_data = pd.read_csv(dataset_path_train)\ntest_data = pd.read_csv(dataset_path_test)\n\n# Split features and labels in the training dataset\nx_train = train_data.drop('label', axis=1).values\ny_train = train_data['label'].values\n\n# For the test dataset, assuming it does not have labels\nx_test = test_data.values\n\nIMG_SIZE = 28  # 28x28 pixels\n\n# Reshape images to flat (n_samples, IMG_SIZE * IMG_SIZE) and normalize\nx_train_flat = x_train.reshape(-1, IMG_SIZE * IMG_SIZE).astype('float32') / 255\nx_test_flat = x_test.reshape(-1, IMG_SIZE * IMG_SIZE).astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-30T09:31:51.675107Z","iopub.execute_input":"2024-05-30T09:31:51.675584Z","iopub.status.idle":"2024-05-30T09:31:58.020873Z","shell.execute_reply.started":"2024-05-30T09:31:51.675543Z","shell.execute_reply":"2024-05-30T09:31:58.019617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the model using a clear input layer approach\nmodel = Sequential([\n    layers.Input(shape=(784,)),  # Correct input shape specification\n    layers.Reshape((28, 28, 1)),  # Reshape flat vector to 28x28x1 image\n    Conv2D(32, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dropout(0.5),\n    BatchNormalization(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    BatchNormalization(),\n    Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Print the model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T09:31:58.022208Z","iopub.execute_input":"2024-05-30T09:31:58.022556Z","iopub.status.idle":"2024-05-30T09:31:58.230236Z","shell.execute_reply.started":"2024-05-30T09:31:58.022527Z","shell.execute_reply":"2024-05-30T09:31:58.229213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(x_train_flat, y_train, epochs=20, validation_split=0.1)\n\n# Predict the classes with the model on the test data\npredictions = model.predict(x_test_flat)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Create a DataFrame for submission\nsubmission = pd.DataFrame({\n    \"ImageId\": range(1, len(predicted_classes)+1),\n    \"Label\": predicted_classes\n})\n\n# Save the DataFrame to a CSV file to upload to Kaggle\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T09:31:58.232242Z","iopub.execute_input":"2024-05-30T09:31:58.232614Z","iopub.status.idle":"2024-05-30T09:41:05.657339Z","shell.execute_reply.started":"2024-05-30T09:31:58.232585Z","shell.execute_reply":"2024-05-30T09:41:05.656095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T09:41:05.659063Z","iopub.execute_input":"2024-05-30T09:41:05.659505Z","iopub.status.idle":"2024-05-30T09:41:05.999441Z","shell.execute_reply.started":"2024-05-30T09:41:05.659472Z","shell.execute_reply":"2024-05-30T09:41:05.997018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training & validation loss values\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T09:41:06.001386Z","iopub.execute_input":"2024-05-30T09:41:06.001891Z","iopub.status.idle":"2024-05-30T09:41:06.340030Z","shell.execute_reply.started":"2024-05-30T09:41:06.001845Z","shell.execute_reply":"2024-05-30T09:41:06.338787Z"},"trusted":true},"execution_count":null,"outputs":[]}]}