{"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":"install required packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:23:02.397157Z","iopub.execute_input":"2022-07-16T11:23:02.397561Z","iopub.status.idle":"2022-07-16T11:23:11.596380Z","shell.execute_reply.started":"2022-07-16T11:23:02.397522Z","shell.execute_reply":"2022-07-16T11:23:11.595137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read dataframe with pandas ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:23:11.598307Z","iopub.execute_input":"2022-07-16T11:23:11.598902Z","iopub.status.idle":"2022-07-16T11:23:16.597395Z","shell.execute_reply.started":"2022-07-16T11:23:11.598867Z","shell.execute_reply":"2022-07-16T11:23:16.596346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"split dataframe as x_train and t_train for split label and pixels","metadata":{}},{"cell_type":"code","source":"x_train = train.iloc[:,1:]\ny_train = train.iloc[:,0:1]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:23:16.599220Z","iopub.execute_input":"2022-07-16T11:23:16.599712Z","iopub.status.idle":"2022-07-16T11:23:16.608811Z","shell.execute_reply.started":"2022-07-16T11:23:16.599671Z","shell.execute_reply":"2022-07-16T11:23:16.605314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"New CNN model for recognizing numbers","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Flatten(input_shape=(28,28)))\nmodel.add(tf.keras.layers.Dense(128,activation=\"relu\"))\nmodel.add(tf.keras.layers.Dense(128,activation=\"relu\"))\nmodel.add(tf.keras.layers.Dense(10,activation=\"softmax\"))\n\nmodel.compile(optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\",metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:23:19.179505Z","iopub.execute_input":"2022-07-16T11:23:19.179891Z","iopub.status.idle":"2022-07-16T11:23:20.715871Z","shell.execute_reply.started":"2022-07-16T11:23:19.179860Z","shell.execute_reply":"2022-07-16T11:23:20.714518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model fitting x_train and y_train with 10 epochs.","metadata":{}},{"cell_type":"code","source":"model.fit(x_train,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:23:34.227739Z","iopub.execute_input":"2022-07-16T11:23:34.228111Z","iopub.status.idle":"2022-07-16T11:24:16.316939Z","shell.execute_reply.started":"2022-07-16T11:23:34.228080Z","shell.execute_reply":"2022-07-16T11:24:16.315522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"predict the test model and find the biggest columns for predict value","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(test)\npred= np.argmax(prediction,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:24:16.319090Z","iopub.execute_input":"2022-07-16T11:24:16.319715Z","iopub.status.idle":"2022-07-16T11:24:17.991928Z","shell.execute_reply.started":"2022-07-16T11:24:16.319670Z","shell.execute_reply":"2022-07-16T11:24:17.990750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there is not index column, we have to generate new index column with pd.series.\nwe saved submission with to_csv.","metadata":{}},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': pd.Series(range(1,28001)),'Label':pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T11:24:17.994292Z","iopub.execute_input":"2022-07-16T11:24:17.994798Z","iopub.status.idle":"2022-07-16T11:24:18.034438Z","shell.execute_reply.started":"2022-07-16T11:24:17.994745Z","shell.execute_reply":"2022-07-16T11:24:18.033155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks for read my code about the digit recognized.","metadata":{}}]}