{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Essential Libraries and Tools","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import LearningRateScheduler","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-04T20:32:40.445155Z","iopub.execute_input":"2022-07-04T20:32:40.446301Z","iopub.status.idle":"2022-07-04T20:32:40.454327Z","shell.execute_reply.started":"2022-07-04T20:32:40.446252Z","shell.execute_reply":"2022-07-04T20:32:40.452714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:40.636580Z","iopub.execute_input":"2022-07-04T20:32:40.637024Z","iopub.status.idle":"2022-07-04T20:32:43.305095Z","shell.execute_reply.started":"2022-07-04T20:32:40.636992Z","shell.execute_reply":"2022-07-04T20:32:43.303853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:43.307136Z","iopub.execute_input":"2022-07-04T20:32:43.307575Z","iopub.status.idle":"2022-07-04T20:32:43.326395Z","shell.execute_reply.started":"2022-07-04T20:32:43.307540Z","shell.execute_reply":"2022-07-04T20:32:43.325491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Data shape:\", (df.shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:43.327753Z","iopub.execute_input":"2022-07-04T20:32:43.328310Z","iopub.status.idle":"2022-07-04T20:32:43.338702Z","shell.execute_reply.started":"2022-07-04T20:32:43.328256Z","shell.execute_reply":"2022-07-04T20:32:43.337162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:43.343456Z","iopub.execute_input":"2022-07-04T20:32:43.343888Z","iopub.status.idle":"2022-07-04T20:32:45.682282Z","shell.execute_reply.started":"2022-07-04T20:32:43.343853Z","shell.execute_reply":"2022-07-04T20:32:45.680986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"There are {(df.isna().sum() > 0).sum()} missing values in Train set\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:45.683549Z","iopub.execute_input":"2022-07-04T20:32:45.683916Z","iopub.status.idle":"2022-07-04T20:32:45.745006Z","shell.execute_reply.started":"2022-07-04T20:32:45.683882Z","shell.execute_reply":"2022-07-04T20:32:45.743688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# X-y Split","metadata":{}},{"cell_type":"code","source":"X = df.drop(\"label\", axis=1)\ny = df[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:45.746498Z","iopub.execute_input":"2022-07-04T20:32:45.746879Z","iopub.status.idle":"2022-07-04T20:32:45.883170Z","shell.execute_reply.started":"2022-07-04T20:32:45.746846Z","shell.execute_reply":"2022-07-04T20:32:45.881760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalization","metadata":{}},{"cell_type":"code","source":"X = X / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:45.884553Z","iopub.execute_input":"2022-07-04T20:32:45.885771Z","iopub.status.idle":"2022-07-04T20:32:46.080081Z","shell.execute_reply.started":"2022-07-04T20:32:45.885726Z","shell.execute_reply":"2022-07-04T20:32:46.078929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reshaping","metadata":{}},{"cell_type":"code","source":"X = X.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:46.081447Z","iopub.execute_input":"2022-07-04T20:32:46.081838Z","iopub.status.idle":"2022-07-04T20:32:46.088463Z","shell.execute_reply.started":"2022-07-04T20:32:46.081795Z","shell.execute_reply":"2022-07-04T20:32:46.087048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Plotting a few Images</h3>","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 3, figsize=(12, 6))\nax[0].imshow(X[10], cmap=\"gray\")\nax[1].imshow(X[100], cmap=\"gray\")\nax[2].imshow(X[1000], cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:46.089533Z","iopub.execute_input":"2022-07-04T20:32:46.089887Z","iopub.status.idle":"2022-07-04T20:32:46.466510Z","shell.execute_reply.started":"2022-07-04T20:32:46.089856Z","shell.execute_reply":"2022-07-04T20:32:46.465204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One-Hot Encoding","metadata":{}},{"cell_type":"code","source":"y = to_categorical(y, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:46.470553Z","iopub.execute_input":"2022-07-04T20:32:46.470983Z","iopub.status.idle":"2022-07-04T20:32:46.477294Z","shell.execute_reply.started":"2022-07-04T20:32:46.470946Z","shell.execute_reply":"2022-07-04T20:32:46.475844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train-Validation Split","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:46.479263Z","iopub.execute_input":"2022-07-04T20:32:46.479633Z","iopub.status.idle":"2022-07-04T20:32:47.131911Z","shell.execute_reply.started":"2022-07-04T20:32:46.479601Z","shell.execute_reply":"2022-07-04T20:32:47.130692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X-Train shape:\", X_train.shape)\nprint(\"X-Validation shape:\", X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.133457Z","iopub.execute_input":"2022-07-04T20:32:47.134183Z","iopub.status.idle":"2022-07-04T20:32:47.139976Z","shell.execute_reply.started":"2022-07-04T20:32:47.134141Z","shell.execute_reply":"2022-07-04T20:32:47.138737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32, kernel_size=(5, 5), activation=\"relu\", input_shape=(28, 28, 1)),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(2, 2)),\n    Dropout(0.3),\n    \n    Conv2D(filters=32, kernel_size=(3, 3), activation=\"relu\", input_shape=(28, 28, 1)),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(2, 2)),\n    Dropout(0.3),\n    \n    Flatten(),\n    Dense(256, activation=\"relu\"),\n    Dropout(0.3),\n    Dense(10, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.141819Z","iopub.execute_input":"2022-07-04T20:32:47.142846Z","iopub.status.idle":"2022-07-04T20:32:47.237839Z","shell.execute_reply.started":"2022-07-04T20:32:47.142796Z","shell.execute_reply":"2022-07-04T20:32:47.236508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.239780Z","iopub.execute_input":"2022-07-04T20:32:47.240603Z","iopub.status.idle":"2022-07-04T20:32:47.252424Z","shell.execute_reply.started":"2022-07-04T20:32:47.240563Z","shell.execute_reply":"2022-07-04T20:32:47.251074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 * x)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.254175Z","iopub.execute_input":"2022-07-04T20:32:47.254788Z","iopub.status.idle":"2022-07-04T20:32:47.265808Z","shell.execute_reply.started":"2022-07-04T20:32:47.254750Z","shell.execute_reply":"2022-07-04T20:32:47.264768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.267449Z","iopub.execute_input":"2022-07-04T20:32:47.267980Z","iopub.status.idle":"2022-07-04T20:32:47.280171Z","shell.execute_reply.started":"2022-07-04T20:32:47.267933Z","shell.execute_reply":"2022-07-04T20:32:47.278738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 15\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.282189Z","iopub.execute_input":"2022-07-04T20:32:47.282822Z","iopub.status.idle":"2022-07-04T20:32:47.297136Z","shell.execute_reply.started":"2022-07-04T20:32:47.282769Z","shell.execute_reply":"2022-07-04T20:32:47.295938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=BATCH_SIZE), \n                              epochs=EPOCHS,\n                              validation_data=(X_val, y_val),\n                              callbacks=[annealer],\n                              steps_per_epoch=X_train.shape[0]//BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:47.298564Z","iopub.execute_input":"2022-07-04T20:32:47.299430Z","iopub.status.idle":"2022-07-04T20:32:53.710055Z","shell.execute_reply.started":"2022-07-04T20:32:47.299387Z","shell.execute_reply":"2022-07-04T20:32:53.707638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_loss, final_accuracy = model.evaluate(X_val, y_val, verbose=0)\nprint(\"Final Loss:\", final_loss)\nprint(\"Final Accuracy:\", final_accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:53.711233Z","iopub.status.idle":"2022-07-04T20:32:53.711718Z","shell.execute_reply.started":"2022-07-04T20:32:53.711491Z","shell.execute_reply":"2022-07-04T20:32:53.711514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 1, figsize=(10, 8))\n\nax[0].plot(history.history[\"loss\"], label=\"Train\")\nax[0].plot(history.history[\"val_loss\"], label=\"Validation\")\nax[0].title.set_text(\"Loss\")\nax[0].legend()\n\nax[1].plot(history.history[\"accuracy\"], label=\"Train\")\nax[1].plot(history.history[\"val_accuracy\"], label=\"Validation\")\nax[1].title.set_text(\"Accuracy\")\nax[1].legend(loc=\"lower right\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:53.713088Z","iopub.status.idle":"2022-07-04T20:32:53.713472Z","shell.execute_reply.started":"2022-07-04T20:32:53.713300Z","shell.execute_reply":"2022-07-04T20:32:53.713318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_val)\ny_hat = np.argmax(predictions, axis=1)\ny_true = np.argmax(y_val, axis=1)\n\nplt.figure(figsize=(10, 8))\nsns.heatmap(confusion_matrix(y_true, y_hat), annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.title(\"Confusion Matrix\", size=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:32:53.714744Z","iopub.status.idle":"2022-07-04T20:32:53.715186Z","shell.execute_reply.started":"2022-07-04T20:32:53.714994Z","shell.execute_reply":"2022-07-04T20:32:53.715017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# This work is in progress 🚧. If you liked the notebook so far, please do not forget to upvote.","metadata":{}}]}