{"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":"import numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom glob import glob\nimport sys\nimport sklearn.metrics as metrics\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nimport seaborn as sns\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:57:20.941397Z","iopub.execute_input":"2022-07-10T15:57:20.941752Z","iopub.status.idle":"2022-07-10T15:57:20.95926Z","shell.execute_reply.started":"2022-07-10T15:57:20.941724Z","shell.execute_reply":"2022-07-10T15:57:20.958292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras\nfrom tensorflow.keras.models import Sequential, Model, load_model\nfrom tensorflow.keras.applications.vgg16 import VGG16,preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, Activation\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.callbacks import Callback, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:50:59.259137Z","iopub.execute_input":"2022-07-10T15:50:59.259834Z","iopub.status.idle":"2022-07-10T15:51:00.226383Z","shell.execute_reply.started":"2022-07-10T15:50:59.259799Z","shell.execute_reply":"2022-07-10T15:51:00.225373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:52:49.976865Z","iopub.execute_input":"2022-07-10T15:52:49.977197Z","iopub.status.idle":"2022-07-10T15:52:55.189136Z","shell.execute_reply.started":"2022-07-10T15:52:49.977147Z","shell.execute_reply":"2022-07-10T15:52:55.188092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:55:15.43678Z","iopub.execute_input":"2022-07-10T15:55:15.437129Z","iopub.status.idle":"2022-07-10T15:55:15.467864Z","shell.execute_reply.started":"2022-07-10T15:55:15.437103Z","shell.execute_reply":"2022-07-10T15:55:15.467229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:55:41.318559Z","iopub.execute_input":"2022-07-10T15:55:41.318949Z","iopub.status.idle":"2022-07-10T15:55:41.342459Z","shell.execute_reply.started":"2022-07-10T15:55:41.318915Z","shell.execute_reply":"2022-07-10T15:55:41.341129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:09:15.716922Z","iopub.execute_input":"2022-07-10T16:09:15.717298Z","iopub.status.idle":"2022-07-10T16:09:15.722985Z","shell.execute_reply.started":"2022-07-10T16:09:15.71727Z","shell.execute_reply":"2022-07-10T16:09:15.721844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('label' , axis = 1)\ny = np.array(train['label'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:09:17.916066Z","iopub.execute_input":"2022-07-10T16:09:17.916429Z","iopub.status.idle":"2022-07-10T16:09:18.004365Z","shell.execute_reply.started":"2022-07-10T16:09:17.916399Z","shell.execute_reply":"2022-07-10T16:09:18.003357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:14:20.161285Z","iopub.execute_input":"2022-07-10T16:14:20.161939Z","iopub.status.idle":"2022-07-10T16:14:20.405265Z","shell.execute_reply.started":"2022-07-10T16:14:20.161904Z","shell.execute_reply":"2022-07-10T16:14:20.40421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (784 , )\nnum_classes = 10","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:17:08.876764Z","iopub.execute_input":"2022-07-10T16:17:08.877837Z","iopub.status.idle":"2022-07-10T16:17:08.882251Z","shell.execute_reply.started":"2022-07-10T16:17:08.877796Z","shell.execute_reply":"2022-07-10T16:17:08.881296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_flattened = X_train/255\nX_test_flattened = X_test/255","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:26:14.315903Z","iopub.execute_input":"2022-07-10T16:26:14.316309Z","iopub.status.idle":"2022-07-10T16:26:14.381212Z","shell.execute_reply.started":"2022-07-10T16:26:14.316274Z","shell.execute_reply":"2022-07-10T16:26:14.380419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:18:10.675888Z","iopub.execute_input":"2022-07-10T16:18:10.676244Z","iopub.status.idle":"2022-07-10T16:18:10.679593Z","shell.execute_reply.started":"2022-07-10T16:18:10.676214Z","shell.execute_reply":"2022-07-10T16:18:10.678906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Dense(512, input_shape=(784,), activation='relu'), \n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(256 , activation = 'relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(128 , activation = 'relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(64 , activation = 'relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(32 , activation = 'relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(16 , activation = 'relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dense(10 , activation = 'softmax'),\n])\n\nmodel.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nhist = model.fit(X_train_flattened,y_train,epochs=25, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:26:17.241842Z","iopub.execute_input":"2022-07-10T16:26:17.242898Z","iopub.status.idle":"2022-07-10T16:28:16.499227Z","shell.execute_reply.started":"2022-07-10T16:26:17.242858Z","shell.execute_reply":"2022-07-10T16:28:16.49843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hist.history['loss'], label='train loss')\nplt.plot(hist.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nplt.savefig('LossVal_loss')\n\n# plot the accuracy\nplt.plot(hist.history['accuracy'], label='train acc')\nplt.plot(hist.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nplt.savefig('AccVal_acc')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:28:23.196752Z","iopub.execute_input":"2022-07-10T16:28:23.197124Z","iopub.status.idle":"2022-07-10T16:28:23.579604Z","shell.execute_reply.started":"2022-07-10T16:28:23.19709Z","shell.execute_reply":"2022-07-10T16:28:23.578689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test_flattened,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:28:33.01562Z","iopub.execute_input":"2022-07-10T16:28:33.015964Z","iopub.status.idle":"2022-07-10T16:28:34.053875Z","shell.execute_reply.started":"2022-07-10T16:28:33.015937Z","shell.execute_reply":"2022-07-10T16:28:34.053196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted = model.predict(X_test_flattened)\nnp.argmax(y_predicted[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:29:12.502026Z","iopub.execute_input":"2022-07-10T16:29:12.502417Z","iopub.status.idle":"2022-07-10T16:29:13.140896Z","shell.execute_reply.started":"2022-07-10T16:29:12.502389Z","shell.execute_reply":"2022-07-10T16:29:13.140072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:31:48.762523Z","iopub.execute_input":"2022-07-10T16:31:48.762923Z","iopub.status.idle":"2022-07-10T16:31:48.769835Z","shell.execute_reply.started":"2022-07-10T16:31:48.762888Z","shell.execute_reply":"2022-07-10T16:31:48.76879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels = [np.argmax(i) for i in y_predicted]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:32:38.276247Z","iopub.execute_input":"2022-07-10T16:32:38.276581Z","iopub.status.idle":"2022-07-10T16:32:38.302835Z","shell.execute_reply.started":"2022-07-10T16:32:38.276554Z","shell.execute_reply":"2022-07-10T16:32:38.3022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = tf.math.confusion_matrix(labels=y_test,predictions=y_predicted_labels)\nprint(cm)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:32:57.392049Z","iopub.execute_input":"2022-07-10T16:32:57.39251Z","iopub.status.idle":"2022-07-10T16:32:57.406977Z","shell.execute_reply.started":"2022-07-10T16:32:57.39247Z","shell.execute_reply":"2022-07-10T16:32:57.406054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nsns.heatmap(cm, annot=True, fmt='d')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:33:53.837069Z","iopub.execute_input":"2022-07-10T16:33:53.837395Z","iopub.status.idle":"2022-07-10T16:33:54.338895Z","shell.execute_reply.started":"2022-07-10T16:33:53.83737Z","shell.execute_reply":"2022-07-10T16:33:54.33798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.array(test)\npredictions = np.argmax(model.predict(X_test), axis=-1)\ntest['Label'] = predictions\ntest['ImageId'] = list(range(1, 28001 , 1))\ntest[['ImageId' , 'Label']].to_csv('submission.csv' , index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:35:01.866752Z","iopub.execute_input":"2022-07-10T16:35:01.867717Z","iopub.status.idle":"2022-07-10T16:35:04.291242Z","shell.execute_reply.started":"2022-07-10T16:35:01.867681Z","shell.execute_reply":"2022-07-10T16:35:04.290397Z"},"trusted":true},"execution_count":null,"outputs":[]}]}