{"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.7"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Libraries and Dataset","metadata":{}},{"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 Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, f1_score\nimport matplotlib.pyplot as plt","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Splitting and Normalization","metadata":{}},{"cell_type":"code","source":"X = df_train.drop(columns='label').values / 255.0  # Normalizing the data\ny = df_train['label']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape the data to fit the model (28x28x1 for grayscale images)\nX = X.reshape(-1, 28, 28, 1)\n\n# One-hot encode the labels\ny = to_categorical(y, num_classes=10)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"# Data Augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\n# Fit the data generator on the training data\ndatagen.fit(X_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the CNN model\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    BatchNormalization(),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model using the augmented data\nmodel.fit(datagen.flow(X_train, y_train, batch_size=32), epochs=10, validation_data=(X_test, y_test))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"# Evaluate the model\ntrain_loss, train_accuracy = model.evaluate(X_train, y_train)\ntest_loss, test_accuracy = model.evaluate(X_test, y_test)\n\nprint(\"Menggunakan Model CNN dengan Augmentasi Data dan Batch Normalization:\")\nprint(f\"Akurasi pada data training: {train_accuracy:.4f}\")\nprint(f\"Akurasi pada data testing: {test_accuracy:.4f}\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualization: Displays predicted and actual results on testing data\nidx = np.random.choice(len(X_test), size=36, replace=False)\nimages, labels = X_test[idx], y_test[idx]\npreds = np.argmax(model.predict(images), axis=1)  # Prediksi dari model\ntrue_labels = np.argmax(labels, axis=1)  # Label asli\n\nfig, axes = plt.subplots(6, 6, figsize=(15, 15))\nfor img, true_label, pred, ax in zip(images, true_labels, preds, axes.flatten()):\n    font = {'color': 'g'} if true_label == pred else {'color': 'r'}\n    ax.imshow(img.reshape(28, 28), cmap='gray')\n    ax.set_title(f\"Label: {true_label} | Pred: {pred}\", fontdict=font)\n    ax.axis('off')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply to New Dataset","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nX_new_test = df_test.values / 255.0\nX_new_test = X_new_test.reshape(-1, 28, 28, 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(X_new_test)\nresults = np.argmax(results, axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create DataFrame for submission\ndf_results = pd.DataFrame({\n    'ImageId': np.arange(1, len(results) + 1),\n    'Label': results\n})","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_results","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the predictions to a CSV file\ndf_results.to_csv(r\"C:\\Users\\ACER\\Downloads\\Digit Recognizer CNN Model III.csv\", index=False)\n\nprint(\"Predictions saved to 'predictions.csv'.\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}