{"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"},{"sourceId":3107,"sourceType":"modelInstanceVersion","modelInstanceId":2320,"modelId":271}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color:#F8F1E8; font-family:newtimeroman;color:#602F44; font-size:150%; text-align:center; border-radius: 15px 50px;\"> Digit Recognizer 🔢 </p>\n   ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#4E5672 solid; padding: 15px; background-color: #F8F1E8; font-size:100%; text-align:left\">\n\n**Dataset**: MNIST (\"Modified National Institute of Standards and Technology\"), a classic in computer vision since 1999. It consists of handwritten images and is commonly used for benchmarking classification algorithms.\n    \n**Objective**: Identify digits from tens of thousands of handwritten images.\n    \n**Evaluation**: Evaluation based on categorization accuracy (the percentage of correctly identified images).\n    \n\n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#4E5672 solid; padding: 15px; background-color: #F8F1E8; font-size:100%; text-align:left\">\n\n**Dataset Details**\n    \n* **Content**: Gray-scale images of hand-drawn digits (0 through 9).\n    \n* **Image Size**: 28x28 pixels, totaling 784 pixels per image. Each pixel has a value (0-255) indicating its lightness or darkness.\n    \n* **Training Set** (train.csv): 785 columns; the first column is the 'label' (digit), and the rest are pixel values.\n    \n* **Testing Set** (test.csv): Similar to the training set but lacks the 'label' column.\n\n    \n","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers,models\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Input, GlobalAveragePooling2D\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\n","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:30.806932Z","iopub.execute_input":"2024-12-18T20:02:30.807275Z","iopub.status.idle":"2024-12-18T20:02:41.541362Z","shell.execute_reply.started":"2024-12-18T20:02:30.807247Z","shell.execute_reply":"2024-12-18T20:02:41.540592Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:41.543107Z","iopub.execute_input":"2024-12-18T20:02:41.543602Z","iopub.status.idle":"2024-12-18T20:02:45.610542Z","shell.execute_reply.started":"2024-12-18T20:02:41.543579Z","shell.execute_reply":"2024-12-18T20:02:45.609552Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:45.611673Z","iopub.execute_input":"2024-12-18T20:02:45.611941Z","iopub.status.idle":"2024-12-18T20:02:45.634828Z","shell.execute_reply.started":"2024-12-18T20:02:45.611918Z","shell.execute_reply":"2024-12-18T20:02:45.634154Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:45.635853Z","iopub.execute_input":"2024-12-18T20:02:45.636749Z","iopub.status.idle":"2024-12-18T20:02:45.665811Z","shell.execute_reply.started":"2024-12-18T20:02:45.636726Z","shell.execute_reply":"2024-12-18T20:02:45.664990Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train  = df_train['label'].values # np array of all the labels (42000, )\nX_train = df_train.drop(columns=['label']).values.reshape(-1,28,28,1)/255.0 # drop the labels and reshape (num_rows, height , width , channel)\nX_test = df_test.values.reshape(-1, 28, 28, 1) / 255.0 # /255.0 --> ormalizing these pixel values to the range [0, 1] ","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:45.667883Z","iopub.execute_input":"2024-12-18T20:02:45.668125Z","iopub.status.idle":"2024-12-18T20:02:45.925878Z","shell.execute_reply.started":"2024-12-18T20:02:45.668105Z","shell.execute_reply":"2024-12-18T20:02:45.925068Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_encoded = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:45.927323Z","iopub.execute_input":"2024-12-18T20:02:45.927747Z","iopub.status.idle":"2024-12-18T20:02:45.933644Z","shell.execute_reply.started":"2024-12-18T20:02:45.927709Z","shell.execute_reply":"2024-12-18T20:02:45.932896Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color:#F8F1E8; font-family:newtimeroman;color:#602F44; font-size:150%; text-align:center; border-radius: 15px 50px;\"> Data Visualizatio 📊</p>\n\n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#484366 solid; padding: 15px; background-color: #FFEBCC; font-size:100%; text-align:left\">\n\n<h3 align=\"left\"><font color='#484366'>💬 Comment</font></h3>\n\nWe randomly choose 10 samples from the training set and visualize it.\n\n\n","metadata":{}},{"cell_type":"code","source":"fig , axes  = plt.subplots(2, 5, figsize=(12,5))\naxes   = axes.flatten()\nidx = np.random.randint(0, 42000, size=10)\nfor i in range(10):\n    axes[i].imshow(X_train[idx[i], :].reshape(28,28), cmap='gray')\n    axes[i].axis('off') # hide the axes ticks\n    axes[i].set_title(str(int(y_train[idx[i]])), color='black', fontsize=25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:45.935264Z","iopub.execute_input":"2024-12-18T20:02:45.935681Z","iopub.status.idle":"2024-12-18T20:02:46.470104Z","shell.execute_reply.started":"2024-12-18T20:02:45.935647Z","shell.execute_reply":"2024-12-18T20:02:46.469159Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color:#F8F1E8; font-family:newtimeroman;color:#602F44; font-size:150%; text-align:center; border-radius: 15px 50px;\"> Model ⚖️</p>\n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#484366 solid; padding: 15px; background-color: #FFEBCC; font-size:100%; text-align:left\">\n\n<h3 align=\"left\"><font color='#484366'>💬 Comment</font></h3>\n\nI want to give credit to @raghaw for this neural network structure\nyou can read his notebook [here](https://www.kaggle.com/code/raghaw/mnist-cnn-external-data-perfect-score)","metadata":{}},{"cell_type":"code","source":"model = models.Sequential([\n    \n    layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)),\n    layers.Conv2D(filters=64, kernel_size=3, padding='same',activation='relu'),\n    layers.Conv2D(filters=128, kernel_size=3, padding='same',activation='relu'),\n    layers.MaxPool2D(pool_size=2),\n\n    layers.Conv2D(filters=128, kernel_size=3, padding='same',activation='relu'),\n    layers.Conv2D(filters=192, kernel_size=3, padding='same',activation='relu'),\n    layers.MaxPool2D(pool_size=2),\n    \n    layers.Conv2D(filters=192, kernel_size=5, padding='same',activation='relu'),\n    layers.MaxPool2D(pool_size=2, padding='same'),\n    \n    layers.Flatten(),\n\n    layers.Dense(units=256, activation='relu'),\n    layers.Dense(units=10, activation='softmax'),\n])\n    ","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:46.471499Z","iopub.execute_input":"2024-12-18T20:02:46.471841Z","iopub.status.idle":"2024-12-18T20:02:47.742450Z","shell.execute_reply.started":"2024-12-18T20:02:46.471810Z","shell.execute_reply":"2024-12-18T20:02:47.741710Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:47.743719Z","iopub.execute_input":"2024-12-18T20:02:47.743973Z","iopub.status.idle":"2024-12-18T20:02:47.776910Z","shell.execute_reply.started":"2024-12-18T20:02:47.743952Z","shell.execute_reply":"2024-12-18T20:02:47.776031Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\nreduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1,\n                              patience=2, min_lr=0.00000001)\n\nhistory = model.fit(\n    X_train, y_train_encoded, \n    epochs=25,\n    validation_split=0.1, \n    callbacks=[reduce_lr],\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:02:47.778018Z","iopub.execute_input":"2024-12-18T20:02:47.778313Z","iopub.status.idle":"2024-12-18T20:06:57.414015Z","shell.execute_reply.started":"2024-12-18T20:02:47.778290Z","shell.execute_reply":"2024-12-18T20:06:57.413336Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[: , ['loss', 'val_loss']].plot()\nhistory_frame.loc[: , ['accuracy', 'val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:06:57.415652Z","iopub.execute_input":"2024-12-18T20:06:57.416505Z","iopub.status.idle":"2024-12-18T20:06:57.800378Z","shell.execute_reply.started":"2024-12-18T20:06:57.416477Z","shell.execute_reply":"2024-12-18T20:06:57.799478Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions for competition submission\npredictions = model.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:06:57.801527Z","iopub.execute_input":"2024-12-18T20:06:57.801755Z","iopub.status.idle":"2024-12-18T20:07:00.961732Z","shell.execute_reply.started":"2024-12-18T20:06:57.801736Z","shell.execute_reply":"2024-12-18T20:07:00.960706Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:07:00.963037Z","iopub.execute_input":"2024-12-18T20:07:00.963360Z","iopub.status.idle":"2024-12-18T20:07:00.970233Z","shell.execute_reply.started":"2024-12-18T20:07:00.963334Z","shell.execute_reply":"2024-12-18T20:07:00.969178Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert the model's probability outputs to digit predictions\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create a DataFrame for the submission\nsubmission = pd.DataFrame({\n    \"ImageId\": range(1, len(predicted_labels) + 1),  # ImageId starts from 1\n    \"Label\": predicted_labels\n})\n\n# Save the DataFrame to a CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:24:33.200729Z","iopub.execute_input":"2024-12-18T20:24:33.201620Z","iopub.status.idle":"2024-12-18T20:24:33.236967Z","shell.execute_reply.started":"2024-12-18T20:24:33.201584Z","shell.execute_reply":"2024-12-18T20:24:33.236046Z"},"trusted":true},"outputs":[],"execution_count":null}]}