{"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_set = pd.read_csv(\"../input/train.csv\")\ntest_set = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b6015e69121223d21a748cc3bbc5ff227bab4dd"},"cell_type":"code","source":"Y_train = train_set[\"label\"]\n\n# Drop 'label' column\nX_train = train_set.drop(labels = [\"label\"],axis = 1) \n\n# free some space\ndel train_set","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5de007677cf2373e1346e5611065ce1424933289"},"cell_type":"code","source":"X_train.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0af6ded79319b5e8b5c3c969c9d0074bb017510d"},"cell_type":"code","source":"X_train=X_train/255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"559bd1823593d525bcf84cba3a711792403f9a59"},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest_set = test_set.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2706b4f87ce9e07f6d7a86b7b7a096d9cbb2fde9"},"cell_type":"code","source":"\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop ,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nY_train = to_categorical(Y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fcafd45b0be0616ee0c0ee8a5741a494d315c44"},"cell_type":"code","source":"\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"144443ac0647c367228964d9893d163a2e7dffa5"},"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"165120bd75ad6c97dc32e04e6263dc7c4a64ff21"},"cell_type":"code","source":"def getmodel():\n    model=Sequential()\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'valid', strides=2\n                     ,activation ='relu', input_shape = (28,28,1)))\n    model.add(Conv2D(filters = 48, kernel_size = (4,4),padding = 'valid', strides=2\n                     ,activation ='relu'))\n    model.add(Dropout(0.25))\n\n\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', strides=1\n                     ,activation ='relu'))\n    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(576, activation = \"relu\"))\n    model.add(Dropout(0.25))\n    model.add(Dense(100,activation='relu'))\n    \n    model.add(Dense(10, activation = \"softmax\"))\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"658d673457b4b02047ed128905bfa1d046534492"},"cell_type":"code","source":"model=getmodel()\nmodel.compile(optimizer=Adam(epsilon=1e-08,lr=0.0005), loss='categorical_crossentropy',metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a736ce81672bec0476cb25dccbe9d87956dd030"},"cell_type":"code","source":"history = model.fit(X_train, Y_train, batch_size = 64, epochs = 30, \n         validation_data = (X_val, Y_val), verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9c9f9ee907f45c1825c0a6c9b1d193e18eb76af"},"cell_type":"code","source":"results = model.predict(test_set)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19daa5d58395fbe2c3ef1ca7002f08d5ec69f544"},"cell_type":"code","source":"results = np.argmax(results,axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27c17ff4df52125de9a15ec41fffcacf5d8b9893"},"cell_type":"code","source":"results = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"173ec88ba1d249be5c34a9e637f247f60cf99195"},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1d765fb9b308a2fe9684e1d46ff8d7b6d95865c"},"cell_type":"code","source":"print (results)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da77d3bfb2f7ea533621316fae83897ea72fbf96"},"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}