{"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 os, warnings\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import Conv2D\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras import regularizers\nfrom sklearn.metrics import jaccard_score\nfrom sklearn.metrics import hamming_loss\nfrom sklearn.metrics import roc_auc_score\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:11.085177Z","iopub.execute_input":"2023-01-26T04:39:11.085523Z","iopub.status.idle":"2023-01-26T04:39:18.376334Z","shell.execute_reply.started":"2023-01-26T04:39:11.085440Z","shell.execute_reply":"2023-01-26T04:39:18.375463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set Matplotlib defaults\nplt.rc('figure', autolayout=True)\nplt.rc('axes', labelweight='bold', labelsize='large',\n       titleweight='bold', titlesize=18, titlepad=10)\nplt.rc('image', cmap='magma')\nwarnings.filterwarnings(\"ignore\")\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:18.378069Z","iopub.execute_input":"2023-01-26T04:39:18.378320Z","iopub.status.idle":"2023-01-26T04:39:18.383524Z","shell.execute_reply.started":"2023-01-26T04:39:18.378286Z","shell.execute_reply":"2023-01-26T04:39:18.382866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:18.384445Z","iopub.execute_input":"2023-01-26T04:39:18.385249Z","iopub.status.idle":"2023-01-26T04:39:23.119754Z","shell.execute_reply.started":"2023-01-26T04:39:18.385214Z","shell.execute_reply":"2023-01-26T04:39:23.118871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX = train.drop('label',axis = 1)\nY = train.label\n\nX_train, X_valid, y_train, y_valid = train_test_split(X, Y, test_size = 0.2, random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.121394Z","iopub.execute_input":"2023-01-26T04:39:23.121658Z","iopub.status.idle":"2023-01-26T04:39:23.437035Z","shell.execute_reply.started":"2023-01-26T04:39:23.121629Z","shell.execute_reply":"2023-01-26T04:39:23.436037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"X_train = X_train.astype('float32')\nX_valid = X_valid.astype('float32')\n\ny_train = tf.keras.utils.to_categorical(y_train,10)\ny_valid = tf.keras.utils.to_categorical(y_valid,10)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.438226Z","iopub.execute_input":"2023-01-26T04:39:23.438462Z","iopub.status.idle":"2023-01-26T04:39:23.481145Z","shell.execute_reply.started":"2023-01-26T04:39:23.438425Z","shell.execute_reply":"2023-01-26T04:39:23.480072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.482488Z","iopub.execute_input":"2023-01-26T04:39:23.482905Z","iopub.status.idle":"2023-01-26T04:39:23.488910Z","shell.execute_reply.started":"2023-01-26T04:39:23.482872Z","shell.execute_reply":"2023-01-26T04:39:23.487694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n# X_train = X_train.reshape(-1,28,28,1)\n# X_valid = X_valid.reshape(-1,28,28,1)\n\n# datagen = ImageDataGenerator(\n#     rotation_range=0,\n#     width_shift_range=0.2,\n#     height_shift_range=0.2,\n#     shear_range=0.2,\n#     zoom_range=0.2,\n#     horizontal_flip=False,\n#     fill_mode='nearest')\n\n# # fit the generator to your data\n# datagen.fit(X_train_reshaped)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.490015Z","iopub.execute_input":"2023-01-26T04:39:23.490238Z","iopub.status.idle":"2023-01-26T04:39:23.499484Z","shell.execute_reply.started":"2023-01-26T04:39:23.490211Z","shell.execute_reply":"2023-01-26T04:39:23.498638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_reshaped = X_train.values.reshape(-1,28,28,1)\nX_valid_reshaped = X_valid.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.500773Z","iopub.execute_input":"2023-01-26T04:39:23.501290Z","iopub.status.idle":"2023-01-26T04:39:23.511962Z","shell.execute_reply.started":"2023-01-26T04:39:23.501259Z","shell.execute_reply":"2023-01-26T04:39:23.511341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"\n\nconv_layer = Conv2D(128, (3, 3), activation='relu', input_shape = (28,28,1))\n\nconv_layer_2 = Conv2D(64, (3, 3), activation='relu', padding = \"same\")\n\nmodel = tf.keras.Sequential([\n    conv_layer,\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(rate=0.2),\n    conv_layer_2,\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(rate=0.2),\n    \n    layers.Flatten(),\n    layers.BatchNormalization(),\n    layers.Dense(10, activation = 'sigmoid')\n])\n\nmodel.compile(optimizer='adam', \n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n\n\nearly_stopping = EarlyStopping(\n    monitor = 'val_loss',\n    patience = 10,\n    restore_best_weights=True\n)\n\ny_valid_reshaped = y_valid.reshape(-1,10)\n\ntraining = model.fit(\n    X_valid_reshaped,\n    y_valid_reshaped,\n    batch_size = 64,\n    epochs = 100,\n    validation_split=0.2,\n    verbose = 0,\n    callbacks = [early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:39:23.512870Z","iopub.execute_input":"2023-01-26T04:39:23.513655Z","iopub.status.idle":"2023-01-26T04:44:52.360122Z","shell.execute_reply.started":"2023-01-26T04:39:23.513625Z","shell.execute_reply":"2023-01-26T04:44:52.359092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Evaluating model**","metadata":{}},{"cell_type":"code","source":"\n\n\n# Calculate the predicted labels for your validation data\ny_pred = model.predict(X_valid_reshaped)\n\n# Convert the predicted labels to a binary format\ny_pred = (y_pred > 0.5).astype(int)\n\n# Calculate the Jaccard similarity score\n# jaccard_score_val = jaccard_score(y_valid, y_pred, average='micro')\n# hamming_loss_val = hamming_loss(y_valid, y_pred)\ntest_loss, test_acc = model.evaluate(X_train_reshaped, y_train)\n\n# print(\"Jaccard Similarity Score:\", jaccard_score_val)\n# print(\"Hamming Loss:\", hamming_loss_val)\nprint('Test Accuracy : {:.2f}%'.format(test_acc*100))\nprint('Test Loss : {:.2f}%'.format(test_loss*100))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:44:52.362151Z","iopub.execute_input":"2023-01-26T04:44:52.362374Z","iopub.status.idle":"2023-01-26T04:45:07.566518Z","shell.execute_reply.started":"2023-01-26T04:44:52.362348Z","shell.execute_reply":"2023-01-26T04:45:07.565896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Make predictions on the validation set\ny_pred = model.predict(X_valid_reshaped)\n\n# Calculate AUC-ROC score\nauc = roc_auc_score(y_valid_reshaped, y_pred)\nprint('AUC-ROC score:', auc)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:45:07.567382Z","iopub.execute_input":"2023-01-26T04:45:07.567587Z","iopub.status.idle":"2023-01-26T04:45:10.348154Z","shell.execute_reply.started":"2023-01-26T04:45:07.567563Z","shell.execute_reply":"2023-01-26T04:45:10.347228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,6))\nplt.subplot(1,2,1)\nplt.plot(training.history['accuracy'])\nplt.plot(training.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'Test'], loc = 'lower right')\nplt.subplot(1,2,2)\nplt.plot(training.history['loss'])\nplt.plot(training.history['val_loss'])\nplt.title('Model Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Test'], loc = 'upper left')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:45:10.349299Z","iopub.execute_input":"2023-01-26T04:45:10.349603Z","iopub.status.idle":"2023-01-26T04:45:10.836964Z","shell.execute_reply.started":"2023-01-26T04:45:10.349578Z","shell.execute_reply":"2023-01-26T04:45:10.835843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_reshaped = test.values.reshape(-1, 28, 28, 1)\n\npred = np.argmax(model.predict(test_reshaped), axis=1)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:47:30.456558Z","iopub.execute_input":"2023-01-26T04:47:30.457111Z","iopub.status.idle":"2023-01-26T04:47:40.332507Z","shell.execute_reply.started":"2023-01-26T04:47:30.457075Z","shell.execute_reply":"2023-01-26T04:47:40.331612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(\n    {\n        'ImageId': np.arange(1, pred.shape[0]+1),\n        'Label': pred\n    }\n)\nsubmission.to_csv('./submission.csv', index=False)\nprint(\"Succ\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T04:47:42.050441Z","iopub.execute_input":"2023-01-26T04:47:42.050737Z","iopub.status.idle":"2023-01-26T04:47:42.088830Z","shell.execute_reply.started":"2023-01-26T04:47:42.050705Z","shell.execute_reply":"2023-01-26T04:47:42.087948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}