{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"# Imports\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPool2D, Flatten\nimport keras.backend as K\nfrom keras.utils import to_categorical\nfrom keras.models import load_model\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":86,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Load train and test data\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":87,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"389db632ff8697cf923fa2d3bb1b7a9fafb4523f","collapsed":true},"cell_type":"code","source":"train.head()","execution_count":88,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b301dbe082040d1f8d78a89c8ec6e83e01bd2367","collapsed":true},"cell_type":"code","source":"test.head()","execution_count":89,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0ec403e41cecb08b36730c1ead63beb7b49a4b4","collapsed":true},"cell_type":"code","source":"# reshape the datasets\ntrain_X = train.drop('label', axis=1)\ntrain_X = np.reshape(train_X.values, (train_X.shape[0], 28, 28, 1))\ntest_X = np.reshape(test.values, (test.shape[0], 28, 28, 1))\n\nif K.image_data_format() == 'channels_first' :\n    train_X = np.reshape(train_X.values, (train_X.shape[0], 1, 28, 28))\n    test_X = np.reshape(test.values, (test.shape[0], 1, 28, 28))\n\ntrain_Y = to_categorical(train['label'])\n\nprint('Train_X shape:', train_X.shape)\nprint('Train_Y shape:', train_Y.shape)\nprint('Test_X shape:', test_X.shape)","execution_count":90,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fae3169d3981a9f3ed6f4ae026a1428787a9bf0f","collapsed":true},"cell_type":"code","source":"# Visualizing an image\nplt.imshow(train_X[1,:,:,0])\nprint(train_Y[1].argmax())","execution_count":91,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5a87e3fa2cb5b2a49ad451d675cf0ac8fccdf39","collapsed":true},"cell_type":"code","source":"model_path = 'model.hdf5'\ntry:\n    print('Loading model -', model_path)\n    model = load_model(model_path)\n    print('Model successfully loaded!')\nexcept:\n    print(\"Can't find the model, creating a new one -\", model_path)\n    \n    # create the model\n    input_shape = (28,28,1) # number of channels, width, height\n    if K.image_data_format() == 'channels_first' :\n        input_shape = (1,28,28) # number of channels, width, height\n\n    model = Sequential()\n    model.add(Conv2D(64, (3,3), padding='same', input_shape=input_shape))\n    model.add(MaxPool2D(pool_size=(2,2)))\n    model.add(Conv2D(32, (2,2), padding='same'))\n    model.add(MaxPool2D(pool_size=(2,2)))\n    model.add(Flatten())\n    model.add(Dense(10, activation='softmax'))\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n    model.summary()\n    \n    # train the model\n    model.fit(train_X, train_Y, validation_split=0.2, batch_size=1000, epochs=20)\n    model.save(model_path)","execution_count":92,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05b77e2b9fe85879b41443c40a89e81e5147e0df","collapsed":true},"cell_type":"code","source":"# evaluate model\n#model.evaluate(train_X, train_Y) # prints loss and accuracy respectively","execution_count":93,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"21d7db6b0681946ab9dd29ed5c07771153e0d365"},"cell_type":"code","source":"# make predictions\npredictions = model.predict(test_X)","execution_count":94,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"458fa60e2f5d475ceef8ea8ebf863d316fc8f79e","collapsed":true},"cell_type":"code","source":"# test prediction\nprint(predictions[0].argmax())\nplt.imshow(test_X[0,:,:,0])","execution_count":95,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51ee19567b9f7757924f8e85d73ee6c1204afe26","collapsed":true},"cell_type":"code","source":"# save final predictions by getting highest probabilities of the numbers\nfinal_preds = []\nfor pred in predictions :\n    final_preds.append(pred.argmax())","execution_count":96,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e54518ce82a7b9c8ca07fc41d7d96ee14e76db55","collapsed":true},"cell_type":"code","source":"# Load sample submission file and insert our predictions into it\nsubmission = pd.read_csv('../input/sample_submission.csv')\nsubmission['Label'] = final_preds","execution_count":97,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df94cf26854f63b4986b23a6c10a98362f17ca9f","collapsed":true},"cell_type":"code","source":"# save submission file\nsubmission.to_csv('final_submission.csv', index=False)","execution_count":98,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74da613cd6c8c23db2c96c2273e05d58241309e9","collapsed":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0cc4b055def27e1d22fc3d0843a3b6f0c0d03164"},"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.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}