{"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)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\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":"2022-07-07T17:47:17.911072Z","iopub.execute_input":"2022-07-07T17:47:17.911428Z","iopub.status.idle":"2022-07-07T17:47:23.091025Z","shell.execute_reply.started":"2022-07-07T17:47:17.911325Z","shell.execute_reply":"2022-07-07T17:47:23.090275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:23.092677Z","iopub.execute_input":"2022-07-07T17:47:23.093423Z","iopub.status.idle":"2022-07-07T17:47:26.559084Z","shell.execute_reply.started":"2022-07-07T17:47:23.093372Z","shell.execute_reply":"2022-07-07T17:47:26.558394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:26.560340Z","iopub.execute_input":"2022-07-07T17:47:26.560603Z","iopub.status.idle":"2022-07-07T17:47:28.317169Z","shell.execute_reply.started":"2022-07-07T17:47:26.560569Z","shell.execute_reply":"2022-07-07T17:47:28.316471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## preprocessing","metadata":{}},{"cell_type":"code","source":"images = train_df.drop(columns=['label'])\nimages = images/255.0\nimages = np.array(images).reshape(-1,28,28)\nimages[0]\nimages.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.319227Z","iopub.execute_input":"2022-07-07T17:47:28.319979Z","iopub.status.idle":"2022-07-07T17:47:28.597134Z","shell.execute_reply.started":"2022-07-07T17:47:28.319942Z","shell.execute_reply":"2022-07-07T17:47:28.596436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example Image","metadata":{}},{"cell_type":"code","source":"plt.imshow(images[5], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.598475Z","iopub.execute_input":"2022-07-07T17:47:28.598740Z","iopub.status.idle":"2022-07-07T17:47:28.784998Z","shell.execute_reply.started":"2022-07-07T17:47:28.598707Z","shell.execute_reply":"2022-07-07T17:47:28.784397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test_df/255.0\ntest_images = np.array(test_images).reshape(-1,28,28)\ntest_images.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.786313Z","iopub.execute_input":"2022-07-07T17:47:28.786580Z","iopub.status.idle":"2022-07-07T17:47:28.921207Z","shell.execute_reply.started":"2022-07-07T17:47:28.786528Z","shell.execute_reply":"2022-07-07T17:47:28.920519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = np.expand_dims(images, axis=-1)\ntest_images = np.expand_dims(test_images, axis=-1)\nprint(train_images.shape)\nprint(test_images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.922441Z","iopub.execute_input":"2022-07-07T17:47:28.922704Z","iopub.status.idle":"2022-07-07T17:47:28.927978Z","shell.execute_reply.started":"2022-07-07T17:47:28.922675Z","shell.execute_reply":"2022-07-07T17:47:28.927274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.array(train_df.label)\nlabels.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.929180Z","iopub.execute_input":"2022-07-07T17:47:28.929625Z","iopub.status.idle":"2022-07-07T17:47:28.941992Z","shell.execute_reply.started":"2022-07-07T17:47:28.929590Z","shell.execute_reply":"2022-07-07T17:47:28.941066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Add some Augmentation","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rotation_range=15,\n                                  zoom_range=0.1)\ntrain_generator = train_datagen.flow(x=train_images,\n                                    y=labels,\n                                    batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:28.943423Z","iopub.execute_input":"2022-07-07T17:47:28.943750Z","iopub.status.idle":"2022-07-07T17:47:30.010123Z","shell.execute_reply.started":"2022-07-07T17:47:28.943713Z","shell.execute_reply":"2022-07-07T17:47:30.009384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_generator.x.shape)\nprint(train_generator.y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:30.012653Z","iopub.execute_input":"2022-07-07T17:47:30.012908Z","iopub.status.idle":"2022-07-07T17:47:30.019646Z","shell.execute_reply.started":"2022-07-07T17:47:30.012873Z","shell.execute_reply":"2022-07-07T17:47:30.018841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# build model","metadata":{}},{"cell_type":"code","source":"model = keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28,28,1)),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(64, 3, activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n    \n])\n\nmodel.compile(optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:30.022796Z","iopub.execute_input":"2022-07-07T17:47:30.023037Z","iopub.status.idle":"2022-07-07T17:47:32.778014Z","shell.execute_reply.started":"2022-07-07T17:47:30.023001Z","shell.execute_reply":"2022-07-07T17:47:32.776160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:47:32.779339Z","iopub.execute_input":"2022-07-07T17:47:32.779605Z","iopub.status.idle":"2022-07-07T17:53:47.909154Z","shell.execute_reply.started":"2022-07-07T17:47:32.779571Z","shell.execute_reply":"2022-07-07T17:53:47.908372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history[\"accuracy\"]\nepochs = range(len(acc))\nplt.plot(epochs, acc)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:47.910782Z","iopub.execute_input":"2022-07-07T17:53:47.911034Z","iopub.status.idle":"2022-07-07T17:53:48.079968Z","shell.execute_reply.started":"2022-07-07T17:53:47.911001Z","shell.execute_reply":"2022-07-07T17:53:48.079332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_y_pred = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:48.081178Z","iopub.execute_input":"2022-07-07T17:53:48.081543Z","iopub.status.idle":"2022-07-07T17:53:49.318456Z","shell.execute_reply.started":"2022-07-07T17:53:48.081509Z","shell.execute_reply":"2022-07-07T17:53:49.317752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = [np.argmax(pred) for pred in pre_y_pred]\nlen(y_pred)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:49.319852Z","iopub.execute_input":"2022-07-07T17:53:49.320112Z","iopub.status.idle":"2022-07-07T17:53:49.395953Z","shell.execute_reply.started":"2022-07-07T17:53:49.320078Z","shell.execute_reply":"2022-07-07T17:53:49.395333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':np.arange(1,len(y_pred)+1),\n                          'Label':y_pred})","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:49.397194Z","iopub.execute_input":"2022-07-07T17:53:49.397444Z","iopub.status.idle":"2022-07-07T17:53:49.418691Z","shell.execute_reply.started":"2022-07-07T17:53:49.397410Z","shell.execute_reply":"2022-07-07T17:53:49.418064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:49.421047Z","iopub.execute_input":"2022-07-07T17:53:49.421228Z","iopub.status.idle":"2022-07-07T17:53:49.429544Z","shell.execute_reply.started":"2022-07-07T17:53:49.421206Z","shell.execute_reply":"2022-07-07T17:53:49.428816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T17:53:49.430955Z","iopub.execute_input":"2022-07-07T17:53:49.431383Z","iopub.status.idle":"2022-07-07T17:53:49.477721Z","shell.execute_reply.started":"2022-07-07T17:53:49.431349Z","shell.execute_reply":"2022-07-07T17:53:49.477123Z"},"trusted":true},"execution_count":null,"outputs":[]}]}