{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-19T11:08:28.951643Z","iopub.execute_input":"2021-09-19T11:08:28.951930Z","iopub.status.idle":"2021-09-19T11:08:30.770973Z","shell.execute_reply.started":"2021-09-19T11:08:28.951877Z","shell.execute_reply":"2021-09-19T11:08:30.770140Z"},"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:08:34.854429Z","iopub.execute_input":"2021-09-19T11:08:34.854726Z","iopub.status.idle":"2021-09-19T11:08:38.274166Z","shell.execute_reply.started":"2021-09-19T11:08:34.854673Z","shell.execute_reply":"2021-09-19T11:08:38.273449Z"},"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:08:38.275894Z","iopub.execute_input":"2021-09-19T11:08:38.276179Z","iopub.status.idle":"2021-09-19T11:08:38.283288Z","shell.execute_reply.started":"2021-09-19T11:08:38.276114Z","shell.execute_reply":"2021-09-19T11:08:38.282600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:08:38.284795Z","iopub.execute_input":"2021-09-19T11:08:38.285294Z","iopub.status.idle":"2021-09-19T11:08:39.099292Z","shell.execute_reply.started":"2021-09-19T11:08:38.285247Z","shell.execute_reply":"2021-09-19T11:08:39.098559Z"},"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:08:45.338361Z","iopub.execute_input":"2021-09-19T11:08:45.338655Z","iopub.status.idle":"2021-09-19T11:08:45.367504Z","shell.execute_reply.started":"2021-09-19T11:08:45.338604Z","shell.execute_reply":"2021-09-19T11:08:45.366737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(tf.keras.layers.Dense(units=256, activation=\"relu\", input_shape=(784, )))","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:08:52.063366Z","iopub.execute_input":"2021-09-19T11:08:52.063868Z","iopub.status.idle":"2021-09-19T11:08:52.100917Z","shell.execute_reply.started":"2021-09-19T11:08:52.063803Z","shell.execute_reply":"2021-09-19T11:08:52.100096Z"},"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:08:55.106569Z","iopub.execute_input":"2021-09-19T11:08:55.106856Z","iopub.status.idle":"2021-09-19T11:08:55.135431Z","shell.execute_reply.started":"2021-09-19T11:08:55.106808Z","shell.execute_reply":"2021-09-19T11:08:55.134776Z"},"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:08:56.087573Z","iopub.execute_input":"2021-09-19T11:08:56.087942Z","iopub.status.idle":"2021-09-19T11:08:56.115410Z","shell.execute_reply.started":"2021-09-19T11:08:56.087815Z","shell.execute_reply":"2021-09-19T11:08:56.114749Z"},"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:09:10.228195Z","iopub.execute_input":"2021-09-19T11:09:10.228502Z","iopub.status.idle":"2021-09-19T11:09:10.279620Z","shell.execute_reply.started":"2021-09-19T11:09:10.228451Z","shell.execute_reply":"2021-09-19T11:09:10.278942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:09:11.378345Z","iopub.execute_input":"2021-09-19T11:09:11.378677Z","iopub.status.idle":"2021-09-19T11:09:11.389801Z","shell.execute_reply.started":"2021-09-19T11:09:11.378619Z","shell.execute_reply":"2021-09-19T11:09:11.388470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the Model","metadata":{"trusted":true}},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:09:17.881731Z","iopub.execute_input":"2021-09-19T11:09:17.882021Z","iopub.status.idle":"2021-09-19T11:09:58.082253Z","shell.execute_reply.started":"2021-09-19T11:09:17.881970Z","shell.execute_reply":"2021-09-19T11:09:58.081422Z"},"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:10:08.198263Z","iopub.execute_input":"2021-09-19T11:10:08.198573Z","iopub.status.idle":"2021-09-19T11:10:10.335985Z","shell.execute_reply.started":"2021-09-19T11:10:08.198522Z","shell.execute_reply":"2021-09-19T11:10:10.335268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:10:34.189749Z","iopub.execute_input":"2021-09-19T11:10:34.190044Z","iopub.status.idle":"2021-09-19T11:10:34.804877Z","shell.execute_reply.started":"2021-09-19T11:10:34.189994Z","shell.execute_reply":"2021-09-19T11:10:34.804171Z"},"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:10:43.597137Z","iopub.execute_input":"2021-09-19T11:10:43.597447Z","iopub.status.idle":"2021-09-19T11:10:44.448603Z","shell.execute_reply.started":"2021-09-19T11:10:43.597394Z","shell.execute_reply":"2021-09-19T11:10:44.447691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Export predictions","metadata":{"trusted":true}},{"cell_type":"code","source":"export = pd.DataFrame([np.argmax(prediction) for prediction in predictions])","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:11:04.130021Z","iopub.execute_input":"2021-09-19T11:11:04.130334Z","iopub.status.idle":"2021-09-19T11:11:04.248353Z","shell.execute_reply.started":"2021-09-19T11:11:04.130286Z","shell.execute_reply":"2021-09-19T11:11:04.247480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export.index += 1 ","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:11:04.881105Z","iopub.execute_input":"2021-09-19T11:11:04.881420Z","iopub.status.idle":"2021-09-19T11:11:04.885092Z","shell.execute_reply.started":"2021-09-19T11:11:04.881368Z","shell.execute_reply":"2021-09-19T11:11:04.884342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export = export.reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:11:05.546198Z","iopub.execute_input":"2021-09-19T11:11:05.546520Z","iopub.status.idle":"2021-09-19T11:11:05.552729Z","shell.execute_reply.started":"2021-09-19T11:11:05.546469Z","shell.execute_reply":"2021-09-19T11:11:05.551738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"export.columns = ['ImageId', 'Label']","metadata":{"execution":{"iopub.status.busy":"2021-09-19T11:11:07.984232Z","iopub.execute_input":"2021-09-19T11:11:07.984546Z","iopub.status.idle":"2021-09-19T11:11:07.989267Z","shell.execute_reply.started":"2021-09-19T11:11:07.984493Z","shell.execute_reply":"2021-09-19T11:11:07.988268Z"},"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:11:14.506722Z","iopub.execute_input":"2021-09-19T11:11:14.507013Z","iopub.status.idle":"2021-09-19T11:11:14.766589Z","shell.execute_reply.started":"2021-09-19T11:11:14.506963Z","shell.execute_reply":"2021-09-19T11:11:14.765790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}