{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-24T11:27:37.020082Z","iopub.execute_input":"2023-05-24T11:27:37.020482Z","iopub.status.idle":"2023-05-24T11:27:37.029825Z","shell.execute_reply.started":"2023-05-24T11:27:37.020449Z","shell.execute_reply":"2023-05-24T11:27:37.028538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:27:41.552714Z","iopub.execute_input":"2023-05-24T11:27:41.553199Z","iopub.status.idle":"2023-05-24T11:27:49.951970Z","shell.execute_reply.started":"2023-05-24T11:27:41.553162Z","shell.execute_reply":"2023-05-24T11:27:49.950670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load the data\ndata = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:27:51.689796Z","iopub.execute_input":"2023-05-24T11:27:51.690851Z","iopub.status.idle":"2023-05-24T11:27:55.996725Z","shell.execute_reply.started":"2023-05-24T11:27:51.690813Z","shell.execute_reply":"2023-05-24T11:27:55.995616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[\"label\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:27:55.998424Z","iopub.execute_input":"2023-05-24T11:27:56.000558Z","iopub.status.idle":"2023-05-24T11:27:56.016130Z","shell.execute_reply.started":"2023-05-24T11:27:56.000523Z","shell.execute_reply":"2023-05-24T11:27:56.014585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = data.drop(\"label\", axis = 1)\ny = data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:27:56.017716Z","iopub.execute_input":"2023-05-24T11:27:56.018141Z","iopub.status.idle":"2023-05-24T11:27:56.135980Z","shell.execute_reply.started":"2023-05-24T11:27:56.018100Z","shell.execute_reply":"2023-05-24T11:27:56.134717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import RandomOverSampler\n\noversample = RandomOverSampler(sampling_strategy='minority')\nxover, yover = oversample.fit_resample(x, y)\nlen(xover), len(yover)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:27:57.676004Z","iopub.execute_input":"2023-05-24T11:27:57.678712Z","iopub.status.idle":"2023-05-24T11:27:59.402331Z","shell.execute_reply.started":"2023-05-24T11:27:57.678669Z","shell.execute_reply":"2023-05-24T11:27:59.401220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DEFINING A FUNCTION TO PREPROCESS THE DATA\n# CONVERTING THEM TO SHAPE(NUMBER,PIXEL,PIXEL)\ndef preprocess(x, y):\n    digits = []\n    labels = []\n    for i in tqdm(range(0,len(x))):\n        # TAKING ONE DIGIT OUT OF THE DATAFRAME\n        digit = np.array(x.iloc[i:i+1,:])\n        # RESHAPING TO MAKE AN IMAGE OF 28 X 28 PIXELS\n        digit = digit.reshape(28,28)\n        # EXTRACTING THE LABELS \n        label = y[i]        \n        digits.append(digit)\n        labels.append(label)\n# MAKING AN ARRAY OF DATA\n    digits = np.array(digits)\n    labels = np.array(labels)\n    return digits,labels","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:28:19.762392Z","iopub.execute_input":"2023-05-24T11:28:19.762821Z","iopub.status.idle":"2023-05-24T11:28:19.770734Z","shell.execute_reply.started":"2023-05-24T11:28:19.762784Z","shell.execute_reply":"2023-05-24T11:28:19.769304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digits,labels = preprocess(xover, yover)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T06:58:17.785770Z","iopub.execute_input":"2023-05-24T06:58:17.786181Z","iopub.status.idle":"2023-05-24T07:07:47.165837Z","shell.execute_reply.started":"2023-05-24T06:58:17.786148Z","shell.execute_reply":"2023-05-24T07:07:47.164741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digits.shape, labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:07:47.167712Z","iopub.execute_input":"2023-05-24T07:07:47.168079Z","iopub.status.idle":"2023-05-24T07:07:47.174298Z","shell.execute_reply.started":"2023-05-24T07:07:47.168047Z","shell.execute_reply":"2023-05-24T07:07:47.173479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SEEING OUR AUGMENTED DATA\nimport random\nfig,ax = plt.subplots(5,5,figsize=(10,10))\nfor i in range(0,5):\n    for j in range(0,5):\n        n = random.randint(0,len(digits))\n        ax[i][j].imshow(digits[n])\n        ax[i][j].set_xticks([])\n        ax[i][j].set_yticks([])\n        ax[i][j].set_title(f\"{labels[n]}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:07:47.175867Z","iopub.execute_input":"2023-05-24T07:07:47.176193Z","iopub.status.idle":"2023-05-24T07:07:48.792200Z","shell.execute_reply.started":"2023-05-24T07:07:47.176165Z","shell.execute_reply":"2023-05-24T07:07:48.790958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\n\ndef cnn_mnist_model():\n    cnn_mnist_model = tf.keras.Sequential(\n        [\n            layers.InputLayer(input_shape=(28, 28, 1)),\n            # BLOCK 1\n            layers.Conv2D(filters=32, kernel_size=3, strides=1, padding=\"valid\", use_bias=False, kernel_initializer=\"he_normal\"),\n            layers.BatchNormalization(),\n            layers.Activation(\"relu\"),\n            layers.Conv2D(filters=32, kernel_size=3, strides=1, padding=\"valid\", use_bias=False, kernel_initializer=\"he_normal\"),\n            layers.BatchNormalization(),\n            layers.Activation(\"relu\"),\n            layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),\n            layers.BatchNormalization(),\n            layers.Dropout(0.4),\n            # BLOCK 2\n            layers.Conv2D(filters=64, kernel_size=3, strides=1, padding=\"valid\", use_bias=False, kernel_initializer=\"he_normal\",),\n            layers.BatchNormalization(),\n            layers.Activation(\"relu\"),\n            layers.Conv2D(filters=64, kernel_size=3, strides=1, padding=\"valid\", use_bias=False, kernel_initializer=\"he_normal\",),\n            layers.BatchNormalization(),\n            layers.Activation(\"relu\"),\n            layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),\n            layers.BatchNormalization(),\n            layers.Dropout(0.4),\n            layers.Flatten(),\n            layers.Dense(128, use_bias=False, kernel_initializer=\"he_normal\"),\n            layers.BatchNormalization(),\n            layers.Activation(\"relu\"),\n            layers.Dropout(0.5),\n            layers.Dense(10, activation=\"softmax\"),\n        ]\n    )\n\n    cnn_mnist_model.compile(\n        loss=\"sparse_categorical_crossentropy\",\n        optimizer=\"nadam\",\n        metrics=[\"accuracy\"],\n    )\n    return cnn_mnist_model\ncnn_mnist_model = cnn_mnist_model()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:07:48.794686Z","iopub.execute_input":"2023-05-24T07:07:48.795543Z","iopub.status.idle":"2023-05-24T07:07:49.240070Z","shell.execute_reply.started":"2023-05-24T07:07:48.795505Z","shell.execute_reply":"2023-05-24T07:07:49.238662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpointer = tf.keras.callbacks.ModelCheckpoint('/kaggle/working/MNISTWeights.h5', verbose=1, save_best_only=True)\n\ncnn_mnist_model.fit(digits, labels,\n          epochs = 30,\n          validation_split=0.15,\n          batch_size=32,\n         callbacks = checkpointer)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:07:49.241682Z","iopub.execute_input":"2023-05-24T07:07:49.242128Z","iopub.status.idle":"2023-05-24T07:44:04.843245Z","shell.execute_reply.started":"2023-05-24T07:07:49.242085Z","shell.execute_reply":"2023-05-24T07:44:04.842362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds = cnn_mnist_model.predict(digits[:20000])\n# TAKING MAXIMUM PROBABILITIES TO MAKE OUR PREDICTIONS\npreds=[]\nfor i in range(0,len(ypreds)):\n    preds.append(np.argmax(ypreds[i]))\n\n# MAKING A DATAFRAME TO ACCES TRUE LABELS AND PREDICTED LABELS WITH EASE\ndf = pd.DataFrame({\"TRUE\":labels[:20000],\"PREDS\":preds})\ndf","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:45:35.400677Z","iopub.execute_input":"2023-05-24T07:45:35.401132Z","iopub.status.idle":"2023-05-24T07:45:44.236265Z","shell.execute_reply.started":"2023-05-24T07:45:35.401093Z","shell.execute_reply":"2023-05-24T07:45:44.235190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:45:50.201610Z","iopub.execute_input":"2023-05-24T07:45:50.202357Z","iopub.status.idle":"2023-05-24T07:45:52.845618Z","shell.execute_reply.started":"2023-05-24T07:45:50.202308Z","shell.execute_reply":"2023-05-24T07:45:52.844498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_test(df):\n    digits = []\n    for i in tqdm(range(0,len(df))):\n        digit = np.array(df.iloc[i:i+1,:])\n        digit = digit.reshape(28,28)\n        digits.append(digit)\n\n    digits = np.array(digits)\n    return digits\n\ntest_digits = preprocess_test(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:46:10.870521Z","iopub.execute_input":"2023-05-24T07:46:10.870941Z","iopub.status.idle":"2023-05-24T07:46:13.292430Z","shell.execute_reply.started":"2023-05-24T07:46:10.870905Z","shell.execute_reply":"2023-05-24T07:46:13.291046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_digits.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T07:46:16.076704Z","iopub.execute_input":"2023-05-24T07:46:16.077104Z","iopub.status.idle":"2023-05-24T07:46:16.084145Z","shell.execute_reply.started":"2023-05-24T07:46:16.077070Z","shell.execute_reply":"2023-05-24T07:46:16.082966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypreds_test = cnn_mnist_model.predict(test_digits)\ntest_preds=[]\nfor i in tqdm(range(0,len(ypreds_test))):\n    test_preds.append(np.argmax(ypreds_test[i]))    \npred_df = pd.DataFrame({\"ImageId\":test_df.index+1, \"Label\":test_preds})\npred_df","metadata":{"execution":{"iopub.status.busy":"2023-05-24T08:13:46.961143Z","iopub.execute_input":"2023-05-24T08:13:46.961583Z","iopub.status.idle":"2023-05-24T08:14:07.993862Z","shell.execute_reply.started":"2023-05-24T08:13:46.961548Z","shell.execute_reply":"2023-05-24T08:14:07.992671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T08:14:16.452463Z","iopub.execute_input":"2023-05-24T08:14:16.453259Z","iopub.status.idle":"2023-05-24T08:14:16.518691Z","shell.execute_reply.started":"2023-05-24T08:14:16.453212Z","shell.execute_reply":"2023-05-24T08:14:16.517482Z"},"trusted":true},"execution_count":null,"outputs":[]}]}