{"cells":[{"metadata":{"_uuid":"02604b0dd424a7fe58f95b9fe74778d124a71e7b"},"cell_type":"markdown","source":"# Digit recogntion with Keras\n## 1. Import required python libraries"},{"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 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\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"813af35badfeedfb0d0056a3f2b2e44bddf3d98f"},"cell_type":"markdown","source":"## 2. Import data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"digits = pd.read_csv('../input/train.csv')\ndigits.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"35c81fb85fedcac73da1a4c1c5eb91c19b269d55"},"cell_type":"markdown","source":"## 3. View sample data"},{"metadata":{"trusted":true,"_uuid":"57e4e2e84bd59bc9b0dc539143b0d86300f31da7"},"cell_type":"code","source":"digits.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3731b17d096e2439fb8315c1e906761b9cb2aeb"},"cell_type":"code","source":"index = 11\nplt.imshow(((digits.iloc[index,1:]).values).reshape(28,28),cmap='Greys')\nprint('The lable is : ' + str(digits.iloc[index, 0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6277b6f11534379d8d4b2774f833966ace8a6dce"},"cell_type":"markdown","source":"## 4. Process data"},{"metadata":{"trusted":true,"_uuid":"f94d8230ffa7d9204fdd62c027a0060bed5da8c3"},"cell_type":"code","source":"X_train = (digits.iloc[5000:,1:].values).reshape(digits.shape[0]-5000,28,28,1)/255.0\nY_train = to_categorical((digits.iloc[5000:,0].values).reshape(digits.shape[0]-5000,1))\nX_test = (digits.iloc[:5000,1:].values).reshape(5000,28,28,1)/255.0\nY_test = to_categorical((digits.iloc[:5000:,0].values).reshape(5000,1))\nprint(\"X_train shape : \"+str(X_train.shape))\nprint(\"Y_train shape : \"+str(Y_train.shape))\nprint(\"X_test shape : \"+str(X_test.shape))\nprint(\"Y_test shape : \"+str(Y_test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb64f257510f9a19fbe5f6d64a84eead3896099f"},"cell_type":"code","source":"index = 51\nplt.imshow(X_train[index,:].reshape(28,28),cmap='Greys')\nprint(\"Image lable : \"+str(Y_train[index]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a11de9b72d35e53ecf93fec17bb130510265e769"},"cell_type":"markdown","source":"## 5. Building the model"},{"metadata":{"trusted":true,"_uuid":"3dd18e347f1e5915b68d5021d141130e60b6b58b"},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\noptimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8256843b579fa0b07d0fc93d3bcf176243a238b"},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f391fa718a5aa00756fcd4ff94fc2264ba780a7b","scrolled":true},"cell_type":"code","source":"model.fit(X_train, Y_train, epochs=6, validation_data=(X_test, Y_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60415756a5d3fbda1790def48f7f3d3657fc3f13"},"cell_type":"code","source":"test_df = pd.read_csv('../input/test.csv')\ntest_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c8748b5bee86237a351e1cb4c9eab30532df592"},"cell_type":"code","source":"predictions = model.predict(((test_df.values).reshape(28000,28,28,1))/255.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f5e2dc98c19fd1e9e5b2498f6cc7085d524f36d"},"cell_type":"code","source":"predictions_array =  np.argmax(predictions, axis = 1)\nprint('pred shape : '+ str(predictions.shape))\nprint('predictions_array shape : '+ str(predictions_array.size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8eff7f2062a89ce5168b3408c0cd6c2a61fc7d35"},"cell_type":"code","source":"index = 53\nplt.imshow(test_df.iloc[index,:].values.reshape(28,28),cmap='Greys')\nprint(\"Predicted lable : \"+str(predictions[index]))\nprint(\"predictions_array lable : \"+str(predictions_array[index]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68e118def86c04c07e69ebbd0f4d973b5f2d5f84"},"cell_type":"code","source":"imageId = np.array(range(1,predictions_array.size + 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2bd2b6493a5de0c674ebd80a20e691aa55917dd"},"cell_type":"code","source":"my_submission = pd.DataFrame({'ImageId': imageId, 'Label': predictions_array})\nmy_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e20a727b99168851ff43b5d5843529c1a48d4cae"},"cell_type":"code","source":"my_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5edf344b4f9c0ece110a65aba4f385e480fc615"},"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}