{"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)\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":"2021-07-17T19:26:44.984648Z","iopub.execute_input":"2021-07-17T19:26:44.985564Z","iopub.status.idle":"2021-07-17T19:26:44.998991Z","shell.execute_reply.started":"2021-07-17T19:26:44.985520Z","shell.execute_reply":"2021-07-17T19:26:44.997229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# `Using Keras Sequential API to create a model that classfies the MNIST Dataset with 99% accuracy on validation set`","metadata":{}},{"cell_type":"markdown","source":"- ## Reading the Train data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer/train.csv')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:26:49.050398Z","iopub.execute_input":"2021-07-17T19:26:49.050900Z","iopub.status.idle":"2021-07-17T19:26:52.668665Z","shell.execute_reply.started":"2021-07-17T19:26:49.050860Z","shell.execute_reply":"2021-07-17T19:26:52.667678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Separating the labels from dataset","metadata":{}},{"cell_type":"code","source":"data = df.drop(['label'],axis=1)\nlabels = df['label']","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:26:54.882099Z","iopub.execute_input":"2021-07-17T19:26:54.882527Z","iopub.status.idle":"2021-07-17T19:26:55.092787Z","shell.execute_reply.started":"2021-07-17T19:26:54.882487Z","shell.execute_reply":"2021-07-17T19:26:55.091718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.unique()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:43:39.176625Z","iopub.execute_input":"2021-07-15T17:43:39.177248Z","iopub.status.idle":"2021-07-15T17:43:39.187295Z","shell.execute_reply.started":"2021-07-15T17:43:39.177209Z","shell.execute_reply":"2021-07-15T17:43:39.185942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Data Preparation","metadata":{}},{"cell_type":"code","source":"data = data.to_numpy()\nlabels = labels.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:26:58.189275Z","iopub.execute_input":"2021-07-17T19:26:58.189669Z","iopub.status.idle":"2021-07-17T19:26:58.195510Z","shell.execute_reply.started":"2021-07-17T19:26:58.189634Z","shell.execute_reply":"2021-07-17T19:26:58.194107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape,labels.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:43:45.07688Z","iopub.execute_input":"2021-07-15T17:43:45.077268Z","iopub.status.idle":"2021-07-15T17:43:45.084178Z","shell.execute_reply.started":"2021-07-15T17:43:45.077237Z","shell.execute_reply":"2021-07-15T17:43:45.083276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_data , val_data , train_labels , val_labels = train_test_split(data,labels,test_size=0.05)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:00.936947Z","iopub.execute_input":"2021-07-17T19:27:00.937416Z","iopub.status.idle":"2021-07-17T19:27:01.639306Z","shell.execute_reply.started":"2021-07-17T19:27:00.937377Z","shell.execute_reply":"2021-07-17T19:27:01.638314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape,val_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-15T17:43:49.63671Z","iopub.execute_input":"2021-07-15T17:43:49.637149Z","iopub.status.idle":"2021-07-15T17:43:49.644125Z","shell.execute_reply.started":"2021-07-15T17:43:49.637112Z","shell.execute_reply":"2021-07-15T17:43:49.642959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.reshape(39900,28,28,1)\nval_data = val_data.reshape(2100,28,28,1)\ntrain_data = train_data/255\nval_data = val_data/255","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:05.015511Z","iopub.execute_input":"2021-07-17T19:27:05.015914Z","iopub.status.idle":"2021-07-17T19:27:05.209048Z","shell.execute_reply.started":"2021-07-17T19:27:05.015879Z","shell.execute_reply":"2021-07-17T19:27:05.208174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Importing Tensorflow and Keras","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:07.489197Z","iopub.execute_input":"2021-07-17T19:27:07.489633Z","iopub.status.idle":"2021-07-17T19:27:07.494438Z","shell.execute_reply.started":"2021-07-17T19:27:07.489596Z","shell.execute_reply":"2021-07-17T19:27:07.493196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Callback function for our model","metadata":{}},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epoch,logs={}):\n        if(logs.get('val_accuracy')>0.99):\n            print('Reached 99% validation accuracy,stopping training')\n            self.model.stop_training = True\ncallbacks = myCallback()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:10.307334Z","iopub.execute_input":"2021-07-17T19:27:10.307718Z","iopub.status.idle":"2021-07-17T19:27:10.313760Z","shell.execute_reply.started":"2021-07-17T19:27:10.307683Z","shell.execute_reply":"2021-07-17T19:27:10.312699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Model Creation using Sequential API from Keras","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)),\n    keras.layers.MaxPooling2D(2,2),\n    keras.layers.Conv2D(32,(3,3),activation='relu'),\n    keras.layers.MaxPooling2D(2,2),\n    keras.layers.Flatten(),\n    keras.layers.Dense(256,activation='relu'),\n    keras.layers.Dense(10,activation='softmax')\n])\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:12.795874Z","iopub.execute_input":"2021-07-17T19:27:12.796222Z","iopub.status.idle":"2021-07-17T19:27:12.873674Z","shell.execute_reply.started":"2021-07-17T19:27:12.796192Z","shell.execute_reply":"2021-07-17T19:27:12.872333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Training the model on training data","metadata":{}},{"cell_type":"code","source":"model.fit(train_data,train_labels,epochs=50,validation_data=(val_data,val_labels),callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:27:17.412639Z","iopub.execute_input":"2021-07-17T19:27:17.413063Z","iopub.status.idle":"2021-07-17T19:29:35.103851Z","shell.execute_reply.started":"2021-07-17T19:27:17.413027Z","shell.execute_reply":"2021-07-17T19:29:35.102725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Reading the test dataset","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_data","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:31:40.827597Z","iopub.execute_input":"2021-07-17T19:31:40.828124Z","iopub.status.idle":"2021-07-17T19:31:43.158497Z","shell.execute_reply.started":"2021-07-17T19:31:40.828062Z","shell.execute_reply":"2021-07-17T19:31:43.157458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Data Preparation for Test set and making predictions on it","metadata":{}},{"cell_type":"code","source":"test_data = test_data.to_numpy()\ntest_data = test_data.reshape(28000,28,28,1)\ntest_data = test_data / 255\npredictions = model.predict(test_data)\npredictions = np.argmax(predictions,axis=1)\npredictions","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:32:22.562826Z","iopub.execute_input":"2021-07-17T19:32:22.563285Z","iopub.status.idle":"2021-07-17T19:32:26.594681Z","shell.execute_reply.started":"2021-07-17T19:32:22.563249Z","shell.execute_reply":"2021-07-17T19:32:26.593668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Creating a Pandas DataFrame with our predictions in it","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission['Label'] = predictions","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:32:30.308738Z","iopub.execute_input":"2021-07-17T19:32:30.309153Z","iopub.status.idle":"2021-07-17T19:32:30.332804Z","shell.execute_reply.started":"2021-07-17T19:32:30.309118Z","shell.execute_reply":"2021-07-17T19:32:30.331456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ## Exporting our dataframe to CSV file format","metadata":{}},{"cell_type":"code","source":"submission.to_csv('mnist_digit_test_predictions.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T19:32:40.132539Z","iopub.execute_input":"2021-07-17T19:32:40.132917Z","iopub.status.idle":"2021-07-17T19:32:40.196590Z","shell.execute_reply.started":"2021-07-17T19:32:40.132886Z","shell.execute_reply":"2021-07-17T19:32:40.195503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The End\n`If you liked the notebook then don't forget to upvote and suggestions are always welcomed.`\n`Follow me on Linkedin :` __[Atharva_Dumbre](https://www.linkedin.com/in/atharva-dumbre-208b5716b)__","metadata":{}}]}