{"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":"Importing the libraries","metadata":{"id":"sUtQl-8wKgqR"}},{"cell_type":"code","source":"# Import the required libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"id":"k4-TwmVHm-s1","execution":{"iopub.status.busy":"2023-06-16T13:16:46.514354Z","iopub.execute_input":"2023-06-16T13:16:46.514777Z","iopub.status.idle":"2023-06-16T13:16:46.521973Z","shell.execute_reply.started":"2023-06-16T13:16:46.514744Z","shell.execute_reply":"2023-06-16T13:16:46.520518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Importing the dataset","metadata":{"id":"fx_bKWWTLZOl"}},{"cell_type":"code","source":"# Loading the dataset\ndf1=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')     # Training data\ndf2=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')      # Testing data","metadata":{"id":"OfWXPIObLLR4","execution":{"iopub.status.busy":"2023-06-16T13:17:01.743914Z","iopub.execute_input":"2023-06-16T13:17:01.744340Z","iopub.status.idle":"2023-06-16T13:17:07.141535Z","shell.execute_reply.started":"2023-06-16T13:17:01.744307Z","shell.execute_reply":"2023-06-16T13:17:07.140550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of rows and columns in both datasets\ndf1.shape,df2.shape","metadata":{"id":"hNljuCfASS4U","outputId":"835727af-a45d-448d-8aff-cdb48cddd6ef","execution":{"iopub.status.busy":"2023-06-16T13:17:24.760738Z","iopub.execute_input":"2023-06-16T13:17:24.761157Z","iopub.status.idle":"2023-06-16T13:17:24.769076Z","shell.execute_reply.started":"2023-06-16T13:17:24.761124Z","shell.execute_reply":"2023-06-16T13:17:24.767559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Preprocessing of training data","metadata":{"id":"11Op3Bm7N7wL"}},{"cell_type":"code","source":"# First five rows in training data\ndf1.head()","metadata":{"id":"PnyKdAPgN1tG","outputId":"020bceb4-b63f-4a51-fce7-91ff4a1fb760","execution":{"iopub.status.busy":"2023-06-16T13:17:28.727299Z","iopub.execute_input":"2023-06-16T13:17:28.727761Z","iopub.status.idle":"2023-06-16T13:17:28.749818Z","shell.execute_reply.started":"2023-06-16T13:17:28.727724Z","shell.execute_reply":"2023-06-16T13:17:28.748631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking null values in training data\ndf1.isnull().sum()","metadata":{"id":"-Z0518AFQiot","outputId":"29085efd-c34c-42a8-b847-3d871d05db5d","execution":{"iopub.status.busy":"2023-06-16T13:17:33.513693Z","iopub.execute_input":"2023-06-16T13:17:33.514135Z","iopub.status.idle":"2023-06-16T13:17:33.557687Z","shell.execute_reply.started":"2023-06-16T13:17:33.514102Z","shell.execute_reply":"2023-06-16T13:17:33.556693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Preprocessing of testing data","metadata":{"id":"TZN1dkOOTlAw"}},{"cell_type":"code","source":"# First five rows in testing dataset\ndf2.head()","metadata":{"id":"aCBgLkuDN7r3","outputId":"1e8bd16a-6154-4649-f4d5-85877fb03544","execution":{"iopub.status.busy":"2023-06-16T13:17:38.749412Z","iopub.execute_input":"2023-06-16T13:17:38.749828Z","iopub.status.idle":"2023-06-16T13:17:38.772787Z","shell.execute_reply.started":"2023-06-16T13:17:38.749796Z","shell.execute_reply":"2023-06-16T13:17:38.771428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking null values in testing dataset\ndf2.isnull().sum()","metadata":{"id":"0j0fzg8q8CFt","outputId":"d7d80370-0715-4ab2-ef4d-868c2259cfe2","execution":{"iopub.status.busy":"2023-06-16T13:17:42.041399Z","iopub.execute_input":"2023-06-16T13:17:42.041903Z","iopub.status.idle":"2023-06-16T13:17:42.073648Z","shell.execute_reply.started":"2023-06-16T13:17:42.041868Z","shell.execute_reply":"2023-06-16T13:17:42.072194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dividing the training dataset into dependent(target) and independent features","metadata":{"id":"ZMbeYyc3UCVe"}},{"cell_type":"code","source":"# Pixel columns will be independent columns and label column will be target column\nx_train=df1.iloc[:,1:]\ny_train=df1['label']","metadata":{"id":"PZ3ujK_pN-d8","execution":{"iopub.status.busy":"2023-06-16T13:17:53.300137Z","iopub.execute_input":"2023-06-16T13:17:53.300543Z","iopub.status.idle":"2023-06-16T13:17:53.307833Z","shell.execute_reply.started":"2023-06-16T13:17:53.300514Z","shell.execute_reply":"2023-06-16T13:17:53.306022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First five rows of independent columns\nx_train.head()","metadata":{"id":"CsfMasUYUlOz","outputId":"f5de1683-9926-4d09-f8ab-462c5f0d6a1e","execution":{"iopub.status.busy":"2023-06-16T13:17:56.994607Z","iopub.execute_input":"2023-06-16T13:17:56.995047Z","iopub.status.idle":"2023-06-16T13:17:57.018252Z","shell.execute_reply.started":"2023-06-16T13:17:56.995018Z","shell.execute_reply":"2023-06-16T13:17:57.017000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First five rows of target column\ny_train.head()","metadata":{"id":"juCJNhZoO6C3","outputId":"fc823e0c-1834-4f99-ebfd-b106a89df7f8","execution":{"iopub.status.busy":"2023-06-16T13:18:01.008894Z","iopub.execute_input":"2023-06-16T13:18:01.009287Z","iopub.status.idle":"2023-06-16T13:18:01.017780Z","shell.execute_reply.started":"2023-06-16T13:18:01.009257Z","shell.execute_reply":"2023-06-16T13:18:01.016633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As it is a Multi-class Classification problem, One-hot encoding will be applied on the target column to get dummy variables.","metadata":{"id":"5Zh2EKeAhECj"}},{"cell_type":"code","source":"# One hot encoding of target column\ny_train=pd.get_dummies(y_train)\ny_train.head()","metadata":{"id":"J6_H-2VHO-Y-","outputId":"7bb9dd2a-c10a-4025-9b59-632f8459f4ac","execution":{"iopub.status.busy":"2023-06-16T13:18:04.442549Z","iopub.execute_input":"2023-06-16T13:18:04.445211Z","iopub.status.idle":"2023-06-16T13:18:04.464912Z","shell.execute_reply.started":"2023-06-16T13:18:04.445145Z","shell.execute_reply":"2023-06-16T13:18:04.463539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Building the ANN Model","metadata":{"id":"hidSe80KduXv"}},{"cell_type":"code","source":"# Model building\nmodel=Sequential()\n\n# First hidden layer with number of input dimensions=784 and number of nodes=256\nmodel.add(Dense(units = 256, kernel_initializer = 'uniform', activation = 'relu',input_dim=784))\n# Drop-out 20 percent nodes in a layer\nmodel.add(Dropout(0.2))\n\n# Second hidden layer with number of nodes=256\nmodel.add(Dense(units= 256,kernel_initializer = 'uniform', activation = 'relu'))\nmodel.add(Dropout(0.2))\n\n# Third hidden layer with number of nodes=256\nmodel.add(Dense(units= 256,kernel_initializer = 'uniform', activation = 'relu'))\nmodel.add(Dropout(0.2))\n\n# Output layer with number of nodes=10\nmodel.add(Dense(units = 10, kernel_initializer = 'uniform', activation = 'softmax'))","metadata":{"id":"g-SYzsIzP0LH","execution":{"iopub.status.busy":"2023-06-16T13:18:09.283690Z","iopub.execute_input":"2023-06-16T13:18:09.284139Z","iopub.status.idle":"2023-06-16T13:18:09.365650Z","shell.execute_reply.started":"2023-06-16T13:18:09.284104Z","shell.execute_reply":"2023-06-16T13:18:09.364001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling the ANN model by applying the stochastic gradient descent\nmodel.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])","metadata":{"id":"Rdx8UcCgBjOK","execution":{"iopub.status.busy":"2023-06-16T13:18:13.971505Z","iopub.execute_input":"2023-06-16T13:18:13.972292Z","iopub.status.idle":"2023-06-16T13:18:13.988945Z","shell.execute_reply.started":"2023-06-16T13:18:13.972251Z","shell.execute_reply":"2023-06-16T13:18:13.987486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model to the training data, in which 10 per-cent is validation data with number of epochs=100\nmodel.fit(x_train,y_train,epochs=100,validation_split=0.1)","metadata":{"id":"g9lGdBtrCCWr","outputId":"0af6891d-3480-47f3-b7e0-3db43259cd57","execution":{"iopub.status.busy":"2023-06-16T13:18:27.500096Z","iopub.execute_input":"2023-06-16T13:18:27.500482Z","iopub.status.idle":"2023-06-16T13:32:06.259311Z","shell.execute_reply.started":"2023-06-16T13:18:27.500453Z","shell.execute_reply":"2023-06-16T13:32:06.257095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prediction","metadata":{"id":"KV_qCtQefzG_"}},{"cell_type":"code","source":"# Predicting the output for testing data\ny_prob=model.predict(df2)\ny_pred=y_prob.argmax(axis=1)\ny_pred","metadata":{"id":"DTFHCFkHGxFi","outputId":"c7288ea9-03cf-4572-9a76-361526b2dbf3","execution":{"iopub.status.busy":"2023-06-16T13:32:13.699384Z","iopub.execute_input":"2023-06-16T13:32:13.699880Z","iopub.status.idle":"2023-06-16T13:32:16.641775Z","shell.execute_reply.started":"2023-06-16T13:32:13.699844Z","shell.execute_reply":"2023-06-16T13:32:16.640567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Submission","metadata":{"id":"Aj2AqlPNqq62"}},{"cell_type":"code","source":"# Make another Dataframe 'Output' for images and their labels for submission\nImageNo=[x for x in range(1,28001)]\noutput=pd.DataFrame(ImageNo,columns=['ImageID'])\noutput['Label']=y_pred","metadata":{"id":"L4q0a0Fmqnft","execution":{"iopub.status.busy":"2023-06-16T13:32:20.998778Z","iopub.execute_input":"2023-06-16T13:32:20.999168Z","iopub.status.idle":"2023-06-16T13:32:21.022599Z","shell.execute_reply.started":"2023-06-16T13:32:20.999138Z","shell.execute_reply":"2023-06-16T13:32:21.021192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert Dataframe to CSV file\noutput.to_csv('output.csv',index=None)\npd.read_csv('output.csv')","metadata":{"id":"fA8YoylPrV80","outputId":"613488bf-200a-431b-f7be-2f56607373c7","execution":{"iopub.status.busy":"2023-06-16T13:32:25.454781Z","iopub.execute_input":"2023-06-16T13:32:25.455234Z","iopub.status.idle":"2023-06-16T13:32:25.543818Z","shell.execute_reply.started":"2023-06-16T13:32:25.455200Z","shell.execute_reply":"2023-06-16T13:32:25.542772Z"},"trusted":true},"execution_count":null,"outputs":[]}]}