{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O\nimport seaborn as sns \nimport matplotlib.pyplot as plt\nimport random\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))","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-24T18:45:02.483971Z","iopub.execute_input":"2022-03-24T18:45:02.484357Z","iopub.status.idle":"2022-03-24T18:45:09.847063Z","shell.execute_reply.started":"2022-03-24T18:45:02.484281Z","shell.execute_reply":"2022-03-24T18:45:09.845618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#setting the random seed so the models will always start using the same weights\ntf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:09.848904Z","iopub.execute_input":"2022-03-24T18:45:09.849106Z","iopub.status.idle":"2022-03-24T18:45:09.854255Z","shell.execute_reply.started":"2022-03-24T18:45:09.849081Z","shell.execute_reply":"2022-03-24T18:45:09.852813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Loading and looking the data:","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(r'/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv(r'/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:09.856426Z","iopub.execute_input":"2022-03-24T18:45:09.856670Z","iopub.status.idle":"2022-03-24T18:45:15.595161Z","shell.execute_reply.started":"2022-03-24T18:45:09.856644Z","shell.execute_reply":"2022-03-24T18:45:15.593665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['label']\nX_train = train.drop('label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.597915Z","iopub.execute_input":"2022-03-24T18:45:15.598176Z","iopub.status.idle":"2022-03-24T18:45:15.719779Z","shell.execute_reply.started":"2022-03-24T18:45:15.598148Z","shell.execute_reply":"2022-03-24T18:45:15.718747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.721112Z","iopub.execute_input":"2022-03-24T18:45:15.721329Z","iopub.status.idle":"2022-03-24T18:45:15.731665Z","shell.execute_reply.started":"2022-03-24T18:45:15.721298Z","shell.execute_reply":"2022-03-24T18:45:15.730641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.732758Z","iopub.execute_input":"2022-03-24T18:45:15.732938Z","iopub.status.idle":"2022-03-24T18:45:15.761462Z","shell.execute_reply.started":"2022-03-24T18:45:15.732915Z","shell.execute_reply":"2022-03-24T18:45:15.760190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.763207Z","iopub.execute_input":"2022-03-24T18:45:15.763462Z","iopub.status.idle":"2022-03-24T18:45:15.825133Z","shell.execute_reply.started":"2022-03-24T18:45:15.763432Z","shell.execute_reply":"2022-03-24T18:45:15.823174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.826284Z","iopub.execute_input":"2022-03-24T18:45:15.826518Z","iopub.status.idle":"2022-03-24T18:45:15.910192Z","shell.execute_reply.started":"2022-03-24T18:45:15.826485Z","shell.execute_reply":"2022-03-24T18:45:15.909147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.911674Z","iopub.execute_input":"2022-03-24T18:45:15.911957Z","iopub.status.idle":"2022-03-24T18:45:15.918875Z","shell.execute_reply.started":"2022-03-24T18:45:15.911922Z","shell.execute_reply":"2022-03-24T18:45:15.917953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Exploratory analysis:","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=y_train)\nplt.title('# Of samples', size=14)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:15.922414Z","iopub.execute_input":"2022-03-24T18:45:15.922669Z","iopub.status.idle":"2022-03-24T18:45:16.115581Z","shell.execute_reply.started":"2022-03-24T18:45:15.922638Z","shell.execute_reply":"2022-03-24T18:45:16.114886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(5, 5, figsize=(8, 8))\nfig.suptitle('Digits images and labels', fontsize=16)\nax = ax.ravel()\nfor i in range(25):\n    sample_n = random.randint(0, X_train.shape[0])\n    ax[i].imshow(np.array(X_train.iloc[sample_n]).reshape(28, 28), cmap='inferno')\n    ax[i].get_xaxis().set_visible(False)\n    ax[i].get_yaxis().set_visible(False)\n    ax[i].set_title(y_train[sample_n], fontsize = 12)\n\nplt.subplots_adjust(hspace=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:16.116644Z","iopub.execute_input":"2022-03-24T18:45:16.117603Z","iopub.status.idle":"2022-03-24T18:45:17.150489Z","shell.execute_reply.started":"2022-03-24T18:45:16.117568Z","shell.execute_reply":"2022-03-24T18:45:17.149779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"if you make an average of these pixels you can look which pixels are more commonly painted, also deppending on how you look to this image you can see different numbers (the most simple are 9 and 3)","metadata":{}},{"cell_type":"code","source":"plt.imshow(np.array(X_train.mean()).reshape(28, 28), cmap='inferno')\nplt.colorbar()\nplt.title('average shape', {'fontsize': 16})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:17.151487Z","iopub.execute_input":"2022-03-24T18:45:17.152457Z","iopub.status.idle":"2022-03-24T18:45:17.383162Z","shell.execute_reply.started":"2022-03-24T18:45:17.152425Z","shell.execute_reply":"2022-03-24T18:45:17.382096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 5, figsize=(8, 4))\nfig.suptitle('Average shape per digit', fontsize=16)\n\nax = ax.ravel()\n\nfor i in range(10):\n    ax[i].imshow(np.array(train[train['label'] == i].drop('label', axis=1).mean()).reshape(28, 28), cmap='inferno')\n    ax[i].get_xaxis().set_visible(False)\n    ax[i].get_yaxis().set_visible(False)\n    ax[i].set_title(i, fontsize = 12)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:17.384403Z","iopub.execute_input":"2022-03-24T18:45:17.384630Z","iopub.status.idle":"2022-03-24T18:45:18.419958Z","shell.execute_reply.started":"2022-03-24T18:45:17.384600Z","shell.execute_reply":"2022-03-24T18:45:18.418868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## transforming Data:\nCNN are sometimes more capable of learning certain data, specially visual data this happens since it have a convolution and a pooling process.","metadata":{}},{"cell_type":"code","source":"X_train_array = np.array(X_train)\nX_train_array","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.421210Z","iopub.execute_input":"2022-03-24T18:45:18.421432Z","iopub.status.idle":"2022-03-24T18:45:18.562016Z","shell.execute_reply.started":"2022-03-24T18:45:18.421403Z","shell.execute_reply":"2022-03-24T18:45:18.561184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_array = np.array(test)\ntest_array","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.563013Z","iopub.execute_input":"2022-03-24T18:45:18.563637Z","iopub.status.idle":"2022-03-24T18:45:18.705077Z","shell.execute_reply.started":"2022-03-24T18:45:18.563604Z","shell.execute_reply":"2022-03-24T18:45:18.703956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.706542Z","iopub.execute_input":"2022-03-24T18:45:18.706826Z","iopub.status.idle":"2022-03-24T18:45:18.712335Z","shell.execute_reply.started":"2022-03-24T18:45:18.706760Z","shell.execute_reply":"2022-03-24T18:45:18.711776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.713270Z","iopub.execute_input":"2022-03-24T18:45:18.713778Z","iopub.status.idle":"2022-03-24T18:45:18.727587Z","shell.execute_reply.started":"2022-03-24T18:45:18.713747Z","shell.execute_reply":"2022-03-24T18:45:18.726777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_array = X_train_array.reshape(42000, 28, 28, 1)\nX_train_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.728672Z","iopub.execute_input":"2022-03-24T18:45:18.728885Z","iopub.status.idle":"2022-03-24T18:45:18.741242Z","shell.execute_reply.started":"2022-03-24T18:45:18.728857Z","shell.execute_reply":"2022-03-24T18:45:18.740715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_array = test_array.reshape(-1, 28, 28, 1)\ntest_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.742293Z","iopub.execute_input":"2022-03-24T18:45:18.742594Z","iopub.status.idle":"2022-03-24T18:45:18.774216Z","shell.execute_reply.started":"2022-03-24T18:45:18.742561Z","shell.execute_reply":"2022-03-24T18:45:18.773749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Setting Data augmentation:\n\nData augmentation is a technic that maks new data from the input data by making small changes on the original data (shifting, rotating, zooming... the image)","metadata":{}},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    fill_mode=\"constant\",\n    \n    horizontal_flip=False,\n    vertical_flip=False,\n    \n    rotation_range=20,\n    \n    validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:18.775244Z","iopub.execute_input":"2022-03-24T18:45:18.775555Z","iopub.status.idle":"2022-03-24T18:45:19.992591Z","shell.execute_reply.started":"2022-03-24T18:45:18.775525Z","shell.execute_reply":"2022-03-24T18:45:19.991666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(X_train_array)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:19.993828Z","iopub.execute_input":"2022-03-24T18:45:19.994049Z","iopub.status.idle":"2022-03-24T18:45:20.157141Z","shell.execute_reply.started":"2022-03-24T18:45:19.994018Z","shell.execute_reply":"2022-03-24T18:45:20.154959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Training model(s):","metadata":{}},{"cell_type":"code","source":"tf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.159442Z","iopub.execute_input":"2022-03-24T18:45:20.159697Z","iopub.status.idle":"2022-03-24T18:45:20.163264Z","shell.execute_reply.started":"2022-03-24T18:45:20.159658Z","shell.execute_reply":"2022-03-24T18:45:20.162671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (5, 5), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D((3, 3)),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Conv2D(48, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D((3, 3)),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units=784, activation='relu', input_shape=(784, )),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(units=392, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(units=151, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(units=50, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(units=10, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.164288Z","iopub.execute_input":"2022-03-24T18:45:20.164806Z","iopub.status.idle":"2022-03-24T18:45:20.372548Z","shell.execute_reply.started":"2022-03-24T18:45:20.164769Z","shell.execute_reply":"2022-03-24T18:45:20.371165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam',loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.373632Z","iopub.execute_input":"2022-03-24T18:45:20.373871Z","iopub.status.idle":"2022-03-24T18:45:20.393280Z","shell.execute_reply.started":"2022-03-24T18:45:20.373837Z","shell.execute_reply":"2022-03-24T18:45:20.392111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.394788Z","iopub.execute_input":"2022-03-24T18:45:20.395066Z","iopub.status.idle":"2022-03-24T18:45:20.409101Z","shell.execute_reply.started":"2022-03-24T18:45:20.395027Z","shell.execute_reply":"2022-03-24T18:45:20.408288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_loss\",\n    min_delta=0.008,\n    patience=10,\n    verbose=0,\n    mode=\"auto\",\n    baseline=None,\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.410367Z","iopub.execute_input":"2022-03-24T18:45:20.410593Z","iopub.status.idle":"2022-03-24T18:45:20.416818Z","shell.execute_reply.started":"2022-03-24T18:45:20.410558Z","shell.execute_reply":"2022-03-24T18:45:20.415962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(datagen.flow(X_train_array, y_train, batch_size=50, subset='training'),\n                    epochs=100,\n                    validation_data=datagen.flow(X_train_array, y_train,\n         batch_size=10, subset='validation'),\n                    callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2022-03-24T18:45:20.417690Z","iopub.execute_input":"2022-03-24T18:45:20.418011Z","iopub.status.idle":"2022-03-24T19:07:00.046215Z","shell.execute_reply.started":"2022-03-24T18:45:20.417983Z","shell.execute_reply":"2022-03-24T19:07:00.044587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"i'll also train the network without using the augmented data, so it'll be able to generalize more.","metadata":{}},{"cell_type":"code","source":"model.fit(X_train_array,\n        y_train,\n        batch_size=50,\n        epochs=100,\n        validation_split=0.3,\n        callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:07:00.052805Z","iopub.execute_input":"2022-03-24T19:07:00.053130Z","iopub.status.idle":"2022-03-24T19:09:36.163915Z","shell.execute_reply.started":"2022-03-24T19:07:00.053089Z","shell.execute_reply":"2022-03-24T19:09:36.163191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5.1. Evaluating model performance:","metadata":{}},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:36.165114Z","iopub.execute_input":"2022-03-24T19:09:36.166207Z","iopub.status.idle":"2022-03-24T19:09:36.173643Z","shell.execute_reply.started":"2022-03-24T19:09:36.166174Z","shell.execute_reply":"2022-03-24T19:09:36.172080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Model loss', fontsize = 14)\nplt.plot(range(len(history.history['loss'])), history.history['loss'], marker='o', c='gray')\nplt.plot(range(len(history.history['loss'])), history.history['val_loss'], marker='o')\nplt.legend(labels=['training loss', 'validation loss'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:36.175255Z","iopub.execute_input":"2022-03-24T19:09:36.175981Z","iopub.status.idle":"2022-03-24T19:09:36.361444Z","shell.execute_reply.started":"2022-03-24T19:09:36.175938Z","shell.execute_reply":"2022-03-24T19:09:36.360682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Model accuracy', fontsize = 14)\nplt.plot(range(len(history.history['loss'])), history.history['accuracy'], c='gray', marker='o')\nplt.plot(range(len(history.history['loss'])), history.history['val_accuracy'], marker='o')\nplt.legend(labels=['training accuracy', 'validation accuracy'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:36.362675Z","iopub.execute_input":"2022-03-24T19:09:36.362995Z","iopub.status.idle":"2022-03-24T19:09:36.573913Z","shell.execute_reply.started":"2022-03-24T19:09:36.362966Z","shell.execute_reply":"2022-03-24T19:09:36.572787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.2. Comparing this model to others:","metadata":{}},{"cell_type":"code","source":"ev = model.evaluate(X_train_array, y_train)\nev","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:36.576135Z","iopub.execute_input":"2022-03-24T19:09:36.576422Z","iopub.status.idle":"2022-03-24T19:09:44.814958Z","shell.execute_reply.started":"2022-03-24T19:09:36.576385Z","shell.execute_reply":"2022-03-24T19:09:44.813572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_train_array)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:44.816172Z","iopub.execute_input":"2022-03-24T19:09:44.817011Z","iopub.status.idle":"2022-03-24T19:09:52.020189Z","shell.execute_reply.started":"2022-03-24T19:09:44.816944Z","shell.execute_reply":"2022-03-24T19:09:52.018780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.021526Z","iopub.execute_input":"2022-03-24T19:09:52.021778Z","iopub.status.idle":"2022-03-24T19:09:52.279381Z","shell.execute_reply.started":"2022-03-24T19:09:52.021709Z","shell.execute_reply":"2022-03-24T19:09:52.278235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.280976Z","iopub.execute_input":"2022-03-24T19:09:52.281264Z","iopub.status.idle":"2022-03-24T19:09:52.289296Z","shell.execute_reply.started":"2022-03-24T19:09:52.281224Z","shell.execute_reply":"2022-03-24T19:09:52.288758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.array(pd.DataFrame(pred).idxmax(axis=1))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.291196Z","iopub.execute_input":"2022-03-24T19:09:52.291480Z","iopub.status.idle":"2022-03-24T19:09:52.389863Z","shell.execute_reply.started":"2022-03-24T19:09:52.291450Z","shell.execute_reply":"2022-03-24T19:09:52.388338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_train, pred))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.391480Z","iopub.execute_input":"2022-03-24T19:09:52.391705Z","iopub.status.idle":"2022-03-24T19:09:52.480864Z","shell.execute_reply.started":"2022-03-24T19:09:52.391676Z","shell.execute_reply":"2022-03-24T19:09:52.479755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.title('Predicted digits', size=14)\nsns.heatmap(confusion_matrix(y_train, pred), cmap='inferno', annot=True, )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.482368Z","iopub.execute_input":"2022-03-24T19:09:52.482975Z","iopub.status.idle":"2022-03-24T19:09:52.943671Z","shell.execute_reply.started":"2022-03-24T19:09:52.482940Z","shell.execute_reply":"2022-03-24T19:09:52.942783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Predicting test data:","metadata":{}},{"cell_type":"code","source":"test_array = test_array / 255","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:52.945573Z","iopub.execute_input":"2022-03-24T19:09:52.945874Z","iopub.status.idle":"2022-03-24T19:09:53.008137Z","shell.execute_reply.started":"2022-03-24T19:09:52.945835Z","shell.execute_reply":"2022-03-24T19:09:53.006931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_result = model.predict(test_array)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:53.009879Z","iopub.execute_input":"2022-03-24T19:09:53.010200Z","iopub.status.idle":"2022-03-24T19:09:57.925834Z","shell.execute_reply.started":"2022-03-24T19:09:53.010161Z","shell.execute_reply":"2022-03-24T19:09:57.924963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_result = np.array(pd.DataFrame(test_result).idxmax(axis=1))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:57.926912Z","iopub.execute_input":"2022-03-24T19:09:57.927094Z","iopub.status.idle":"2022-03-24T19:09:57.973944Z","shell.execute_reply.started":"2022-03-24T19:09:57.927071Z","shell.execute_reply":"2022-03-24T19:09:57.972964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(5, 5, figsize=(8, 8))\nfig.suptitle('Digits images and labels', fontsize=16)\nax = ax.ravel()\nfor i in range(25):\n    sample_n = random.randint(0, test.shape[0])\n    ax[i].imshow(np.array(test.iloc[sample_n]).reshape(28, 28), cmap='inferno')\n    ax[i].get_xaxis().set_visible(False)\n    ax[i].get_yaxis().set_visible(False)\n    ax[i].set_title(test_result[sample_n], fontsize = 12)\n\nplt.subplots_adjust(hspace=0.3)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:57.975389Z","iopub.execute_input":"2022-03-24T19:09:57.975611Z","iopub.status.idle":"2022-03-24T19:09:59.585561Z","shell.execute_reply.started":"2022-03-24T19:09:57.975585Z","shell.execute_reply":"2022-03-24T19:09:59.584624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_data = pd.DataFrame({\n    'ImageId': test.index+1,\n    'Label': test_result\n})","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:59.587372Z","iopub.execute_input":"2022-03-24T19:09:59.587753Z","iopub.status.idle":"2022-03-24T19:09:59.592408Z","shell.execute_reply.started":"2022-03-24T19:09:59.587712Z","shell.execute_reply":"2022-03-24T19:09:59.591660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_data","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:59.593619Z","iopub.execute_input":"2022-03-24T19:09:59.594221Z","iopub.status.idle":"2022-03-24T19:09:59.613333Z","shell.execute_reply.started":"2022-03-24T19:09:59.594194Z","shell.execute_reply":"2022-03-24T19:09:59.612418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_data.to_csv('pred.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T19:09:59.614621Z","iopub.execute_input":"2022-03-24T19:09:59.614909Z","iopub.status.idle":"2022-03-24T19:09:59.663537Z","shell.execute_reply.started":"2022-03-24T19:09:59.614863Z","shell.execute_reply":"2022-03-24T19:09:59.662548Z"},"trusted":true},"execution_count":null,"outputs":[]}]}