{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nnp.random.seed(0) # set seed for reproducibility\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-20T05:29:55.507053Z","iopub.execute_input":"2022-01-20T05:29:55.507429Z","iopub.status.idle":"2022-01-20T05:30:02.002248Z","shell.execute_reply.started":"2022-01-20T05:29:55.507335Z","shell.execute_reply":"2022-01-20T05:30:02.001527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## READING THE DATASET","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\")\n\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:02.003572Z","iopub.execute_input":"2022-01-20T05:30:02.004276Z","iopub.status.idle":"2022-01-20T05:30:07.86301Z","shell.execute_reply.started":"2022-01-20T05:30:02.004229Z","shell.execute_reply":"2022-01-20T05:30:07.862221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#inspecting the dataset\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:07.866489Z","iopub.execute_input":"2022-01-20T05:30:07.866769Z","iopub.status.idle":"2022-01-20T05:30:07.893705Z","shell.execute_reply.started":"2022-01-20T05:30:07.866738Z","shell.execute_reply":"2022-01-20T05:30:07.892593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#splitting into features X and target Y\nX = train.drop(columns='label')\ny = train['label']","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:07.896284Z","iopub.execute_input":"2022-01-20T05:30:07.896659Z","iopub.status.idle":"2022-01-20T05:30:08.020755Z","shell.execute_reply.started":"2022-01-20T05:30:07.896602Z","shell.execute_reply":"2022-01-20T05:30:08.019589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PREPROCESSING","metadata":{}},{"cell_type":"code","source":"# split the dataset \nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)\n\n# inpect the shapes\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.022555Z","iopub.execute_input":"2022-01-20T05:30:08.023034Z","iopub.status.idle":"2022-01-20T05:30:08.29657Z","shell.execute_reply.started":"2022-01-20T05:30:08.022968Z","shell.execute_reply":"2022-01-20T05:30:08.29577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the date frame to numpay array to feed it to the model\n# convert the train dataset\nX_train = X_train.to_numpy()\ny_train = y_train.to_numpy()\n\n# convert the validation dataset\nX_val = X_val.to_numpy()\ny_val = y_val.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.297927Z","iopub.execute_input":"2022-01-20T05:30:08.298139Z","iopub.status.idle":"2022-01-20T05:30:08.303038Z","shell.execute_reply.started":"2022-01-20T05:30:08.298113Z","shell.execute_reply":"2022-01-20T05:30:08.302485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Digit Inspection (before Scaling) ","metadata":{}},{"cell_type":"code","source":"# ispect the first number\nX_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.304097Z","iopub.execute_input":"2022-01-20T05:30:08.304836Z","iopub.status.idle":"2022-01-20T05:30:08.322703Z","shell.execute_reply.started":"2022-01-20T05:30:08.304793Z","shell.execute_reply":"2022-01-20T05:30:08.3219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# view the first number\nplt.imshow(X_train[0].reshape(28, 28))  # (rows, columns)\n# the label of the first number\nplt.title(f\"Digit: {y_train[0]}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.323878Z","iopub.execute_input":"2022-01-20T05:30:08.324127Z","iopub.status.idle":"2022-01-20T05:30:08.537202Z","shell.execute_reply.started":"2022-01-20T05:30:08.324097Z","shell.execute_reply":"2022-01-20T05:30:08.536194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scaling the Pixel Distribution Values","metadata":{}},{"cell_type":"code","source":"# plot the pixels distributions\nplt.hist(X_train[0].reshape(784))\nplt.title(\"Pixel Values Distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.538704Z","iopub.execute_input":"2022-01-20T05:30:08.538928Z","iopub.status.idle":"2022-01-20T05:30:08.74528Z","shell.execute_reply.started":"2022-01-20T05:30:08.5389Z","shell.execute_reply":"2022-01-20T05:30:08.744339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since majority of values in the samples are 0, the dataset must be scaled to improve training times ","metadata":{}},{"cell_type":"code","source":"#Dividing all pixel values by 255 to scale the values b/w 0 & 1\nX_train = X_train/255\nX_val = X_val/255","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.748931Z","iopub.execute_input":"2022-01-20T05:30:08.749266Z","iopub.status.idle":"2022-01-20T05:30:08.859603Z","shell.execute_reply.started":"2022-01-20T05:30:08.74923Z","shell.execute_reply":"2022-01-20T05:30:08.858862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### First Digit Inspection (after Scaling) ","metadata":{}},{"cell_type":"code","source":"# plot the pixels distributions after Scaling\nplt.hist(X_train[0].reshape(784))\nplt.title(\"Pixel Values Distribution after Scaling\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:08.860651Z","iopub.execute_input":"2022-01-20T05:30:08.861054Z","iopub.status.idle":"2022-01-20T05:30:09.078601Z","shell.execute_reply.started":"2022-01-20T05:30:08.861013Z","shell.execute_reply":"2022-01-20T05:30:09.077568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.080033Z","iopub.execute_input":"2022-01-20T05:30:09.080318Z","iopub.status.idle":"2022-01-20T05:30:09.096725Z","shell.execute_reply.started":"2022-01-20T05:30:09.080287Z","shell.execute_reply":"2022-01-20T05:30:09.095599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### One Hot Encoding the Target Values","metadata":{}},{"cell_type":"code","source":"np.unique(y_train, return_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.098195Z","iopub.execute_input":"2022-01-20T05:30:09.098661Z","iopub.status.idle":"2022-01-20T05:30:09.109365Z","shell.execute_reply.started":"2022-01-20T05:30:09.098603Z","shell.execute_reply":"2022-01-20T05:30:09.108543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's encode our categories - digits from 0 to 9 - using one-hot encoding. The result is a vector with a length equal to the number of categories. The vector is all zeroes except in the position for the respective category. Thus a '5' will be represented by [0,0,0,0,1,0,0,0,0].","metadata":{}},{"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train, num_classes=10)\ny_val = keras.utils.to_categorical(y_val, num_classes=10)\nX_train.shape[1]\ny_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.11106Z","iopub.execute_input":"2022-01-20T05:30:09.11131Z","iopub.status.idle":"2022-01-20T05:30:09.12234Z","shell.execute_reply.started":"2022-01-20T05:30:09.111282Z","shell.execute_reply":"2022-01-20T05:30:09.121198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating Model using Keras","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nmodel = keras.Sequential([\n    layers.Dense(516, activation='relu', input_shape=[X_train.shape[1]]),\n    layers.Dropout(0.7),\n    layers.Dense(1024, activation='relu'),\n    layers.Dropout(0.1),\n    layers.Dense(10, activation='softmax')    \n])","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.123898Z","iopub.execute_input":"2022-01-20T05:30:09.124449Z","iopub.status.idle":"2022-01-20T05:30:09.251498Z","shell.execute_reply.started":"2022-01-20T05:30:09.124412Z","shell.execute_reply":"2022-01-20T05:30:09.250655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.253039Z","iopub.execute_input":"2022-01-20T05:30:09.253576Z","iopub.status.idle":"2022-01-20T05:30:09.263003Z","shell.execute_reply.started":"2022-01-20T05:30:09.253528Z","shell.execute_reply":"2022-01-20T05:30:09.262042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compiling the sequential model\nmodel.compile(optimizer='Adam',\n              loss='categorical_crossentropy',\n              metrics=['categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.264381Z","iopub.execute_input":"2022-01-20T05:30:09.265211Z","iopub.status.idle":"2022-01-20T05:30:09.280952Z","shell.execute_reply.started":"2022-01-20T05:30:09.26515Z","shell.execute_reply":"2022-01-20T05:30:09.279922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = keras.callbacks.EarlyStopping(\n    patience=4,\n    min_delta=0.001,\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:30:09.282233Z","iopub.execute_input":"2022-01-20T05:30:09.282623Z","iopub.status.idle":"2022-01-20T05:30:09.287745Z","shell.execute_reply.started":"2022-01-20T05:30:09.282584Z","shell.execute_reply":"2022-01-20T05:30:09.286853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train the model\ntraining = model.fit(\n    X_train, y_train,\n    validation_data=(X_val, y_val),\n    batch_size=128,\n    epochs=25,\n    callbacks=[early_stopping],\n    verbose=0\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:32:03.471815Z","iopub.execute_input":"2022-01-20T05:32:03.472157Z","iopub.status.idle":"2022-01-20T05:33:04.942217Z","shell.execute_reply.started":"2022-01-20T05:32:03.472121Z","shell.execute_reply":"2022-01-20T05:33:04.941277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate the model\nloss_and_metrics = model.evaluate(X_val, y_val, verbose=2)\n\nprint(\"Val Loss\", loss_and_metrics[0])\nprint(\"Val Accuracy\", loss_and_metrics[1])","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:33:39.717815Z","iopub.execute_input":"2022-01-20T05:33:39.718797Z","iopub.status.idle":"2022-01-20T05:33:40.73772Z","shell.execute_reply.started":"2022-01-20T05:33:39.718741Z","shell.execute_reply":"2022-01-20T05:33:40.736605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PREDICTIONS","metadata":{}},{"cell_type":"code","source":"# predect the labels of the test dataset, and return the exact number not an array (decode it)\ny_pred = np.argmax(model.predict(test.to_numpy()), axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:35:10.328204Z","iopub.execute_input":"2022-01-20T05:35:10.328694Z","iopub.status.idle":"2022-01-20T05:35:12.44431Z","shell.execute_reply.started":"2022-01-20T05:35:10.328644Z","shell.execute_reply":"2022-01-20T05:35:12.443339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# inspect the first number in the test dataset predicted by the model\nplt.imshow(test.to_numpy()[0].reshape(28, 28))\n# the label of the first number\nplt.title(f\"Digit: {y_pred[0]}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:35:33.535544Z","iopub.execute_input":"2022-01-20T05:35:33.535882Z","iopub.status.idle":"2022-01-20T05:35:33.723935Z","shell.execute_reply.started":"2022-01-20T05:35:33.535845Z","shell.execute_reply":"2022-01-20T05:35:33.723346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate Submission File \nsubmission = pd.DataFrame({'ImageId':pd.Series(list(range(1, len(y_pred)+1))),\n                                   'Label':pd.Series(y_pred)})\n# save the df\nsubmission.to_csv(\"Submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:35:56.195104Z","iopub.execute_input":"2022-01-20T05:35:56.195442Z","iopub.status.idle":"2022-01-20T05:35:56.276305Z","shell.execute_reply.started":"2022-01-20T05:35:56.195406Z","shell.execute_reply":"2022-01-20T05:35:56.275183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-01-20T05:36:10.466907Z","iopub.execute_input":"2022-01-20T05:36:10.467201Z","iopub.status.idle":"2022-01-20T05:36:10.478296Z","shell.execute_reply.started":"2022-01-20T05:36:10.467171Z","shell.execute_reply":"2022-01-20T05:36:10.477646Z"},"trusted":true},"execution_count":null,"outputs":[]}]}