{"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":"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-13T02:33:38.809745Z","iopub.execute_input":"2021-11-13T02:33:38.810059Z","iopub.status.idle":"2021-11-13T02:33:44.707724Z","shell.execute_reply.started":"2021-11-13T02:33:38.809978Z","shell.execute_reply":"2021-11-13T02:33:44.706812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Data Preprocess\n## 1.1 Import Modules","metadata":{}},{"cell_type":"code","source":"from keras.utils import np_utils\nnp.random.seed(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:34:50.205491Z","iopub.execute_input":"2021-11-13T02:34:50.205806Z","iopub.status.idle":"2021-11-13T02:34:50.210393Z","shell.execute_reply.started":"2021-11-13T02:34:50.205775Z","shell.execute_reply":"2021-11-13T02:34:50.209348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.2 Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:35:06.706920Z","iopub.execute_input":"2021-11-13T02:35:06.707747Z","iopub.status.idle":"2021-11-13T02:35:12.568464Z","shell.execute_reply.started":"2021-11-13T02:35:06.707705Z","shell.execute_reply":"2021-11-13T02:35:12.567529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.3 Data Reshape","metadata":{}},{"cell_type":"code","source":"x_Train = train_data.drop(['label'],axis=1)\nx_Train = np.array(x_Train)\nx_Test = np.array(test_data)\ny_Train = np.array(train_data['label'])\n\nx_Train_4D = x_Train.reshape(x_Train.shape[0],28,28,1).astype('float32')\nx_Test_4D = x_Test.reshape(x_Test.shape[0],28,28,1).astype('float32')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:41:30.451641Z","iopub.execute_input":"2021-11-13T02:41:30.452300Z","iopub.status.idle":"2021-11-13T02:41:30.805402Z","shell.execute_reply.started":"2021-11-13T02:41:30.452260Z","shell.execute_reply":"2021-11-13T02:41:30.804738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.4 Features Normalize","metadata":{}},{"cell_type":"code","source":"x_Train_4D_normalize = x_Train_4D/255.0\nx_Test_4D_normalize = x_Test_4D/255.0","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:42:51.717621Z","iopub.execute_input":"2021-11-13T02:42:51.718027Z","iopub.status.idle":"2021-11-13T02:42:51.804662Z","shell.execute_reply.started":"2021-11-13T02:42:51.717996Z","shell.execute_reply":"2021-11-13T02:42:51.803891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.5 Label Encoding","metadata":{}},{"cell_type":"code","source":"y_Train_OneHot = np_utils.to_categorical(y_Train)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:43:41.244406Z","iopub.execute_input":"2021-11-13T02:43:41.244670Z","iopub.status.idle":"2021-11-13T02:43:41.249442Z","shell.execute_reply.started":"2021-11-13T02:43:41.244641Z","shell.execute_reply":"2021-11-13T02:43:41.248561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Build Model\n## 2.1 Import Modules","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nimport tensorflow as tf\ntf.config.experimental_run_functions_eagerly(True)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:45:17.694892Z","iopub.execute_input":"2021-11-13T02:45:17.695783Z","iopub.status.idle":"2021-11-13T02:45:17.700628Z","shell.execute_reply.started":"2021-11-13T02:45:17.695740Z","shell.execute_reply":"2021-11-13T02:45:17.699850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.2 Build","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=16,\n                kernel_size=(5,5),\n                padding='same',\n                input_shape=(28,28,1),\n                activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters=36,\n                kernel_size=(5,5),\n                padding='same',\n                activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:45:23.078663Z","iopub.execute_input":"2021-11-13T02:45:23.079099Z","iopub.status.idle":"2021-11-13T02:45:23.252158Z","shell.execute_reply.started":"2021-11-13T02:45:23.079050Z","shell.execute_reply":"2021-11-13T02:45:23.251166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.3 Complie Model","metadata":{}},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n             optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:46:34.769587Z","iopub.execute_input":"2021-11-13T02:46:34.770324Z","iopub.status.idle":"2021-11-13T02:46:34.787468Z","shell.execute_reply.started":"2021-11-13T02:46:34.770273Z","shell.execute_reply":"2021-11-13T02:46:34.786308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.4 Train","metadata":{}},{"cell_type":"code","source":"train_history = model.fit(x = x_Train_4D_normalize,\n                          y = y_Train_OneHot,\n                          validation_split = 0.2,\n                          epochs = 20,\n                          batch_size = 200,\n                          verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T02:52:27.100481Z","iopub.execute_input":"2021-11-13T02:52:27.100761Z","iopub.status.idle":"2021-11-13T03:02:49.209614Z","shell.execute_reply.started":"2021-11-13T02:52:27.100733Z","shell.execute_reply":"2021-11-13T03:02:49.208625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Prediction","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(x_Test_4D_normalize)\nprediction=np.argmax(prediction,axis=1)\nprediction.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-13T03:03:26.467451Z","iopub.execute_input":"2021-11-13T03:03:26.468210Z","iopub.status.idle":"2021-11-13T03:03:47.115311Z","shell.execute_reply.started":"2021-11-13T03:03:26.468159Z","shell.execute_reply":"2021-11-13T03:03:47.114247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label']=prediction\nsubmission.head()\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T03:04:06.173321Z","iopub.execute_input":"2021-11-13T03:04:06.173607Z","iopub.status.idle":"2021-11-13T03:04:06.244496Z","shell.execute_reply.started":"2021-11-13T03:04:06.173574Z","shell.execute_reply":"2021-11-13T03:04:06.243482Z"},"trusted":true},"execution_count":null,"outputs":[]}]}