{"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-27T04:02:58.114812Z","iopub.execute_input":"2023-01-27T04:02:58.115219Z","iopub.status.idle":"2023-01-27T04:02:58.124583Z","shell.execute_reply.started":"2023-01-27T04:02:58.115187Z","shell.execute_reply":"2023-01-27T04:02:58.123405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\n\ntrain_input = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_input = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nprint(train_input.shape)\nprint(test_input.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:05:34.427921Z","iopub.execute_input":"2023-01-27T04:05:34.428331Z","iopub.status.idle":"2023-01-27T04:05:46.560775Z","shell.execute_reply.started":"2023-01-27T04:05:34.428298Z","shell.execute_reply":"2023-01-27T04:05:46.559898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_input.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:05:50.280997Z","iopub.execute_input":"2023-01-27T04:05:50.281461Z","iopub.status.idle":"2023-01-27T04:05:50.309101Z","shell.execute_reply.started":"2023-01-27T04:05:50.281423Z","shell.execute_reply":"2023-01-27T04:05:50.308170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_input.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:05:59.834530Z","iopub.execute_input":"2023-01-27T04:05:59.835074Z","iopub.status.idle":"2023-01-27T04:05:59.859360Z","shell.execute_reply.started":"2023-01-27T04:05:59.835023Z","shell.execute_reply":"2023-01-27T04:05:59.858102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_target = train_input['label']\ntrain_scaled = train_input.drop(labels=['label'], axis=1)\ntrain_scaled = train_scaled.to_numpy().reshape(-1, 28, 28, 1) / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:06:11.369911Z","iopub.execute_input":"2023-01-27T04:06:11.370312Z","iopub.status.idle":"2023-01-27T04:06:11.655605Z","shell.execute_reply.started":"2023-01-27T04:06:11.370278Z","shell.execute_reply":"2023-01-27T04:06:11.654572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_scaled, val_scaled, train_target, val_target = train_test_split(train_scaled, train_target, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:06:20.902719Z","iopub.execute_input":"2023-01-27T04:06:20.903783Z","iopub.status.idle":"2023-01-27T04:06:21.319760Z","shell.execute_reply.started":"2023-01-27T04:06:20.903739Z","shell.execute_reply":"2023-01-27T04:06:21.318564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\n\nmodel.add(keras.layers.Conv2D(32, kernel_size=3, activation='relu', padding='same', input_shape=(28,28,1)))\nmodel.add(keras.layers.MaxPooling2D(2))\nmodel.add(keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add(keras.layers.MaxPooling2D(2))","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:07:01.335554Z","iopub.execute_input":"2023-01-27T04:07:01.336364Z","iopub.status.idle":"2023-01-27T04:07:01.373427Z","shell.execute_reply.started":"2023-01-27T04:07:01.336279Z","shell.execute_reply":"2023-01-27T04:07:01.372395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(100, activation='relu'))\nmodel.add(keras.layers.Dropout(0.4))\nmodel.add(keras.layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:07:03.711368Z","iopub.execute_input":"2023-01-27T04:07:03.711756Z","iopub.status.idle":"2023-01-27T04:07:03.745600Z","shell.execute_reply.started":"2023-01-27T04:07:03.711724Z","shell.execute_reply":"2023-01-27T04:07:03.744508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:07:11.828229Z","iopub.execute_input":"2023-01-27T04:07:11.829537Z","iopub.status.idle":"2023-01-27T04:07:11.836788Z","shell.execute_reply.started":"2023-01-27T04:07:11.829488Z","shell.execute_reply":"2023-01-27T04:07:11.835146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics='accuracy')\ncheckpoint = keras.callbacks.ModelCheckpoint('best-cnn-model.h5', save_best_only=True)\nearly_stopping = keras.callbacks.EarlyStopping(patience=2, restore_best_weights=True)\nhistory = model.fit(train_scaled, train_target, epochs=20, validation_data=(val_scaled, val_target), callbacks=[checkpoint, early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:08:54.222905Z","iopub.execute_input":"2023-01-27T04:08:54.223294Z","iopub.status.idle":"2023-01-27T04:11:12.081007Z","shell.execute_reply.started":"2023-01-27T04:08:54.223264Z","shell.execute_reply":"2023-01-27T04:11:12.079766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(val_scaled[0].reshape(28, 28), cmap='gray_r')","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:12:53.235384Z","iopub.execute_input":"2023-01-27T04:12:53.235831Z","iopub.status.idle":"2023-01-27T04:12:53.473241Z","shell.execute_reply.started":"2023-01-27T04:12:53.235794Z","shell.execute_reply":"2023-01-27T04:12:53.472385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(val_scaled[0:1])\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:13:03.014419Z","iopub.execute_input":"2023-01-27T04:13:03.014814Z","iopub.status.idle":"2023-01-27T04:13:03.184048Z","shell.execute_reply.started":"2023-01-27T04:13:03.014783Z","shell.execute_reply":"2023-01-27T04:13:03.182569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(range(1, 11), preds[0])\nplt.xlabel('class')\nplt.ylabel('prob.')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:13:10.670875Z","iopub.execute_input":"2023-01-27T04:13:10.671264Z","iopub.status.idle":"2023-01-27T04:13:10.878236Z","shell.execute_reply.started":"2023-01-27T04:13:10.671233Z","shell.execute_reply":"2023-01-27T04:13:10.877174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n\nimport numpy as np\nprint(classes[np.argmax(preds)])","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:13:22.478523Z","iopub.execute_input":"2023-01-27T04:13:22.479682Z","iopub.status.idle":"2023-01-27T04:13:22.485750Z","shell.execute_reply.started":"2023-01-27T04:13:22.479641Z","shell.execute_reply":"2023-01-27T04:13:22.484660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_scaled = test_input.to_numpy().reshape(-1, 28, 28, 1) / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:13:31.305205Z","iopub.execute_input":"2023-01-27T04:13:31.305613Z","iopub.status.idle":"2023-01-27T04:13:31.412538Z","shell.execute_reply.started":"2023-01-27T04:13:31.305580Z","shell.execute_reply":"2023-01-27T04:13:31.411283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(val_scaled, val_target)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:36:30.595228Z","iopub.execute_input":"2023-01-27T04:36:30.595650Z","iopub.status.idle":"2023-01-27T04:36:32.158404Z","shell.execute_reply.started":"2023-01-27T04:36:30.595617Z","shell.execute_reply":"2023-01-27T04:36:32.157540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:48:57.516272Z","iopub.execute_input":"2023-01-27T04:48:57.516701Z","iopub.status.idle":"2023-01-27T04:48:57.534565Z","shell.execute_reply.started":"2023-01-27T04:48:57.516661Z","shell.execute_reply":"2023-01-27T04:48:57.533735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_scaled)\nprediction = prediction.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:48:59.384954Z","iopub.execute_input":"2023-01-27T04:48:59.385715Z","iopub.status.idle":"2023-01-27T04:49:03.618665Z","shell.execute_reply.started":"2023-01-27T04:48:59.385678Z","shell.execute_reply":"2023-01-27T04:49:03.617376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:49:05.683747Z","iopub.execute_input":"2023-01-27T04:49:05.684524Z","iopub.status.idle":"2023-01-27T04:49:05.690659Z","shell.execute_reply.started":"2023-01-27T04:49:05.684484Z","shell.execute_reply":"2023-01-27T04:49:05.689766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label'] = prediction","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:49:07.703199Z","iopub.execute_input":"2023-01-27T04:49:07.703628Z","iopub.status.idle":"2023-01-27T04:49:07.708294Z","shell.execute_reply.started":"2023-01-27T04:49:07.703591Z","shell.execute_reply":"2023-01-27T04:49:07.707491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:49:09.416752Z","iopub.execute_input":"2023-01-27T04:49:09.417168Z","iopub.status.idle":"2023-01-27T04:49:09.429395Z","shell.execute_reply.started":"2023-01-27T04:49:09.417132Z","shell.execute_reply":"2023-01-27T04:49:09.428286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T04:49:14.372553Z","iopub.execute_input":"2023-01-27T04:49:14.372953Z","iopub.status.idle":"2023-01-27T04:49:14.407549Z","shell.execute_reply.started":"2023-01-27T04:49:14.372918Z","shell.execute_reply":"2023-01-27T04:49:14.406277Z"},"trusted":true},"execution_count":null,"outputs":[]}]}