{"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 seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom keras.datasets import mnist\nimport tensorflow as tf\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\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-24T19:46:34.713231Z","iopub.execute_input":"2023-09-24T19:46:34.713764Z","iopub.status.idle":"2023-09-24T19:46:44.744907Z","shell.execute_reply.started":"2023-09-24T19:46:34.713713Z","shell.execute_reply":"2023-09-24T19:46:44.743789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\nsubmission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\n\nprint(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:44.747126Z","iopub.execute_input":"2023-09-24T19:46:44.747904Z","iopub.status.idle":"2023-09-24T19:46:50.203022Z","shell.execute_reply.started":"2023-09-24T19:46:44.747864Z","shell.execute_reply":"2023-09-24T19:46:50.201755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:50.204363Z","iopub.execute_input":"2023-09-24T19:46:50.205127Z","iopub.status.idle":"2023-09-24T19:46:50.213001Z","shell.execute_reply.started":"2023-09-24T19:46:50.205097Z","shell.execute_reply":"2023-09-24T19:46:50.211933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train[\"label\"]\n\nX_train = train.drop(\"label\", axis = 1)\n\ny_train.shape, X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:50.215240Z","iopub.execute_input":"2023-09-24T19:46:50.215563Z","iopub.status.idle":"2023-09-24T19:46:50.359945Z","shell.execute_reply.started":"2023-09-24T19:46:50.215537Z","shell.execute_reply":"2023-09-24T19:46:50.358925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.countplot(x = 'label', data = train)\nplt.title(\"Number of digit of each classes\");","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:50.361262Z","iopub.execute_input":"2023-09-24T19:46:50.361653Z","iopub.status.idle":"2023-09-24T19:46:50.686252Z","shell.execute_reply.started":"2023-09-24T19:46:50.361625Z","shell.execute_reply":"2023-09-24T19:46:50.685174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chose the random images\nnp.random.seed(42)\nrandom_indices = np.random.randint(0, 42000, 25)\n\nimages=[]\nlabels=[]\nfor i in random_indices:\n    img = X_train.iloc[i].to_numpy()\n    img = img.reshape((28,28,1))\n    images.append(img)\n    label = y_train[i]\n    labels.append(label)\n    \n# Plot the images\nplt.subplots(figsize = (5, 5))\nfor i, image in enumerate(images):\n    plt.subplot(5, 5, i + 1)\n    plt.imshow(image, cmap=\"gray\")\n    plt.title(labels[i])\n    plt.axis('off')\n\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:50.687292Z","iopub.execute_input":"2023-09-24T19:46:50.687749Z","iopub.status.idle":"2023-09-24T19:46:52.193075Z","shell.execute_reply.started":"2023-09-24T19:46:50.687722Z","shell.execute_reply":"2023-09-24T19:46:52.192237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### It is worth noting that due to the not very large amount of data, the model can easily retrain on training data and have low accuracy on the test sample. \n#### Therefore, to solve this problem, there is a proposal to take an open MNIST dataset, connect it with existing data and train the model on them.","metadata":{}},{"cell_type":"markdown","source":"# Increase dataset with MNIST dataset\nTo download dataset click -> https://www.kaggle.com/datasets/oddrationale/mnist-in-csv","metadata":{}},{"cell_type":"code","source":"# Increase dataset by MNIST (Modified National Institute of Standards and Technology database)\ntrain_mnist = pd.read_csv('../input/mnist/mnist_train.csv')\ntest_mnist = pd.read_csv('../input/mnist/mnist_test.csv')\n\n# Concatenate tarin and test splits for one big train split\ntrain_mnist_all = np.concatenate([train_mnist, test_mnist], axis=0)\n\nprint(f\"Training data size is {train_mnist_all.shape}\\n\")\n\ntrain_mnist_all","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:52.194187Z","iopub.execute_input":"2023-09-24T19:46:52.195039Z","iopub.status.idle":"2023-09-24T19:46:57.638909Z","shell.execute_reply.started":"2023-09-24T19:46:52.194999Z","shell.execute_reply":"2023-09-24T19:46:57.638061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divide this large training set into attributes X and target\nX_mnist = np.delete(train_mnist_all,obj=[0],axis=1)\nY_mnist = np.delete(train_mnist_all,obj=np.s_[1:],axis=1)\n\nprint(f\"Training data X size is {X_mnist.shape}\\nTraining data Y size is {Y_mnist.shape}\\n\")\n\nX_mnist, Y_mnist","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:57.640239Z","iopub.execute_input":"2023-09-24T19:46:57.640559Z","iopub.status.idle":"2023-09-24T19:46:57.875514Z","shell.execute_reply.started":"2023-09-24T19:46:57.640533Z","shell.execute_reply":"2023-09-24T19:46:57.874534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Conver numpy array Y MNIST to dataFrame for plotting\nY_mnist_df = pd.DataFrame(data=Y_mnist, columns=[\"label\"])\n\n# Plot the MNIST classes\nplt.figure(figsize=(10,5))\nsns.countplot(x = 'label', data = Y_mnist_df)\nplt.title(\"Number of digit of each classes of MNIST\");","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:57.877046Z","iopub.execute_input":"2023-09-24T19:46:57.877341Z","iopub.status.idle":"2023-09-24T19:46:58.208422Z","shell.execute_reply.started":"2023-09-24T19:46:57.877304Z","shell.execute_reply":"2023-09-24T19:46:58.207446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chose the random images from MNIST dataset\nnp.random.seed(70)\nrandom_indices = np.random.randint(0, 70000, 25)\n\nimages=[]\nlabels=[]\nfor i in random_indices:\n    img = X_mnist[i]\n    img = img.reshape((28,28,1))\n    images.append(img)\n    label = Y_mnist[i]\n    labels.append(label)\n    \n# Plot the images\nplt.subplots(figsize = (5, 5))\nfor i, image in enumerate(images):\n    plt.subplot(5, 5, i + 1)\n    plt.imshow(image, cmap=\"gray\")\n    plt.title(labels[i])\n    plt.axis('off')\n\n\nplt.tight_layout()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:58.211917Z","iopub.execute_input":"2023-09-24T19:46:58.212224Z","iopub.status.idle":"2023-09-24T19:46:59.924552Z","shell.execute_reply.started":"2023-09-24T19:46:58.212199Z","shell.execute_reply":"2023-09-24T19:46:59.923366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalization\nX_train = X_train / 255.0\ntest = test / 255.0\n\nX_mnist = X_mnist / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:46:59.925819Z","iopub.execute_input":"2023-09-24T19:46:59.926125Z","iopub.status.idle":"2023-09-24T19:47:00.243019Z","shell.execute_reply.started":"2023-09-24T19:46:59.926098Z","shell.execute_reply":"2023-09-24T19:47:00.241789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training data X size is {X_train.shape}\\n\")\nprint(f\"Training data X MNIST size is {X_mnist.shape}\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.244314Z","iopub.execute_input":"2023-09-24T19:47:00.244754Z","iopub.status.idle":"2023-09-24T19:47:00.250489Z","shell.execute_reply.started":"2023-09-24T19:47:00.244719Z","shell.execute_reply":"2023-09-24T19:47:00.248985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training data Y size is {y_train.shape}\")\nprint(f\"Training data Y MNIST size is {Y_mnist.shape}\\n\")\n\nprint(f\"Type of data Y MNIST is {type(y_train)}\")\nprint(f\"Type of data Y MNIST is {type(Y_mnist)}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.251685Z","iopub.execute_input":"2023-09-24T19:47:00.251958Z","iopub.status.idle":"2023-09-24T19:47:00.267039Z","shell.execute_reply.started":"2023-09-24T19:47:00.251934Z","shell.execute_reply":"2023-09-24T19:47:00.266097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Concatenate normal dataset with MNIST dataset","metadata":{}},{"cell_type":"code","source":"X_train = np.concatenate((X_train, X_mnist))\nY_train = np.concatenate((y_train.to_numpy().reshape(-1,1), Y_mnist))\n\nX_train.shape, Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.268362Z","iopub.execute_input":"2023-09-24T19:47:00.269544Z","iopub.status.idle":"2023-09-24T19:47:00.628016Z","shell.execute_reply.started":"2023-09-24T19:47:00.269454Z","shell.execute_reply":"2023-09-24T19:47:00.626912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training data X size is {X_train.shape}\")\nprint(f\"Training data Y size is {Y_train.shape}\\n\")\nX_train.shape, Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.629274Z","iopub.execute_input":"2023-09-24T19:47:00.629580Z","iopub.status.idle":"2023-09-24T19:47:00.636647Z","shell.execute_reply.started":"2023-09-24T19:47:00.629556Z","shell.execute_reply":"2023-09-24T19:47:00.635532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\n\nprint(f\"Training data X size is {X_train.shape}\\n\")\nprint(f\"Test data X size is {test.shape}\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.638197Z","iopub.execute_input":"2023-09-24T19:47:00.638615Z","iopub.status.idle":"2023-09-24T19:47:00.647932Z","shell.execute_reply.started":"2023-09-24T19:47:00.638588Z","shell.execute_reply":"2023-09-24T19:47:00.647094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Label one hot encode for y target","metadata":{}},{"cell_type":"code","source":"# Convert label to one hot vectors (ex : 4 -> [0,0,0,0,1,0,0,0,0,0])\nY_train = to_categorical(Y_train, num_classes = 10)\nY_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.649222Z","iopub.execute_input":"2023-09-24T19:47:00.649677Z","iopub.status.idle":"2023-09-24T19:47:00.662882Z","shell.execute_reply.started":"2023-09-24T19:47:00.649642Z","shell.execute_reply":"2023-09-24T19:47:00.662120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train validation sets split","metadata":{}},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.2, random_state=2)\n\nprint(f\"Train data X size is {X_train.shape}\")\nprint(f\"Validation data X size is {X_val.shape}\\n\")\n\nprint(f\"Train data Y size is {Y_train.shape}\")\nprint(f\"Validation data Y size is {Y_val.shape}\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:00.664014Z","iopub.execute_input":"2023-09-24T19:47:00.664483Z","iopub.status.idle":"2023-09-24T19:47:01.785484Z","shell.execute_reply.started":"2023-09-24T19:47:00.664457Z","shell.execute_reply":"2023-09-24T19:47:01.784362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = plt.imshow(X_train[777][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:01.786993Z","iopub.execute_input":"2023-09-24T19:47:01.787639Z","iopub.status.idle":"2023-09-24T19:47:02.029300Z","shell.execute_reply.started":"2023-09-24T19:47:01.787599Z","shell.execute_reply":"2023-09-24T19:47:02.028182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Model - CNN","metadata":{}},{"cell_type":"code","source":"# Creating CNN model\n\"\"\"\n  [[Conv2D->relu] -> BatchNormalization]*2 -> MaxPool2D -> Dropout]*2 -> \n  -> [Conv2D->relu] -> BatchNormalization -> Dropout -> \n  -> Flatten -> Dense -> BatchNormalization -> Dropout -> Out\n\"\"\"\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28,28,1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same',  activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.030929Z","iopub.execute_input":"2023-09-24T19:47:02.031358Z","iopub.status.idle":"2023-09-24T19:47:02.374874Z","shell.execute_reply.started":"2023-09-24T19:47:02.031303Z","shell.execute_reply":"2023-09-24T19:47:02.373987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.376122Z","iopub.execute_input":"2023-09-24T19:47:02.376451Z","iopub.status.idle":"2023-09-24T19:47:02.430469Z","shell.execute_reply.started":"2023-09-24T19:47:02.376424Z","shell.execute_reply":"2023-09-24T19:47:02.429484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reading such information about the model is not always convenient. \n### Fortunately, in the Keras library there is an opportunity to interpret information about the model in a more convenient format","metadata":{}},{"cell_type":"code","source":"# Print info about model\nfrom keras.utils import plot_model\nfrom IPython.display import Image\n\nplot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)\nImage(\"model.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.431926Z","iopub.execute_input":"2023-09-24T19:47:02.432343Z","iopub.status.idle":"2023-09-24T19:47:02.792320Z","shell.execute_reply.started":"2023-09-24T19:47:02.432289Z","shell.execute_reply":"2023-09-24T19:47:02.791027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define Optimizer\noptimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.793809Z","iopub.execute_input":"2023-09-24T19:47:02.794171Z","iopub.status.idle":"2023-09-24T19:47:02.799635Z","shell.execute_reply.started":"2023-09-24T19:47:02.794143Z","shell.execute_reply":"2023-09-24T19:47:02.798598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = optimizer,\n              loss = \"categorical_crossentropy\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.801104Z","iopub.execute_input":"2023-09-24T19:47:02.801394Z","iopub.status.idle":"2023-09-24T19:47:02.824132Z","shell.execute_reply.started":"2023-09-24T19:47:02.801370Z","shell.execute_reply":"2023-09-24T19:47:02.823143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Audjusting learning rate\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.825274Z","iopub.execute_input":"2023-09-24T19:47:02.825980Z","iopub.status.idle":"2023-09-24T19:47:02.830421Z","shell.execute_reply.started":"2023-09-24T19:47:02.825951Z","shell.execute_reply":"2023-09-24T19:47:02.829402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adjusting epochs and batch_size\nepochs = 3\nbatch_size = 256","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.832030Z","iopub.execute_input":"2023-09-24T19:47:02.832410Z","iopub.status.idle":"2023-09-24T19:47:02.840647Z","shell.execute_reply.started":"2023-09-24T19:47:02.832376Z","shell.execute_reply":"2023-09-24T19:47:02.839790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,Y_train,batch_size=batch_size,\n                    epochs=epochs,\n                    validation_data=(X_val,Y_val),\n                    verbose=2,\n                    steps_per_epoch=X_train.shape[0] // batch_size,\n                    callbacks=[learning_rate_reduction],\n                    validation_steps = X_val.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T19:47:02.841987Z","iopub.execute_input":"2023-09-24T19:47:02.842378Z","iopub.status.idle":"2023-09-24T20:27:47.095045Z","shell.execute_reply.started":"2023-09-24T19:47:02.842342Z","shell.execute_reply":"2023-09-24T20:27:47.094192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the history of fiting the CNN model\nprint(history.history.keys())","metadata":{"execution":{"iopub.status.busy":"2023-09-24T20:27:47.100399Z","iopub.execute_input":"2023-09-24T20:27:47.100815Z","iopub.status.idle":"2023-09-24T20:27:47.105991Z","shell.execute_reply.started":"2023-09-24T20:27:47.100787Z","shell.execute_reply":"2023-09-24T20:27:47.105008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Draw the loss and accuracy curves of the training set and the validation set.\n# Can judge whether it is under-fitting or over-fitting\nfig, ax = plt.subplots(2,1)\n\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"Validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T20:27:47.107194Z","iopub.execute_input":"2023-09-24T20:27:47.107575Z","iopub.status.idle":"2023-09-24T20:27:47.586839Z","shell.execute_reply.started":"2023-09-24T20:27:47.107548Z","shell.execute_reply":"2023-09-24T20:27:47.586021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{}},{"cell_type":"code","source":"# Make predictions about test sets\nresults = model.predict(test)\n\n# Convert one-hot vector to number\nresults = np.argmax(results,axis=1)\n\nresults = pd.Series(results,name=\"Label\")\n\n# Save the final result in cnn_mnist_submission.csv\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T20:27:47.587975Z","iopub.execute_input":"2023-09-24T20:27:47.589036Z","iopub.status.idle":"2023-09-24T20:28:41.913681Z","shell.execute_reply.started":"2023-09-24T20:27:47.588996Z","shell.execute_reply":"2023-09-24T20:28:41.912719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-09-24T20:30:59.060298Z","iopub.execute_input":"2023-09-24T20:30:59.060771Z","iopub.status.idle":"2023-09-24T20:30:59.078111Z","shell.execute_reply.started":"2023-09-24T20:30:59.060737Z","shell.execute_reply":"2023-09-24T20:30:59.076991Z"},"trusted":true},"execution_count":null,"outputs":[]}]}