{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-19T11:23:37.292458Z","iopub.execute_input":"2021-09-19T11:23:37.292671Z","iopub.status.idle":"2021-09-19T11:23:39.294644Z","shell.execute_reply.started":"2021-09-19T11:23:37.292629Z","shell.execute_reply":"2021-09-19T11:23:39.29384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Training Data","metadata":{}},{"cell_type":"code","source":"training = pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:23:45.112848Z","iopub.execute_input":"2021-09-19T11:23:45.113147Z","iopub.status.idle":"2021-09-19T11:23:48.576872Z","shell.execute_reply.started":"2021-09-19T11:23:45.113096Z","shell.execute_reply":"2021-09-19T11:23:48.576125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare Traing Data","metadata":{}},{"cell_type":"code","source":"x_train, y_train = training.iloc[:, 1:], training.iloc[:, 0:1]","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:23:48.578694Z","iopub.execute_input":"2021-09-19T11:23:48.578982Z","iopub.status.idle":"2021-09-19T11:23:48.585649Z","shell.execute_reply.started":"2021-09-19T11:23:48.578939Z","shell.execute_reply":"2021-09-19T11:23:48.584631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:23:48.58741Z","iopub.execute_input":"2021-09-19T11:23:48.587915Z","iopub.status.idle":"2021-09-19T11:23:49.418595Z","shell.execute_reply.started":"2021-09-19T11:23:48.587644Z","shell.execute_reply":"2021-09-19T11:23:49.417697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:23:50.521357Z","iopub.execute_input":"2021-09-19T11:23:50.521673Z","iopub.status.idle":"2021-09-19T11:23:50.528716Z","shell.execute_reply.started":"2021-09-19T11:23:50.52161Z","shell.execute_reply":"2021-09-19T11:23:50.528049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Configure the Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:23:55.079009Z","iopub.execute_input":"2021-09-19T11:23:55.079325Z","iopub.status.idle":"2021-09-19T11:23:55.10685Z","shell.execute_reply.started":"2021-09-19T11:23:55.079272Z","shell.execute_reply":"2021-09-19T11:23:55.106215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=5, padding=\"same\", activation=\"relu\", input_shape=[28, 28, 1]))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:07.885608Z","iopub.execute_input":"2021-09-19T11:24:07.885907Z","iopub.status.idle":"2021-09-19T11:24:07.930593Z","shell.execute_reply.started":"2021-09-19T11:24:07.885858Z","shell.execute_reply":"2021-09-19T11:24:07.929923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=5, padding=\"same\", activation=\"relu\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:08.513062Z","iopub.execute_input":"2021-09-19T11:24:08.513397Z","iopub.status.idle":"2021-09-19T11:24:08.542326Z","shell.execute_reply.started":"2021-09-19T11:24:08.513342Z","shell.execute_reply":"2021-09-19T11:24:08.541676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:09.321748Z","iopub.execute_input":"2021-09-19T11:24:09.322049Z","iopub.status.idle":"2021-09-19T11:24:09.333428Z","shell.execute_reply.started":"2021-09-19T11:24:09.321997Z","shell.execute_reply":"2021-09-19T11:24:09.332535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dropout(0.2))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:12.177406Z","iopub.execute_input":"2021-09-19T11:24:12.177718Z","iopub.status.idle":"2021-09-19T11:24:12.207325Z","shell.execute_reply.started":"2021-09-19T11:24:12.177667Z","shell.execute_reply":"2021-09-19T11:24:12.206668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding=\"same\", activation=\"relu\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:14.497781Z","iopub.execute_input":"2021-09-19T11:24:14.498101Z","iopub.status.idle":"2021-09-19T11:24:14.526411Z","shell.execute_reply.started":"2021-09-19T11:24:14.498048Z","shell.execute_reply":"2021-09-19T11:24:14.525629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding=\"same\", activation=\"relu\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:14.881327Z","iopub.execute_input":"2021-09-19T11:24:14.881584Z","iopub.status.idle":"2021-09-19T11:24:14.907282Z","shell.execute_reply.started":"2021-09-19T11:24:14.881541Z","shell.execute_reply":"2021-09-19T11:24:14.906621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:15.304893Z","iopub.execute_input":"2021-09-19T11:24:15.305171Z","iopub.status.idle":"2021-09-19T11:24:15.314932Z","shell.execute_reply.started":"2021-09-19T11:24:15.305123Z","shell.execute_reply":"2021-09-19T11:24:15.313976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dropout(0.2))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:15.889314Z","iopub.execute_input":"2021-09-19T11:24:15.889609Z","iopub.status.idle":"2021-09-19T11:24:15.914753Z","shell.execute_reply.started":"2021-09-19T11:24:15.889562Z","shell.execute_reply":"2021-09-19T11:24:15.913943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Flatten())","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:18.002118Z","iopub.execute_input":"2021-09-19T11:24:18.002453Z","iopub.status.idle":"2021-09-19T11:24:18.019682Z","shell.execute_reply.started":"2021-09-19T11:24:18.002398Z","shell.execute_reply":"2021-09-19T11:24:18.018968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dense(units=256, activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:18.803445Z","iopub.execute_input":"2021-09-19T11:24:18.803754Z","iopub.status.idle":"2021-09-19T11:24:18.830443Z","shell.execute_reply.started":"2021-09-19T11:24:18.803695Z","shell.execute_reply":"2021-09-19T11:24:18.829659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dropout(0.4))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:19.090768Z","iopub.execute_input":"2021-09-19T11:24:19.091028Z","iopub.status.idle":"2021-09-19T11:24:19.117234Z","shell.execute_reply.started":"2021-09-19T11:24:19.090983Z","shell.execute_reply":"2021-09-19T11:24:19.116406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:19.513486Z","iopub.execute_input":"2021-09-19T11:24:19.513773Z","iopub.status.idle":"2021-09-19T11:24:19.540798Z","shell.execute_reply.started":"2021-09-19T11:24:19.513723Z","shell.execute_reply":"2021-09-19T11:24:19.539929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:21.552691Z","iopub.execute_input":"2021-09-19T11:24:21.553002Z","iopub.status.idle":"2021-09-19T11:24:21.609825Z","shell.execute_reply.started":"2021-09-19T11:24:21.552949Z","shell.execute_reply":"2021-09-19T11:24:21.608973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:22.104289Z","iopub.execute_input":"2021-09-19T11:24:22.104581Z","iopub.status.idle":"2021-09-19T11:24:22.114333Z","shell.execute_reply.started":"2021-09-19T11:24:22.104536Z","shell.execute_reply":"2021-09-19T11:24:22.112145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:24:32.946077Z","iopub.execute_input":"2021-09-19T11:24:32.946455Z","iopub.status.idle":"2021-09-19T11:26:20.423769Z","shell.execute_reply.started":"2021-09-19T11:24:32.946405Z","shell.execute_reply":"2021-09-19T11:26:20.42317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load & Prepare Test Data","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:27.252364Z","iopub.execute_input":"2021-09-19T11:26:27.252681Z","iopub.status.idle":"2021-09-19T11:26:29.511598Z","shell.execute_reply.started":"2021-09-19T11:26:27.25263Z","shell.execute_reply":"2021-09-19T11:26:29.510844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:29.513335Z","iopub.execute_input":"2021-09-19T11:26:29.513621Z","iopub.status.idle":"2021-09-19T11:26:30.13814Z","shell.execute_reply.started":"2021-09-19T11:26:29.513574Z","shell.execute_reply":"2021-09-19T11:26:30.137307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:30.139588Z","iopub.execute_input":"2021-09-19T11:26:30.139875Z","iopub.status.idle":"2021-09-19T11:26:30.144488Z","shell.execute_reply.started":"2021-09-19T11:26:30.13983Z","shell.execute_reply":"2021-09-19T11:26:30.143782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict numbers","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:35.122664Z","iopub.execute_input":"2021-09-19T11:26:35.122981Z","iopub.status.idle":"2021-09-19T11:26:36.441122Z","shell.execute_reply.started":"2021-09-19T11:26:35.122926Z","shell.execute_reply":"2021-09-19T11:26:36.440257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Export predictions","metadata":{}},{"cell_type":"code","source":"export = pd.DataFrame([np.argmax(prediction) for prediction in predictions])","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:39.247831Z","iopub.execute_input":"2021-09-19T11:26:39.248122Z","iopub.status.idle":"2021-09-19T11:26:39.365391Z","shell.execute_reply.started":"2021-09-19T11:26:39.248075Z","shell.execute_reply":"2021-09-19T11:26:39.364671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export.index += 1 ","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:39.45442Z","iopub.execute_input":"2021-09-19T11:26:39.454655Z","iopub.status.idle":"2021-09-19T11:26:39.458943Z","shell.execute_reply.started":"2021-09-19T11:26:39.454613Z","shell.execute_reply":"2021-09-19T11:26:39.458202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export = export.reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:39.523242Z","iopub.execute_input":"2021-09-19T11:26:39.523477Z","iopub.status.idle":"2021-09-19T11:26:39.530353Z","shell.execute_reply.started":"2021-09-19T11:26:39.523433Z","shell.execute_reply":"2021-09-19T11:26:39.529622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export.columns = ['ImageId', 'Label']","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:40.069311Z","iopub.execute_input":"2021-09-19T11:26:40.069609Z","iopub.status.idle":"2021-09-19T11:26:40.074251Z","shell.execute_reply.started":"2021-09-19T11:26:40.069563Z","shell.execute_reply":"2021-09-19T11:26:40.073361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export.to_csv('export.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:26:40.476696Z","iopub.execute_input":"2021-09-19T11:26:40.477021Z","iopub.status.idle":"2021-09-19T11:26:40.748074Z","shell.execute_reply.started":"2021-09-19T11:26:40.476968Z","shell.execute_reply":"2021-09-19T11:26:40.747366Z"},"trusted":true},"execution_count":null,"outputs":[]}]}