{"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":"<h3>Imports:</h3>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:15.575605Z","iopub.execute_input":"2022-05-06T11:23:15.576152Z","iopub.status.idle":"2022-05-06T11:23:15.583850Z","shell.execute_reply.started":"2022-05-06T11:23:15.576101Z","shell.execute_reply":"2022-05-06T11:23:15.582786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Reading the data:</h3>","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:15.727539Z","iopub.execute_input":"2022-05-06T11:23:15.727872Z","iopub.status.idle":"2022-05-06T11:23:18.836727Z","shell.execute_reply.started":"2022-05-06T11:23:15.727834Z","shell.execute_reply":"2022-05-06T11:23:18.835747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Splitting the train data into train, test, validation datasets</h3>","metadata":{}},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:18.838555Z","iopub.execute_input":"2022-05-06T11:23:18.838821Z","iopub.status.idle":"2022-05-06T11:23:18.846101Z","shell.execute_reply.started":"2022-05-06T11:23:18.838786Z","shell.execute_reply":"2022-05-06T11:23:18.845019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= train_data[:33600]\nval = train_data[33600:37800]\ntest = train_data[37800:42000]\n\nprint(f'Train dataset shape {train.shape} | Validation dataset shape {val.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:18.847409Z","iopub.execute_input":"2022-05-06T11:23:18.847658Z","iopub.status.idle":"2022-05-06T11:23:18.860329Z","shell.execute_reply.started":"2022-05-06T11:23:18.847626Z","shell.execute_reply":"2022-05-06T11:23:18.859406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Spliting the datasets into X and y </h3>","metadata":{}},{"cell_type":"code","source":"x_train = train.drop('label',axis=1)      # Reading the images by not including the labels\ny_train = train['label']\nx_val = val.drop(columns=['label'])      # Reading the images by not including the labels\ny_val = val['label']\ny_test = test.label\nx_test = test.drop('label', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:18.862367Z","iopub.execute_input":"2022-05-06T11:23:18.862629Z","iopub.status.idle":"2022-05-06T11:23:19.021753Z","shell.execute_reply.started":"2022-05-06T11:23:18.862596Z","shell.execute_reply":"2022-05-06T11:23:19.020774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Preparing the model </h3>","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Input(shape = (784)),\n  \n    Dense(16, activation = \"relu\"),\n    BatchNormalization(),\n    Dense(16, activation = \"relu\"),\n    BatchNormalization(),\n    Dense(16, activation = \"relu\"),\n    BatchNormalization(),\n    Dropout(0.1),\n      \n    Dense(10, activation = \"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:19.023241Z","iopub.execute_input":"2022-05-06T11:23:19.023677Z","iopub.status.idle":"2022-05-06T11:23:19.115208Z","shell.execute_reply.started":"2022-05-06T11:23:19.023628Z","shell.execute_reply":"2022-05-06T11:23:19.114274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:19.116410Z","iopub.execute_input":"2022-05-06T11:23:19.116651Z","iopub.status.idle":"2022-05-06T11:23:19.127141Z","shell.execute_reply.started":"2022-05-06T11:23:19.116620Z","shell.execute_reply":"2022-05-06T11:23:19.126178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\", \n    loss=\"sparse_categorical_crossentropy\", \n    metrics=[\"accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:19.128820Z","iopub.execute_input":"2022-05-06T11:23:19.129176Z","iopub.status.idle":"2022-05-06T11:23:19.141469Z","shell.execute_reply.started":"2022-05-06T11:23:19.129128Z","shell.execute_reply":"2022-05-06T11:23:19.140747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Fit the data: </h3>","metadata":{}},{"cell_type":"code","source":"model.fit(\n    x_train,\n    y_train,\n    validation_data =(x_val, y_val),\n    epochs=10, \n    batch_size=128\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:23:19.142548Z","iopub.execute_input":"2022-05-06T11:23:19.143317Z","iopub.status.idle":"2022-05-06T11:23:31.894598Z","shell.execute_reply.started":"2022-05-06T11:23:19.143280Z","shell.execute_reply":"2022-05-06T11:23:31.893650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3> Results </h3>","metadata":{}},{"cell_type":"code","source":"test_prediction = model.predict(x_test)\nanswer_label = test_prediction.argmax(axis=1)\n\nfor i in range(25):\n    print(f'Real value : {y_test.iloc[i]} vs Predicted value :{answer_label[i]}')","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:33:08.660800Z","iopub.execute_input":"2022-05-06T11:33:08.661764Z","iopub.status.idle":"2022-05-06T11:33:08.948302Z","shell.execute_reply.started":"2022-05-06T11:33:08.661704Z","shell.execute_reply":"2022-05-06T11:33:08.947315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = len(y_test)\ncorrect = 0\nwrong = 0\n\nfor i in range(total):\n    if y_test.iloc[i] == answer_label[i]:\n        correct += 1\n    else:\n        wrong += 1\n\nprint(correct, wrong, f'{correct/total}')","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:35:33.540538Z","iopub.execute_input":"2022-05-06T11:35:33.541078Z","iopub.status.idle":"2022-05-06T11:35:33.588253Z","shell.execute_reply.started":"2022-05-06T11:35:33.541032Z","shell.execute_reply":"2022-05-06T11:35:33.587251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}