{"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":"2022-07-09T11:23:33.108627Z","iopub.execute_input":"2022-07-09T11:23:33.109113Z","iopub.status.idle":"2022-07-09T11:23:33.119795Z","shell.execute_reply.started":"2022-07-09T11:23:33.109073Z","shell.execute_reply":"2022-07-09T11:23:33.118727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D, MaxPool2D,MaxPooling2D,Dropout,Flatten\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:33.177215Z","iopub.execute_input":"2022-07-09T11:23:33.177654Z","iopub.status.idle":"2022-07-09T11:23:33.184693Z","shell.execute_reply.started":"2022-07-09T11:23:33.177617Z","shell.execute_reply":"2022-07-09T11:23:33.183499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loading","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:19:48.614783Z","iopub.execute_input":"2022-07-03T19:19:48.615133Z","iopub.status.idle":"2022-07-03T19:19:48.627238Z","shell.execute_reply.started":"2022-07-03T19:19:48.615101Z","shell.execute_reply":"2022-07-03T19:19:48.625946Z"}}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:33.238440Z","iopub.execute_input":"2022-07-09T11:23:33.239210Z","iopub.status.idle":"2022-07-09T11:23:37.853662Z","shell.execute_reply.started":"2022-07-09T11:23:33.239158Z","shell.execute_reply":"2022-07-09T11:23:37.852659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring data","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:37.856000Z","iopub.execute_input":"2022-07-09T11:23:37.857038Z","iopub.status.idle":"2022-07-09T11:23:37.878374Z","shell.execute_reply.started":"2022-07-09T11:23:37.856975Z","shell.execute_reply":"2022-07-09T11:23:37.877139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:37.880043Z","iopub.execute_input":"2022-07-09T11:23:37.881263Z","iopub.status.idle":"2022-07-09T11:23:37.900247Z","shell.execute_reply.started":"2022-07-09T11:23:37.881216Z","shell.execute_reply":"2022-07-09T11:23:37.899090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"X_train = train.drop(columns=[\"label\"])\ny_train = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:37.903908Z","iopub.execute_input":"2022-07-09T11:23:37.904771Z","iopub.status.idle":"2022-07-09T11:23:38.038508Z","shell.execute_reply.started":"2022-07-09T11:23:37.904725Z","shell.execute_reply":"2022-07-09T11:23:38.037277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.039992Z","iopub.execute_input":"2022-07-09T11:23:38.040329Z","iopub.status.idle":"2022-07-09T11:23:38.051174Z","shell.execute_reply.started":"2022-07-09T11:23:38.040299Z","shell.execute_reply":"2022-07-09T11:23:38.050049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255\ntest = test / 255","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.052730Z","iopub.execute_input":"2022-07-09T11:23:38.053907Z","iopub.status.idle":"2022-07-09T11:23:38.194229Z","shell.execute_reply.started":"2022-07-09T11:23:38.053858Z","shell.execute_reply":"2022-07-09T11:23:38.193314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.195502Z","iopub.execute_input":"2022-07-09T11:23:38.195810Z","iopub.status.idle":"2022-07-09T11:23:38.200838Z","shell.execute_reply.started":"2022-07-09T11:23:38.195780Z","shell.execute_reply":"2022-07-09T11:23:38.199831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.202178Z","iopub.execute_input":"2022-07-09T11:23:38.202536Z","iopub.status.idle":"2022-07-09T11:23:38.214926Z","shell.execute_reply.started":"2022-07-09T11:23:38.202470Z","shell.execute_reply":"2022-07-09T11:23:38.213886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_hot = to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.216420Z","iopub.execute_input":"2022-07-09T11:23:38.217232Z","iopub.status.idle":"2022-07-09T11:23:38.225550Z","shell.execute_reply.started":"2022-07-09T11:23:38.217076Z","shell.execute_reply":"2022-07-09T11:23:38.224542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**A Sequential model** is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor. <br>","metadata":{}},{"cell_type":"markdown","source":"# Model training","metadata":{}},{"cell_type":"code","source":"model = Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.229514Z","iopub.execute_input":"2022-07-09T11:23:38.230192Z","iopub.status.idle":"2022-07-09T11:23:38.239443Z","shell.execute_reply.started":"2022-07-09T11:23:38.230158Z","shell.execute_reply":"2022-07-09T11:23:38.238546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Keras Conv2D** is a 2D Convolution Layer, this layer creates a convolution kernel that is wind with layers input which helps produce a tensor of outputs. <br>\nMandatory **Conv2D parameter** is the numbers of filters that convolutional layers will learn from. <br>\nIt is an integer value and also determines the number of output filters in the convolution. <br>\nBelow we are learning from a total of **32 filters** and then we use **Max Pooling to reduce the spatial dimensions** of the output volume. <br>\n**Kernel_size** is parameter determines the dimensions of the kernel. It is an integer or tuple/list of 2 integers, **specifying the height and width of the 2D convolution window**. Below we are using dimension 5x5 reprezented as tuple (5,5). <br>\n**Activation parameter** specifies the name of the activation function. <br>\n**MaxPooling2D -> pool_size** = it will take max value over 2x2 pooling window. It is a pooling operation that selects the maximum element from the region of the feature map covered by the filter (2, 2).\n","metadata":{}},{"cell_type":"code","source":"model.add(Conv2D(32,kernel_size =(5,5),activation='relu',input_shape = (28,28,1)))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Conv2D(32,kernel_size =(5,5),activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.241506Z","iopub.execute_input":"2022-07-09T11:23:38.242258Z","iopub.status.idle":"2022-07-09T11:23:38.324794Z","shell.execute_reply.started":"2022-07-09T11:23:38.242211Z","shell.execute_reply":"2022-07-09T11:23:38.323874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Flatten function** flattens the multi-dimensional input tensors into a single dimension, so you can model your input layer and build your neural network model, then pass those data into every single neuron of the model effectively.","metadata":{}},{"cell_type":"code","source":"model.add(Flatten())","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.326283Z","iopub.execute_input":"2022-07-09T11:23:38.327357Z","iopub.status.idle":"2022-07-09T11:23:38.336803Z","shell.execute_reply.started":"2022-07-09T11:23:38.327311Z","shell.execute_reply":"2022-07-09T11:23:38.335795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Adding layers** - **2 hidden layers** with 64 neurons. <br> Last one is **the output layer** having 10 neurons for 10 classes/numbers of output that is using the softmax function. <br>\n**Droupout function** is reducing overfitting by preventing complex co-adaptations on training data.","metadata":{}},{"cell_type":"code","source":"model.add(Dense(64,activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(64,activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.338006Z","iopub.execute_input":"2022-07-09T11:23:38.338563Z","iopub.status.idle":"2022-07-09T11:23:38.378696Z","shell.execute_reply.started":"2022-07-09T11:23:38.338518Z","shell.execute_reply":"2022-07-09T11:23:38.377464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compile function** serves for configuration of model for training. It has nothing to do with the weights and you can compile a model as many times as you want without causing any problem to pretrained weights. <br> \n**Optimizer** - update the model in response to the output of the loss function. Optimizers assist in minimizing the loss function.\n**Loss function** - since we’re trying to predict classes, we use **categorical crossentropy**. <br>\n**Metric** functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model.","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'categorical_crossentropy',metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.380158Z","iopub.execute_input":"2022-07-09T11:23:38.380526Z","iopub.status.idle":"2022-07-09T11:23:38.395209Z","shell.execute_reply.started":"2022-07-09T11:23:38.380467Z","shell.execute_reply":"2022-07-09T11:23:38.394130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Number of **Epochs** means how many times you go through your training set.","metadata":{}},{"cell_type":"code","source":"hist = model.fit(X_train,y_train_hot,epochs = 10,validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:23:38.396437Z","iopub.execute_input":"2022-07-09T11:23:38.397102Z","iopub.status.idle":"2022-07-09T11:25:34.333117Z","shell.execute_reply.started":"2022-07-09T11:23:38.397064Z","shell.execute_reply":"2022-07-09T11:25:34.331998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hist.history['accuracy'])\nplt.plot(hist.history['val_accuracy'])\nplt.title(\"Model Accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel('Epochs')\nplt.legend(['Train','Val'],loc = 'upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:25:34.334582Z","iopub.execute_input":"2022-07-09T11:25:34.334899Z","iopub.status.idle":"2022-07-09T11:25:34.521311Z","shell.execute_reply.started":"2022-07-09T11:25:34.334870Z","shell.execute_reply":"2022-07-09T11:25:34.520127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction on test set","metadata":{}},{"cell_type":"code","source":"ans = tf.keras.backend.argmax(model.predict(test))\nans = np.array(ans)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:25:34.523124Z","iopub.execute_input":"2022-07-09T11:25:34.523434Z","iopub.status.idle":"2022-07-09T11:25:37.322419Z","shell.execute_reply.started":"2022-07-09T11:25:34.523406Z","shell.execute_reply":"2022-07-09T11:25:37.321559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"df = pd.DataFrame(ans)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('submission.csv', header=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:25:37.323823Z","iopub.execute_input":"2022-07-09T11:25:37.324759Z","iopub.status.idle":"2022-07-09T11:25:37.389797Z","shell.execute_reply.started":"2022-07-09T11:25:37.324727Z","shell.execute_reply":"2022-07-09T11:25:37.388838Z"},"trusted":true},"execution_count":null,"outputs":[]}]}