{"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-08-24T16:08:04.091539Z","iopub.execute_input":"2021-08-24T16:08:04.091914Z","iopub.status.idle":"2021-08-24T16:08:04.100507Z","shell.execute_reply.started":"2021-08-24T16:08:04.091882Z","shell.execute_reply":"2021-08-24T16:08:04.099223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import necessary libraries","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import models, datasets\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Activation,Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import History\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:21:18.018043Z","iopub.execute_input":"2021-08-24T16:21:18.018503Z","iopub.status.idle":"2021-08-24T16:21:18.025017Z","shell.execute_reply.started":"2021-08-24T16:21:18.018472Z","shell.execute_reply":"2021-08-24T16:21:18.023964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import the datasets","metadata":{}},{"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":"2021-08-24T16:08:04.945991Z","iopub.execute_input":"2021-08-24T16:08:04.946374Z","iopub.status.idle":"2021-08-24T16:08:10.835928Z","shell.execute_reply.started":"2021-08-24T16:08:04.946338Z","shell.execute_reply":"2021-08-24T16:08:10.835042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:10.837263Z","iopub.execute_input":"2021-08-24T16:08:10.837716Z","iopub.status.idle":"2021-08-24T16:08:10.873138Z","shell.execute_reply.started":"2021-08-24T16:08:10.837675Z","shell.execute_reply":"2021-08-24T16:08:10.872238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:10.875021Z","iopub.execute_input":"2021-08-24T16:08:10.875328Z","iopub.status.idle":"2021-08-24T16:08:10.881329Z","shell.execute_reply.started":"2021-08-24T16:08:10.875297Z","shell.execute_reply":"2021-08-24T16:08:10.8804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:10.883443Z","iopub.execute_input":"2021-08-24T16:08:10.883892Z","iopub.status.idle":"2021-08-24T16:08:10.908456Z","shell.execute_reply.started":"2021-08-24T16:08:10.883824Z","shell.execute_reply":"2021-08-24T16:08:10.907458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"seperate the features and labels","metadata":{}},{"cell_type":"code","source":"train_data=train.drop('label',axis=1)\ntrain_label=train['label']\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:10.910031Z","iopub.execute_input":"2021-08-24T16:08:10.910457Z","iopub.status.idle":"2021-08-24T16:08:11.027636Z","shell.execute_reply.started":"2021-08-24T16:08:10.910415Z","shell.execute_reply":"2021-08-24T16:08:11.02688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Splitting the datasets into training and test","metadata":{}},{"cell_type":"code","source":"X_train,X_test,Y_train,Y_test=train_test_split(train_data,train_label,test_size=0.2,random_state=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.028911Z","iopub.execute_input":"2021-08-24T16:08:11.029409Z","iopub.status.idle":"2021-08-24T16:08:11.308684Z","shell.execute_reply.started":"2021-08-24T16:08:11.029377Z","shell.execute_reply":"2021-08-24T16:08:11.30791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"reshape the datasets","metadata":{}},{"cell_type":"code","source":"X_train = np.reshape(np.array(X_train), (X_train.shape[0],28,28,1))\nX_test = np.reshape(np.array(X_test), (X_test.shape[0],28,28,1))\ntest_data=np.reshape(np.array(test), (test.shape[0],28,28,1))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.309998Z","iopub.execute_input":"2021-08-24T16:08:11.310495Z","iopub.status.idle":"2021-08-24T16:08:11.502375Z","shell.execute_reply.started":"2021-08-24T16:08:11.310461Z","shell.execute_reply":"2021-08-24T16:08:11.501459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.504268Z","iopub.execute_input":"2021-08-24T16:08:11.504697Z","iopub.status.idle":"2021-08-24T16:08:11.51013Z","shell.execute_reply.started":"2021-08-24T16:08:11.504656Z","shell.execute_reply":"2021-08-24T16:08:11.509208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label=Y_train.values\ntest_label=Y_test.values\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.511879Z","iopub.execute_input":"2021-08-24T16:08:11.512169Z","iopub.status.idle":"2021-08-24T16:08:11.5227Z","shell.execute_reply.started":"2021-08-24T16:08:11.512141Z","shell.execute_reply":"2021-08-24T16:08:11.521588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.524308Z","iopub.execute_input":"2021-08-24T16:08:11.524638Z","iopub.status.idle":"2021-08-24T16:08:11.539093Z","shell.execute_reply.started":"2021-08-24T16:08:11.52461Z","shell.execute_reply":"2021-08-24T16:08:11.537954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One hot encoding on the labels","metadata":{}},{"cell_type":"code","source":"Y_train=to_categorical(train_label)\nY_test=to_categorical(test_label)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.540642Z","iopub.execute_input":"2021-08-24T16:08:11.541074Z","iopub.status.idle":"2021-08-24T16:08:11.551379Z","shell.execute_reply.started":"2021-08-24T16:08:11.541041Z","shell.execute_reply":"2021-08-24T16:08:11.550464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plotting an image","metadata":{}},{"cell_type":"code","source":"plt.imshow(X_train[33])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.552542Z","iopub.execute_input":"2021-08-24T16:08:11.553034Z","iopub.status.idle":"2021-08-24T16:08:11.748781Z","shell.execute_reply.started":"2021-08-24T16:08:11.552995Z","shell.execute_reply":"2021-08-24T16:08:11.747865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Build a CNN model","metadata":{}},{"cell_type":"code","source":"model=models.Sequential()\nmodel.add(Conv2D(16,(5,5),strides=(1,1),padding='valid',input_shape=(28,28,1)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(16,(5,5),strides=(1,1),padding='valid'))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.750218Z","iopub.execute_input":"2021-08-24T16:08:11.750602Z","iopub.status.idle":"2021-08-24T16:08:11.893443Z","shell.execute_reply.started":"2021-08-24T16:08:11.750558Z","shell.execute_reply":"2021-08-24T16:08:11.892365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.894706Z","iopub.execute_input":"2021-08-24T16:08:11.895087Z","iopub.status.idle":"2021-08-24T16:08:11.911584Z","shell.execute_reply.started":"2021-08-24T16:08:11.895052Z","shell.execute_reply":"2021-08-24T16:08:11.91051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train the model","metadata":{}},{"cell_type":"code","source":"history=model.fit(X_train,Y_train,batch_size=10,epochs=10,validation_data=(X_test,Y_test))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:08:11.91287Z","iopub.execute_input":"2021-08-24T16:08:11.913188Z","iopub.status.idle":"2021-08-24T16:11:53.565596Z","shell.execute_reply.started":"2021-08-24T16:08:11.913155Z","shell.execute_reply":"2021-08-24T16:11:53.564784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualising the loss and accuracy","metadata":{}},{"cell_type":"code","source":"ev=model.evaluate(X_test,Y_test)\nprint(\"val_loss {} , val_acc {}\".format(ev[0],ev[1]))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:38:21.155913Z","iopub.execute_input":"2021-08-24T16:38:21.156277Z","iopub.status.idle":"2021-08-24T16:38:22.483599Z","shell.execute_reply.started":"2021-08-24T16:38:21.156249Z","shell.execute_reply":"2021-08-24T16:38:22.482644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss_acc = pd.DataFrame(history.history)\ndf_loss=df_loss_acc[['loss','val_loss']]\ndf_acc=df_loss_acc[['accuracy','val_accuracy']]","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:15:00.449326Z","iopub.execute_input":"2021-08-24T16:15:00.449729Z","iopub.status.idle":"2021-08-24T16:15:00.457577Z","shell.execute_reply.started":"2021-08-24T16:15:00.449696Z","shell.execute_reply":"2021-08-24T16:15:00.456678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss.plot()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:15:12.606922Z","iopub.execute_input":"2021-08-24T16:15:12.607433Z","iopub.status.idle":"2021-08-24T16:15:12.80909Z","shell.execute_reply.started":"2021-08-24T16:15:12.607401Z","shell.execute_reply":"2021-08-24T16:15:12.80808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_acc.plot()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:15:38.888742Z","iopub.execute_input":"2021-08-24T16:15:38.889155Z","iopub.status.idle":"2021-08-24T16:15:39.08061Z","shell.execute_reply.started":"2021-08-24T16:15:38.889119Z","shell.execute_reply":"2021-08-24T16:15:39.079765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict the values using one random index number\n","metadata":{}},{"cell_type":"code","source":"plt.imshow(test_data[50])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:44:24.840804Z","iopub.execute_input":"2021-08-24T16:44:24.841197Z","iopub.status.idle":"2021-08-24T16:44:25.00956Z","shell.execute_reply.started":"2021-08-24T16:44:24.841163Z","shell.execute_reply":"2021-08-24T16:44:25.008484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"y_pred=model.predict_classes(test_data)\nprint(\"predicted value of the image\",(y_pred[50]))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:49:43.021274Z","iopub.execute_input":"2021-08-24T16:49:43.021631Z","iopub.status.idle":"2021-08-24T16:49:46.28845Z","shell.execute_reply.started":"2021-08-24T16:49:43.021601Z","shell.execute_reply":"2021-08-24T16:49:46.287543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:49:49.191679Z","iopub.execute_input":"2021-08-24T16:49:49.192046Z","iopub.status.idle":"2021-08-24T16:49:49.19772Z","shell.execute_reply.started":"2021-08-24T16:49:49.192016Z","shell.execute_reply":"2021-08-24T16:49:49.196682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions=pd.DataFrame({\"ImageId\": list(range(1,len(y_pred)+1)),\n                         \"Label\": y_pred})\nsubmissions.to_csv(\"Submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T16:55:08.534049Z","iopub.execute_input":"2021-08-24T16:55:08.53504Z","iopub.status.idle":"2021-08-24T16:55:08.611328Z","shell.execute_reply.started":"2021-08-24T16:55:08.534973Z","shell.execute_reply":"2021-08-24T16:55:08.61046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}