{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport keras\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dropout\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38135eeaac6cf85e7c595ab58f7635b6ede81b4f"},"cell_type":"code","source":"# digits = pd.read_csv(\"../input/digit-recognizer/train.csv\") # 1170th\ndigitsTest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ndigits = pd.read_csv(\"../input/mnist-in-csv/mnist_train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9365d1c243a50eb16c63f7008631ffd62702702"},"cell_type":"code","source":"digits_ext.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e4c6d8d550a97cb95b2f1ed8fc9901e62db19e7"},"cell_type":"code","source":"digits.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2f41ae0ebcaf517648138308a65b67a58295804"},"cell_type":"code","source":"digits.shape, digitsTest.shape, digits_ext.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0bdd3b5e3a58e148576e42a86b8339efba82097b"},"cell_type":"markdown","source":"> We are seperating the digits of MNIST dataset ie the labels and the pixels. Train and trainLabels are two sets of arrays to do that! "},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = digits.values[:,1:]\ntrainLabels = digits.values[:,0]\ntest = digitsTest.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ff60bbe6a21bb73a269b896fddc4ed696f4fe08"},"cell_type":"code","source":"train.shape, trainLabels.shape, test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4a6cf4dbc802e40d6c04553b76dcc9fb3eb7efa"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig = plt.figure()\nfor i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.tight_layout()\n    plt.imshow(train[i].reshape(28,-1),cmap=\"gray\")\n    plt.title(\"Digit: {}\".format(trainLabels[i]))\n    plt.xticks([])\n    plt.yticks([])\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d32f24d335271233b649214189b3016fbaec91d"},"cell_type":"code","source":"train = train.astype(\"float32\")\ntrain/=255.0\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aeb58b6a06752d27046900099af2640fc0acb84d"},"cell_type":"code","source":"trainLabels[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a785127085a089dd8942a64f1278f66f775979b4"},"cell_type":"code","source":"num_categories = 10\ntrainLabels = keras.utils.to_categorical(trainLabels,num_categories)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16d0da53b1034d17354f24557a1849c82a4c4830"},"cell_type":"code","source":"trainLabels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f446352e50b7aa8bca4a4d754c651c4ef2efaa56"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import Flatten\nfrom keras.layers import Dense","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dcacc58dc67d6c1f453d563aae42d276215efc24"},"cell_type":"code","source":"\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6bdee8a464e2f2c1a287174d8e7f9f6ecfd95d2"},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b4550c3f8807cf32a23d83fd295a1fa2eb918f0"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, \n                 kernel_size=(3,3), \n                 activation = \"relu\", \n                 input_shape = (28,28,1)\n))\n\nmodel.add(Conv2D(64,(3,3),activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3),activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(128,activation = \"relu\"))\n\nmodel.add(Dense(128, activation = \"relu\"))\n\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_categories, activation = \"softmax\"))\n\n#Adaptive learning rate (adaDelta) is a popular form of gradient descent rivaled only by adam and adagrad\n#categorical ce since we have multiple classes (10) \nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer=keras.optimizers.Adam(),\n              metrics=['accuracy'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29c2698846da06aba9a7fbef8b634fe1cb1419e6"},"cell_type":"code","source":"batch_size = 128\nnum_epoch = 15\n#model training\n\nmodel_log = model.fit(train.reshape(-1, 28,28,1), trainLabels,\n          batch_size=batch_size,\n          epochs=num_epoch,\n          verbose=1,\n          \n          )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a878a6a857e64dabc3c9b99a268c7e5996db5fb9"},"cell_type":"code","source":"ypred = model.predict(test.reshape(-1,28,28,1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0287d7927603b188e1bfb273e701066f2bd692a4"},"cell_type":"code","source":"import os\n# plotting the metrics\nfig = plt.figure()\n# plt.subplot(2,1,1)\nplt.plot(model_log.history['acc'])\nplt.plot(model_log.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='lower right')\nplt.tight_layout()\nfig","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb549dd893535bb6d2a98fa12ebd19ba30991336"},"cell_type":"code","source":"ypred = np.argmax(ypred, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51d76174cb95d411df96cd5acff7b965d1682115"},"cell_type":"code","source":"plt.imshow(test[0].reshape(-1,28), cmap=\"gray\")\nprint(ypred[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfe97b2e47f939af35ae5e1ef80b6f47359991c4"},"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nfig = plt.figure(1)\nplt.figure(figsize=(12,12))\nfor i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.tight_layout()\n    plt.imshow(test[i].reshape(28,-1),cmap=\"gray\")\n    plt.title(\"Digit: {}\".format((ypred[i])))\n    plt.xticks([])\n    plt.yticks([])\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79ad5e729d97e079bef11b52290b6f7cf7ef3c8b"},"cell_type":"code","source":"ypreddf = pd.DataFrame(ypred)\nypreddf.columns = [\"Label\"]\nypreddf.head()\n\nypredDF = []\nfor i in range(len(ypred)):\n    ypredDF.append([i+1, ypred[i]])\nypredDF = pd.DataFrame(ypredDF, columns=[\"ImageId\",\"Label\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a6ffe17b872c3dad0902d86713d66de4be34fc9"},"cell_type":"code","source":"ypredDF.to_csv(\"submissions.csv\", index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3772e92b3e3a91757a16574cb541245774f0212"},"cell_type":"code","source":"pd.read_csv(\"submissions.csv\")","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}