{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Preparing Data\n\n<br>\n\n### reading and exploring data","metadata":{}},{"cell_type":"code","source":"import os\n\nfor dirname, _, filenames in os.walk('../input'):\n    for filename in  filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:16:45.111926Z","iopub.execute_input":"2023-09-09T02:16:45.112357Z","iopub.status.idle":"2023-09-09T02:16:45.121314Z","shell.execute_reply.started":"2023-09-09T02:16:45.112322Z","shell.execute_reply":"2023-09-09T02:16:45.120036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\nans = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\n\nprint(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:16:34.067111Z","iopub.execute_input":"2023-09-09T02:16:34.068120Z","iopub.status.idle":"2023-09-09T02:16:39.873641Z","shell.execute_reply.started":"2023-09-09T02:16:34.068078Z","shell.execute_reply":"2023-09-09T02:16:39.871642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(2)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-09-09T02:16:39.875894Z","iopub.execute_input":"2023-09-09T02:16:39.877046Z","iopub.status.idle":"2023-09-09T02:16:39.898532Z","shell.execute_reply.started":"2023-09-09T02:16:39.877005Z","shell.execute_reply":"2023-09-09T02:16:39.897447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = train.drop(columns = ['label']).values, train['label'].values\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:16:39.900245Z","iopub.execute_input":"2023-09-09T02:16:39.901138Z","iopub.status.idle":"2023-09-09T02:16:39.992415Z","shell.execute_reply.started":"2023-09-09T02:16:39.901105Z","shell.execute_reply":"2023-09-09T02:16:39.991252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test.values\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:16:39.995199Z","iopub.execute_input":"2023-09-09T02:16:39.995905Z","iopub.status.idle":"2023-09-09T02:16:40.007313Z","shell.execute_reply.started":"2023-09-09T02:16:39.995869Z","shell.execute_reply":"2023-09-09T02:16:40.006109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ploting a sample from MNIST dataset","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\nnp.random.seed(42)\n\n# drawing a random 36 indices\nindex = np.random.randint(0, 42000, size = 36)\n\n# selecting these images and reshaping it\nimgs = X[index].reshape(36, 28, 28, 1)\nimgs_label = y[index]\nprint(imgs.shape, imgs_label.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:28:03.090295Z","iopub.execute_input":"2023-09-09T00:28:03.091022Z","iopub.status.idle":"2023-09-09T00:28:03.098856Z","shell.execute_reply.started":"2023-09-09T00:28:03.090980Z","shell.execute_reply":"2023-09-09T00:28:03.097602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nplt.style.use('fivethirtyeight')\nplt.rc('font', size=10)\nplt.rc('legend', fontsize= 10)\nplt.rc('xtick', labelsize= 8)\nplt.rc('ytick', labelsize= 8)\n\nfig, ax = plt.subplots(6, 6, figsize = (12, 12))\n\nimg_idx = 0\nfor i in range(6):\n    for j in range(6):\n        ax[i][j].imshow(imgs[img_idx], cmap = 'gray')\n        ax[i][j].set_title(imgs_label[img_idx])\n        ax[i][j].axis('off')\n        img_idx += 1\n        \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:28:04.243413Z","iopub.execute_input":"2023-09-09T00:28:04.243779Z","iopub.status.idle":"2023-09-09T00:28:07.284907Z","shell.execute_reply.started":"2023-09-09T00:28:04.243748Z","shell.execute_reply":"2023-09-09T00:28:07.283846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### All classes are almost balanced","metadata":{}},{"cell_type":"code","source":"classes_per = (100*train['label'].value_counts(normalize = True).sort_index()).round(3)\npd.DataFrame(classes_per).T","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:28:25.560173Z","iopub.execute_input":"2023-09-09T00:28:25.560571Z","iopub.status.idle":"2023-09-09T00:28:25.584114Z","shell.execute_reply.started":"2023-09-09T00:28:25.560539Z","shell.execute_reply":"2023-09-09T00:28:25.583231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport seaborn as sns\n\nsns.countplot(x = 'label', data = train)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:28:26.342821Z","iopub.execute_input":"2023-09-09T00:28:26.343937Z","iopub.status.idle":"2023-09-09T00:28:27.147415Z","shell.execute_reply.started":"2023-09-09T00:28:26.343891Z","shell.execute_reply":"2023-09-09T00:28:27.146732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Making sure there is no missing values","metadata":{}},{"cell_type":"code","source":"assert train.isna().any().sum() == 0","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:28:43.254745Z","iopub.execute_input":"2023-09-09T00:28:43.255150Z","iopub.status.idle":"2023-09-09T00:28:43.270135Z","shell.execute_reply.started":"2023-09-09T00:28:43.255115Z","shell.execute_reply":"2023-09-09T00:28:43.269016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### One-Hot Encoding the train labels","metadata":{}},{"cell_type":"code","source":"from keras.utils import to_categorical\n\ntrain_labels = to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:29:42.332602Z","iopub.execute_input":"2023-09-09T00:29:42.333004Z","iopub.status.idle":"2023-09-09T00:29:45.257264Z","shell.execute_reply.started":"2023-09-09T00:29:42.332947Z","shell.execute_reply":"2023-09-09T00:29:45.256141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Normalizing and reshaping the train and test data","metadata":{}},{"cell_type":"code","source":"train_images = X/255\ntest_images  = X_test/255\n\ntrain_images = train_images.reshape(-1, 28, 28, 1)\ntest_images = test_images.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:29:56.052670Z","iopub.execute_input":"2023-09-09T00:29:56.053074Z","iopub.status.idle":"2023-09-09T00:29:56.223222Z","shell.execute_reply.started":"2023-09-09T00:29:56.053042Z","shell.execute_reply":"2023-09-09T00:29:56.222156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Spliting the data into train and validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size = 0.2, random_state = 42)\n\nprint(X_train.shape, y_train.shape)\nprint(X_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:29:58.928198Z","iopub.execute_input":"2023-09-09T00:29:58.928570Z","iopub.status.idle":"2023-09-09T00:29:59.365394Z","shell.execute_reply.started":"2023-09-09T00:29:58.928540Z","shell.execute_reply":"2023-09-09T00:29:59.364181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training densely connected layers","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\n\nnetwork = Sequential()\n\nnetwork.add(Dense(512, activation= \"relu\",  input_shape = (28 * 28, )))\nnetwork.add(Dense(512, activation= \"relu\",  input_shape = (28 * 28, )))\nnetwork.add(Dense(512, activation= \"relu\",  input_shape = (28 * 28, )))\n\nnetwork.add(Dense(10, activation = 'softmax'))\n\nnetwork.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:30:13.090450Z","iopub.execute_input":"2023-09-09T00:30:13.090856Z","iopub.status.idle":"2023-09-09T00:30:15.776614Z","shell.execute_reply.started":"2023-09-09T00:30:13.090822Z","shell.execute_reply":"2023-09-09T00:30:15.775854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy',\n                        metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:30:33.259157Z","iopub.execute_input":"2023-09-09T00:30:33.259533Z","iopub.status.idle":"2023-09-09T00:30:33.280348Z","shell.execute_reply.started":"2023-09-09T00:30:33.259501Z","shell.execute_reply":"2023-09-09T00:30:33.279189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = network.fit(X_train.reshape(-1, 28*28, ), y_train, epochs = 30,\n                    batch_size = 128,\n                    validation_data = (X_val.reshape(-1, 28*28, ), y_val))","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:30:37.929764Z","iopub.execute_input":"2023-09-09T00:30:37.930153Z","iopub.status.idle":"2023-09-09T00:31:19.464090Z","shell.execute_reply.started":"2023-09-09T00:30:37.930120Z","shell.execute_reply":"2023-09-09T00:31:19.463075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n    \n    fig, ax = plt.subplots(2, 1, figsize = (8, 6))\n\n    epochs = range(1, len(acc) + 1)\n    ax[0].plot(epochs, acc, 'b--', label='Training acc')\n    ax[0].plot(epochs, val_acc, 'r', label='Validation acc')\n    ax[0].set_title('Training and validation accuracy')\n    ax[0].legend()\n\n    ax[1].plot(epochs, loss, 'b--', label='Training loss')\n    ax[1].plot(epochs, val_loss, 'r', label='Validation loss')\n    ax[1].set_title('Training and validation loss')\n    ax[1].legend()\n    \n    plt.show()\n\nplot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:31:22.925766Z","iopub.execute_input":"2023-09-09T00:31:22.926517Z","iopub.status.idle":"2023-09-09T00:31:23.445843Z","shell.execute_reply.started":"2023-09-09T00:31:22.926474Z","shell.execute_reply":"2023-09-09T00:31:23.444933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The models scores mean $97.662$% on the validation set not the best\n<br>After the 4th epoch the model starts to overfit heavily as the train accuracy increases till it reaches $99.96$% <br> While the validation accuracy remains between $97$% and $98$%**","metadata":{}},{"cell_type":"code","source":"predict_x = network.predict(test_images.reshape(-1, 28*28)) \nclasses_x = np.argmax(predict_x, axis=1)\n\nsub0 = ans.copy()\nsub0['Label'] = classes_x\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:31:29.231462Z","iopub.execute_input":"2023-09-09T00:31:29.231841Z","iopub.status.idle":"2023-09-09T00:31:32.131023Z","shell.execute_reply.started":"2023-09-09T00:31:29.231808Z","shell.execute_reply":"2023-09-09T00:31:32.129919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0.to_csv('/kaggle/working/submission0.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:31:36.438711Z","iopub.execute_input":"2023-09-09T00:31:36.439120Z","iopub.status.idle":"2023-09-09T00:31:36.499406Z","shell.execute_reply.started":"2023-09-09T00:31:36.439086Z","shell.execute_reply":"2023-09-09T00:31:36.498491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**`'submission0.csv'`  scored $98.007$ % accuracy on the test-set**","metadata":{}},{"cell_type":"markdown","source":"# Training  a CNN","metadata":{}},{"cell_type":"code","source":"def build_model():\n    model = Sequential()\n\n    model.add(Conv2D(filters = 32, kernel_size = (5,5), padding = 'Same', \n                     activation ='relu', input_shape = (28,28,1)))\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),\n                     padding = 'Same', activation ='relu'))\n    model.add(MaxPool2D(pool_size=(2,2)))\n\n    \n    model.add(Conv2D(filters = 64, kernel_size = (3,3),\n                     padding = 'Same', activation ='relu'))\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),\n                     padding = 'Same', activation ='relu'))\n    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Flatten())\n\n    model.add(Dense(512, activation = \"relu\"))\n    model.add(Dropout(0.5))\n\n\n    model.add(Dense(10, activation = \"softmax\"))\n    \n    return model\n\nmodel0 = build_model()\nmodel0.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:23:00.129922Z","iopub.execute_input":"2023-09-09T01:23:00.130901Z","iopub.status.idle":"2023-09-09T01:23:00.244980Z","shell.execute_reply.started":"2023-09-09T01:23:00.130864Z","shell.execute_reply":"2023-09-09T01:23:00.244172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model0.compile(optimizer = 'rmsprop',\n                            loss = 'categorical_crossentropy',\n                            metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:42:13.304924Z","iopub.execute_input":"2023-09-09T00:42:13.305406Z","iopub.status.idle":"2023-09-09T00:42:13.321643Z","shell.execute_reply.started":"2023-09-09T00:42:13.305357Z","shell.execute_reply":"2023-09-09T00:42:13.320350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model0.fit(X_train, y_train, epochs = 30,\n                    batch_size = 128,\n                    validation_data = (X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:42:15.007577Z","iopub.execute_input":"2023-09-09T00:42:15.007976Z","iopub.status.idle":"2023-09-09T00:44:38.228749Z","shell.execute_reply.started":"2023-09-09T00:42:15.007927Z","shell.execute_reply":"2023-09-09T00:44:38.227552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:44:38.231232Z","iopub.execute_input":"2023-09-09T00:44:38.231628Z","iopub.status.idle":"2023-09-09T00:44:38.738389Z","shell.execute_reply.started":"2023-09-09T00:44:38.231590Z","shell.execute_reply":"2023-09-09T00:44:38.736879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The validation mean score improved a lot it reached $99.001$% accuracy.\n<br> however, the model still overfits after  $5-7$ epochs, the train accuracy $99.904$% <br> While the validation accuracy remains between $98$% and $99$%**","metadata":{}},{"cell_type":"code","source":"predict_x = model0.predict(test_images) \nclasses_x = np.argmax(predict_x, axis=1)\n\nsub1 = ans.copy()\nsub1['Label'] = classes_x\nsub1.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:48:42.720734Z","iopub.execute_input":"2023-09-09T00:48:42.721136Z","iopub.status.idle":"2023-09-09T00:48:45.531890Z","shell.execute_reply.started":"2023-09-09T00:48:42.721102Z","shell.execute_reply":"2023-09-09T00:48:45.530885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub1.to_csv('/kaggle/working/submission1.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:48:48.534363Z","iopub.execute_input":"2023-09-09T00:48:48.534751Z","iopub.status.idle":"2023-09-09T00:48:48.598292Z","shell.execute_reply.started":"2023-09-09T00:48:48.534720Z","shell.execute_reply":"2023-09-09T00:48:48.597350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**`'submission1.csv'`  scored $98.989$ % accuracy on the test-set**","metadata":{}},{"cell_type":"markdown","source":"# Data Augmentation to prevent Overfitting","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(\n        # set input mean to 0 over the dataset\n        featurewise_center = False,  \n    \n        # set each sample mean to 0\n        samplewise_center = False,  \n        \n        # divide inputs by std of the dataset\n        featurewise_std_normalization = False, \n    \n        # divide each input by its std\n        samplewise_std_normalization = False,  \n    \n        # apply ZCA whitening\n        zca_whitening=False,  \n    \n        # randomly rotate images in the range (degrees, 0 to 180)\n        rotation_range = 20,  \n    \n        # Randomly zoom image \n        zoom_range = 0.2, \n    \n        # randomly shift images horizontally (fraction of total width)\n        width_shift_range = 0.2,  \n    \n        # randomly shift images vertically (fraction of total height)\n        height_shift_range = 0.2,  \n    \n        # randomly flip images\n        horizontal_flip = False,  \n    \n        # randomly flip images\n        vertical_flip = False,\n    \n        # fill missing pixals method\n        fill_mode = 'nearest',\n)  \n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:57:05.736228Z","iopub.execute_input":"2023-09-09T00:57:05.736607Z","iopub.status.idle":"2023-09-09T00:57:05.830819Z","shell.execute_reply.started":"2023-09-09T00:57:05.736575Z","shell.execute_reply":"2023-09-09T00:57:05.829807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Looking at different augmentations of the same  sample image","metadata":{}},{"cell_type":"code","source":"np.random.seed(42)\nidx = np.random.randint(low = 0, high = X_train.shape[0], size = 1)\nx = X_train[idx]\n\nfig, ax = plt.subplots(3, 3, figsize = (9, 9))\ni, j = 0, 0\n\nfor batch in  datagen.flow(x, batch_size = 1):\n    ax[i][j].imshow(batch[0], cmap = 'gray')\n    ax[i][j].axis('off')\n    i += 1\n    if i == 3: i, j = 0, j+1\n    if j == 3: break","metadata":{"execution":{"iopub.status.busy":"2023-09-09T00:57:47.074504Z","iopub.execute_input":"2023-09-09T00:57:47.074881Z","iopub.status.idle":"2023-09-09T00:57:47.491812Z","shell.execute_reply.started":"2023-09-09T00:57:47.074850Z","shell.execute_reply":"2023-09-09T00:57:47.490850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final Model","metadata":{}},{"cell_type":"code","source":"model1 = build_model()\nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:23:07.197574Z","iopub.execute_input":"2023-09-09T01:23:07.197967Z","iopub.status.idle":"2023-09-09T01:23:07.302743Z","shell.execute_reply.started":"2023-09-09T01:23:07.197920Z","shell.execute_reply":"2023-09-09T01:23:07.302016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import RMSprop\n\noptimizer = RMSprop(learning_rate=0.001, rho=0.9, epsilon=1e-08)\n\nmodel1.compile(optimizer = optimizer,\n                            loss = 'categorical_crossentropy',\n                            metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:23:08.159096Z","iopub.execute_input":"2023-09-09T01:23:08.159463Z","iopub.status.idle":"2023-09-09T01:23:08.171861Z","shell.execute_reply.started":"2023-09-09T01:23:08.159433Z","shell.execute_reply":"2023-09-09T01:23:08.170913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:23:09.407924Z","iopub.execute_input":"2023-09-09T01:23:09.409254Z","iopub.status.idle":"2023-09-09T01:23:09.414751Z","shell.execute_reply.started":"2023-09-09T01:23:09.409208Z","shell.execute_reply":"2023-09-09T01:23:09.413774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50 \nbatch_size = 86\n\n# Fit the model\nhistory = model1.fit(datagen.flow(X_train, y_train, batch_size = batch_size),\n                              epochs = epochs, validation_data = (X_val, y_val),\n                              verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size\n                              , callbacks = [learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:23:09.980647Z","iopub.execute_input":"2023-09-09T01:23:09.981024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:17:10.765982Z","iopub.execute_input":"2023-09-09T02:17:10.766367Z","iopub.status.idle":"2023-09-09T02:17:11.277012Z","shell.execute_reply.started":"2023-09-09T02:17:10.766338Z","shell.execute_reply":"2023-09-09T02:17:11.275998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lookig at errors","metadata":{}},{"cell_type":"code","source":"val_predict = model1.predict(X_val)\nval_predict_labels = np.argmax(val_predict, axis = 1)\n\nval_true_labels = np.argmax(y_val, axis = 1)\nmsk = (val_predict_labels != val_true_labels)\n\nerrors = X_val[msk]\nerrors_tl = val_true_labels[msk]\nerrors_pl = val_predict_labels[msk]\n\nprint(errors.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:21:17.672868Z","iopub.execute_input":"2023-09-09T02:21:17.673800Z","iopub.status.idle":"2023-09-09T02:21:19.051825Z","shell.execute_reply.started":"2023-09-09T02:21:17.673761Z","shell.execute_reply":"2023-09-09T02:21:19.045021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\nindices = np.random.randint(0, errors.shape[0], size = 25)\nimgs = errors[indices]\nimgs_tl = errors_tl[indices]\nimgs_pl = errors_pl[indices]\n\nprint(imgs.shape, imgs_tl.shape, imgs_pl.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:22:50.296396Z","iopub.execute_input":"2023-09-09T02:22:50.296784Z","iopub.status.idle":"2023-09-09T02:22:50.304150Z","shell.execute_reply.started":"2023-09-09T02:22:50.296750Z","shell.execute_reply":"2023-09-09T02:22:50.303124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(5, 5, figsize = (20, 20))\n\nimg_idx = 0\nfor i in range(5):\n    for j in range(5):\n        ax[i][j].imshow(imgs[img_idx], cmap = 'gray')\n        ax[i][j].set_title(f'Predicted Label: {imgs_pl[img_idx]}\\nTrue Label: {imgs_tl[img_idx]}')\n        ax[i][j].axis('off')\n        img_idx += 1\n        \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T02:24:41.890982Z","iopub.execute_input":"2023-09-09T02:24:41.891521Z","iopub.status.idle":"2023-09-09T02:24:44.020831Z","shell.execute_reply.started":"2023-09-09T02:24:41.891485Z","shell.execute_reply":"2023-09-09T02:24:44.019644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**error images seem to be unclear and very close to each other that a human may struggle to get right**","metadata":{}},{"cell_type":"markdown","source":"# Final Submission","metadata":{}},{"cell_type":"code","source":"results = model1.predict(test_images)\nresults = np.argmax(results,axis = 1)\n\nsub2 = ans.copy()\nsub2['Label'] = results\nsub2.to_csv('/kaggle/working/submission2.csv', index= False)\nsub2.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-09T01:17:28.971984Z","iopub.execute_input":"2023-09-09T01:17:28.972367Z","iopub.status.idle":"2023-09-09T01:17:34.459723Z","shell.execute_reply.started":"2023-09-09T01:17:28.972337Z","shell.execute_reply":"2023-09-09T01:17:34.458547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**`'submission2.csv'`  scored $99.375$ % accuracy on the test-set**","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"}}