{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#LOAD DATA\nrawTrainData = pd.read_csv('../input/train.csv')\nrawTestData = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0eb93759d1bd8613980500c15fb17f08da4a48c"},"cell_type":"code","source":"trainData = rawTrainData.values\ntestData  = rawTestData.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7689bf933c2d783aa67603e771363fd70cd385d"},"cell_type":"code","source":"#test code\nprint(trainData.shape)\nprint(testData.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1b2ef06aa6fe63c102a9890bcc8899aef749c19"},"cell_type":"code","source":"from keras import models\nfrom keras import layers\nimport keras\ncallbacks_list = [\n    keras.callbacks.EarlyStopping(\n    monitor='val_acc',\n    patience=4\n    ),\n# keras.callbacks.ModelCheckpoint(\n#     filepath='my_model.h5',\n#     monitor='val_loss',\n#     save_best_only=True,\n#     ),\nkeras.callbacks.ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.1,\n    patience=2,\n    )\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1569da1b435decbbef91dd9725e94ba0e0fc840"},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation=\"relu\",input_shape = (28,28,1)))\nmodel.add(layers.MaxPool2D(2,2))\nmodel.add(layers.Conv2D(64,(3,3),activation=\"relu\"))\nmodel.add(layers.MaxPool2D(2,2))\nmodel.add(layers.Conv2D(80,(3,3),activation=\"relu\"))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512,activation=\"relu\"))\nmodel.add(layers.Dense(10,activation=\"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8f9101743da9220087e928d38d09357ac7b0d33"},"cell_type":"code","source":"model.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5f50e8a5b96f64d3b190828001602871ca347c9"},"cell_type":"code","source":"from keras import optimizers\n\nmodel.compile(optimizer='rmsprop',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fe20cec4e48e92b2c81b5f1823a642885011195"},"cell_type":"code","source":"#separating lables and input from trainData\ntrainInputs = trainData[:,1:]\ntrainLabels = trainData[:,:1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f852e1321c342de3e8affb531f8ebe56050840d1"},"cell_type":"code","source":"print(trainData.shape)\n\n\nprint(trainInputs.shape)\nprint(trainLabels.shape)\n\n\nrawTrainData.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67d109db676fc850bcf4828086b1e27f83a21d0d"},"cell_type":"code","source":"#reshape input data and labels\n\nfrom keras.utils import to_categorical\ntrainLabels = to_categorical(trainLabels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9b39af6dbf89d6eaf7f3dbcd85d1b7b1d9d8f8d"},"cell_type":"code","source":"#reshape testData\ntrainInputs = trainInputs.reshape(42000,28,28,1)\ntestData = testData.reshape(28000,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0115cd28e9df96011e2ad065c4da9978f70b10b4"},"cell_type":"code","source":"print(trainInputs.shape)\nprint(trainLabels.shape)\nprint(testData.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc355233b525d413af72336061119ae44bad3c08"},"cell_type":"code","source":"trainInputs = trainInputs.astype('float32') / 255\ntestData = testData.astype('float32') / 255\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1978963e1581b93601a3855fd35d3747a8beb18f"},"cell_type":"code","source":"history = model.fit(trainInputs,trainLabels,epochs=40,batch_size=64,callbacks=callbacks_list,validation_split=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07ec7960b33600442a0fc4d5a5b3f0cc57f4d1eb"},"cell_type":"code","source":"testOutputs = model.predict_classes(testData)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb2f8d08085ed09d50cfb677209663231120c489"},"cell_type":"code","source":"print(testOutputs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"772ad8fd5519a8d095722b64e50d9de1bbc2616c"},"cell_type":"code","source":"imageIDs = []\nfor i in range(1,28001):\n    imageIDs.append(i)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"951309d4f7163df2dd318f0e1e3d44b848bf682b"},"cell_type":"code","source":"#make submission file\nsubmission = pd.DataFrame({\n        \"ImageId\": imageIDs,\n        \"Label\": testOutputs\n    })\nsubmission.to_csv(\"kaggle_mnist_sub.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}