{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom subprocess import check_output\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-12T15:34:59.704238Z","iopub.execute_input":"2021-06-12T15:34:59.704581Z","iopub.status.idle":"2021-06-12T15:34:59.714170Z","shell.execute_reply.started":"2021-06-12T15:34:59.704552Z","shell.execute_reply":"2021-06-12T15:34:59.713267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading The Data","metadata":{}},{"cell_type":"code","source":"# Read training and test data files\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:34:59.716109Z","iopub.execute_input":"2021-06-12T15:34:59.716910Z","iopub.status.idle":"2021-06-12T15:35:03.433661Z","shell.execute_reply.started":"2021-06-12T15:34:59.716844Z","shell.execute_reply":"2021-06-12T15:35:03.432871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.435669Z","iopub.execute_input":"2021-06-12T15:35:03.436009Z","iopub.status.idle":"2021-06-12T15:35:03.456532Z","shell.execute_reply.started":"2021-06-12T15:35:03.435973Z","shell.execute_reply":"2021-06-12T15:35:03.455772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.457944Z","iopub.execute_input":"2021-06-12T15:35:03.458413Z","iopub.status.idle":"2021-06-12T15:35:03.476601Z","shell.execute_reply.started":"2021-06-12T15:35:03.458366Z","shell.execute_reply":"2021-06-12T15:35:03.475775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape) #Printing the shape of the dataset \nprint(test.shape) #printing the shape of test dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.477712Z","iopub.execute_input":"2021-06-12T15:35:03.478066Z","iopub.status.idle":"2021-06-12T15:35:03.483543Z","shell.execute_reply.started":"2021-06-12T15:35:03.478032Z","shell.execute_reply":"2021-06-12T15:35:03.482455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Printing the dimension of train_x and train_y\ntrain_y = train['label'].astype('int32')\ntrain_x = train.drop(['label'],axis = 1).astype('int32')\ntest_x = test.astype('int32')\n\n#Printing the shape of train_x and train_y\ntrain_x.shape, train_y.shape, test_x.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.484988Z","iopub.execute_input":"2021-06-12T15:35:03.485417Z","iopub.status.idle":"2021-06-12T15:35:03.663098Z","shell.execute_reply.started":"2021-06-12T15:35:03.485372Z","shell.execute_reply":"2021-06-12T15:35:03.662235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshaping the image \ntrain_x = train_x.values.reshape(-1,28,28,1)\n#normalisation\ntrain_x = train_x/255.0\ntest_x = test_x.values.reshape(-1,28,28,1)\ntest_x=test_x/255.0\n\n#checking the updated shape \ntrain_x.shape, test_x.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.664462Z","iopub.execute_input":"2021-06-12T15:35:03.664811Z","iopub.status.idle":"2021-06-12T15:35:03.818956Z","shell.execute_reply.started":"2021-06-12T15:35:03.664772Z","shell.execute_reply":"2021-06-12T15:35:03.817962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_x[900].reshape(28,28),cmap = matplotlib.cm.binary)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.821668Z","iopub.execute_input":"2021-06-12T15:35:03.822087Z","iopub.status.idle":"2021-06-12T15:35:03.946659Z","shell.execute_reply.started":"2021-06-12T15:35:03.822049Z","shell.execute_reply":"2021-06-12T15:35:03.945699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8, 8))\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    plt.imshow(train_x[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:03.948454Z","iopub.execute_input":"2021-06-12T15:35:03.948784Z","iopub.status.idle":"2021-06-12T15:35:04.764710Z","shell.execute_reply.started":"2021-06-12T15:35:03.948747Z","shell.execute_reply":"2021-06-12T15:35:04.763870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert label into categorical \ntrain_y = tf.keras.utils.to_categorical(train_y,10)\ntrain_y.shape  #Printing the shape ","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:04.766132Z","iopub.execute_input":"2021-06-12T15:35:04.766736Z","iopub.status.idle":"2021-06-12T15:35:04.774043Z","shell.execute_reply.started":"2021-06-12T15:35:04.766694Z","shell.execute_reply":"2021-06-12T15:35:04.772936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Printing the Labels for top 5 rows\nprint(train['label'].head())  #checking Data type ","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:04.775718Z","iopub.execute_input":"2021-06-12T15:35:04.776389Z","iopub.status.idle":"2021-06-12T15:35:04.788575Z","shell.execute_reply.started":"2021-06-12T15:35:04.776343Z","shell.execute_reply":"2021-06-12T15:35:04.787735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Printing Categorical label\nprint(train_y[0:5,:])","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:04.790269Z","iopub.execute_input":"2021-06-12T15:35:04.791072Z","iopub.status.idle":"2021-06-12T15:35:04.799991Z","shell.execute_reply.started":"2021-06-12T15:35:04.791032Z","shell.execute_reply":"2021-06-12T15:35:04.799176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Model","metadata":{}},{"cell_type":"code","source":"#Defining the Model\nmodel = tf.keras.models.Sequential([\n      tf.keras.layers.Conv2D(32,(3,3),activation = 'relu', input_shape=(28,28,1)),\n      tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),\n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Conv2D(64,(3,3),activation = 'relu', padding = 'Same'),\n      tf.keras.layers.Conv2D(64,(3,3),activation = 'relu', padding = 'Same'),\n      tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n      tf.keras.layers.Dropout(0.25),\n      tf.keras.layers.Conv2D(64,(3,3),activation = 'relu', padding = 'Same'),\n      tf.keras.layers.Conv2D(64,(3,3),activation = 'relu', padding = 'Same'),\n      tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),\n      tf.keras.layers.Dropout(0.25),\n      tf.keras.layers.Flatten(),\n      tf.keras.layers.Dense(256, activation='relu'),\n      tf.keras.layers.Dense(256,activation='relu'),\n      tf.keras.layers.Dropout(0.50),\n      tf.keras.layers.Dense(10, activation='softmax')\n                         \n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:04.801461Z","iopub.execute_input":"2021-06-12T15:35:04.802910Z","iopub.status.idle":"2021-06-12T15:35:04.984017Z","shell.execute_reply.started":"2021-06-12T15:35:04.802867Z","shell.execute_reply":"2021-06-12T15:35:04.983215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the callback function \n\nclass myCallback(tf.keras.callbacks.Callback):\n  def on_epoch_end(self, epoch, logs={}):\n    if(logs.get('accuracy') > 0.999):\n      print(\"\\nReached 99.9% accuracy!\")\n      self.model.stop_training = True\n\ncallbacks = myCallback() ","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:04.988569Z","iopub.execute_input":"2021-06-12T15:35:04.991525Z","iopub.status.idle":"2021-06-12T15:35:05.000100Z","shell.execute_reply.started":"2021-06-12T15:35:04.991480Z","shell.execute_reply":"2021-06-12T15:35:04.998759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling and model training\n#BatchSize = 50, epochs = 20 and optimizer = adam\nOptimizer = tf.keras.optimizers.Adam(\n    learning_rate = 0.0005,\n    beta_1=0.9,\n    beta_2=0.999,\n    epsilon=1e-07,\n    name='Adam'\n)\nmodel.compile(optimizer=Optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(train_x, train_y, batch_size =50, epochs=20, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:35:05.005430Z","iopub.execute_input":"2021-06-12T15:35:05.007036Z","iopub.status.idle":"2021-06-12T15:38:00.219970Z","shell.execute_reply.started":"2021-06-12T15:35:05.006997Z","shell.execute_reply":"2021-06-12T15:38:00.219244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test_x)\n\n#index with the maximum probability\nresults = np.argmax(results,axis=1)\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:38:00.223273Z","iopub.execute_input":"2021-06-12T15:38:00.223575Z","iopub.status.idle":"2021-06-12T15:38:04.694985Z","shell.execute_reply.started":"2021-06-12T15:38:00.223547Z","shell.execute_reply":"2021-06-12T15:38:04.694053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:38:04.696511Z","iopub.execute_input":"2021-06-12T15:38:04.696873Z","iopub.status.idle":"2021-06-12T15:38:04.704058Z","shell.execute_reply.started":"2021-06-12T15:38:04.696819Z","shell.execute_reply":"2021-06-12T15:38:04.703049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001), name=\"ImageId\"),results],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:45:26.429034Z","iopub.execute_input":"2021-06-12T15:45:26.429350Z","iopub.status.idle":"2021-06-12T15:45:26.435761Z","shell.execute_reply.started":"2021-06-12T15:45:26.429320Z","shell.execute_reply":"2021-06-12T15:45:26.434863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:45:27.752355Z","iopub.execute_input":"2021-06-12T15:45:27.752683Z","iopub.status.idle":"2021-06-12T15:45:27.765492Z","shell.execute_reply.started":"2021-06-12T15:45:27.752651Z","shell.execute_reply":"2021-06-12T15:45:27.764392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T15:38:40.834281Z","iopub.execute_input":"2021-06-12T15:38:40.834603Z","iopub.status.idle":"2021-06-12T15:38:40.911693Z","shell.execute_reply.started":"2021-06-12T15:38:40.834574Z","shell.execute_reply":"2021-06-12T15:38:40.910887Z"},"trusted":true},"execution_count":null,"outputs":[]}]}