{"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":"markdown","source":"<center>\n  <h1 style=\"background-color:red; color:white; font-size:40px; font-weight:bold; font-family:Times New Roman; border:3px solid black;\">Digit Recognizer </h1>\n</center>","metadata":{}},{"cell_type":"markdown","source":"# <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #003380; border: 10px solid #80ffff\"> TABLE OF CONTENTS<br><div>  \n\n* [Import Libraries](#0)\n* [Read Data](#1)\n* [CNN Model](#2)\n* [Model Check](#3)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"0.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" > Import Libraries<br><div> \n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport keras \nfrom keras.callbacks import EarlyStopping,ModelCheckpoint\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:33:04.439155Z","iopub.execute_input":"2023-09-20T17:33:04.439562Z","iopub.status.idle":"2023-09-20T17:33:04.447363Z","shell.execute_reply.started":"2023-09-20T17:33:04.439527Z","shell.execute_reply":"2023-09-20T17:33:04.446006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"0.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" >Read Data<br><div> \n","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')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:53:47.591587Z","iopub.execute_input":"2023-09-20T16:53:47.592709Z","iopub.status.idle":"2023-09-20T16:53:52.234973Z","shell.execute_reply.started":"2023-09-20T16:53:47.592676Z","shell.execute_reply":"2023-09-20T16:53:52.233769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=(train.iloc[:,1:].values).reshape(-1,28,28,1)\nlabel=train.iloc[:,0]\ntest_data=(test.values).reshape(-1,28,28,1)\nprint(\"Train Data Shape Is :\",train.shape)\nprint('Test Data Shape Is :',test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:53:52.268624Z","iopub.execute_input":"2023-09-20T16:53:52.269030Z","iopub.status.idle":"2023-09-20T16:53:52.277455Z","shell.execute_reply.started":"2023-09-20T16:53:52.268992Z","shell.execute_reply":"2023-09-20T16:53:52.275694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,25))\ni=1\nfor x in np.random.randint(0,len(train_data),50):\n    plt.subplot(10,5,i)\n    plt.imshow(train_data[x],'gray')\n    plt.title(f'Number {label[x]}',fontsize=20)\n    plt.xticks([])\n    plt.yticks([])\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:00:17.415492Z","iopub.execute_input":"2023-09-20T17:00:17.415923Z","iopub.status.idle":"2023-09-20T17:00:20.052920Z","shell.execute_reply.started":"2023-09-20T17:00:17.415854Z","shell.execute_reply":"2023-09-20T17:00:20.051855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,8))\nsns.countplot(label)\nplt.title('Label Distrubution',fontsize=20)\nplt.xlabel('Label',fontsize=20)\nplt.ylabel('Count',fontsize=20)\nplt.grid(True)\nplt.xticks(rotation=45)\nplt.yticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:05:00.752002Z","iopub.execute_input":"2023-09-20T17:05:00.752968Z","iopub.status.idle":"2023-09-20T17:05:01.015983Z","shell.execute_reply.started":"2023-09-20T17:05:00.752915Z","shell.execute_reply":"2023-09-20T17:05:01.014842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"0.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" >CNN Model<br><div> ","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train_data,label, test_size=0.1, random_state=44, shuffle =True,stratify=label)\nprint('X_train shape is ' , X_train.shape)\nprint('X_test shape is ' , X_test.shape)\nprint('y_train shape is ' , y_train.shape)\nprint('y_test shape is ' , y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:06:20.609817Z","iopub.execute_input":"2023-09-20T17:06:20.610235Z","iopub.status.idle":"2023-09-20T17:06:21.047182Z","shell.execute_reply.started":"2023-09-20T17:06:20.610203Z","shell.execute_reply":"2023-09-20T17:06:21.045945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_num=len(pd.unique(label))\nmodel=keras.models.Sequential()\nmodel.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3),activation=tf.nn.relu,input_shape=(28,28,1)))\nmodel.add(keras.layers.BatchNormalization())  \nmodel.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3),activation=tf.nn.relu)) \nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(filters=32, kernel_size=(5,5),strides=2, padding='same',activation=tf.nn.relu))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Dropout(.4)) \nmodel.add(keras.layers.Conv2D(filters=64, kernel_size=(3,3),activation=tf.nn.relu,input_shape=(28,28,1)))\nmodel.add(keras.layers.BatchNormalization())  \nmodel.add(keras.layers.Conv2D(filters=64, kernel_size=(3,3),activation=tf.nn.relu)) \nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(filters=64, kernel_size=(5,5),strides=2, padding='same',activation=tf.nn.relu))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Dropout(.4)) \nmodel.add(keras.layers.Conv2D(filters=128, kernel_size=(3,3),activation=tf.nn.relu))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dropout(.4))\nmodel.add(keras.layers.Dense(class_num,activation=tf.nn.softmax))","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:00:25.392004Z","iopub.execute_input":"2023-09-20T18:00:25.392412Z","iopub.status.idle":"2023-09-20T18:00:25.571768Z","shell.execute_reply.started":"2023-09-20T18:00:25.392380Z","shell.execute_reply":"2023-09-20T18:00:25.570708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:00:32.036088Z","iopub.execute_input":"2023-09-20T18:00:32.036469Z","iopub.status.idle":"2023-09-20T18:00:32.045613Z","shell.execute_reply.started":"2023-09-20T18:00:32.036438Z","shell.execute_reply":"2023-09-20T18:00:32.044239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True,show_dtype=True,dpi=120)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:05:18.015631Z","iopub.execute_input":"2023-09-20T18:05:18.016088Z","iopub.status.idle":"2023-09-20T18:05:18.469345Z","shell.execute_reply.started":"2023-09-20T18:05:18.016051Z","shell.execute_reply":"2023-09-20T18:05:18.467947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range=10,\n        zoom_range = 0.1, \n        width_shift_range=0.1, \n        height_shift_range=0.1\n)\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:00:39.579280Z","iopub.execute_input":"2023-09-20T18:00:39.579669Z","iopub.status.idle":"2023-09-20T18:00:39.678630Z","shell.execute_reply.started":"2023-09-20T18:00:39.579633Z","shell.execute_reply":"2023-09-20T18:00:39.677526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_cb =ModelCheckpoint(\"my_keras_model.h5\", save_best_only=True)\nearly_stopping_cb =EarlyStopping(patience=3, restore_best_weights=True)\nmodel.compile(optimizer ='adam',loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nhist=model.fit_generator(datagen.flow(X_train,y_train, batch_size=32)\n                    ,epochs=10,validation_data=(X_test,y_test),callbacks=[checkpoint_cb,early_stopping_cb])","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:17:03.637437Z","iopub.execute_input":"2023-09-20T18:17:03.637837Z","iopub.status.idle":"2023-09-20T18:20:01.996493Z","shell.execute_reply.started":"2023-09-20T18:17:03.637804Z","shell.execute_reply":"2023-09-20T18:20:01.995407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score, acc = model.evaluate(X_test,y_test)\nprint('Test Loss =', score)\nprint('Test Accuracy =', acc)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:20:02.607051Z","iopub.execute_input":"2023-09-20T18:20:02.608111Z","iopub.status.idle":"2023-09-20T18:20:03.131130Z","shell.execute_reply.started":"2023-09-20T18:20:02.608072Z","shell.execute_reply":"2023-09-20T18:20:03.130141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_=pd.DataFrame(hist.history)\nhist_","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:20:06.276444Z","iopub.execute_input":"2023-09-20T18:20:06.276820Z","iopub.status.idle":"2023-09-20T18:20:06.292510Z","shell.execute_reply.started":"2023-09-20T18:20:06.276787Z","shell.execute_reply":"2023-09-20T18:20:06.291113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.subplot(1,2,1)\nplt.plot(hist_['loss'],label='Train_Loss')\nplt.plot(hist_['val_loss'],label='Validation_Loss')\nplt.title('Train_Loss & Validation_Loss',fontsize=20)\nplt.legend()\nplt.subplot(1,2,2)\nplt.plot(hist_['accuracy'],label='Train_Accuracy')\nplt.plot(hist_['val_accuracy'],label='Validation_Accuracy')\nplt.title('Train_Accuracy & Validation_Accuracy',fontsize=20)\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:20:11.981038Z","iopub.execute_input":"2023-09-20T18:20:11.981419Z","iopub.status.idle":"2023-09-20T18:20:12.417894Z","shell.execute_reply.started":"2023-09-20T18:20:11.981386Z","shell.execute_reply":"2023-09-20T18:20:12.416920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction=model.predict(test_data)\ny_pred=[]\nfor i in prediction: \n    y_pred.append(np.argmax(i))","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:22:49.700249Z","iopub.execute_input":"2023-09-20T18:22:49.700657Z","iopub.status.idle":"2023-09-20T18:22:52.781898Z","shell.execute_reply.started":"2023-09-20T18:22:49.700622Z","shell.execute_reply":"2023-09-20T18:22:52.780765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,25))\ni=1\nfor x in np.random.randint(0,len(test_data),50):\n    plt.subplot(10,5,i)\n    plt.imshow(test_data[x],'gray')\n    plt.title(f'Number {y_pred[x]}',fontsize=20)\n    plt.xticks([])\n    plt.yticks([])\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:20:35.339767Z","iopub.execute_input":"2023-09-20T18:20:35.340158Z","iopub.status.idle":"2023-09-20T18:20:37.654829Z","shell.execute_reply.started":"2023-09-20T18:20:35.340120Z","shell.execute_reply":"2023-09-20T18:20:37.653807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"0.1\"></a>\n## <div style= \"font-family: Cambria; font-weight:bold; letter-spacing: 0px; color:#ffffff; font-size:120%; text-align:left;padding:3.0px; background: #0059b3; border-bottom: 8px solid #e6e6e6\" >Model Check<br><div> ","metadata":{}},{"cell_type":"code","source":"prediction1=model.predict(X_test)\ny_pred1=[]\nfor i in prediction1: \n    y_pred1.append(np.argmax(i))\nax= plt.subplot()\nCM = confusion_matrix(y_test,y_pred1)\nsns.heatmap(CM, annot=True, fmt='g', ax=ax,cbar=False,cmap='RdBu')\nax.set_xlabel('Predicted labels')\nax.set_ylabel('True labels') \nax.set_title('Confusion Matrix')\nplt.show()\nCM","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:22:15.987012Z","iopub.execute_input":"2023-09-20T18:22:15.987394Z","iopub.status.idle":"2023-09-20T18:22:16.991235Z","shell.execute_reply.started":"2023-09-20T18:22:15.987361Z","shell.execute_reply":"2023-09-20T18:22:16.990168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ClassificationReport = classification_report(y_test,y_pred1)\nprint('Classification Report is : ', ClassificationReport )","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:22:29.701545Z","iopub.execute_input":"2023-09-20T18:22:29.702020Z","iopub.status.idle":"2023-09-20T18:22:29.726468Z","shell.execute_reply.started":"2023-09-20T18:22:29.701985Z","shell.execute_reply":"2023-09-20T18:22:29.725126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = list(range(1, len(y_pred) + 1))\ndf = pd.DataFrame({'Label': y_pred}, index=ImageId)\ndf.index.name = 'ImageId'\ndf.to_csv('/kaggle/working/submission_pred.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:23:05.034357Z","iopub.execute_input":"2023-09-20T18:23:05.034742Z","iopub.status.idle":"2023-09-20T18:23:05.134590Z","shell.execute_reply.started":"2023-09-20T18:23:05.034708Z","shell.execute_reply":"2023-09-20T18:23:05.133358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center>\n  <h1 style=\"background-color:red; color:white; font-size:40px; font-weight:bold; font-family:Times New Roman; border:3px solid black;\">Upvotes</h1>\n</center>","metadata":{}}]}