{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":3107,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2320}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2023-12-03T16:14:48.217945Z","iopub.execute_input":"2023-12-03T16:14:48.218335Z","iopub.status.idle":"2023-12-03T16:15:02.695714Z","shell.execute_reply.started":"2023-12-03T16:14:48.218305Z","shell.execute_reply":"2023-12-03T16:15:02.694657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T16:15:02.701493Z","iopub.execute_input":"2023-12-03T16:15:02.703613Z","iopub.status.idle":"2023-12-03T16:15:09.749296Z","shell.execute_reply.started":"2023-12-03T16:15:02.703565Z","shell.execute_reply":"2023-12-03T16:15:09.748275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:15:09.750545Z","iopub.execute_input":"2023-12-03T16:15:09.750914Z","iopub.status.idle":"2023-12-03T16:15:09.785915Z","shell.execute_reply.started":"2023-12-03T16:15:09.750880Z","shell.execute_reply":"2023-12-03T16:15:09.784798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:15:13.842391Z","iopub.execute_input":"2023-12-03T16:15:13.842802Z","iopub.status.idle":"2023-12-03T16:15:13.880617Z","shell.execute_reply.started":"2023-12-03T16:15:13.842771Z","shell.execute_reply":"2023-12-03T16:15:13.879454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T16:15:17.774663Z","iopub.execute_input":"2023-12-03T16:15:17.775111Z","iopub.status.idle":"2023-12-03T16:15:18.131911Z","shell.execute_reply.started":"2023-12-03T16:15:17.775075Z","shell.execute_reply":"2023-12-03T16:15:18.130630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_encoded = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:15:20.990819Z","iopub.execute_input":"2023-12-03T16:15:20.991256Z","iopub.status.idle":"2023-12-03T16:15:20.996685Z","shell.execute_reply.started":"2023-12-03T16:15:20.991224Z","shell.execute_reply":"2023-12-03T16:15:20.995472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T16:15:27.773720Z","iopub.execute_input":"2023-12-03T16:15:27.774138Z","iopub.status.idle":"2023-12-03T16:15:28.741596Z","shell.execute_reply.started":"2023-12-03T16:15:27.774107Z","shell.execute_reply":"2023-12-03T16:15:28.740278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T16:15:32.705429Z","iopub.execute_input":"2023-12-03T16:15:32.706691Z","iopub.status.idle":"2023-12-03T16:15:33.062362Z","shell.execute_reply.started":"2023-12-03T16:15:32.706637Z","shell.execute_reply":"2023-12-03T16:15:33.060457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:15:36.792987Z","iopub.execute_input":"2023-12-03T16:15:36.793400Z","iopub.status.idle":"2023-12-03T16:15:36.834255Z","shell.execute_reply.started":"2023-12-03T16:15:36.793369Z","shell.execute_reply":"2023-12-03T16:15:36.833143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T16:15:41.255662Z","iopub.execute_input":"2023-12-03T16:15:41.256091Z","iopub.status.idle":"2023-12-03T20:03:05.427198Z","shell.execute_reply.started":"2023-12-03T16:15:41.256059Z","shell.execute_reply":"2023-12-03T20:03:05.425297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T20:03:05.430106Z","iopub.execute_input":"2023-12-03T20:03:05.430535Z","iopub.status.idle":"2023-12-03T20:03:06.134157Z","shell.execute_reply.started":"2023-12-03T20:03:05.430501Z","shell.execute_reply":"2023-12-03T20:03:06.133119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions for competition submission\npredictions = model.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-03T20:03:06.135862Z","iopub.execute_input":"2023-12-03T20:03:06.137037Z","iopub.status.idle":"2023-12-03T20:05:28.588130Z","shell.execute_reply.started":"2023-12-03T20:03:06.136991Z","shell.execute_reply":"2023-12-03T20:05:28.586912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2023-12-03T20:05:28.590685Z","iopub.execute_input":"2023-12-03T20:05:28.591079Z","iopub.status.idle":"2023-12-03T20:05:28.600877Z","shell.execute_reply.started":"2023-12-03T20:05:28.591047Z","shell.execute_reply":"2023-12-03T20:05:28.599758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-12-03T20:15:32.070228Z","iopub.execute_input":"2023-12-03T20:15:32.071283Z","iopub.status.idle":"2023-12-03T20:15:32.153392Z","shell.execute_reply.started":"2023-12-03T20:15:32.071237Z","shell.execute_reply":"2023-12-03T20:15:32.152019Z"},"trusted":true},"execution_count":null,"outputs":[]}]}