{"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# visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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-07-06T04:40:00.913018Z","iopub.execute_input":"2021-07-06T04:40:00.913429Z","iopub.status.idle":"2021-07-06T04:40:01.858282Z","shell.execute_reply.started":"2021-07-06T04:40:00.913348Z","shell.execute_reply":"2021-07-06T04:40:01.857296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's get the train data\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:01.025934Z","iopub.execute_input":"2021-07-06T05:06:01.026329Z","iopub.status.idle":"2021-07-06T05:06:03.827412Z","shell.execute_reply.started":"2021-07-06T05:06:01.026296Z","shell.execute_reply":"2021-07-06T05:06:03.826544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Each row in train dataset is a image and label column tells which number is in the image\n# so, if label is 1, it means that image has digit 1 in it\n# we will take this label and assign it to y_train(output variable) and rest will be X_train(input varibale)\nX_train = train.drop('label',axis =1)\ny_train = train['label']","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:03.985548Z","iopub.execute_input":"2021-07-06T05:06:03.98627Z","iopub.status.idle":"2021-07-06T05:06:04.098563Z","shell.execute_reply.started":"2021-07-06T05:06:03.986213Z","shell.execute_reply":"2021-07-06T05:06:04.097396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's get the test dataset\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:05.588199Z","iopub.execute_input":"2021-07-06T05:06:05.5886Z","iopub.status.idle":"2021-07-06T05:06:07.319124Z","shell.execute_reply.started":"2021-07-06T05:06:05.58856Z","shell.execute_reply":"2021-07-06T05:06:07.318334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now we have X_train and y_train which we can use to train our model and test dataset to predict.**\n\n**Important thing to note here is all these pixel combines to form a image. One row consist of one image**\n\n**We can convert each row into numpy array and then use matplotlib to view these images**","metadata":{}},{"cell_type":"code","source":"# Let's try to view one image. 2nd row in X_train\nimg = X_train.iloc[2]\n\n# Let's convert it into numpy array of shape (28,28)\nimg = img.to_numpy().reshape((28,28))\n\n# Let's view this image\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:08.829211Z","iopub.execute_input":"2021-07-06T05:06:08.829733Z","iopub.status.idle":"2021-07-06T05:06:09.002031Z","shell.execute_reply.started":"2021-07-06T05:06:08.829699Z","shell.execute_reply":"2021-07-06T05:06:08.999532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# So the image has digit 1\n# we can compare it with y_train\nprint(y_train[2])","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:10.276315Z","iopub.execute_input":"2021-07-06T05:06:10.276703Z","iopub.status.idle":"2021-07-06T05:06:10.282136Z","shell.execute_reply.started":"2021-07-06T05:06:10.276664Z","shell.execute_reply":"2021-07-06T05:06:10.280755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now let's convert each image(row) in X_train and test dataset into numpy array of (28,28) shape\nX_train = X_train.to_numpy()\nX_test = test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:13.427688Z","iopub.execute_input":"2021-07-06T05:06:13.428039Z","iopub.status.idle":"2021-07-06T05:06:13.432891Z","shell.execute_reply.started":"2021-07-06T05:06:13.428009Z","shell.execute_reply":"2021-07-06T05:06:13.432059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's reshape the X_train and X_test in to right format\nX_train = X_train.reshape(-1,28,28,1) # -1 will be replaced by lenth of X_train\n\n# (28,28) is the size of each image\n# 1 means image is grascale image(0-255)\n\nX_test = X_test.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:14.936564Z","iopub.execute_input":"2021-07-06T05:06:14.93715Z","iopub.status.idle":"2021-07-06T05:06:14.942164Z","shell.execute_reply.started":"2021-07-06T05:06:14.937069Z","shell.execute_reply":"2021-07-06T05:06:14.941108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's check the shape of X_train and X_test\nprint(f'Shape of X_train is {X_train.shape}')\nprint('\\n')\nprint(f'Shape of X_test is {X_test.shape}')","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:06:17.275868Z","iopub.execute_input":"2021-07-06T05:06:17.276534Z","iopub.status.idle":"2021-07-06T05:06:17.283415Z","shell.execute_reply.started":"2021-07-06T05:06:17.276482Z","shell.execute_reply":"2021-07-06T05:06:17.282317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's view some images and there labels\nplt.figure(figsize=(10,5))\nfor i in range(8):\n    plt.subplot(2,4,i+1)\n    plt.imshow(X_train[i])\n    plt.axis('off')\n    plt.title(y_train[i])\nplt.tight_layout()\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:13:28.748003Z","iopub.execute_input":"2021-07-06T06:13:28.748373Z","iopub.status.idle":"2021-07-06T06:13:29.216231Z","shell.execute_reply.started":"2021-07-06T06:13:28.748341Z","shell.execute_reply":"2021-07-06T06:13:29.215374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's resize each image\nX_train = X_train/255\nX_test  = X_test/255","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:13:40.574029Z","iopub.execute_input":"2021-07-06T06:13:40.574436Z","iopub.status.idle":"2021-07-06T06:13:40.829779Z","shell.execute_reply.started":"2021-07-06T06:13:40.5744Z","shell.execute_reply":"2021-07-06T06:13:40.828576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**X_train and X_test are now in right shape and size to be used in model**","metadata":{}},{"cell_type":"code","source":"# Let's check y_train\n\n# First let's convert the y_train into numpy array\ny_train = y_train.to_numpy()\n\ny_train","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:13:49.688734Z","iopub.execute_input":"2021-07-06T06:13:49.689121Z","iopub.status.idle":"2021-07-06T06:13:49.695307Z","shell.execute_reply.started":"2021-07-06T06:13:49.689071Z","shell.execute_reply":"2021-07-06T06:13:49.694358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's convert y_train into categories.1 will [0,1,0,0,0,0,0,0,0,0] and so on\n# if y_train value is 7, only value at index 7 will be 1 and rest will be 0.\nfrom tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:13:50.940133Z","iopub.execute_input":"2021-07-06T06:13:50.940517Z","iopub.status.idle":"2021-07-06T06:13:50.950736Z","shell.execute_reply.started":"2021-07-06T06:13:50.940482Z","shell.execute_reply":"2021-07-06T06:13:50.949612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Model","metadata":{}},{"cell_type":"code","source":"# It's time to create our CNN model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPool2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\n# from tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:13:53.171126Z","iopub.execute_input":"2021-07-06T06:13:53.171482Z","iopub.status.idle":"2021-07-06T06:13:53.177604Z","shell.execute_reply.started":"2021-07-06T06:13:53.171452Z","shell.execute_reply":"2021-07-06T06:13:53.175964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model:\n\nmodel = Sequential()\n\n# 1st convolution layer\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=(28,28,1)))\nmodel.add(MaxPool2D(pool_size=2))\n\n# 2nd convolution layer\nmodel.add(Conv2D(filters=64,kernel_size=(3,3)))\nmodel.add(MaxPool2D(pool_size=2))\n\n# Flatten so that it can be read by ANN\nmodel.add(Flatten())\n\n# 1st ANN layer\nmodel.add(Dense(512,activation='relu'))\n\n#2nd ANN layer\n# model.add(Dense(256,activation='relu'))\n\n# 3rd ANN layer - Output layer\nmodel.add(Dense(10,activation = 'softmax'))\n\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:27:27.109671Z","iopub.execute_input":"2021-07-06T06:27:27.110272Z","iopub.status.idle":"2021-07-06T06:27:27.181879Z","shell.execute_reply.started":"2021-07-06T06:27:27.110237Z","shell.execute_reply":"2021-07-06T06:27:27.180497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Before training our model. Let's spilt our train data into train and validation data\n# # This is done so that we can test our model using validation data during training\n# batch_size = 150 # we will use 150 images for validation image\n\n# X_valid,y_valid = X_train[:batch_size],y_train[:batch_size]\n# X_train1,y_train1 = X_train[batch_size:],y_train[batch_size:]","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:30:02.483023Z","iopub.execute_input":"2021-07-06T06:30:02.483421Z","iopub.status.idle":"2021-07-06T06:30:02.488864Z","shell.execute_reply.started":"2021-07-06T06:30:02.483388Z","shell.execute_reply":"2021-07-06T06:30:02.487595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# early_stop = EarlyStopping(monitor='val_loss',patience=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:30:25.112012Z","iopub.execute_input":"2021-07-06T06:30:25.112427Z","iopub.status.idle":"2021-07-06T06:30:25.116883Z","shell.execute_reply.started":"2021-07-06T06:30:25.112392Z","shell.execute_reply":"2021-07-06T06:30:25.115906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's train our model\nmodel.fit(X_train,y_train,epochs=100,batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:31:31.408019Z","iopub.execute_input":"2021-07-06T06:31:31.408415Z","iopub.status.idle":"2021-07-06T06:33:07.914159Z","shell.execute_reply.started":"2021-07-06T06:31:31.408377Z","shell.execute_reply":"2021-07-06T06:33:07.913214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# metrics = pd.DataFrame(model.history.history)\n\n# metrics.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:34:06.504368Z","iopub.execute_input":"2021-07-06T06:34:06.504728Z","iopub.status.idle":"2021-07-06T06:34:06.520648Z","shell.execute_reply.started":"2021-07-06T06:34:06.504698Z","shell.execute_reply":"2021-07-06T06:34:06.519322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's comapre the accuracy\n# metrics[['accuracy','val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:34:30.550143Z","iopub.execute_input":"2021-07-06T06:34:30.550513Z","iopub.status.idle":"2021-07-06T06:34:30.782628Z","shell.execute_reply.started":"2021-07-06T06:34:30.550481Z","shell.execute_reply":"2021-07-06T06:34:30.781251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's compare the loss\n# metrics[['loss','val_loss']].plot()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:35:00.025557Z","iopub.execute_input":"2021-07-06T06:35:00.025909Z","iopub.status.idle":"2021-07-06T06:35:00.224618Z","shell.execute_reply.started":"2021-07-06T06:35:00.02588Z","shell.execute_reply":"2021-07-06T06:35:00.223416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's do the prediction of X_test\nprediction = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:35:36.423319Z","iopub.execute_input":"2021-07-06T06:35:36.423674Z","iopub.status.idle":"2021-07-06T06:35:42.489474Z","shell.execute_reply.started":"2021-07-06T06:35:36.423643Z","shell.execute_reply":"2021-07-06T06:35:42.488208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's view the predictions\nprediction","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:35:59.55619Z","iopub.execute_input":"2021-07-06T06:35:59.556619Z","iopub.status.idle":"2021-07-06T06:35:59.563651Z","shell.execute_reply.started":"2021-07-06T06:35:59.556582Z","shell.execute_reply":"2021-07-06T06:35:59.562629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's convert them into proper label.\n# We can use the argmax to get the index of maximum value in each prediction\nfinal_prediction = prediction.argmax(axis=1)\n\nfinal_prediction","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:37:02.153332Z","iopub.execute_input":"2021-07-06T06:37:02.153727Z","iopub.status.idle":"2021-07-06T06:37:02.160825Z","shell.execute_reply.started":"2021-07-06T06:37:02.153691Z","shell.execute_reply":"2021-07-06T06:37:02.159672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now lets save it\n# we can take ImageId from index in test data set and Label from final_prediction\nsubmission = pd.concat([pd.Series(np.arange(1,28001)),pd.Series(final_prediction)],axis =1)\nsubmission.columns=['ImageId','Label']\n\nsubmission.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:07:05.652481Z","iopub.execute_input":"2021-07-06T07:07:05.652841Z","iopub.status.idle":"2021-07-06T07:07:05.666496Z","shell.execute_reply.started":"2021-07-06T07:07:05.652809Z","shell.execute_reply":"2021-07-06T07:07:05.665064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's save it\nsubmission.to_csv('my_submission_1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-06T06:54:25.006017Z","iopub.execute_input":"2021-07-06T06:54:25.006616Z","iopub.status.idle":"2021-07-06T06:54:25.615207Z","shell.execute_reply.started":"2021-07-06T06:54:25.006581Z","shell.execute_reply":"2021-07-06T06:54:25.614017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}