{"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":"<h1 align=\"center\"><font color='lavander'>DIGIT RECOGNISER</font></h1>","metadata":{}},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>Import necessary libraries 📚</font></h2>","metadata":{}},{"cell_type":"code","source":"from IPython.display import Image\nimport os\nImage(\"/kaggle/input/imagedigit/image_1-ce90274e2478c82a09b07ba664d59d3a.png\")","metadata":{"execution":{"iopub.status.busy":"2023-07-31T10:16:18.996777Z","iopub.execute_input":"2023-07-31T10:16:18.997273Z","iopub.status.idle":"2023-07-31T10:16:19.025848Z","shell.execute_reply.started":"2023-07-31T10:16:18.997240Z","shell.execute_reply":"2023-07-31T10:16:19.024548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:29:58.175491Z","iopub.execute_input":"2023-07-08T15:29:58.175825Z","iopub.status.idle":"2023-07-08T15:30:07.789716Z","shell.execute_reply.started":"2023-07-08T15:29:58.175798Z","shell.execute_reply":"2023-07-08T15:30:07.788761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>Import the  dataset  💾\n </font></h2>","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:07.791463Z","iopub.execute_input":"2023-07-08T15:30:07.792120Z","iopub.status.idle":"2023-07-08T15:30:07.799220Z","shell.execute_reply.started":"2023-07-08T15:30:07.792086Z","shell.execute_reply":"2023-07-08T15:30:07.798135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>EDA - Exploratary Data Analysis  ⚒️\n</font></h2>","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":"2023-07-08T15:30:07.800373Z","iopub.execute_input":"2023-07-08T15:30:07.800669Z","iopub.status.idle":"2023-07-08T15:30:11.873750Z","shell.execute_reply.started":"2023-07-08T15:30:07.800644Z","shell.execute_reply":"2023-07-08T15:30:11.872809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:11.876169Z","iopub.execute_input":"2023-07-08T15:30:11.876462Z","iopub.status.idle":"2023-07-08T15:30:11.913059Z","shell.execute_reply.started":"2023-07-08T15:30:11.876435Z","shell.execute_reply":"2023-07-08T15:30:11.911915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:11.914281Z","iopub.execute_input":"2023-07-08T15:30:11.914504Z","iopub.status.idle":"2023-07-08T15:30:11.921252Z","shell.execute_reply.started":"2023-07-08T15:30:11.914484Z","shell.execute_reply":"2023-07-08T15:30:11.920161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train.drop(['label'],axis = 1).astype('int32')\ntrain_y = train['label'].astype('float32')\ntest_x = test.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:11.922897Z","iopub.execute_input":"2023-07-08T15:30:11.923258Z","iopub.status.idle":"2023-07-08T15:30:12.026294Z","shell.execute_reply.started":"2023-07-08T15:30:11.923231Z","shell.execute_reply":"2023-07-08T15:30:12.025041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.values.reshape(-1,28,28,1)\ntrain_x = train_x/255.0\ntest_x = test_x.values.reshape(-1,28,28,1)\ntest_x = test_x/255.0","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:12.027445Z","iopub.execute_input":"2023-07-08T15:30:12.027683Z","iopub.status.idle":"2023-07-08T15:30:12.086575Z","shell.execute_reply.started":"2023-07-08T15:30:12.027664Z","shell.execute_reply":"2023-07-08T15:30:12.085546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = tf.keras.utils.to_categorical(train_y,10)\ntrain_y.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:12.087680Z","iopub.execute_input":"2023-07-08T15:30:12.087905Z","iopub.status.idle":"2023-07-08T15:30:12.097269Z","shell.execute_reply.started":"2023-07-08T15:30:12.087885Z","shell.execute_reply":"2023-07-08T15:30:12.095917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:12.099789Z","iopub.execute_input":"2023-07-08T15:30:12.100233Z","iopub.status.idle":"2023-07-08T15:30:12.109755Z","shell.execute_reply.started":"2023-07-08T15:30:12.100198Z","shell.execute_reply":"2023-07-08T15:30:12.108580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>Model \n </font></h2>","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    \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    \n    tf.keras.layers.Conv2D(32,(3,3),activation='relu',padding='Same'),\n    tf.keras.layers.Conv2D(32,(3,3),activation='relu',padding='Same'),\n    tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n    \n    tf.keras.layers.Conv2D(32,(3,3),activation='relu',padding='Same'),\n    tf.keras.layers.Conv2D(32,(3,3),activation='relu',padding='Same'),\n    tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n    \n    tf.keras.layers.Flatten(),\n    \n    tf.keras.layers.Dense(256, activation = 'relu'),\n    tf.keras.layers.Dense(256, activation = 'relu'),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:12.112725Z","iopub.execute_input":"2023-07-08T15:30:12.113053Z","iopub.status.idle":"2023-07-08T15:30:12.383679Z","shell.execute_reply.started":"2023-07-08T15:30:12.113027Z","shell.execute_reply":"2023-07-08T15:30:12.382519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'Adam', loss = 'categorical_crossentropy',metrics=['accuracy'])\nmodel.fit(train_x,train_y,batch_size=50,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:30:12.385649Z","iopub.execute_input":"2023-07-08T15:30:12.386055Z","iopub.status.idle":"2023-07-08T15:37:35.892334Z","shell.execute_reply.started":"2023-07-08T15:30:12.386020Z","shell.execute_reply":"2023-07-08T15:37:35.891122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>Predictions 🔮\n </font></h2>","metadata":{}},{"cell_type":"code","source":"result = model.predict(test_x)\nresult = np.argmax(result,axis = 1)\nresult = pd.Series(result,name='Label')","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:37:35.893938Z","iopub.execute_input":"2023-07-08T15:37:35.894537Z","iopub.status.idle":"2023-07-08T15:37:45.416536Z","shell.execute_reply.started":"2023-07-08T15:37:35.894506Z","shell.execute_reply":"2023-07-08T15:37:45.415793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 align=\"left\"><font color='gray'>Submission 🟰\n </font></h2>","metadata":{}},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = 'ImageId'),result],axis = 1)\nsubmission.to_csv(\"submission.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2023-07-08T15:38:01.035621Z","iopub.execute_input":"2023-07-08T15:38:01.035953Z","iopub.status.idle":"2023-07-08T15:38:01.070623Z","shell.execute_reply.started":"2023-07-08T15:38:01.035931Z","shell.execute_reply":"2023-07-08T15:38:01.069590Z"},"trusted":true},"execution_count":null,"outputs":[]}]}