{"cells":[{"metadata":{"_cell_guid":"816a8f4d-7862-4ced-aa8a-5edc4e53e84a","_uuid":"4e5f1239a9f7ed9a537f4bc65a2888bd25957bda"},"cell_type":"markdown","source":""},{"metadata":{"_cell_guid":"3ffb80c5-6778-4130-961b-aa17b57c7741","_uuid":"8353a531a6034b1c21239bcf9657a94a718bcece"},"cell_type":"markdown","source":"## Importing Necessary Modules\n## To discover something new is to explore where it has never been explored.\n\n\n**Added Conv  Visuals Also (Working)**"},{"metadata":{"_cell_guid":"2d75481f-2b19-d824-6a77-a6f8fa1f63f8","_execution_state":"idle","_uuid":"e8e286a4d699f3294f0ff773baba59f015ae11b9","_active":false,"trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt #for plotting\nfrom collections import Counter\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nimport seaborn as sns\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f9f804d7-72e7-415f-88b9-f43ded389d39","_uuid":"4375276c8b31c78a81859766e49db726e73cf4b5"},"cell_type":"markdown","source":"## Loading The Dataset"},{"metadata":{"_cell_guid":"d2ecfe40-ec05-6623-6f2d-a296d7eeec9e","_execution_state":"idle","_uuid":"1de17f87d4eb39ebb9772e29bbc70186d09ac927","_active":true,"trusted":true},"cell_type":"code","source":"#loading the dataset.......(Train)\ntrain = pd.read_csv(\"../input/train.csv\")\nprint(train.shape)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"74d1193c-49c0-4f83-bb76-9280bf544959","_uuid":"8da998a4c8f1e8ee218a6118217516754fb51868","trusted":true},"cell_type":"code","source":"z_train = Counter(train['label'])\nz_train","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"49685d71-7f60-4df4-936e-1903e0d8bc97","_uuid":"94787bc95e72b77062f3c997bab575584f10f5bf","trusted":true},"cell_type":"code","source":"sns.countplot(train['label'])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"32eef273-6dd8-7bac-df10-691c90a4112f","_execution_state":"idle","_uuid":"3d5c172812a24ca6a65f6b57258a743d7e9da810","_active":false,"trusted":true},"cell_type":"code","source":"#loading the dataset.......(Test)\ntest= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"04b01849-64c8-4701-9d1a-4622f827b1a2","_uuid":"9d47ae3027e049f31a0a3046da9db13c72c82e60","_execution_state":"idle","trusted":true},"cell_type":"code","source":"x_train = (train.ix[:,1:].values).astype('float32') # all pixel values\ny_train = train.ix[:,0].values.astype('int32') # only labels i.e targets digits\nx_test = test.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fe3486b1-1e6b-472a-9512-dc893cbdb166","_uuid":"46f3eed5b689e4d69dd9ebaa67665fbcaeadb4fc","trusted":true},"cell_type":"code","source":"%matplotlib inline\n# preview the images first\nplt.figure(figsize=(12,10))\nx, y = 10, 4\nfor i in range(40):  \n    plt.subplot(y, x, i+1)\n    plt.imshow(x_train[i].reshape((28,28)),interpolation='nearest')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ad1ce800-483d-4d53-8e72-aac58e025625","_uuid":"a57bba5834baddf55952760b01ccb7c492f7c7f7"},"cell_type":"markdown","source":"## Normalising The Data "},{"metadata":{"_cell_guid":"2af344a9-8fbe-4b2f-af5e-a108adf04722","_uuid":"29756ff1b015740f63a1e3b0ee707d4ee3ee0aab","_execution_state":"idle","trusted":true},"cell_type":"code","source":"x_train = x_train/255.0\nx_test = x_test/255.0","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"632d3bf7-9682-4d6d-b51f-b1503190e2b6","_uuid":"e9e6b0fb776f4400788325fe66ac58f90f56388a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"df775f7c-a839-4a9b-baee-bef876debd08","_uuid":"98a75d9fb3e0ed369ccd0b4bb3f67805a51c8d95"},"cell_type":"markdown","source":"## Printing the shape of the Datasets"},{"metadata":{"_cell_guid":"72e903bd-ad8d-4751-ba2d-0deddadd4ab3","_uuid":"ac95a6317ce1baebb4f17d18388028318f48b509","_execution_state":"idle","trusted":true},"cell_type":"code","source":"print('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e4e58453-9f36-4265-bdc7-0a3b5337129c","_uuid":"478435d2e2526e99f684a8c063b05ae08a82e8f0"},"cell_type":"markdown","source":">  ## Reshape To Match The Keras's Expectations"},{"metadata":{"_cell_guid":"5d6167d5-1e96-40e6-af0c-b960567ef033","_uuid":"5e4de6b35dbd4db773e890c4d16840485a4f74ad","_execution_state":"idle","trusted":true},"cell_type":"code","source":"X_train = x_train.reshape(x_train.shape[0], 28, 28,1)\nX_test = x_test.reshape(x_test.shape[0], 28, 28,1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"bb79bc10-9ba2-44fe-a349-570520cd8a80","_uuid":"297c1b8aab418daee490ae79f474ca7814d86378","_execution_state":"idle","trusted":true},"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nbatch_size = 64\nnum_classes = 10\nepochs = 20\ninput_shape = (28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"8c4ecbcb-268c-48e3-ab93-9015e12d0e79","_uuid":"7a5463286287b10f7bdbbbfae8fabf764f6d5b1d","_execution_state":"idle","trusted":true},"cell_type":"code","source":"# convert class vectors to binary class matrices One Hot Encoding\ny_train = keras.utils.to_categorical(y_train, num_classes)\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4448455e-bbb5-4276-9d71-142d6b724c87","_uuid":"56980ded182bdfd2eeb5b1fe919fdefe5cd5e309","collapsed":true},"cell_type":"markdown","source":"**Linear Model**"},{"metadata":{"_cell_guid":"097867df-c77b-4ca8-988a-7baea6f4e387","_uuid":"577c7efb2d90f284383906e712a68df6233fe4e0","_execution_state":"idle","trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))\nmodel.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Dropout(0.20))\nmodel.add(Conv2D(64, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))\nmodel.add(Conv2D(64, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(128, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer=keras.optimizers.RMSprop(),\n              metrics=['accuracy'])\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)\n\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=15, # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"8dd6be49-de4f-4e76-a6c6-5f2dc9d4f304","scrolled":false,"_uuid":"9197045f0084aa72f0bd8ece8fb03eca9ab08622","_execution_state":"idle","trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"33d2485e-df3b-415b-bf2c-3cc1d1afddc1","_uuid":"fc13b3bb1a906117ae78cfd03639e165072f2e45","_execution_state":"idle","trusted":true},"cell_type":"code","source":"datagen.fit(X_train)\nh = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,Y_val),\n                              verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size\n                              , callbacks=[learning_rate_reduction],)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e0f319f7-c701-4ce4-b100-a0db07b9edef","_uuid":"4410fb664affc57163466e4949390b9b97a5d291"},"cell_type":"markdown","source":"## Basic Simple Plot And Evaluation"},{"metadata":{"_cell_guid":"19003034-e42b-4ec4-99d6-abefc7411451","_uuid":"0963a61adf30eba3b8c0d885eee69063e086634d","trusted":true},"cell_type":"code","source":"final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)\nprint(\"Final loss: {0:.6f}, final accuracy: {1:.6f}\".format(final_loss, final_acc))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2c64c843-e1d0-449f-b5a5-e51955a30caf","_uuid":"f5355803304624fae4bde1b4829d6329433395d6","trusted":true},"cell_type":"code","source":"# Look at confusion matrix \n#Note, this code is taken straight from the SKLEARN website, an nice way of viewing confusion matrix.\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred, axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val, axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"04c6a010-c928-4acb-bfed-3426a474cf18","_uuid":"cfffb6b672be442d54317e6d92b2c0b180a91934","_execution_state":"idle","trusted":true},"cell_type":"code","source":"print(h.history.keys())\naccuracy = h.history['acc']\nval_accuracy = h.history['val_acc']\nloss = h.history['loss']\nval_loss = h.history['val_loss']\nepochs = range(len(accuracy))\nplt.plot(epochs, accuracy, 'bo', label='Training accuracy')\nplt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.show()\nplt.figure()\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"57d9d586-928d-4d6b-9ee7-21e2071e3631","_uuid":"a4c3acd1a00f77cf809d7f7867545e03133ae36c","trusted":true},"cell_type":"code","source":"# Errors are difference between predicted labels and true labels\nerrors = (Y_pred_classes - Y_true != 0)\n\nY_pred_classes_errors = Y_pred_classes[errors]\nY_pred_errors = Y_pred[errors]\nY_true_errors = Y_true[errors]\nX_val_errors = X_val[errors]\n\ndef display_errors(errors_index,img_errors,pred_errors, obs_errors):\n    \"\"\" This function shows 6 images with their predicted and real labels\"\"\"\n    n = 0\n    nrows = 2\n    ncols = 3\n    fig, ax = plt.subplots(nrows,ncols,sharex=True,sharey=True)\n    for row in range(nrows):\n        for col in range(ncols):\n            error = errors_index[n]\n            ax[row,col].imshow((img_errors[error]).reshape((28,28)))\n            ax[row,col].set_title(\"Predicted label :{}\\nTrue label :{}\".format(pred_errors[error],obs_errors[error]))\n            n += 1\n\n# Probabilities of the wrong predicted numbers\nY_pred_errors_prob = np.max(Y_pred_errors,axis = 1)\n\n# Predicted probabilities of the true values in the error set\ntrue_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))\n\n# Difference between the probability of the predicted label and the true label\ndelta_pred_true_errors = Y_pred_errors_prob - true_prob_errors\n\n# Sorted list of the delta prob errors\nsorted_dela_errors = np.argsort(delta_pred_true_errors)\n\n# Top 6 errors \nmost_important_errors = sorted_dela_errors[-6:]\n\n# Show the top 6 errors\ndisplay_errors(most_important_errors, X_val_errors, Y_pred_classes_errors, Y_true_errors)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"bcff5ccd-a9e2-4b1b-bd5a-1ba7342a5063","_uuid":"bdbee7b242258cfed6bdb133690ef60b2460b6a1"},"cell_type":"markdown","source":"## Activations Look Like What?"},{"metadata":{"_cell_guid":"59000bb4-e5e5-4c69-8c32-6e4555297b2b","_uuid":"e3140af1b8d24957702f3aa0ad36b269faa2c84d"},"cell_type":"markdown","source":"It looks like diversity of the similar patterns present on multiple classes effect the performance of the classifier although CNN is a robust architechture."},{"metadata":{"_cell_guid":"f2f32aac-0426-456c-aacd-c009052027f0","_uuid":"fc25e889e5783e51be1bf6cfa0a43189ee3f9d97","trusted":true},"cell_type":"code","source":"test_im = X_train[154]\nplt.imshow(test_im.reshape(28,28), cmap='viridis', interpolation='none')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d3f60e88-da0c-4b99-bb3d-104bf36ea1c9","_uuid":"2c1df53f0cd628623fc0db430b2a11bc5a915ef9"},"cell_type":"markdown","source":"**Let's see the activation of the 2nd channel of the first layer:**\n\n**Had taken help from the keras [docs](https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer), this [answer](https://stackoverflow.com/questions/41711190/keras-how-to-get-the-output-of-each-layer) on  StackOverFlow**"},{"metadata":{"_cell_guid":"49d82f65-b772-4e52-addc-b0dad8c32831","_uuid":"75c2f529fc00f9d7afd1a9ee82107339cf9a0316","trusted":true},"cell_type":"code","source":"from keras import models\nlayer_outputs = [layer.output for layer in model.layers[:8]]\nactivation_model = models.Model(input=model.input, output=layer_outputs)\nactivations = activation_model.predict(test_im.reshape(1,28,28,1))\n\nfirst_layer_activation = activations[0]\nplt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"39aea2f5-efd4-4dc4-850d-677f16f27a61","_uuid":"52223ddedf9c6d4e5d408a1535e7d2d718a171fc"},"cell_type":"markdown","source":"**Let's plot the activations of the other conv layers as well.**"},{"metadata":{"_cell_guid":"80c2eda5-be86-4b86-ba18-55816fee55a9","_uuid":"7dc5fe4bc164f504d9584ccab9efa70fac60e1ee","trusted":true},"cell_type":"code","source":"model.layers[:-1]# Droping The Last Dense Layer","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3eb0d4ec-fb0a-4b90-8c3e-9763957794d1","_uuid":"d08dcd431e4103537a18a2eccc6956b57860618b","trusted":true},"cell_type":"code","source":"layer_names = []\nfor layer in model.layers[:-1]:\n    layer_names.append(layer.name) \nimages_per_row = 16\nfor layer_name, layer_activation in zip(layer_names, activations):\n    if layer_name.startswith('conv'):\n        n_features = layer_activation.shape[-1]\n        size = layer_activation.shape[1]\n        n_cols = n_features // images_per_row\n        display_grid = np.zeros((size * n_cols, images_per_row * size))\n        for col in range(n_cols):\n            for row in range(images_per_row):\n                channel_image = layer_activation[0,:, :, col * images_per_row + row]\n                channel_image -= channel_image.mean()\n                channel_image /= channel_image.std()\n                channel_image *= 64\n                channel_image += 128\n                channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n                display_grid[col * size : (col + 1) * size,\n                             row * size : (row + 1) * size] = channel_image\n        scale = 1. / size\n        plt.figure(figsize=(scale * display_grid.shape[1],\n                            scale * display_grid.shape[0]))\n        plt.title(layer_name)\n        plt.grid(False)\n        plt.imshow(display_grid, aspect='auto', cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"62162db6-d6b1-4639-8604-5c672065b2b4","_uuid":"5a8b881e43f6cb5ddf242a03d2589388012cf10c","trusted":true},"cell_type":"code","source":"layer_names = []\nfor layer in model.layers[:-1]:\n    layer_names.append(layer.name) \nimages_per_row = 16\nfor layer_name, layer_activation in zip(layer_names, activations):\n    if layer_name.startswith('max'):\n        n_features = layer_activation.shape[-1]\n        size = layer_activation.shape[1]\n        n_cols = n_features // images_per_row\n        display_grid = np.zeros((size * n_cols, images_per_row * size))\n        for col in range(n_cols):\n            for row in range(images_per_row):\n                channel_image = layer_activation[0,:, :, col * images_per_row + row]\n                channel_image -= channel_image.mean()\n                channel_image /= channel_image.std()\n                channel_image *= 64\n                channel_image += 128\n                channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n                display_grid[col * size : (col + 1) * size,\n                             row * size : (row + 1) * size] = channel_image\n        scale = 1. / size\n        plt.figure(figsize=(scale * display_grid.shape[1],\n                            scale * display_grid.shape[0]))\n        plt.title(layer_name)\n        plt.grid(False)\n        plt.imshow(display_grid, aspect='auto', cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"eba54d2a-7078-44ec-a7ca-87b0fece7861","_uuid":"c695cedecaaed016df2983fbf271177b7dc3a6e4","trusted":true},"cell_type":"code","source":"layer_names = []\nfor layer in model.layers[:-1]:\n    layer_names.append(layer.name) \nimages_per_row = 16\nfor layer_name, layer_activation in zip(layer_names, activations):\n    if layer_name.startswith('drop'):\n        n_features = layer_activation.shape[-1]\n        size = layer_activation.shape[1]\n        n_cols = n_features // images_per_row\n        display_grid = np.zeros((size * n_cols, images_per_row * size))\n        for col in range(n_cols):\n            for row in range(images_per_row):\n                channel_image = layer_activation[0,:, :, col * images_per_row + row]\n                channel_image -= channel_image.mean()\n                channel_image /= channel_image.std()\n                channel_image *= 64\n                channel_image += 128\n                channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n                display_grid[col * size : (col + 1) * size,\n                             row * size : (row + 1) * size] = channel_image\n        scale = 1. / size\n        plt.figure(figsize=(scale * display_grid.shape[1],\n                            scale * display_grid.shape[0]))\n        plt.title(layer_name)\n        plt.grid(False)\n        plt.imshow(display_grid, aspect='auto', cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"755625bf-de66-45ac-96d2-4204d95e2ced","_uuid":"52100b7260f819aeab482023ebc181990d96fcde"},"cell_type":"markdown","source":"## Classifcation Report"},{"metadata":{"_cell_guid":"fdf357db-a224-4073-9f74-c3b0e748b858","_uuid":"fade6e45a61151bed5bfebfa77fe6e52e44ee1b7","trusted":true},"cell_type":"code","source":"# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred, axis = 1)\nY_true_classes = np.argmax(Y_val, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_pred_classes[:5], Y_true_classes[:5]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f1e6858b-0fbb-476c-8e74-56c57c80057f","_uuid":"49e840a9a0c94b05b6dfbb9bb761fee412ea7624","trusted":true},"cell_type":"code","source":"from sklearn.metrics import classification_report\ntarget_names = [\"Class {}\".format(i) for i in range(num_classes)]\nprint(classification_report(Y_true_classes, Y_pred_classes, target_names=target_names))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"7fe384c1-4777-4c9a-a11e-762c448b5265","_uuid":"4cadfded20b04c8dfd1929699330565d774285b9","_execution_state":"idle","trusted":true},"cell_type":"code","source":"predicted_classes = model.predict_classes(X_test)\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(predicted_classes)+1)),\n                         \"Label\": predicted_classes})\nsubmissions.to_csv(\"asd.csv\", index=False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ebe5f5cb-1603-401f-a2a5-11cf2d957475","_uuid":"b98487e93dceaf5dbee33150dfc7f2aedaf6f80e","trusted":true},"cell_type":"code","source":"model.save('my_model_1.h5')\njson_string = model.to_json()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","language":"python","display_name":"Python 3"},"language_info":{"mimetype":"text/x-python","version":"3.6.4","pygments_lexer":"ipython3","nbconvert_exporter":"python","name":"python","codemirror_mode":{"name":"ipython","version":3},"file_extension":".py"}},"nbformat":4,"nbformat_minor":1}