{"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":"# modules\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-02-23T14:06:33.417895Z","iopub.execute_input":"2022-02-23T14:06:33.418185Z","iopub.status.idle":"2022-02-23T14:06:33.422960Z","shell.execute_reply.started":"2022-02-23T14:06:33.418154Z","shell.execute_reply":"2022-02-23T14:06:33.422281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import the train and test dataset\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:41:25.558152Z","iopub.execute_input":"2022-02-23T13:41:25.559035Z","iopub.status.idle":"2022-02-23T13:41:30.308687Z","shell.execute_reply.started":"2022-02-23T13:41:25.558983Z","shell.execute_reply":"2022-02-23T13:41:30.307917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create X as feature and Y as label/target\nX_train = train.drop('label', axis=1)\nY_train = train['label']","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:41:45.098678Z","iopub.execute_input":"2022-02-23T13:41:45.098953Z","iopub.status.idle":"2022-02-23T13:41:45.180797Z","shell.execute_reply.started":"2022-02-23T13:41:45.098923Z","shell.execute_reply":"2022-02-23T13:41:45.180025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change from DataFrame to array\n\n# train data\nX_train = X_train.values\nY_train = Y_train.values\n\n# test data\nX_test = test.values","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:41:49.166781Z","iopub.execute_input":"2022-02-23T13:41:49.167290Z","iopub.status.idle":"2022-02-23T13:41:49.171390Z","shell.execute_reply.started":"2022-02-23T13:41:49.167251Z","shell.execute_reply":"2022-02-23T13:41:49.170568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Nomalize the dataset\nX_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:41:51.809283Z","iopub.execute_input":"2022-02-23T13:41:51.809889Z","iopub.status.idle":"2022-02-23T13:41:51.983110Z","shell.execute_reply.started":"2022-02-23T13:41:51.809848Z","shell.execute_reply":"2022-02-23T13:41:51.982329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape the dataset (Index,Height,Width,Channels)\nX_train = X_train.reshape(42000,28,28,1)\nX_test = X_test.reshape(28000,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:42:03.865416Z","iopub.execute_input":"2022-02-23T13:42:03.866136Z","iopub.status.idle":"2022-02-23T13:42:03.871121Z","shell.execute_reply.started":"2022-02-23T13:42:03.866082Z","shell.execute_reply":"2022-02-23T13:42:03.870318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change the label to categorical\nY_train_cat = to_categorical(Y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:42:08.384729Z","iopub.execute_input":"2022-02-23T13:42:08.384982Z","iopub.status.idle":"2022-02-23T13:42:08.391446Z","shell.execute_reply.started":"2022-02-23T13:42:08.384954Z","shell.execute_reply":"2022-02-23T13:42:08.390512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the train data to train and validation data\nx_train, x_val, y_train, y_val = train_test_split(X_train, \n                                                    Y_train_cat, \n                                                    test_size=0.3, \n                                                    random_state=101)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:44:55.719402Z","iopub.execute_input":"2022-02-23T13:44:55.720090Z","iopub.status.idle":"2022-02-23T13:44:56.103574Z","shell.execute_reply.started":"2022-02-23T13:44:55.720052Z","shell.execute_reply":"2022-02-23T13:44:56.102791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# build the model\nmodel = Sequential()\n\nmodel.add(Conv2D(filters=32, kernel_size=(4,4), input_shape=(28,28,1), activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\n\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:45:08.714656Z","iopub.execute_input":"2022-02-23T13:45:08.714921Z","iopub.status.idle":"2022-02-23T13:45:08.753922Z","shell.execute_reply.started":"2022-02-23T13:45:08.714890Z","shell.execute_reply":"2022-02-23T13:45:08.753195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set the eary stopping\nearly_stop = EarlyStopping(monitor='val_loss', patience=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:45:13.170237Z","iopub.execute_input":"2022-02-23T13:45:13.170807Z","iopub.status.idle":"2022-02-23T13:45:13.174687Z","shell.execute_reply.started":"2022-02-23T13:45:13.170767Z","shell.execute_reply":"2022-02-23T13:45:13.173830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit the model to train data\nmodel.fit(x=x_train, y=y_train, epochs=200, validation_data=(x_val,y_val), callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:45:39.612639Z","iopub.execute_input":"2022-02-23T13:45:39.613383Z","iopub.status.idle":"2022-02-23T13:45:50.445007Z","shell.execute_reply.started":"2022-02-23T13:45:39.613333Z","shell.execute_reply":"2022-02-23T13:45:50.444276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the model loss history\nloss = pd.DataFrame(model.history.history)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:46:38.758179Z","iopub.execute_input":"2022-02-23T13:46:38.758461Z","iopub.status.idle":"2022-02-23T13:46:38.763343Z","shell.execute_reply.started":"2022-02-23T13:46:38.758410Z","shell.execute_reply":"2022-02-23T13:46:38.762489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the train loss and validation loss\nloss[['loss', 'val_loss']].plot()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:46:41.064505Z","iopub.execute_input":"2022-02-23T13:46:41.065124Z","iopub.status.idle":"2022-02-23T13:46:41.295477Z","shell.execute_reply.started":"2022-02-23T13:46:41.065087Z","shell.execute_reply":"2022-02-23T13:46:41.294760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the prediction\npreds_prob = model.predict(X_test) # probability\npredictions = np.argmax(preds_prob, axis=1) # get the predicition label","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:56:48.682942Z","iopub.execute_input":"2022-02-23T13:56:48.683220Z","iopub.status.idle":"2022-02-23T13:56:50.204252Z","shell.execute_reply.started":"2022-02-23T13:56:48.683189Z","shell.execute_reply":"2022-02-23T13:56:50.203476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example of submission file\nexample = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:59:25.812113Z","iopub.execute_input":"2022-02-23T13:59:25.812367Z","iopub.status.idle":"2022-02-23T13:59:25.824608Z","shell.execute_reply.started":"2022-02-23T13:59:25.812339Z","shell.execute_reply":"2022-02-23T13:59:25.823944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create submission file\nsubmission = pd.DataFrame({'ImageId':example['ImageId'],'Label':predictions})","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:59:28.443286Z","iopub.execute_input":"2022-02-23T13:59:28.444192Z","iopub.status.idle":"2022-02-23T13:59:28.450512Z","shell.execute_reply.started":"2022-02-23T13:59:28.444147Z","shell.execute_reply":"2022-02-23T13:59:28.449327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save submission file\nsubmission.to_csv('./sub2.csv', index='False')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T13:59:32.192808Z","iopub.execute_input":"2022-02-23T13:59:32.193552Z","iopub.status.idle":"2022-02-23T13:59:32.257682Z","shell.execute_reply.started":"2022-02-23T13:59:32.193511Z","shell.execute_reply":"2022-02-23T13:59:32.256921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}