{"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":"2023-01-18T16:31:59.057524Z","iopub.execute_input":"2023-01-18T16:31:59.057957Z","iopub.status.idle":"2023-01-18T16:31:59.086213Z","shell.execute_reply.started":"2023-01-18T16:31:59.057870Z","shell.execute_reply":"2023-01-18T16:31:59.084997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nss = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:32:28.215414Z","iopub.execute_input":"2023-01-18T16:32:28.215999Z","iopub.status.idle":"2023-01-18T16:32:31.690318Z","shell.execute_reply.started":"2023-01-18T16:32:28.215955Z","shell.execute_reply":"2023-01-18T16:32:31.688939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:01.430206Z","iopub.execute_input":"2023-01-18T16:33:01.431286Z","iopub.status.idle":"2023-01-18T16:33:01.470793Z","shell.execute_reply.started":"2023-01-18T16:33:01.431240Z","shell.execute_reply":"2023-01-18T16:33:01.469791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train.iloc[:, 1:].values.reshape(-1, 28, 28, 1)\ntrain_label = train['label'].values\n\nprint(train_label)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:03.527419Z","iopub.execute_input":"2023-01-18T16:33:03.527895Z","iopub.status.idle":"2023-01-18T16:33:03.538512Z","shell.execute_reply.started":"2023-01-18T16:33:03.527853Z","shell.execute_reply":"2023-01-18T16:33:03.537411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_valid, y_train, y_valid = train_test_split(train_image, train_label, test_size=0.20, random_state=0)\n\nprint(x_train.shape, y_train.shape)\nprint(x_valid.shape, y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:05.998302Z","iopub.execute_input":"2023-01-18T16:33:05.998685Z","iopub.status.idle":"2023-01-18T16:33:06.820897Z","shell.execute_reply.started":"2023-01-18T16:33:05.998654Z","shell.execute_reply":"2023-01-18T16:33:06.819812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \n\nplt.figure(figsize=(5, 4))\nfor i in range(20):\n    plt.subplot(4, 5, i+1)\n    plt.imshow(x_train[i], cmap='gray')\n    plt.xticks([])\n    plt.yticks([])\nplt.tight_layout()\nplt.savefig('train_mnist.png', dpi=300)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:09.580201Z","iopub.execute_input":"2023-01-18T16:33:09.580629Z","iopub.status.idle":"2023-01-18T16:33:10.697052Z","shell.execute_reply.started":"2023-01-18T16:33:09.580589Z","shell.execute_reply":"2023-01-18T16:33:10.696092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\nx_train = x_train.astype('float32') / 255\nx_valid = x_valid.astype('float32') / 255\n\ny_train = to_categorical(y_train, num_classes=10)\ny_valid= to_categorical(y_valid, num_classes=10)\n\nprint(x_train.shape)\nprint(x_valid.shape)\nprint(x_train.min(), x_train.max())","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:15.013438Z","iopub.execute_input":"2023-01-18T16:33:15.013816Z","iopub.status.idle":"2023-01-18T16:33:19.996416Z","shell.execute_reply.started":"2023-01-18T16:33:15.013782Z","shell.execute_reply":"2023-01-18T16:33:19.995306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:22.550778Z","iopub.execute_input":"2023-01-18T16:33:22.551602Z","iopub.status.idle":"2023-01-18T16:33:22.561247Z","shell.execute_reply.started":"2023-01-18T16:33:22.551555Z","shell.execute_reply":"2023-01-18T16:33:22.560387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))\nmodel.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))\nmodel.add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2, padding='same'))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:24.592829Z","iopub.execute_input":"2023-01-18T16:33:24.593204Z","iopub.status.idle":"2023-01-18T16:33:27.345761Z","shell.execute_reply.started":"2023-01-18T16:33:24.593172Z","shell.execute_reply":"2023-01-18T16:33:27.344745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:30.541400Z","iopub.execute_input":"2023-01-18T16:33:30.541779Z","iopub.status.idle":"2023-01-18T16:33:30.559685Z","shell.execute_reply.started":"2023-01-18T16:33:30.541722Z","shell.execute_reply":"2023-01-18T16:33:30.558232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\n\nreduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1, patience=2, min_lr=0.00000001)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T16:33:33.369424Z","iopub.execute_input":"2023-01-18T16:33:33.370081Z","iopub.status.idle":"2023-01-18T16:33:33.378811Z","shell.execute_reply.started":"2023-01-18T16:33:33.370027Z","shell.execute_reply":"2023-01-18T16:33:33.376364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(x_train, y_train, batch_size = 100, validation_split = 0.2, epochs = 20, verbose=1, callbacks=[reduce_lr], shuffle=True)\n\nplt.plot(hist.history['accuracy'])                                           ## plot accuracy curve during training \nplt.plot(hist.history['val_accuracy'])\nplt.legend(['train accuracy','validation accuracy'])\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:42:00.820001Z","iopub.execute_input":"2023-01-03T12:42:00.820362Z","iopub.status.idle":"2023-01-03T12:44:23.631422Z","shell.execute_reply.started":"2023-01-03T12:42:00.820329Z","shell.execute_reply":"2023-01-03T12:44:23.630706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(x_valid, y_valid, verbose=0)\nprint('loss ', score[0])\nprint('accuracy ', score[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:23.633206Z","iopub.execute_input":"2023-01-03T12:44:23.635125Z","iopub.status.idle":"2023-01-03T12:44:24.584238Z","shell.execute_reply.started":"2023-01-03T12:44:23.635085Z","shell.execute_reply":"2023-01-03T12:44:24.583180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:29.384992Z","iopub.execute_input":"2023-01-03T12:44:29.385964Z","iopub.status.idle":"2023-01-03T12:44:29.426522Z","shell.execute_reply.started":"2023-01-03T12:44:29.385910Z","shell.execute_reply":"2023-01-03T12:44:29.425140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = test.values.reshape(-1, 28, 28, 1)\nprint(test_image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:33.297926Z","iopub.execute_input":"2023-01-03T12:44:33.298288Z","iopub.status.idle":"2023-01-03T12:44:33.304294Z","shell.execute_reply.started":"2023-01-03T12:44:33.298254Z","shell.execute_reply":"2023-01-03T12:44:33.303202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes = model.predict(x_valid)\npredicted_classes = np.argmax(predicted_classes, axis=-1)\ny_true = np.argmax(y_valid, axis=-1)\n\ncorrect = np.nonzero(predicted_classes == y_true)[0]        ## เปรียบเทียบค่า\nincorrect = np.nonzero(predicted_classes != y_true)[0]\nprint('Correct predicted classes: ', correct.shape[0])\nprint('Incorrect predicted classes: ', incorrect.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:35.926005Z","iopub.execute_input":"2023-01-03T12:44:35.927429Z","iopub.status.idle":"2023-01-03T12:44:36.724735Z","shell.execute_reply.started":"2023-01-03T12:44:35.927383Z","shell.execute_reply":"2023-01-03T12:44:36.723684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\nfor i in range(20):\n    plt.subplot(4, 5, i+1)\n    plt.imshow(test_image[i], cmap='gray')\n    plt.xticks([])\n    plt.yticks([])    \nplt.tight_layout()\nplt.savefig('test_mnist.png', dpi=300)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:39.786809Z","iopub.execute_input":"2023-01-03T12:44:39.787169Z","iopub.status.idle":"2023-01-03T12:44:40.851318Z","shell.execute_reply.started":"2023-01-03T12:44:39.787137Z","shell.execute_reply":"2023-01-03T12:44:40.850413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(test_image)\nresult = np.argmax(result, axis=-1)\n\nprint(result)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:44.007739Z","iopub.execute_input":"2023-01-03T12:44:44.008104Z","iopub.status.idle":"2023-01-03T12:44:46.313620Z","shell.execute_reply.started":"2023-01-03T12:44:44.008071Z","shell.execute_reply":"2023-01-03T12:44:46.312555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label': result})\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:48.681635Z","iopub.execute_input":"2023-01-03T12:44:48.682383Z","iopub.status.idle":"2023-01-03T12:44:48.699879Z","shell.execute_reply.started":"2023-01-03T12:44:48.682336Z","shell.execute_reply":"2023-01-03T12:44:48.698326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T12:44:58.850963Z","iopub.execute_input":"2023-01-03T12:44:58.851340Z","iopub.status.idle":"2023-01-03T12:44:58.883516Z","shell.execute_reply.started":"2023-01-03T12:44:58.851308Z","shell.execute_reply":"2023-01-03T12:44:58.882624Z"},"trusted":true},"execution_count":null,"outputs":[]}]}