{"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":"from tensorflow.keras import Sequential # Loading Sequential \nfrom tensorflow.keras.models import load_model # to load \nfrom tensorflow.keras.layers import Flatten, Dense, Dropout\nfrom sklearn.model_selection import train_test_split # used to split the data \nfrom tensorflow.keras.utils import to_categorical # One hot encoding if you dont want to use one hot encoding use SparseCategoricalCrossentropy as loss function\nfrom tensorflow.keras import layers\nimport tensorflow as tf\nimport matplotlib.pyplot as plt # plotting graphs\nimport pandas as pd # data manupulation\nimport numpy as np # numerical operation\nimport math","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:07:38.742142Z","iopub.execute_input":"2022-06-10T05:07:38.742458Z","iopub.status.idle":"2022-06-10T05:07:48.349364Z","shell.execute_reply.started":"2022-06-10T05:07:38.742422Z","shell.execute_reply":"2022-06-10T05:07:48.348316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Reading Data**","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:30.644675Z","iopub.execute_input":"2022-06-10T05:37:30.645017Z","iopub.status.idle":"2022-06-10T05:37:35.228807Z","shell.execute_reply.started":"2022-06-10T05:37:30.644974Z","shell.execute_reply":"2022-06-10T05:37:35.227734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:35.230552Z","iopub.execute_input":"2022-06-10T05:37:35.230825Z","iopub.status.idle":"2022-06-10T05:37:35.247911Z","shell.execute_reply.started":"2022-06-10T05:37:35.230793Z","shell.execute_reply":"2022-06-10T05:37:35.247258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:35.248918Z","iopub.execute_input":"2022-06-10T05:37:35.249723Z","iopub.status.idle":"2022-06-10T05:37:35.276993Z","shell.execute_reply.started":"2022-06-10T05:37:35.249685Z","shell.execute_reply":"2022-06-10T05:37:35.276377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'training data shape {train_data.shape}, testing data shape {test_data.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:38.371228Z","iopub.execute_input":"2022-06-10T05:37:38.371530Z","iopub.status.idle":"2022-06-10T05:37:38.377997Z","shell.execute_reply.started":"2022-06-10T05:37:38.371500Z","shell.execute_reply":"2022-06-10T05:37:38.377147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = to_categorical(train_data.label)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:44.665451Z","iopub.execute_input":"2022-06-10T05:37:44.665892Z","iopub.status.idle":"2022-06-10T05:37:44.670637Z","shell.execute_reply.started":"2022-06-10T05:37:44.665849Z","shell.execute_reply":"2022-06-10T05:37:44.669768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.drop(['label'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:46.139140Z","iopub.execute_input":"2022-06-10T05:37:46.139717Z","iopub.status.idle":"2022-06-10T05:37:46.271125Z","shell.execute_reply.started":"2022-06-10T05:37:46.139681Z","shell.execute_reply":"2022-06-10T05:37:46.270403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'training data shape {train_data.shape}, testing data shape {test_data.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:48.008598Z","iopub.execute_input":"2022-06-10T05:37:48.009456Z","iopub.status.idle":"2022-06-10T05:37:48.014655Z","shell.execute_reply.started":"2022-06-10T05:37:48.009414Z","shell.execute_reply":"2022-06-10T05:37:48.013670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Spliting the data**","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train_data,Y,test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:37:49.304320Z","iopub.execute_input":"2022-06-10T05:37:49.304854Z","iopub.status.idle":"2022-06-10T05:37:49.628713Z","shell.execute_reply.started":"2022-06-10T05:37:49.304816Z","shell.execute_reply":"2022-06-10T05:37:49.627532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_scaled_train = x_train/255\nx_scaled_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:38:15.305990Z","iopub.execute_input":"2022-06-10T05:38:15.306298Z","iopub.status.idle":"2022-06-10T05:38:15.401145Z","shell.execute_reply.started":"2022-06-10T05:38:15.306267Z","shell.execute_reply":"2022-06-10T05:38:15.400301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Training model on non scaled data**","metadata":{}},{"cell_type":"code","source":"model_dense = Sequential([\n    layers.Dense(500, input_dim=x_train.shape[1],activation='relu', name='input_layer'),\n    layers.Dense(100, activation='relu', name='dense_layer'),\n    layers.Dropout(0.2),\n    layers.Dense(10, activation='softmax', name='output_layer')    \n])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:44:08.582281Z","iopub.execute_input":"2022-06-10T05:44:08.582611Z","iopub.status.idle":"2022-06-10T05:44:08.629466Z","shell.execute_reply.started":"2022-06-10T05:44:08.582577Z","shell.execute_reply":"2022-06-10T05:44:08.628369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), \n                    loss='categorical_crossentropy',\n                    metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:44:11.247224Z","iopub.execute_input":"2022-06-10T05:44:11.247540Z","iopub.status.idle":"2022-06-10T05:44:11.258565Z","shell.execute_reply.started":"2022-06-10T05:44:11.247506Z","shell.execute_reply":"2022-06-10T05:44:11.257614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_before_scaling = model_dense.fit(x_scaled_train,x_scaled_test,\n                          batch_size=32,\n                          epochs=20,\n                          validation_data = (x_test, y_test),\n                          validation_steps = int(0.25 * int(x_test.shape[0]))\n                         )","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:44:20.496209Z","iopub.execute_input":"2022-06-10T05:44:20.496518Z","iopub.status.idle":"2022-06-10T05:45:53.531038Z","shell.execute_reply.started":"2022-06-10T05:44:20.496483Z","shell.execute_reply":"2022-06-10T05:45:53.530234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Accuracy ~ Loss Curve | Non Scaled Data**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\naccuracy = history_before_scaling.history['acc']\nloss = history_before_scaling.history['loss']\nplt.xlabel(xlabel='Accuracy')\nplt.ylabel(ylabel='Loss')\nplt.title(label='Accuracy ~ Loss Curve')\nplt.plot(accuracy, loss)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:49:02.787518Z","iopub.execute_input":"2022-06-10T05:49:02.787838Z","iopub.status.idle":"2022-06-10T05:49:03.031650Z","shell.execute_reply.started":"2022-06-10T05:49:02.787808Z","shell.execute_reply":"2022-06-10T05:49:03.030747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Training model on scaled data**","metadata":{}},{"cell_type":"code","source":"model_s_dense = Sequential([\n    layers.Dense(500, input_dim=x_train.shape[1],activation='relu', name='input_layer'),\n    layers.Dense(100, activation='relu', name='dense_layer'),\n    layers.Dropout(0.2),\n    layers.Dense(10, activation='softmax', name='output_layer')    \n])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:46:15.981947Z","iopub.execute_input":"2022-06-10T05:46:15.982309Z","iopub.status.idle":"2022-06-10T05:46:16.027719Z","shell.execute_reply.started":"2022-06-10T05:46:15.982267Z","shell.execute_reply":"2022-06-10T05:46:16.026967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_s_dense.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), \n                    loss='categorical_crossentropy',\n                    metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:46:27.315649Z","iopub.execute_input":"2022-06-10T05:46:27.316318Z","iopub.status.idle":"2022-06-10T05:46:27.327978Z","shell.execute_reply.started":"2022-06-10T05:46:27.316261Z","shell.execute_reply":"2022-06-10T05:46:27.326939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_after_scaling = model_s_dense.fit(x_scaled_train,y_train,\n                          batch_size=32,\n                          epochs=20,\n                          validation_data = (x_scaled_test, y_test),\n                          validation_steps = int(0.25 * int(x_scaled_test.shape[0]))\n                         )","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:46:50.304105Z","iopub.execute_input":"2022-06-10T05:46:50.304458Z","iopub.status.idle":"2022-06-10T05:48:25.177030Z","shell.execute_reply.started":"2022-06-10T05:46:50.304420Z","shell.execute_reply":"2022-06-10T05:48:25.176208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Accuracy ~ Loss Curve| Scaled Data**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\naccuracy = history_after_scaling.history['acc']\nloss = history_after_scaling.history['loss']\nplt.xlabel(xlabel='Accuracy')\nplt.ylabel(ylabel='Loss')\nplt.title(label='Accuracy ~ Loss Curve')\nplt.plot(accuracy, loss)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:49:12.624475Z","iopub.execute_input":"2022-06-10T05:49:12.624772Z","iopub.status.idle":"2022-06-10T05:49:12.864383Z","shell.execute_reply.started":"2022-06-10T05:49:12.624739Z","shell.execute_reply":"2022-06-10T05:49:12.863123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Evalutaing Models**","metadata":{}},{"cell_type":"code","source":"model_dense.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:50:08.114201Z","iopub.execute_input":"2022-06-10T05:50:08.115236Z","iopub.status.idle":"2022-06-10T05:50:09.263149Z","shell.execute_reply.started":"2022-06-10T05:50:08.115151Z","shell.execute_reply":"2022-06-10T05:50:09.262250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_s_dense.evaluate(x_scaled_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:50:25.145114Z","iopub.execute_input":"2022-06-10T05:50:25.145443Z","iopub.status.idle":"2022-06-10T05:50:26.245282Z","shell.execute_reply.started":"2022-06-10T05:50:25.145411Z","shell.execute_reply":"2022-06-10T05:50:26.244414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(model_s_dense.predict(test_data/255), axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:51:05.813874Z","iopub.execute_input":"2022-06-10T05:51:05.814201Z","iopub.status.idle":"2022-06-10T05:51:07.624038Z","shell.execute_reply.started":"2022-06-10T05:51:05.814150Z","shell.execute_reply":"2022-06-10T05:51:07.623252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **Submitting Predictions**","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:51:32.922278Z","iopub.execute_input":"2022-06-10T05:51:32.922725Z","iopub.status.idle":"2022-06-10T05:51:32.938753Z","shell.execute_reply.started":"2022-06-10T05:51:32.922690Z","shell.execute_reply":"2022-06-10T05:51:32.937760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Label'] =y_pred","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:51:33.995772Z","iopub.execute_input":"2022-06-10T05:51:33.996509Z","iopub.status.idle":"2022-06-10T05:51:34.002552Z","shell.execute_reply.started":"2022-06-10T05:51:33.996455Z","shell.execute_reply":"2022-06-10T05:51:34.001519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:51:35.010002Z","iopub.execute_input":"2022-06-10T05:51:35.010332Z","iopub.status.idle":"2022-06-10T05:51:35.022652Z","shell.execute_reply.started":"2022-06-10T05:51:35.010296Z","shell.execute_reply":"2022-06-10T05:51:35.021569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T05:51:37.916151Z","iopub.execute_input":"2022-06-10T05:51:37.916502Z","iopub.status.idle":"2022-06-10T05:51:37.972943Z","shell.execute_reply.started":"2022-06-10T05:51:37.916465Z","shell.execute_reply":"2022-06-10T05:51:37.972078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}}]}