{"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":"markdown","source":"**Import all Necessary Libraries**","metadata":{"_uuid":"7ed04b1e7a00a7ad31ee98a7d8e55880e6274f29"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Lambda, MaxPooling2D \nfrom keras.layers import Dense, Dropout, Flatten \n\nfrom keras.layers.normalization import BatchNormalization\n\nfrom keras.utils.np_utils import to_categorical\n\nfrom keras.datasets import mnist","metadata":{"_uuid":"0f935bddcc5ca02702578e712bf7f396ba7154ef","execution":{"iopub.status.busy":"2021-09-21T11:20:10.889186Z","iopub.execute_input":"2021-09-21T11:20:10.889786Z","iopub.status.idle":"2021-09-21T11:20:10.896601Z","shell.execute_reply.started":"2021-09-21T11:20:10.889443Z","shell.execute_reply":"2021-09-21T11:20:10.895709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir('../input'))","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:10.898587Z","iopub.execute_input":"2021-09-21T11:20:10.899298Z","iopub.status.idle":"2021-09-21T11:20:10.911371Z","shell.execute_reply.started":"2021-09-21T11:20:10.898827Z","shell.execute_reply":"2021-09-21T11:20:10.910555Z"},"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":"2021-09-21T11:20:10.912804Z","iopub.execute_input":"2021-09-21T11:20:10.913292Z","iopub.status.idle":"2021-09-21T11:20:16.451711Z","shell.execute_reply.started":"2021-09-21T11:20:10.913245Z","shell.execute_reply":"2021-09-21T11:20:16.450863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:16.454175Z","iopub.execute_input":"2021-09-21T11:20:16.454467Z","iopub.status.idle":"2021-09-21T11:20:16.46269Z","shell.execute_reply.started":"2021-09-21T11:20:16.454421Z","shell.execute_reply":"2021-09-21T11:20:16.461527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(['label'], 1).values\ny = train['label'].values\n\ntest_x = test.values\n","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:16.46589Z","iopub.execute_input":"2021-09-21T11:20:16.466467Z","iopub.status.idle":"2021-09-21T11:20:16.647002Z","shell.execute_reply.started":"2021-09-21T11:20:16.466234Z","shell.execute_reply":"2021-09-21T11:20:16.646105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X / 255.0\ntest_x = test_x / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:16.648903Z","iopub.execute_input":"2021-09-21T11:20:16.649235Z","iopub.status.idle":"2021-09-21T11:20:16.987437Z","shell.execute_reply.started":"2021-09-21T11:20:16.649175Z","shell.execute_reply":"2021-09-21T11:20:16.9866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1,28,28,1)\ntest_x = test_x.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:16.989224Z","iopub.execute_input":"2021-09-21T11:20:16.989768Z","iopub.status.idle":"2021-09-21T11:20:16.994519Z","shell.execute_reply.started":"2021-09-21T11:20:16.989498Z","shell.execute_reply":"2021-09-21T11:20:16.993724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:16.995988Z","iopub.execute_input":"2021-09-21T11:20:16.996535Z","iopub.status.idle":"2021-09-21T11:20:17.006518Z","shell.execute_reply.started":"2021-09-21T11:20:16.996463Z","shell.execute_reply":"2021-09-21T11:20:17.005559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom 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=0)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:17.008096Z","iopub.execute_input":"2021-09-21T11:20:17.008476Z","iopub.status.idle":"2021-09-21T11:20:17.593346Z","shell.execute_reply.started":"2021-09-21T11:20:17.008417Z","shell.execute_reply":"2021-09-21T11:20:17.592515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape, y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:17.594808Z","iopub.execute_input":"2021-09-21T11:20:17.595168Z","iopub.status.idle":"2021-09-21T11:20:17.602113Z","shell.execute_reply.started":"2021-09-21T11:20:17.595089Z","shell.execute_reply":"2021-09-21T11:20:17.600991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nbatch_size = 64","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:17.603616Z","iopub.execute_input":"2021-09-21T11:20:17.604043Z","iopub.status.idle":"2021-09-21T11:20:17.610515Z","shell.execute_reply.started":"2021-09-21T11:20:17.603874Z","shell.execute_reply":"2021-09-21T11:20:17.609254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n  \nmodel.add(Conv2D(filters=128, kernel_size = (3,3), activation=\"relu\", input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), activation=\"relu\"))\n\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=128, kernel_size = (3,3), activation=\"relu\"))\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), activation=\"relu\"))\n\nmodel.add(MaxPooling2D(pool_size=(2,2)))   \nmodel.add(Conv2D(filters=32, kernel_size = (3,3), activation=\"relu\"))\n    \nmodel.add(MaxPooling2D(pool_size=(2,2)))\n    \nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\n    \nmodel.add(Dense(10,activation=\"softmax\"))\n    \nmodel.compile(loss=\"categorical_crossentropy\", optimizer=\"SGD\", metrics=[\"accuracy\"])","metadata":{"_uuid":"e935c979b0715a68bc8739e6a31f5d9a8189a750","execution":{"iopub.status.busy":"2021-09-21T11:20:17.612335Z","iopub.execute_input":"2021-09-21T11:20:17.613007Z","iopub.status.idle":"2021-09-21T11:20:18.053639Z","shell.execute_reply.started":"2021-09-21T11:20:17.612757Z","shell.execute_reply":"2021-09-21T11:20:18.052868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit(X_train, y_train,\n                              epochs = epochs, \n                             batch_size=32,\n                              validation_data = (X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:20:18.055404Z","iopub.execute_input":"2021-09-21T11:20:18.055782Z","iopub.status.idle":"2021-09-21T11:21:32.370165Z","shell.execute_reply.started":"2021-09-21T11:20:18.055711Z","shell.execute_reply":"2021-09-21T11:21:32.369233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig, ax = plt.subplots(2,1, figsize=(18, 10))\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:32.372374Z","iopub.execute_input":"2021-09-21T11:21:32.372659Z","iopub.status.idle":"2021-09-21T11:21:32.871983Z","shell.execute_reply.started":"2021-09-21T11:21:32.372612Z","shell.execute_reply":"2021-09-21T11:21:32.870985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:32.873509Z","iopub.execute_input":"2021-09-21T11:21:32.873781Z","iopub.status.idle":"2021-09-21T11:21:33.332663Z","shell.execute_reply.started":"2021-09-21T11:21:32.873735Z","shell.execute_reply":"2021-09-21T11:21:33.331757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_labels=np.argmax(y_pred, axis=-1)\ny_test=np.argmax(y_test, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:33.334169Z","iopub.execute_input":"2021-09-21T11:21:33.334504Z","iopub.status.idle":"2021-09-21T11:21:33.341852Z","shell.execute_reply.started":"2021-09-21T11:21:33.334453Z","shell.execute_reply":"2021-09-21T11:21:33.340996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:33.344107Z","iopub.execute_input":"2021-09-21T11:21:33.345277Z","iopub.status.idle":"2021-09-21T11:21:33.351509Z","shell.execute_reply.started":"2021-09-21T11:21:33.345229Z","shell.execute_reply":"2021-09-21T11:21:33.35084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred_labels))","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:33.353937Z","iopub.execute_input":"2021-09-21T11:21:33.354856Z","iopub.status.idle":"2021-09-21T11:21:33.375418Z","shell.execute_reply.started":"2021-09-21T11:21:33.354117Z","shell.execute_reply":"2021-09-21T11:21:33.374608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test, y_pred_labels))","metadata":{"execution":{"iopub.status.busy":"2021-09-21T11:21:33.378034Z","iopub.execute_input":"2021-09-21T11:21:33.378309Z","iopub.status.idle":"2021-09-21T11:21:33.387305Z","shell.execute_reply.started":"2021-09-21T11:21:33.378265Z","shell.execute_reply":"2021-09-21T11:21:33.386392Z"},"trusted":true},"execution_count":null,"outputs":[]}]}