{"cells":[{"metadata":{"_cell_guid":"64d7cadc-9089-4b52-99b9-1e067bc60900","_uuid":"8a7247379c9c85dfa9699354d63666d46dc4f478","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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e474dd9a-a958-4d21-b045-785c2f2733f7","_uuid":"4872c195b88d64893bbad14d960ed85131a01c5a","trusted":true},"cell_type":"code","source":"#load other modules\nimport time\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"835e35ad-4b2c-4554-9846-3ca09411d9fa","collapsed":true,"_uuid":"4b571007642a783bd3577fe17e8b11984dacf81e","trusted":true},"cell_type":"code","source":"#Load train and test dataset\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"763ef5fa-29cc-477c-83e9-61a315299f26","_uuid":"5e83f138ad7e7673bda3692a1334dbd39d609f91","trusted":true},"cell_type":"code","source":"#Printing the size of the dataframe\nprint (\"Training dataset has %i observations and %i variables\" %(train.shape[0], train.shape[1]))\nprint (\"Testing dataset has %i observations and %i variables\" %(test.shape[0], test.shape[1]))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"774dade0-b5e8-438c-8c51-1d18fd068d7d","collapsed":true,"_uuid":"77fa5444f8c630dc6f09f180cc5a9ee34c484471","trusted":true},"cell_type":"code","source":"# Seperating target and image pixels\nX_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels\ntest_x = test.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"627cf734-1adc-412a-89dc-4c7e3236eec9","_uuid":"9f2270813fc420e87dc1a5c4ca92c8244e719e2b","trusted":true},"cell_type":"code","source":"# Find the unique numbers from the train labels\nclasses = np.unique(y_train)\nnClasses = len(classes)\nprint('Total number of outputs : ', nClasses)\nprint('Output classes : ', classes)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c902e0ed-a187-450d-89ad-62247c29b563","_uuid":"a53705fa680210a2cc3b3506cf2ef154d0408f9d","trusted":true},"cell_type":"code","source":"#Reshaping train to 28 * 28 pixel size with 1 color (grey scale)\nX_train = X_train.reshape(X_train.shape[0], 28, 28,1)\nX_train.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"7a82ec0a-718f-4cd3-a491-7565f6e5a116","_uuid":"83dca75d7f0efbba90feaf28c053db2f48e79e1d","trusted":true},"cell_type":"code","source":"#Reshaping test to 28 * 28 pixel size with 1 color (grey scale)\ntest_x = test_x.reshape(test_x.shape[0], 28, 28,1)\ntest_x.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"40b36c06-5903-4abc-beab-ae1a050bb2be","collapsed":true,"_uuid":"3da63f4af9871d701235715d4625a96029f5aead","trusted":true},"cell_type":"code","source":"# Normalize the data,\n# Colors are from 0 to 255. \nX_train = X_train / 255.0\ntest_x = test_x / 255.0","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d405f4d8-98bb-4cce-b8db-232ec6c3dc75","collapsed":true,"_uuid":"a67f99094d75717f49dae4f69dbf6d1ccd7e43f8","trusted":true},"cell_type":"code","source":"#Label encoding\ny_train = to_categorical(y_train, num_classes = nClasses)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fd185b4b-c38f-4009-8a8e-0a272b78ae45","_uuid":"953d994ad3cc2a4e8785ead571e1af1b96f4f15d","trusted":true},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f65e8a7a-ac51-4e88-b78e-8fab5db1b0b3","collapsed":true,"_uuid":"24a22011fb455361c6761bc50f41bb0b5abf8941","trusted":true},"cell_type":"code","source":"np.random.seed(0)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0d98f78f-2718-4509-880d-64ccf6bb06bb","_uuid":"dfd86d21fc20f268cf59ec3e5e08ccd98c7e43b2","trusted":true},"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=0)\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3892942d-89ea-4a9b-b1b0-3d3f3398447c","collapsed":true,"_uuid":"1df545d53ea23d77efa976713195e27bb96d4216","trusted":true},"cell_type":"code","source":"input_shape = X_train.shape[1:]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2b5ece88-ebc0-4ba6-ba40-331b42bd73de","_uuid":"1cc5c0eb43bcf438270a91de64ef34ef557f876b","trusted":true},"cell_type":"code","source":"def createModel():\n    model = Sequential()\n    # The first two layers with 32 filters of filter size 3x3\n    model.add(Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=input_shape))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.5))\n\n    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.5))\n\n#    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n#    model.add(Conv2D(64, (3, 3), activation='relu'))\n#    model.add(MaxPooling2D(pool_size=(2, 2)))\n#    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(nClasses, activation='softmax'))\n    \n    return model\n\nmodel = createModel()\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5512abb5-61e0-4657-96d7-9588e79b4bf1","_uuid":"e18d6c7578d32240e6c3aa7aa907fbdc33d4546d","trusted":true},"cell_type":"code","source":"starttime = time.time()\ncnn = model.fit(X_train, y_train, epochs=20, batch_size=100, verbose=1, validation_data = (X_test, y_test))\nprint (\"Time elapsed in training: \", time.time()- starttime, \"s\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d224396d-e75c-49b2-a36d-bfcbd9374a4d","_uuid":"bd289fbbc45757856ac3bb4b8512980836f8fceb","trusted":true},"cell_type":"code","source":"#PLot\nplt.figure(figsize=[8,6])\nplt.plot(cnn.history['loss'],'r',linewidth=3.0)\nplt.plot(cnn.history['val_loss'],'b',linewidth=3.0)\nplt.legend(['Training loss', 'Validation Loss'],fontsize=18)\nplt.xlabel('Epochs ',fontsize=16)\nplt.ylabel('Loss',fontsize=16)\nplt.title('Loss Curves',fontsize=16)\n\nplt.figure(figsize=[8,6])\nplt.plot(cnn.history['acc'],'r',linewidth=3.0)\nplt.plot(cnn.history['val_acc'],'b',linewidth=3.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=18)\nplt.xlabel('Epochs ',fontsize=16)\nplt.ylabel('Accuracy',fontsize=16)\nplt.title('Accuracy Curves',fontsize=16)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b5d7e88c-19fd-47a4-b6e8-38a1499d8753","collapsed":true,"_uuid":"5f2126754ebc6beab7988b354b7d21321d609d08","trusted":true},"cell_type":"code","source":"# predict results\nresults = model.predict(test_x)\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2b31a146-a474-4a45-8648-0e673746e335","collapsed":true,"_uuid":"29f2fe37513d879fd30cb65f9d5f46f90bc48a36","trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"mnist_cnn.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"add0698e-bf61-4ee7-b062-f978668fe6fb","collapsed":true,"_uuid":"d787ef9dfe68d902600852da26aab8204e94ae83","trusted":false},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}