{"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":"# HM Attaullah","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-25T08:01:17.356442Z","iopub.execute_input":"2021-05-25T08:01:17.356859Z","iopub.status.idle":"2021-05-25T08:01:17.361651Z","shell.execute_reply.started":"2021-05-25T08:01:17.356822Z","shell.execute_reply":"2021-05-25T08:01:17.360347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**importing libraries**\n\n\n\n1.   data load\n2.   data preparation\n*   Normalization\n*   reshape\n*   label encoding\n*   spliting training and validation \n3.   introduction to convents\n4.   saving submission file\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport seaborn as sns\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:17.363516Z","iopub.execute_input":"2021-05-25T08:01:17.363821Z","iopub.status.idle":"2021-05-25T08:01:17.375669Z","shell.execute_reply.started":"2021-05-25T08:01:17.363792Z","shell.execute_reply":"2021-05-25T08:01:17.374355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Load","metadata":{}},{"cell_type":"code","source":"# loading data \ntrain_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntest_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:17.377855Z","iopub.execute_input":"2021-05-25T08:01:17.378215Z","iopub.status.idle":"2021-05-25T08:01:18.113050Z","shell.execute_reply.started":"2021-05-25T08:01:17.378185Z","shell.execute_reply":"2021-05-25T08:01:18.110507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display first five rows of train_data\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.114143Z","iopub.status.idle":"2021-05-25T08:01:18.114582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking shape of train_data\ntrain_data.shape # ","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.115629Z","iopub.status.idle":"2021-05-25T08:01:18.116087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking shape of test_data\n\ntest_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.116975Z","iopub.status.idle":"2021-05-25T08:01:18.117432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the data\ntrain_data.describe()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.118363Z","iopub.status.idle":"2021-05-25T08:01:18.118785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check missing and null values\ntest_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.119673Z","iopub.status.idle":"2021-05-25T08:01:18.120135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now train","metadata":{}},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.121188Z","iopub.status.idle":"2021-05-25T08:01:18.121622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train_data['label']\n\n# Drop 'label' column\nX_train = train_data.drop(labels = [\"label\"],axis = 1) \n\n# free some space\ndel train_data \n\ng = sns.countplot(Y_train)\n\nY_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.122552Z","iopub.status.idle":"2021-05-25T08:01:18.122979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data\nX_train= X_train / 255.0\ntest_data= test_data / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.123943Z","iopub.status.idle":"2021-05-25T08:01:18.124425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape image in 3 dimensions (height = 28px, width = 28px , channel = 1)\nX_train = X_train.values.reshape((-1,28,28,1))\ntest_data = test_data.values.reshape((-1,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.125351Z","iopub.status.idle":"2021-05-25T08:01:18.125765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.126624Z","iopub.status.idle":"2021-05-25T08:01:18.127062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])\nY_train = to_categorical(Y_train, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.127910Z","iopub.status.idle":"2021-05-25T08:01:18.128373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the random seed\nrandom_seed = 2","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.129457Z","iopub.status.idle":"2021-05-25T08:01:18.129878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.130755Z","iopub.status.idle":"2021-05-25T08:01:18.131199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Some examples\nimport matplotlib.pyplot as plt\n\nh = plt.imshow(X_train[0][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.132126Z","iopub.status.idle":"2021-05-25T08:01:18.132557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras import models\n\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.133363Z","iopub.status.idle":"2021-05-25T08:01:18.133777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.134670Z","iopub.status.idle":"2021-05-25T08:01:18.135109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.135931Z","iopub.status.idle":"2021-05-25T08:01:18.136403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:01:18.137312Z","iopub.status.idle":"2021-05-25T08:01:18.137729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:03:14.584586Z","iopub.execute_input":"2021-05-25T08:03:14.585040Z","iopub.status.idle":"2021-05-25T08:03:14.620720Z","shell.execute_reply.started":"2021-05-25T08:03:14.584982Z","shell.execute_reply":"2021-05-25T08:03:14.618987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}