{"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":"2021-07-30T12:32:35.432927Z","iopub.execute_input":"2021-07-30T12:32:35.433289Z","iopub.status.idle":"2021-07-30T12:32:35.442428Z","shell.execute_reply.started":"2021-07-30T12:32:35.433259Z","shell.execute_reply":"2021-07-30T12:32:35.441684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nfinal_test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:35.444240Z","iopub.execute_input":"2021-07-30T12:32:35.444670Z","iopub.status.idle":"2021-07-30T12:32:40.197013Z","shell.execute_reply.started":"2021-07-30T12:32:35.444627Z","shell.execute_reply":"2021-07-30T12:32:40.195850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()\ntrain_labels = train_data.label\ntrain_data.drop(['label'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.199055Z","iopub.execute_input":"2021-07-30T12:32:40.199385Z","iopub.status.idle":"2021-07-30T12:32:40.296022Z","shell.execute_reply.started":"2021-07-30T12:32:40.199355Z","shell.execute_reply":"2021-07-30T12:32:40.294910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.299955Z","iopub.execute_input":"2021-07-30T12:32:40.300574Z","iopub.status.idle":"2021-07-30T12:32:40.305940Z","shell.execute_reply.started":"2021-07-30T12:32:40.300522Z","shell.execute_reply":"2021-07-30T12:32:40.305207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_data, test_data, train_labels, test_labels = train_test_split(train_data, train_labels, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.307157Z","iopub.execute_input":"2021-07-30T12:32:40.307622Z","iopub.status.idle":"2021-07-30T12:32:40.608746Z","shell.execute_reply.started":"2021-07-30T12:32:40.307567Z","shell.execute_reply":"2021-07-30T12:32:40.607647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape\ntest_data.shape\ntrain_data.iloc[:,1].dtype\ntrain_data = train_data.to_numpy().reshape(train_data.shape[0], 28, 28, 1 )\ntest_data = test_data.to_numpy().reshape(test_data.shape[0], 28, 28, 1 )\nfinal_test_data = final_test_data.to_numpy().reshape(final_test_data.shape[0], 28, 28, 1 )","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.610017Z","iopub.execute_input":"2021-07-30T12:32:40.610356Z","iopub.status.idle":"2021-07-30T12:32:40.617336Z","shell.execute_reply.started":"2021-07-30T12:32:40.610326Z","shell.execute_reply":"2021-07-30T12:32:40.616194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalization\ntrain_data = train_data/255.0\ntest_data = test_data/255.0\nfinal_test_data = final_test_data/255.0","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.618783Z","iopub.execute_input":"2021-07-30T12:32:40.619094Z","iopub.status.idle":"2021-07-30T12:32:40.808042Z","shell.execute_reply.started":"2021-07-30T12:32:40.619065Z","shell.execute_reply":"2021-07-30T12:32:40.806955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\ntrain_labels = to_categorical(train_labels)\ntest_labels = to_categorical(test_labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.809490Z","iopub.execute_input":"2021-07-30T12:32:40.809833Z","iopub.status.idle":"2021-07-30T12:32:40.815628Z","shell.execute_reply.started":"2021-07-30T12:32:40.809800Z","shell.execute_reply":"2021-07-30T12:32:40.814568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.818825Z","iopub.execute_input":"2021-07-30T12:32:40.819159Z","iopub.status.idle":"2021-07-30T12:32:40.828000Z","shell.execute_reply.started":"2021-07-30T12:32:40.819120Z","shell.execute_reply":"2021-07-30T12:32:40.826708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import models\nfrom keras import layers\nfrom keras import optimizers\n\nmy_model = models.Sequential()\n# Convolutional layers\nmy_model.add(layers.Conv2D(40, (3,3), activation = 'relu', input_shape=(28, 28, 1), name = 'First_Conv'))\nmy_model.add(layers.MaxPooling2D((2,2)))\nmy_model.add(layers.Dropout(0.3))\n#my_model.add(layers.BatchNormalization())\nmy_model.add(layers.Conv2D(80, (3,3), activation = 'relu', name = 'Second_Conv'))\nmy_model.add(layers.MaxPooling2D((2,2)))\nmy_model.add(layers.Dropout(0.3))\nmy_model.add(layers.Conv2D(160, (3,3), activation = 'relu', name = 'Third_Conv'))\n\n# Dense classificator \nmy_model.add(layers.Flatten())\n#my_model.add(layers.BatchNormalization())\nmy_model.add(layers.Dropout(0.3))\nmy_model.add(layers.Dense(64, activation = 'relu', name = 'First_Dense'))\n#my_model.add(layers.BatchNormalization())\n#my_model.add(layers.Dense(64, activation = 'relu', name = 'Second_Dense'))\nmy_model.add(layers.Dense(10, activation = 'softmax', name = 'Output'))\n\nmy_model.summary()\n\nfrom keras.callbacks import EarlyStopping\ncallbacks_list = [EarlyStopping(monitor = 'categorical_accuracy', patience = 3, restore_best_weights = True)]\n\noptimizer_use = optimizers.RMSprop(momentum = 0.05)\nmy_model.compile(optimizer = optimizer_use, loss = 'categorical_crossentropy', metrics = ['categorical_accuracy'])\n#history = my_model.fit(train_data, train_labels, epochs = 10, batch_size = 64, validation_split = 0.2, callbacks = callbacks_list)\nhistory = my_model.fit(datagen.flow(train_data, train_labels, batch_size = 64), epochs = 10, validation_data = datagen.flow(train_data, train_labels, batch_size = 12), callbacks = callbacks_list)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:32:40.829789Z","iopub.execute_input":"2021-07-30T12:32:40.830239Z","iopub.status.idle":"2021-07-30T12:38:21.993443Z","shell.execute_reply.started":"2021-07-30T12:32:40.830208Z","shell.execute_reply":"2021-07-30T12:38:21.992481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluations = my_model.evaluate(test_data, test_labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:21.994755Z","iopub.execute_input":"2021-07-30T12:38:21.995051Z","iopub.status.idle":"2021-07-30T12:38:24.197013Z","shell.execute_reply.started":"2021-07-30T12:38:21.995020Z","shell.execute_reply":"2021-07-30T12:38:24.196029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()\nloss_values = history.history['loss']\nval_loss_values = history.history['val_loss']\nacc_values = history.history['categorical_accuracy']\nval_acc_values = history.history['val_categorical_accuracy']\nimport matplotlib.pyplot as plt\nepochs = range(1, len(loss_values) + 1)\nplt.plot(epochs, loss_values, 'b', label = 'Training loss')\nplt.plot(epochs, val_loss_values, 'r', label = 'Validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()\n\nplt.clf()\nplt.plot(epochs, acc_values, 'b', label = 'Training accuracy')\nplt.plot(epochs, val_acc_values, 'r', label = 'Validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:24.198490Z","iopub.execute_input":"2021-07-30T12:38:24.198842Z","iopub.status.idle":"2021-07-30T12:38:24.525153Z","shell.execute_reply.started":"2021-07-30T12:38:24.198804Z","shell.execute_reply":"2021-07-30T12:38:24.524007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluations[1]","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:24.527668Z","iopub.execute_input":"2021-07-30T12:38:24.528146Z","iopub.status.idle":"2021-07-30T12:38:24.535137Z","shell.execute_reply.started":"2021-07-30T12:38:24.528099Z","shell.execute_reply":"2021-07-30T12:38:24.533676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = my_model.predict(final_test_data)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:24.536315Z","iopub.execute_input":"2021-07-30T12:38:24.536649Z","iopub.status.idle":"2021-07-30T12:38:30.401594Z","shell.execute_reply.started":"2021-07-30T12:38:24.536618Z","shell.execute_reply":"2021-07-30T12:38:30.400790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"predictions[2]","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:30.402949Z","iopub.execute_input":"2021-07-30T12:38:30.403546Z","iopub.status.idle":"2021-07-30T12:38:30.410831Z","shell.execute_reply.started":"2021-07-30T12:38:30.403501Z","shell.execute_reply":"2021-07-30T12:38:30.409527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:30.412073Z","iopub.execute_input":"2021-07-30T12:38:30.412643Z","iopub.status.idle":"2021-07-30T12:38:30.425661Z","shell.execute_reply.started":"2021-07-30T12:38:30.412597Z","shell.execute_reply":"2021-07-30T12:38:30.424768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions, axis = 1)\nsubmission = pd.DataFrame({'ImageId':range(1,28001),'Label':predictions})\nsubmission.to_csv('sub.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:30.426905Z","iopub.execute_input":"2021-07-30T12:38:30.427453Z","iopub.status.idle":"2021-07-30T12:38:30.490602Z","shell.execute_reply.started":"2021-07-30T12:38:30.427410Z","shell.execute_reply":"2021-07-30T12:38:30.489773Z"},"trusted":true},"execution_count":null,"outputs":[]}]}