{"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":"# Read Data","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Read data\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\n\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nx_train=train.iloc[:,1:].values.astype('float32') # all pixel values\nx_test=test.values.astype('float32')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-18T01:09:40.149913Z","iopub.execute_input":"2022-02-18T01:09:40.150483Z","iopub.status.idle":"2022-02-18T01:09:45.173456Z","shell.execute_reply.started":"2022-02-18T01:09:40.150445Z","shell.execute_reply":"2022-02-18T01:09:45.172681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reshape Data from 784 to (28,28)","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\n\nimage_index = 50 # You may select anything up to 60,000\nx_train_reshape=[]\nprint(y_train[image_index])\n\nfor i in range(len(x_train)):\n    x_train_reshape.append(x_train[i].reshape(28,28))\nplt.imshow(x_train_reshape[image_index], cmap='Greys')\n\nx_train_update=np.array(x_train_reshape)\nx_train_update.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:09:46.402907Z","iopub.execute_input":"2022-02-18T01:09:46.403571Z","iopub.status.idle":"2022-02-18T01:09:46.74734Z","shell.execute_reply.started":"2022-02-18T01:09:46.403527Z","shell.execute_reply":"2022-02-18T01:09:46.746665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"######Reshape x_test also \nx_test_reshape=[]\nfor i in range(len(x_test)):\n    x_test_reshape.append(x_test[i].reshape(28,28))\nplt.imshow(x_test_reshape[image_index], cmap='Greys')\n\nx_test_update=np.array(x_test_reshape)\nx_test_update.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:09:50.468448Z","iopub.execute_input":"2022-02-18T01:09:50.468875Z","iopub.status.idle":"2022-02-18T01:09:50.870522Z","shell.execute_reply.started":"2022-02-18T01:09:50.468835Z","shell.execute_reply":"2022-02-18T01:09:50.8666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Reshape Data from (42000,28,28) to four dimensions (42000,38,38,1) to make it can use Keras API**","metadata":{}},{"cell_type":"code","source":"# Reshaping the array to 4-dims so that it can work with the Keras API\nx_train_update = x_train_update.reshape(x_train.shape[0], 28, 28, 1)\nx_test_update = x_test_update.reshape(x_test.shape[0], 28, 28, 1)\ninput_shape = (28, 28, 1)\n# Making sure that the values are float so that we can get decimal points after division\nx_train_update = x_train_update.astype('float32')\nx_test_update = x_test_update.astype('float32')\n# Normalizing the RGB codes by dividing it to the max RGB value.\nx_train_update /= 255\nx_test_update /= 255\nprint('x_train_update shape:', x_train.shape)\nprint('Number of images in x_train_update', x_train_update.shape[0])\nprint('Number of images in x_test_update', x_test_update.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:09:55.218915Z","iopub.execute_input":"2022-02-18T01:09:55.219486Z","iopub.status.idle":"2022-02-18T01:09:55.330433Z","shell.execute_reply.started":"2022-02-18T01:09:55.219448Z","shell.execute_reply":"2022-02-18T01:09:55.329666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the Convolutional Neural Network","metadata":{}},{"cell_type":"markdown","source":"Build the model using high-level Keras API which uses either TensorFlow or Theano on the backend. \nThere are several high-level TensorFlow APIs such as Layers, Keras, and Estimators which helps us create neural networks with high-level knowledge. However, this may lead to confusion since they all vary in their implementation structure. \nTherefore, if you see completely different codes for the same neural network although they all use TensorFlow, this is why. \n\nThe most straightforward API ==> Keras ==> import the Sequential Model from Keras and add Conv2D, MaxPooling, Flatten, Dropout, and Dense layers. \n","metadata":{}},{"cell_type":"code","source":"# Importing the required Keras modules containing model and layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\n# Creating a Sequential Model and adding the layers\nmodel = Sequential()\nmodel.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Flatten()) # Flattening the 2D arrays for fully connected layers\nmodel.add(Dense(128, activation=tf.nn.relu))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation=tf.nn.softmax))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:10:03.339106Z","iopub.execute_input":"2022-02-18T01:10:03.339669Z","iopub.status.idle":"2022-02-18T01:10:03.37835Z","shell.execute_reply.started":"2022-02-18T01:10:03.339634Z","shell.execute_reply":"2022-02-18T01:10:03.377671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling and Fitting the Model\n\n","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam', \n              loss='sparse_categorical_crossentropy', \n              metrics=['accuracy'])\nmodel.fit(x=x_train_update,y=y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:10:35.690919Z","iopub.execute_input":"2022-02-18T01:10:35.69163Z","iopub.status.idle":"2022-02-18T01:11:14.832348Z","shell.execute_reply.started":"2022-02-18T01:10:35.691593Z","shell.execute_reply":"2022-02-18T01:11:14.831542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction and Save Submission File","metadata":{}},{"cell_type":"code","source":"pred=[]\nfor i in range(len(x_test_update)):\n    pred.append(model.predict(x_test_update[i].reshape(1,28,28,1)).argmax())\n\n    \nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(pred)+1)),\"Label\": pred})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:25:54.289865Z","iopub.execute_input":"2022-02-18T01:25:54.290629Z","iopub.status.idle":"2022-02-18T01:41:40.657991Z","shell.execute_reply.started":"2022-02-18T01:25:54.290582Z","shell.execute_reply":"2022-02-18T01:41:40.657256Z"},"trusted":true},"execution_count":null,"outputs":[]}]}