{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport tensorflow as tf\nfrom tensorflow.keras import Sequential, regularizers\nfrom tensorflow.keras.layers import Dense, Dropout, Conv2D, Flatten\nfrom IPython.display import display\n\nprint('import done')\nsns.set(style='white', context='notebook', palette='deep')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e9e0caf684f35b1c53f2c4dea243b42142e9d45d"},"cell_type":"code","source":"# data = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":false},"cell_type":"code","source":"class LoadData:\n    def __init__(self, path):\n        data = pd.read_csv(path)\n        self.hasY = 'label' in data.columns\n        if self.hasY:\n            self.label = tf.keras.utils.to_categorical(data.label, num_classes = 10)\n        self.feature = data.drop(columns='label') / 255 if self.hasY else data\n        self.feature = self.feature.values.reshape(-1, 28, 28, 1) # -1 to retain the dimension, 1 for channels (RGB)\n        \n    def showImages(self, imgIds):\n        factor = 10\n        COL = 7\n        nrows, ncols = len(imgIds) / COL + 1, COL\n        plt.figure(figsize=(3 * COL, COL))        \n        for i, imgId in enumerate(imgIds):\n            plt.subplot(nrows, ncols, i + 1)\n            plt.imshow(self.feature[imgId][:,:,0], cmap='gray_r')\n            if self.hasY:\n                plt.title(self.label[imgId].argmax())\n            \n    def splitData(self, test_size):\n        self.trainX, self.testX, self.trainY, self.testY = train_test_split(self.feature, self.label, test_size = test_size, random_state=0)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2140c12459b3ac0560c044b0eca27922fa95cc79"},"cell_type":"code","source":"inputData = LoadData('../input/train.csv')\ninputData.showImages([2, 4, 6, 7, 9, 11, 41])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"708bbdc698698168a6f591070edd92802bd0945b"},"cell_type":"code","source":"inputData.splitData(0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d7cb431c189d2e8d73045f9083a8b2ea112f28c"},"cell_type":"code","source":"inputData.trainX[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c743ff01e2a5c339af021bc9d59daaf3a421c38"},"cell_type":"code","source":"# Model\nmodel = Sequential()\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=inputData.trainX[0].shape,\n                kernel_regularizer=regularizers.l2(0.001)))\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu',\n                kernel_regularizer=regularizers.l2(0.001)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu',\n                kernel_regularizer=regularizers.l2(0.01)))\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu',\n                kernel_regularizer=regularizers.l2(0.01)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(10, activation = \"softmax\"))\n\nmodel.summary()\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adagrad(lr=0.01),\n              metrics=['categorical_accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e6f8bc703602d938ceb230b9f36a54397a58ac5"},"cell_type":"code","source":"history = model.fit(inputData.trainX, inputData.trainY, epochs=3,\n                    validation_data=(inputData.testX, inputData.testY))\nplt.figure()\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.plot(history.epoch, np.array(history.history['categorical_accuracy']),\n       label='Train accuracy')\nplt.plot(history.epoch, np.array(history.history['val_categorical_accuracy']),\n       label = 'Val accuracy')\nplt.plot(history.epoch, np.array(history.history['loss']),\n       label = 'Train loss')\nplt.plot(history.epoch, np.array(history.history['val_loss']),\n       label = 'Val loss')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba5b4058d4c35d0036939874f4e841713e6fde02"},"cell_type":"code","source":"history.history","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ac90ca3c4170a46894deef8d2515f18197f439e"},"cell_type":"code","source":"testData = LoadData('../input/test.csv')\ntestData.showImages([4, 8, 19])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b0dad30c60e1104a5f2f20c6d799bbf75ece374"},"cell_type":"code","source":"predicted_digit = model.predict(testData.feature)\npredicted_digit","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56078c06d3bf1df93fc0aa3a41a272daf4236b26"},"cell_type":"code","source":"# display(type(predicted_digit))\n# display(predicted_digit.shape)\nsubmission = pd.DataFrame({'ImageId': range(1, testData.feature.shape[0] + 1), 'Label': predicted_digit.argmax(axis=1)})\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2277e302682a3a491c5db0220cd9c373add121ab"},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}