{"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"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten, Input, BatchNormalization\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\n\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.utils import to_categorical\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:18.755692Z","iopub.execute_input":"2024-12-22T02:41:18.756064Z","iopub.status.idle":"2024-12-22T02:41:18.763786Z","shell.execute_reply.started":"2024-12-22T02:41:18.756036Z","shell.execute_reply":"2024-12-22T02:41:18.762631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the data\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:20.041046Z","iopub.execute_input":"2024-12-22T02:41:20.041418Z","iopub.status.idle":"2024-12-22T02:41:23.263239Z","shell.execute_reply.started":"2024-12-22T02:41:20.041385Z","shell.execute_reply":"2024-12-22T02:41:23.262374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.264413Z","iopub.execute_input":"2024-12-22T02:41:23.264722Z","iopub.status.idle":"2024-12-22T02:41:23.269658Z","shell.execute_reply.started":"2024-12-22T02:41:23.264697Z","shell.execute_reply":"2024-12-22T02:41:23.268776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.271201Z","iopub.execute_input":"2024-12-22T02:41:23.271461Z","iopub.status.idle":"2024-12-22T02:41:23.276641Z","shell.execute_reply.started":"2024-12-22T02:41:23.271437Z","shell.execute_reply":"2024-12-22T02:41:23.275438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.278085Z","iopub.execute_input":"2024-12-22T02:41:23.278315Z","iopub.status.idle":"2024-12-22T02:41:23.292306Z","shell.execute_reply.started":"2024-12-22T02:41:23.278294Z","shell.execute_reply":"2024-12-22T02:41:23.291205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# No null values\ntrain.isnull().sum(axis=1).sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.293331Z","iopub.execute_input":"2024-12-22T02:41:23.293710Z","iopub.status.idle":"2024-12-22T02:41:23.336278Z","shell.execute_reply.started":"2024-12-22T02:41:23.293681Z","shell.execute_reply":"2024-12-22T02:41:23.335180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = train[\"label\"]\nX_train = train.drop([\"label\"], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.337205Z","iopub.execute_input":"2024-12-22T02:41:23.337517Z","iopub.status.idle":"2024-12-22T02:41:23.437147Z","shell.execute_reply.started":"2024-12-22T02:41:23.337491Z","shell.execute_reply":"2024-12-22T02:41:23.436025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4d - (numbers, height, width, color)\n#      (10000, 28, 28, 0 (colors))\n\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.438136Z","iopub.execute_input":"2024-12-22T02:41:23.438436Z","iopub.status.idle":"2024-12-22T02:41:23.442931Z","shell.execute_reply.started":"2024-12-22T02:41:23.438402Z","shell.execute_reply":"2024-12-22T02:41:23.441897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_digit = X_train[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.445613Z","iopub.execute_input":"2024-12-22T02:41:23.445917Z","iopub.status.idle":"2024-12-22T02:41:23.450736Z","shell.execute_reply.started":"2024-12-22T02:41:23.445889Z","shell.execute_reply":"2024-12-22T02:41:23.449795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(first_digit, cmap='gray_r')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.451963Z","iopub.execute_input":"2024-12-22T02:41:23.452330Z","iopub.status.idle":"2024-12-22T02:41:23.673473Z","shell.execute_reply.started":"2024-12-22T02:41:23.452293Z","shell.execute_reply":"2024-12-22T02:41:23.672452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**HERE IS THE MATH BEHIND -** https://learning.oreilly.com/course/the-essential-machine/9780137903245/\n","metadata":{}},{"cell_type":"code","source":"# change a numerical to one-hot encoding.\n# to_categorical(class, total_classes)\n\n# best practice for multiclassification because \n# softmax gives probability distribution\n# class entropy \n\nto_categorical(5, 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:23.674500Z","iopub.execute_input":"2024-12-22T02:41:23.674916Z","iopub.status.idle":"2024-12-22T02:41:23.680432Z","shell.execute_reply.started":"2024-12-22T02:41:23.674878Z","shell.execute_reply":"2024-12-22T02:41:23.679616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = to_categorical(y_train, 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:25.661688Z","iopub.execute_input":"2024-12-22T02:41:25.662039Z","iopub.status.idle":"2024-12-22T02:41:25.666734Z","shell.execute_reply.started":"2024-12-22T02:41:25.662012Z","shell.execute_reply":"2024-12-22T02:41:25.665914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# normalize\nX_train = X_train/255.0\ntest = test / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:26.214159Z","iopub.execute_input":"2024-12-22T02:41:26.214478Z","iopub.status.idle":"2024-12-22T02:41:26.401167Z","shell.execute_reply.started":"2024-12-22T02:41:26.214452Z","shell.execute_reply":"2024-12-22T02:41:26.400197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"random_seed = 42","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:26.752019Z","iopub.execute_input":"2024-12-22T02:41:26.752345Z","iopub.status.idle":"2024-12-22T02:41:26.756036Z","shell.execute_reply.started":"2024-12-22T02:41:26.752319Z","shell.execute_reply":"2024-12-22T02:41:26.755068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=random_seed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:30.464190Z","iopub.execute_input":"2024-12-22T02:41:30.464525Z","iopub.status.idle":"2024-12-22T02:41:30.879979Z","shell.execute_reply.started":"2024-12-22T02:41:30.464497Z","shell.execute_reply":"2024-12-22T02:41:30.878728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Define the model\nmodel = Sequential()\n\n# Input Layer\nmodel.add(Input(shape=(28, 28, 1)))\n\n# Convolutional Block 1\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same'))\nmodel.add(BatchNormalization())  # Helps in faster convergence and stability\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))  # Prevents overfitting\n\n# Convolutional Block 2\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n# Flatten\nmodel.add(Flatten())\n\n# Fully Connected Layer (Dense)\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\n# Output Layer\nmodel.add(Dense(10, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer= Adam(learning_rate=0.001),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nprint(model.summary())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:31.597463Z","iopub.execute_input":"2024-12-22T02:41:31.597862Z","iopub.status.idle":"2024-12-22T02:41:31.773973Z","shell.execute_reply.started":"2024-12-22T02:41:31.597829Z","shell.execute_reply":"2024-12-22T02:41:31.772752Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Image Generator metrics\nhttps://www.kaggle.com/code/yassineghouzam/introduction-to-cnn-keras-0-997-top-6?scriptVersionId=1437220&cellId=40","metadata":{}},{"cell_type":"code","source":"epochs = 2 # Turn epochs to 30 to get 0.9967 accuracy\nbatch_size = 64","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:41:52.257719Z","iopub.execute_input":"2024-12-22T02:41:52.258093Z","iopub.status.idle":"2024-12-22T02:41:52.261887Z","shell.execute_reply.started":"2024-12-22T02:41:52.258061Z","shell.execute_reply":"2024-12-22T02:41:52.260816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size = batch_size, epochs = epochs, validation_data = (X_val, y_val), verbose = 2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:42:15.149257Z","iopub.execute_input":"2024-12-22T02:42:15.149650Z","iopub.status.idle":"2024-12-22T02:43:19.259831Z","shell.execute_reply.started":"2024-12-22T02:42:15.149609Z","shell.execute_reply":"2024-12-22T02:43:19.258890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:43:35.777436Z","iopub.execute_input":"2024-12-22T02:43:35.777822Z","iopub.status.idle":"2024-12-22T02:43:37.051537Z","shell.execute_reply.started":"2024-12-22T02:43:35.777791Z","shell.execute_reply":"2024-12-22T02:43:37.050664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_one_hot = np.zeros_like(y_pred, dtype=int)\ny_pred_one_hot[np.arange(len(y_pred)), np.argmax(y_pred, axis=1)] = 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:43:37.640744Z","iopub.execute_input":"2024-12-22T02:43:37.641085Z","iopub.status.idle":"2024-12-22T02:43:37.645629Z","shell.execute_reply.started":"2024-12-22T02:43:37.641056Z","shell.execute_reply":"2024-12-22T02:43:37.644693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_pred_one_hot, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:43:38.071473Z","iopub.execute_input":"2024-12-22T02:43:38.071881Z","iopub.status.idle":"2024-12-22T02:43:38.090161Z","shell.execute_reply.started":"2024-12-22T02:43:38.071850Z","shell.execute_reply":"2024-12-22T02:43:38.089286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:44:04.680929Z","iopub.execute_input":"2024-12-22T02:44:04.681460Z","iopub.status.idle":"2024-12-22T02:44:11.631729Z","shell.execute_reply.started":"2024-12-22T02:44:04.681409Z","shell.execute_reply":"2024-12-22T02:44:11.630661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"sample_submission.csv\",index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T02:46:59.141525Z","iopub.execute_input":"2024-12-22T02:46:59.141977Z","iopub.status.idle":"2024-12-22T02:46:59.171114Z","shell.execute_reply.started":"2024-12-22T02:46:59.141946Z","shell.execute_reply":"2024-12-22T02:46:59.170078Z"}},"outputs":[],"execution_count":null}]}