{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"training_data = pd.read_csv('../input/train.csv')\ntesting_data = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0702d5e0af794081e74301c52f0317a5ffb03d7"},"cell_type":"code","source":"# Transform DataFrame into array\ntraining_data_X = training_data.iloc[:, 1:].values\ntraining_data_y = training_data.iloc[:, 0].values\ntesting_data = testing_data.iloc[:, :].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfe7968a3bd91f4a630845a9df148fdeb9af3826"},"cell_type":"code","source":"# Data Normalization\nimport tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras.layers import Dense, Flatten, Activation, MaxPooling2D, Conv2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ae1382a6d5f992b80ba1ebd9e8c296cd262adca"},"cell_type":"code","source":"training_data_X = tf.keras.utils.normalize(training_data_X, axis=1)\ntesting_data = tf.keras.utils.normalize(testing_data, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b15284472e435d82a6e5cad0572e9900d7428a7d"},"cell_type":"code","source":"# Transform 1*784array into 28*28array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddfb3088d7b30de58a46e7d4395b6cd7cb59e3b6"},"cell_type":"code","source":"print(training_data_X.shape)\nprint(testing_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e3ba0f5ee490bb56bac46acc434ea1bec702c9a"},"cell_type":"code","source":"training_data_X = training_data_X.reshape(42000, 28, 28)\ntesting_data = testing_data.reshape(28000, 28, 28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b89a63555d59b195e5123bef462acc1b54c6f69"},"cell_type":"code","source":"print(training_data_X.shape)\nprint(testing_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f986e0bde9b0d649c359003b00ac29354e5d1a1c"},"cell_type":"code","source":"# Data Visualization \nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f045eb5db063bc25887dadfa65b19fbda66b2b67"},"cell_type":"code","source":"plt.imshow(training_data_X[0], cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b1facc32eb3f9b95213209e6ac1f0f5b8243b49"},"cell_type":"code","source":"# Creat CNN Model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e80741ee64f1559b64b7629ec7b80192542c67f"},"cell_type":"code","source":"# Keras CNN requires an extra dimension in the end which correspond to channels. \n# MNIST images are gray scaled so it use only one channel.For RGB images, there is 3 channels\n\ntraining_data_X = training_data_X.reshape(-1, 28, 28, 1)\ntesting_data = testing_data.reshape(-1, 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e54465a34c093a7cb2254aeaa27f51eb55cf581"},"cell_type":"code","source":"print(training_data_X.shape)\nprint(testing_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"387644b8a9717a98a4dbd5107ddea74ba45333ee"},"cell_type":"code","source":"model = tf.keras.Sequential()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30eaa8db044d2949219b706ebfa96ebac37b27bc"},"cell_type":"code","source":"model.add(Conv2D(50, (3, 3), input_shape=(28, 28, 1), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3a80346a94567d2f816de069efbdc14a21434f5"},"cell_type":"code","source":"model.add(Conv2D(50, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ae4330702ffe030c7d4339413b04972ec08af0c"},"cell_type":"code","source":"model.add(Conv2D(50, (3, 3)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"462e6fbf9bbf668420b1b30649bca10f76a136de"},"cell_type":"code","source":"model.add(Flatten())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96865c01804272cc1638f6d3a70c6ec7116916b8"},"cell_type":"code","source":"model.add(Dense(256, activation=tf.nn.relu))\nmodel.add(Dense(10, activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed79a53de4f7a0529e2070555b76b42bc7100d1e"},"cell_type":"code","source":"# Compile the model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6773582ce0a45eabe8ea24283519b1dab5b59592"},"cell_type":"code","source":"model.compile(optimizer='Adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d693bff5049e698d74b8e19f3f66577347599ac6"},"cell_type":"code","source":"model.fit(training_data_X, training_data_y, batch_size=500, epochs=30, validation_split=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb8ddce399efdb9c6564fdadb45b8b82a543f460"},"cell_type":"code","source":"predictions = model.predict_classes(testing_data, verbose=0)\n\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n                         \"Label\": predictions})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5dc8bcacd24dacd1217804e6b22c21a02ef2c1f"},"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}