{"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":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"401a1be73993ca3209c1f738dc85603c4e728fc4"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96e134dcf7abcaabe9fbd43eccb36e4ad0556796"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9abdecd83f0c3d761d098b923680b888668e8e4e"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f27b4e02b2ec849930e031598be03404c0433f8"},"cell_type":"code","source":"X_train = train\nX_train = X_train.drop('label',axis = 1)\ny_train = train['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5af20d8bfff894e65b9ef5968ef865caa2e536e"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0067995e7ec845e1656a1dc40c3a1ad2dfb0fc2"},"cell_type":"code","source":"#Checking for null values\nX_train.isnull().any().describe()\ntest.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47f379e9c01846ec3ae2a6f9211ae3ba70ef1232"},"cell_type":"code","source":"X_train = X_train/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3125e2b623ddadd711659948962f254f424efee2"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36a76b5aee44e7d18dc5a81ec923a92df23e3e97"},"cell_type":"code","source":"test = test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bf96b2744718b7bd8c721f961ce72ea2a92e545"},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e2348201f0ae90ac023336b5e945f1c8e63ae08"},"cell_type":"code","source":"X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5718c5fa7a680a1727f29e7bd762e873bc40d848"},"cell_type":"code","source":"#one hot encoding\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\ny_train = to_categorical(y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44595eff29c4c467ab1ebbd79719a9f3f55343f4"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2d0570cdd74b7ead918ba3811b4485bb7334985"},"cell_type":"code","source":"g = plt.imshow(X_train[3][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6667bc2ee4074d2331c6d36fd68b12218f513159"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11a0dd2069b388dbdf8f06752b42e888144a42e5"},"cell_type":"code","source":"from keras import backend as K\nK.set_image_dim_ordering('tf')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f6dd6e271d600825782c07cfc9fb9f28c3c89a82"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same',activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aacfd3809b0b46b66c303d4eef0b2109f44556ac"},"cell_type":"code","source":"model.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d770eef4da8a05b7b6dff9490aa0dc48da2201c"},"cell_type":"code","source":"from keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6a951bf1a3233d285dfd5c66651a9a4dcdc930f"},"cell_type":"code","source":"optimizer = Adam()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51b59feb9b87fae758bc18a574a66fa97a5ecc26"},"cell_type":"code","source":"model.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3d5f6101eae62108afb527b90c333080cc466a7"},"cell_type":"code","source":"epochs = 30\nbatch_size = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c35b82757a411d8941f9b8d73f29d0583762d5b1"},"cell_type":"code","source":"model.fit(X_train,y_train,batch_size=batch_size,epochs = epochs, validation_data = (X_val,y_val),verbose = 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9258a4b1edf00d68e7a0b2a3f9755813c5f0a3e"},"cell_type":"code","source":"results = model.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74e5f6054c9ef3aa13ec7711971dfc101cd753a2"},"cell_type":"code","source":"results","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d121be65cc0845f5e50b57a7c428e26a18e69e50"},"cell_type":"code","source":"results = np.argmax(results,axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ef959b9469b384ef42d07a5f9af6faad5b7c8c6"},"cell_type":"code","source":"results = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"321b58d552428d15e35ce3e8c597c43fea1f04b0"},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"cnn_mnist.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}