{"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 seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\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-06-01T06:27:51.727475Z","iopub.execute_input":"2021-06-01T06:27:51.728041Z","iopub.status.idle":"2021-06-01T06:27:58.645805Z","shell.execute_reply.started":"2021-06-01T06:27:51.727953Z","shell.execute_reply":"2021-06-01T06:27:58.644791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Data preparation****","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\nprint(train_data.shape,test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:32:06.403593Z","iopub.execute_input":"2021-06-01T06:32:06.404224Z","iopub.status.idle":"2021-06-01T06:32:12.985514Z","shell.execute_reply.started":"2021-06-01T06:32:06.404187Z","shell.execute_reply":"2021-06-01T06:32:12.984321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data.drop([\"label\"],axis = 1).values\nY = train_data[\"label\"].values\n","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:43:08.733350Z","iopub.execute_input":"2021-06-01T06:43:08.733749Z","iopub.status.idle":"2021-06-01T06:43:08.858080Z","shell.execute_reply.started":"2021-06-01T06:43:08.733713Z","shell.execute_reply":"2021-06-01T06:43:08.856903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape,Y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:53:28.112543Z","iopub.execute_input":"2021-06-01T06:53:28.112937Z","iopub.status.idle":"2021-06-01T06:53:28.118874Z","shell.execute_reply.started":"2021-06-01T06:53:28.112906Z","shell.execute_reply":"2021-06-01T06:53:28.117971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape([42000,28,28,1])\nY = Y.reshape([42000,1])","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:55:32.613913Z","iopub.execute_input":"2021-06-01T06:55:32.614283Z","iopub.status.idle":"2021-06-01T06:55:32.619520Z","shell.execute_reply.started":"2021-06-01T06:55:32.614254Z","shell.execute_reply":"2021-06-01T06:55:32.618323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert to one-hot-encoding\nfrom keras.utils.np_utils import to_categorical \nY = to_categorical(Y, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:55:51.560255Z","iopub.execute_input":"2021-06-01T06:55:51.560640Z","iopub.status.idle":"2021-06-01T06:55:51.645740Z","shell.execute_reply.started":"2021-06-01T06:55:51.560604Z","shell.execute_reply":"2021-06-01T06:55:51.644662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state = 14)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:56:17.580938Z","iopub.execute_input":"2021-06-01T06:56:17.581301Z","iopub.status.idle":"2021-06-01T06:56:18.322086Z","shell.execute_reply.started":"2021-06-01T06:56:17.581271Z","shell.execute_reply":"2021-06-01T06:56:18.321082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\nprint(x_test.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:56:26.200096Z","iopub.execute_input":"2021-06-01T06:56:26.200455Z","iopub.status.idle":"2021-06-01T06:56:26.206736Z","shell.execute_reply.started":"2021-06-01T06:56:26.200425Z","shell.execute_reply":"2021-06-01T06:56:26.205596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train/255\nx_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:57:47.276030Z","iopub.execute_input":"2021-06-01T06:57:47.277004Z","iopub.status.idle":"2021-06-01T06:57:47.495259Z","shell.execute_reply.started":"2021-06-01T06:57:47.276936Z","shell.execute_reply":"2021-06-01T06:57:47.494278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:57:59.150389Z","iopub.execute_input":"2021-06-01T06:57:59.150953Z","iopub.status.idle":"2021-06-01T06:57:59.340638Z","shell.execute_reply.started":"2021-06-01T06:57:59.150920Z","shell.execute_reply":"2021-06-01T06:57:59.339647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)\nmodel.compile(optimizer = optimizer,\n              loss = 'categorical_crossentropy',\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:58:26.529574Z","iopub.execute_input":"2021-06-01T06:58:26.529979Z","iopub.status.idle":"2021-06-01T06:58:26.548876Z","shell.execute_reply.started":"2021-06-01T06:58:26.529942Z","shell.execute_reply":"2021-06-01T06:58:26.547389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.6, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:59:39.841544Z","iopub.execute_input":"2021-06-01T06:59:39.842121Z","iopub.status.idle":"2021-06-01T06:59:39.847822Z","shell.execute_reply.started":"2021-06-01T06:59:39.842081Z","shell.execute_reply":"2021-06-01T06:59:39.846775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nepochs = 30","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:58:38.985767Z","iopub.execute_input":"2021-06-01T06:58:38.986126Z","iopub.status.idle":"2021-06-01T06:58:38.990645Z","shell.execute_reply.started":"2021-06-01T06:58:38.986094Z","shell.execute_reply":"2021-06-01T06:58:38.989318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator( \n        rotation_range=10,  \n        zoom_range = 0.1, \n        width_shift_range=0.1,  \n        height_shift_range=0.1,\n        shear_range = 0.1,\n        horizontal_flip=False,  \n        vertical_flip=False\n        )\ntrain_datagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:58:57.057869Z","iopub.execute_input":"2021-06-01T06:58:57.058229Z","iopub.status.idle":"2021-06-01T06:58:57.160652Z","shell.execute_reply.started":"2021-06-01T06:58:57.058199Z","shell.execute_reply":"2021-06-01T06:58:57.159538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n            train_datagen.flow(x_train,y_train,batch_size = batch_size),\n            validation_data = (x_test,y_test),\n            batch_size = batch_size,\n            steps_per_epoch = x_train.shape[0]//batch_size,\n            epochs = epochs,\n            verbose = 1,\n            callbacks=[learning_rate_reduction]\n            )","metadata":{"execution":{"iopub.status.busy":"2021-06-01T06:59:45.180182Z","iopub.execute_input":"2021-06-01T06:59:45.180562Z","iopub.status.idle":"2021-06-01T07:29:25.628980Z","shell.execute_reply.started":"2021-06-01T06:59:45.180529Z","shell.execute_reply":"2021-06-01T07:29:25.627262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:31:12.390631Z","iopub.execute_input":"2021-06-01T07:31:12.391236Z","iopub.status.idle":"2021-06-01T07:31:14.248552Z","shell.execute_reply.started":"2021-06-01T07:31:12.391199Z","shell.execute_reply":"2021-06-01T07:31:14.247417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure()\nplt.plot(acc,color = 'green',label = 'Training Acuracy')\nplt.plot(val_acc,color = 'red',label = 'Validation Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:31:31.722582Z","iopub.execute_input":"2021-06-01T07:31:31.722979Z","iopub.status.idle":"2021-06-01T07:31:31.935991Z","shell.execute_reply.started":"2021-06-01T07:31:31.722945Z","shell.execute_reply":"2021-06-01T07:31:31.935019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.plot(loss,color = 'green',label = 'Training Loss')\nplt.plot(val_loss,color = 'red',label = 'Validation Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:31:43.880205Z","iopub.execute_input":"2021-06-01T07:31:43.880607Z","iopub.status.idle":"2021-06-01T07:31:44.053254Z","shell.execute_reply.started":"2021-06-01T07:31:43.880566Z","shell.execute_reply":"2021-06-01T07:31:44.052356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = test_data.values\ndata = data.reshape([28000,28,28,1])\nprint(data.shape)\ndata = data/255","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:32:02.279722Z","iopub.execute_input":"2021-06-01T07:32:02.280234Z","iopub.status.idle":"2021-06-01T07:32:02.778031Z","shell.execute_reply.started":"2021-06-01T07:32:02.280201Z","shell.execute_reply":"2021-06-01T07:32:02.777147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(data)\ntest_pred = np.argmax(test_pred,axis=1)\nprint(test_pred.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:32:13.044788Z","iopub.execute_input":"2021-06-01T07:32:13.045375Z","iopub.status.idle":"2021-06-01T07:32:24.460968Z","shell.execute_reply.started":"2021-06-01T07:32:13.045328Z","shell.execute_reply":"2021-06-01T07:32:24.459933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:32:24.462509Z","iopub.execute_input":"2021-06-01T07:32:24.462820Z","iopub.status.idle":"2021-06-01T07:32:24.512412Z","shell.execute_reply.started":"2021-06-01T07:32:24.462790Z","shell.execute_reply":"2021-06-01T07:32:24.511301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = sample_submission.ImageId\ndata = {'ImageId' : index,'Label': test_pred}\ndf = pd.DataFrame(data)\ndf.head","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:32:59.108761Z","iopub.execute_input":"2021-06-01T07:32:59.109239Z","iopub.status.idle":"2021-06-01T07:32:59.123035Z","shell.execute_reply.started":"2021-06-01T07:32:59.109200Z","shell.execute_reply":"2021-06-01T07:32:59.121721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission_mnist.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T07:33:37.056621Z","iopub.execute_input":"2021-06-01T07:33:37.057236Z","iopub.status.idle":"2021-06-01T07:33:37.112743Z","shell.execute_reply.started":"2021-06-01T07:33:37.057201Z","shell.execute_reply":"2021-06-01T07:33:37.111832Z"},"trusted":true},"execution_count":null,"outputs":[]}]}