{"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":"2022-03-01T11:10:39.371557Z","iopub.execute_input":"2022-03-01T11:10:39.371993Z","iopub.status.idle":"2022-03-01T11:10:39.381074Z","shell.execute_reply.started":"2022-03-01T11:10:39.371951Z","shell.execute_reply":"2022-03-01T11:10:39.380066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np \nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf \nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom tensorflow import keras\nfrom keras.utils.np_utils import to_categorical \nfrom keras.utils.vis_utils import plot_model","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:10:39.382939Z","iopub.execute_input":"2022-03-01T11:10:39.383379Z","iopub.status.idle":"2022-03-01T11:10:39.389481Z","shell.execute_reply.started":"2022-03-01T11:10:39.383342Z","shell.execute_reply":"2022-03-01T11:10:39.388692Z"},"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-03-01T11:10:39.39077Z","iopub.execute_input":"2022-03-01T11:10:39.39114Z","iopub.status.idle":"2022-03-01T11:10:43.100558Z","shell.execute_reply.started":"2022-03-01T11:10:39.391104Z","shell.execute_reply":"2022-03-01T11:10:43.099835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:10:43.102015Z","iopub.execute_input":"2022-03-01T11:10:43.102268Z","iopub.status.idle":"2022-03-01T11:10:43.124403Z","shell.execute_reply.started":"2022-03-01T11:10:43.102234Z","shell.execute_reply":"2022-03-01T11:10:43.123627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop([\"label\"],axis=1).astype('int32')\ny_train = train[\"label\"].astype('float32')\nX_test = test.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:10:43.126675Z","iopub.execute_input":"2022-03-01T11:10:43.126953Z","iopub.status.idle":"2022-03-01T11:10:43.354913Z","shell.execute_reply.started":"2022-03-01T11:10:43.12691Z","shell.execute_reply":"2022-03-01T11:10:43.353827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\nX_train = X_train/255.0\nX_test = X_test.values.reshape(-1,28,28,1)\nX_test = X_test/255.0\ny_train = to_categorical(y_train,10)\nX_train, X_val, y_train, y_val= train_test_split(X_train, y_train, test_size=0.2, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:10:43.35971Z","iopub.execute_input":"2022-03-01T11:10:43.361649Z","iopub.status.idle":"2022-03-01T11:10:43.933366Z","shell.execute_reply.started":"2022-03-01T11:10:43.36161Z","shell.execute_reply":"2022-03-01T11:10:43.932569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32,(5,5),activation = 'relu', padding = 'same', input_shape=(28,28,1)),\n    tf.keras.layers.Conv2D(32,(5,5),activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.5),\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(2,2),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1024, activation = 'relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n])\nmodel.summary()\nplot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:10:43.934507Z","iopub.execute_input":"2022-03-01T11:10:43.936333Z","iopub.status.idle":"2022-03-01T11:10:44.188024Z","shell.execute_reply.started":"2022-03-01T11:10:43.936291Z","shell.execute_reply":"2022-03-01T11:10:44.186051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nhistory = model.fit(X_train, y_train, epochs = 100, batch_size = 120, validation_data=(X_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:13:49.065736Z","iopub.execute_input":"2022-03-01T11:13:49.066429Z","iopub.status.idle":"2022-03-01T11:17:13.028254Z","shell.execute_reply.started":"2022-03-01T11:13:49.066392Z","shell.execute_reply":"2022-03-01T11:17:13.027541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_training = history.history['loss']\nloss_validation = history.history['val_loss']\nepochs = range(1,101)\nplt.plot(epochs, loss_training, label='Training loss')\nplt.plot(epochs, loss_validation, label='Validation loss')\nplt.legend()\nplt.title('Training and Vaidation loss vs Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:19:29.308555Z","iopub.execute_input":"2022-03-01T11:19:29.308845Z","iopub.status.idle":"2022-03-01T11:19:29.507542Z","shell.execute_reply.started":"2022-03-01T11:19:29.308808Z","shell.execute_reply":"2022-03-01T11:19:29.50686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_training = history.history['accuracy']\naccuracy_validation = history.history['val_accuracy']\nplt.plot(epochs, accuracy_training, label='Training Accuracy')\nplt.plot(epochs, accuracy_validation, label='Validation Accuracy')\nplt.legend()\nplt.title('Training and Vaidation Accuracy vs Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:25:53.932368Z","iopub.execute_input":"2022-03-01T11:25:53.932624Z","iopub.status.idle":"2022-03-01T11:25:54.123355Z","shell.execute_reply.started":"2022-03-01T11:25:53.932597Z","shell.execute_reply":"2022-03-01T11:25:54.12264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test) #make predictions on test images\nprint(predictions.shape) #This will be 28K x 10. For each test image, probability of 10 classes\n\npredictions = np.argmax(predictions,axis=1) \nprint(predictions.shape) ","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:26:07.6224Z","iopub.execute_input":"2022-03-01T11:26:07.622658Z","iopub.status.idle":"2022-03-01T11:26:08.989243Z","shell.execute_reply.started":"2022-03-01T11:26:07.62263Z","shell.execute_reply":"2022-03-01T11:26:08.988461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\n\n#Overwrite labels in dataframe sub\nsub[\"Label\"] = predictions\n\n#Write updated dataframes as submission.csv\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T11:26:11.06759Z","iopub.execute_input":"2022-03-01T11:26:11.067869Z","iopub.status.idle":"2022-03-01T11:26:11.128447Z","shell.execute_reply.started":"2022-03-01T11:26:11.067839Z","shell.execute_reply":"2022-03-01T11:26:11.127707Z"},"trusted":true},"execution_count":null,"outputs":[]}]}