{"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":"Score: 0.99328","metadata":{}},{"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":"2022-04-29T12:44:18.199578Z","iopub.execute_input":"2022-04-29T12:44:18.199848Z","iopub.status.idle":"2022-04-29T12:44:18.208582Z","shell.execute_reply.started":"2022-04-29T12:44:18.199817Z","shell.execute_reply":"2022-04-29T12:44:18.207785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:18.210549Z","iopub.execute_input":"2022-04-29T12:44:18.211199Z","iopub.status.idle":"2022-04-29T12:44:23.151889Z","shell.execute_reply.started":"2022-04-29T12:44:18.211160Z","shell.execute_reply":"2022-04-29T12:44:23.150976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:23.153610Z","iopub.execute_input":"2022-04-29T12:44:23.153884Z","iopub.status.idle":"2022-04-29T12:44:27.747778Z","shell.execute_reply.started":"2022-04-29T12:44:23.153849Z","shell.execute_reply":"2022-04-29T12:44:27.747057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:27.749503Z","iopub.execute_input":"2022-04-29T12:44:27.749759Z","iopub.status.idle":"2022-04-29T12:44:27.774634Z","shell.execute_reply.started":"2022-04-29T12:44:27.749725Z","shell.execute_reply":"2022-04-29T12:44:27.773881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:27.775816Z","iopub.execute_input":"2022-04-29T12:44:27.776143Z","iopub.status.idle":"2022-04-29T12:44:27.827160Z","shell.execute_reply.started":"2022-04-29T12:44:27.776100Z","shell.execute_reply":"2022-04-29T12:44:27.825917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:27.828506Z","iopub.execute_input":"2022-04-29T12:44:27.828752Z","iopub.status.idle":"2022-04-29T12:44:27.874424Z","shell.execute_reply.started":"2022-04-29T12:44:27.828720Z","shell.execute_reply":"2022-04-29T12:44:27.873591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train['label'].astype('float32')\ntrain_x = train.drop(['label'],axis = 1).astype('int32')\ntest_x = test.astype('float32')\ntrain_x.shape, train_y.shape, test_x.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:27.875892Z","iopub.execute_input":"2022-04-29T12:44:27.876154Z","iopub.status.idle":"2022-04-29T12:44:28.048252Z","shell.execute_reply.started":"2022-04-29T12:44:27.876121Z","shell.execute_reply":"2022-04-29T12:44:28.047538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.values.reshape(-1,28,28,1)\ntrain_x = train_x / 255.0\ntest_x = test_x.values.reshape(-1,28,28,1)\ntest_x = test_x / 255.0\ntrain_x.shape, test_x.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:28.049318Z","iopub.execute_input":"2022-04-29T12:44:28.049717Z","iopub.status.idle":"2022-04-29T12:44:28.169129Z","shell.execute_reply.started":"2022-04-29T12:44:28.049681Z","shell.execute_reply":"2022-04-29T12:44:28.168298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#One Hot Encoding\ntrain_y = tf.keras.utils.to_categorical(train_y,10)\ntrain_y.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:28.171875Z","iopub.execute_input":"2022-04-29T12:44:28.172209Z","iopub.status.idle":"2022-04-29T12:44:29.218400Z","shell.execute_reply.started":"2022-04-29T12:44:28.172171Z","shell.execute_reply":"2022-04-29T12:44:29.217754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Printing original labels of top 5 rows\nprint(train['label'].head())","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:29.221852Z","iopub.execute_input":"2022-04-29T12:44:29.224048Z","iopub.status.idle":"2022-04-29T12:44:29.236674Z","shell.execute_reply.started":"2022-04-29T12:44:29.222526Z","shell.execute_reply":"2022-04-29T12:44:29.236039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#One hot encoding of the same labels\nprint(train_y[0:5,:])","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:29.237677Z","iopub.execute_input":"2022-04-29T12:44:29.238275Z","iopub.status.idle":"2022-04-29T12:44:29.254786Z","shell.execute_reply.started":"2022-04-29T12:44:29.238241Z","shell.execute_reply":"2022-04-29T12:44:29.253836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the model\nmodel = tf.keras.models.Sequential([\n  tf.keras.layers.Conv2D(32,(3,3),activation = 'relu', input_shape=(28,28,1)),\n  tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n  tf.keras.layers.Dropout(0.25),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n  tf.keras.layers.Dropout(0.25),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(256, activation='relu'),\n  #tf.keras.layers.Dropout(0.50),\n  tf.keras.layers.Dense(256, activation='relu'),\n  tf.keras.layers.Dropout(0.50),\n  tf.keras.layers.Dense(10, activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:29.257158Z","iopub.execute_input":"2022-04-29T12:44:29.259227Z","iopub.status.idle":"2022-04-29T12:44:31.464374Z","shell.execute_reply.started":"2022-04-29T12:44:29.257376Z","shell.execute_reply":"2022-04-29T12:44:31.463661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the callback function to stop our training once the acceptable accuracy is reached\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self, epoch, logs={}):\n            if(logs.get('accuracy') > 0.999):\n                print(\"\\nReached 99.9% accuracy so cancelling training!\")\n                self.model.stop_training = True\n    \ncallbacks = myCallback()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:31.465685Z","iopub.execute_input":"2022-04-29T12:44:31.466210Z","iopub.status.idle":"2022-04-29T12:44:31.473084Z","shell.execute_reply.started":"2022-04-29T12:44:31.466172Z","shell.execute_reply":"2022-04-29T12:44:31.471512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling and model training with batch size = 50, epochs = 20, and optimizer = adam\nOptimizer = tf.keras.optimizers.Adam(\n            learning_rate=0.0005, \n            beta_1=0.9, \n            beta_2=0.999, \n            epsilon=1e-07,\n            name='Adam'\n)\nmodel.compile(optimizer=Optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(train_x, train_y, batch_size = 20, epochs = 50, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:44:31.474326Z","iopub.execute_input":"2022-04-29T12:44:31.475060Z","iopub.status.idle":"2022-04-29T12:52:54.441671Z","shell.execute_reply.started":"2022-04-29T12:44:31.475018Z","shell.execute_reply":"2022-04-29T12:52:54.440911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test_x)\n\n# select the index with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:52:54.443112Z","iopub.execute_input":"2022-04-29T12:52:54.443362Z","iopub.status.idle":"2022-04-29T12:52:57.311958Z","shell.execute_reply.started":"2022-04-29T12:52:54.443328Z","shell.execute_reply":"2022-04-29T12:52:57.310985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:52:57.313447Z","iopub.execute_input":"2022-04-29T12:52:57.313702Z","iopub.status.idle":"2022-04-29T12:52:57.369959Z","shell.execute_reply.started":"2022-04-29T12:52:57.313666Z","shell.execute_reply":"2022-04-29T12:52:57.369197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Submitting the results on Kaggle\n!kaggle competitions submit -c digit-recognizer -f submission.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:52:57.371289Z","iopub.execute_input":"2022-04-29T12:52:57.371682Z","iopub.status.idle":"2022-04-29T12:52:58.667980Z","shell.execute_reply.started":"2022-04-29T12:52:57.371647Z","shell.execute_reply":"2022-04-29T12:52:58.667119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\nimport base64\n\n# function that takes in a dataframe and creates a text link to  \n# download it (will only work for files < 2MB or so)\ndef create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"):  \n    csv = df.to_csv()\n    b64 = base64.b64encode(csv.encode())\n    payload = b64.decode()\n    html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n    html = html.format(payload=payload,title=title,filename=filename)\n    return HTML(html)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:52:58.671634Z","iopub.execute_input":"2022-04-29T12:52:58.671877Z","iopub.status.idle":"2022-04-29T12:52:58.677521Z","shell.execute_reply.started":"2022-04-29T12:52:58.671848Z","shell.execute_reply":"2022-04-29T12:52:58.676809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_download_link(submission)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T12:52:58.678811Z","iopub.execute_input":"2022-04-29T12:52:58.679355Z","iopub.status.idle":"2022-04-29T12:52:58.761399Z","shell.execute_reply.started":"2022-04-29T12:52:58.679318Z","shell.execute_reply":"2022-04-29T12:52:58.759076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}