{"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-09-18T05:20:46.999048Z","iopub.execute_input":"2021-09-18T05:20:46.999471Z","iopub.status.idle":"2021-09-18T05:20:47.016384Z","shell.execute_reply.started":"2021-09-18T05:20:46.999375Z","shell.execute_reply":"2021-09-18T05:20:47.015324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n%matplotlib inline\nimport math\n\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:49.059144Z","iopub.execute_input":"2021-09-18T05:20:49.059488Z","iopub.status.idle":"2021-09-18T05:20:49.973357Z","shell.execute_reply.started":"2021-09-18T05:20:49.059458Z","shell.execute_reply":"2021-09-18T05:20:49.972510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing the dataset\ntrain_data = pd.read_csv(\"../input/digit-recognizer/train.csv\")\n\n# importing prediction dataset\nprediction_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n\n# CREATING TRAINING AND TEST SET\ntrain, test = train_test_split(train_data, test_size = 0.3, random_state = 100)\n\n# CREATING TRAINING AND VALIDATION SET\ntrain, val = train_test_split(train_data, test_size = 0.2, random_state = 100)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:49.974890Z","iopub.execute_input":"2021-09-18T05:20:49.975242Z","iopub.status.idle":"2021-09-18T05:20:55.735343Z","shell.execute_reply.started":"2021-09-18T05:20:49.975205Z","shell.execute_reply":"2021-09-18T05:20:55.734469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:55.737603Z","iopub.execute_input":"2021-09-18T05:20:55.738100Z","iopub.status.idle":"2021-09-18T05:20:55.762522Z","shell.execute_reply.started":"2021-09-18T05:20:55.738061Z","shell.execute_reply":"2021-09-18T05:20:55.761586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No of observations in training set:\", train.shape[0])\nprint(\"No of observations in validation set:\", val.shape[0])\nprint(\"No of observations in test set:\", test.shape[0])\nprint(\"No of observations in prediction set:\", prediction_data.shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:55.764053Z","iopub.execute_input":"2021-09-18T05:20:55.764422Z","iopub.status.idle":"2021-09-18T05:20:55.771596Z","shell.execute_reply.started":"2021-09-18T05:20:55.764382Z","shell.execute_reply":"2021-09-18T05:20:55.770705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_list = list(np.sort(train['label'].unique()))\nprint(\"Class list of labels:\", class_list)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:58.623772Z","iopub.execute_input":"2021-09-18T05:20:58.624095Z","iopub.status.idle":"2021-09-18T05:20:58.636187Z","shell.execute_reply.started":"2021-09-18T05:20:58.624067Z","shell.execute_reply":"2021-09-18T05:20:58.635344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.set_style(\"whitegrid\")\nsns.countplot(x=train['label'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:20:59.189068Z","iopub.execute_input":"2021-09-18T05:20:59.189420Z","iopub.status.idle":"2021-09-18T05:20:59.405171Z","shell.execute_reply.started":"2021-09-18T05:20:59.189390Z","shell.execute_reply":"2021-09-18T05:20:59.404395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From above plot we can say that there are comparable instances of each of classes (digits).\\\nSo class imbalance is not critical. We can proceed with the given data","metadata":{}},{"cell_type":"code","source":"x_train = (train.iloc[:,1:].values).astype(\"float32\")\ny_train = train.iloc[:,0].values.astype(\"int32\")\n\nx_val = (val.iloc[:,1:].values).astype(\"float32\")\ny_val = val.iloc[:,0].values.astype(\"int32\")\n\nx_test = (test.iloc[:,1:].values).astype(\"float32\")\ny_test = test.iloc[:,0].values.astype(\"int32\")\n\nx_train = x_train.reshape(x_train.shape[0],28,28,1).astype(np.float32) / 255\nx_val = x_val.reshape(x_val.shape[0],28,28,1).astype(np.float32) / 255\nx_test = x_test.reshape(x_test.shape[0],28,28,1).astype(np.float32) / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:21:03.935060Z","iopub.execute_input":"2021-09-18T05:21:03.935399Z","iopub.status.idle":"2021-09-18T05:21:04.088418Z","shell.execute_reply.started":"2021-09-18T05:21:03.935369Z","shell.execute_reply":"2021-09-18T05:21:04.087540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_pred = prediction_data.values.astype(\"float32\")\nx_pred = x_pred.reshape(x_pred.shape[0],28,28,1).astype(\"float32\") / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:13:13.766224Z","iopub.execute_input":"2021-09-18T06:13:13.766598Z","iopub.status.idle":"2021-09-18T06:13:13.848994Z","shell.execute_reply.started":"2021-09-18T06:13:13.766567Z","shell.execute_reply":"2021-09-18T06:13:13.848143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_samples = x_train.shape[0]\ntest_samples = x_test.shape[0]\nval_samples = x_val.shape[0]\npred_samples = x_pred.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:21:08.093745Z","iopub.execute_input":"2021-09-18T05:21:08.094060Z","iopub.status.idle":"2021-09-18T05:21:08.098170Z","shell.execute_reply.started":"2021-09-18T05:21:08.094031Z","shell.execute_reply":"2021-09-18T05:21:08.097156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## IMPORTING NECESSARY LIBRARIES FOR MODEL BUILDING\nimport tensorflow as tf\nfrom tensorflow.keras import backend as K\ntf.random.set_seed(30)\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, Dropout, BatchNormalization, Activation\nfrom tensorflow.keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:21:10.113850Z","iopub.execute_input":"2021-09-18T05:21:10.114181Z","iopub.status.idle":"2021-09-18T05:21:14.624364Z","shell.execute_reply.started":"2021-09-18T05:21:10.114152Z","shell.execute_reply":"2021-09-18T05:21:14.623523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (2,2),padding='same', activation='relu',input_shape=(28,28,1)),\n    MaxPooling2D(pool_size=(2,2), strides = 2),\n    Conv2D(64, (2,2),padding='same', activation='relu'),\n    MaxPooling2D(pool_size=(2,2), strides = 2),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])\n\n# COMPILE THE MODEL\nmodel.compile(optimizer='adam',\n             loss = tf.keras.losses.SparseCategoricalCrossentropy(),\n             metrics= ['accuracy'])\n\nbatch_size = 64\n\nhistory = model.fit(x = x_train,\n         y = y_train,\n         epochs= 10,\n         steps_per_epoch= math.ceil(train_samples/batch_size),\n         validation_data=(x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:23:25.129879Z","iopub.execute_input":"2021-09-18T05:23:25.130211Z","iopub.status.idle":"2021-09-18T05:23:51.735093Z","shell.execute_reply.started":"2021-09-18T05:23:25.130182Z","shell.execute_reply":"2021-09-18T05:23:51.734229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(10)\n\nplt.figure(figsize=(16, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:28:22.278164Z","iopub.execute_input":"2021-09-18T05:28:22.278564Z","iopub.status.idle":"2021-09-18T05:28:22.621132Z","shell.execute_reply.started":"2021-09-18T05:28:22.278531Z","shell.execute_reply":"2021-09-18T05:28:22.620110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss,test_accuracy = model.evaluate(x=x_test,y=y_test,steps=math.ceil(pred_samples/batch_size))\nprint(\"\\nTest accuracy: \", test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:34:10.812137Z","iopub.execute_input":"2021-09-18T05:34:10.812505Z","iopub.status.idle":"2021-09-18T05:34:11.761821Z","shell.execute_reply.started":"2021-09-18T05:34:10.812472Z","shell.execute_reply":"2021-09-18T05:34:11.760287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset_predictions = model.predict(x_test, verbose=0)\ntest_images = x_test\ntest_labels = y_test","metadata":{"execution":{"iopub.status.busy":"2021-09-18T05:51:27.036229Z","iopub.execute_input":"2021-09-18T05:51:27.036598Z","iopub.status.idle":"2021-09-18T05:51:27.575639Z","shell.execute_reply.started":"2021-09-18T05:51:27.036568Z","shell.execute_reply":"2021-09-18T05:51:27.574725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(i, predicted_labels, true_labels, images):\n    predicted_label_array,true_label,img = testset_predictions[i],test_labels[i],test_images[i]\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([])\n    \n    plt.imshow(img, cmap=plt.cm.binary)\n    \n    predicted_label = np.argmax(predicted_label_array)\n    if predicted_label == true_label:\n        color = 'blue'\n    else:\n        color = 'red'\n    \n    plt.xlabel(\"{} {:2.0f}% ({})\".format(predicted_label,\n                                   100*np.max(predicted_label_array),\n                                   true_label),\n                                   color=color)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:05:18.061018Z","iopub.execute_input":"2021-09-18T06:05:18.061357Z","iopub.status.idle":"2021-09-18T06:05:18.068005Z","shell.execute_reply.started":"2021-09-18T06:05:18.061323Z","shell.execute_reply":"2021-09-18T06:05:18.066806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\ntot_img = rows*cols\nplt.figure(figsize = (2*rows, 2*cols))\n\nfor i in range(tot_img):\n    plt.subplot(rows,cols,i+1)\n    plot_images(i, testset_predictions, test_images ,test_images)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:05:05.324120Z","iopub.execute_input":"2021-09-18T06:05:05.324494Z","iopub.status.idle":"2021-09-18T06:05:06.959114Z","shell.execute_reply.started":"2021-09-18T06:05:05.324463Z","shell.execute_reply":"2021-09-18T06:05:06.958136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_pred, verbose = 0)\npredictions_images = x_pred","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:15:08.386988Z","iopub.execute_input":"2021-09-18T06:15:08.387383Z","iopub.status.idle":"2021-09-18T06:15:09.501084Z","shell.execute_reply.started":"2021-09-18T06:15:08.387348Z","shell.execute_reply":"2021-09-18T06:15:09.500269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_img_pred(i, pred_set, pred_images):\n    pred_set, pred_images = predictions[i], predictions_images[i]\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([])\n    \n    plt.imshow(pred_images, cmap=plt.cm.binary)\n    \n    pred_label = np.argmax(pred_set)\n    plt.xlabel(\"{} {:2.0f}%\".format(pred_label,\n                                  100*np.max(pred_set)),\n                                  color='blue')","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:20:44.939347Z","iopub.execute_input":"2021-09-18T06:20:44.939680Z","iopub.status.idle":"2021-09-18T06:20:44.947190Z","shell.execute_reply.started":"2021-09-18T06:20:44.939649Z","shell.execute_reply":"2021-09-18T06:20:44.946344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\ntot_img = rows*cols\nplt.figure(figsize = (2*rows, 2*cols))\n\nfor i in range(tot_img):\n    plt.subplot(rows,cols,i+1)\n    plot_img_pred(i, predictions,predictions_images)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:20:45.709539Z","iopub.execute_input":"2021-09-18T06:20:45.709859Z","iopub.status.idle":"2021-09-18T06:20:46.893223Z","shell.execute_reply.started":"2021-09-18T06:20:45.709829Z","shell.execute_reply":"2021-09-18T06:20:46.892113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solution = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsolution['Label'] = pd.Series(np.argmax(predictions, axis=1))\nsolution.to_csv(\"final_submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-18T06:29:51.167888Z","iopub.execute_input":"2021-09-18T06:29:51.168234Z","iopub.status.idle":"2021-09-18T06:29:51.233717Z","shell.execute_reply.started":"2021-09-18T06:29:51.168206Z","shell.execute_reply":"2021-09-18T06:29:51.232745Z"},"trusted":true},"execution_count":null,"outputs":[]}]}