{"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-09-03T08:54:40.623267Z","iopub.execute_input":"2021-09-03T08:54:40.623701Z","iopub.status.idle":"2021-09-03T08:54:40.636194Z","shell.execute_reply.started":"2021-09-03T08:54:40.623609Z","shell.execute_reply":"2021-09-03T08:54:40.635412Z"},"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":"2021-09-03T08:54:40.637533Z","iopub.execute_input":"2021-09-03T08:54:40.638031Z","iopub.status.idle":"2021-09-03T08:54:46.553247Z","shell.execute_reply.started":"2021-09-03T08:54:40.637987Z","shell.execute_reply":"2021-09-03T08:54:46.552265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:46.554780Z","iopub.execute_input":"2021-09-03T08:54:46.555085Z","iopub.status.idle":"2021-09-03T08:54:46.560346Z","shell.execute_reply.started":"2021-09-03T08:54:46.555057Z","shell.execute_reply":"2021-09-03T08:54:46.559315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = train['label']\ntrain = train.drop('label' , axis =1)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:46.561866Z","iopub.execute_input":"2021-09-03T08:54:46.562254Z","iopub.status.idle":"2021-09-03T08:54:46.688591Z","shell.execute_reply.started":"2021-09-03T08:54:46.562216Z","shell.execute_reply":"2021-09-03T08:54:46.687552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(label.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:46.689974Z","iopub.execute_input":"2021-09-03T08:54:46.690381Z","iopub.status.idle":"2021-09-03T08:54:46.695430Z","shell.execute_reply.started":"2021-09-03T08:54:46.690342Z","shell.execute_reply":"2021-09-03T08:54:46.694417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train / 255\ntest = test / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:46.697042Z","iopub.execute_input":"2021-09-03T08:54:46.697362Z","iopub.status.idle":"2021-09-03T08:54:46.854663Z","shell.execute_reply.started":"2021-09-03T08:54:46.697333Z","shell.execute_reply":"2021-09-03T08:54:46.853657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nmodel = keras.models.Sequential()\nmodel.add(keras.layers.InputLayer(input_shape=[None,784]))\nmodel.add(keras.layers.Dense(1000, activation=\"relu\"))\nmodel.add(keras.layers.Dense(500, activation=\"relu\"))\nmodel.add(keras.layers.Dense(100, activation=\"relu\"))\nmodel.add(keras.layers.Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:46.856070Z","iopub.execute_input":"2021-09-03T08:54:46.856370Z","iopub.status.idle":"2021-09-03T08:54:53.186673Z","shell.execute_reply.started":"2021-09-03T08:54:46.856340Z","shell.execute_reply":"2021-09-03T08:54:53.185517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optim = keras.optimizers.SGD(learning_rate=0.1)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:53.189458Z","iopub.execute_input":"2021-09-03T08:54:53.189834Z","iopub.status.idle":"2021-09-03T08:54:53.194940Z","shell.execute_reply.started":"2021-09-03T08:54:53.189778Z","shell.execute_reply":"2021-09-03T08:54:53.193844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=optim, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:53.196511Z","iopub.execute_input":"2021-09-03T08:54:53.196908Z","iopub.status.idle":"2021-09-03T08:54:53.223484Z","shell.execute_reply.started":"2021-09-03T08:54:53.196864Z","shell.execute_reply":"2021-09-03T08:54:53.222246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early = keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:53.225128Z","iopub.execute_input":"2021-09-03T08:54:53.225470Z","iopub.status.idle":"2021-09-03T08:54:53.230634Z","shell.execute_reply.started":"2021-09-03T08:54:53.225432Z","shell.execute_reply":"2021-09-03T08:54:53.229545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train, label, epochs=200, validation_split=0.2, callbacks=[early])","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:54:53.232227Z","iopub.execute_input":"2021-09-03T08:54:53.232554Z","iopub.status.idle":"2021-09-03T08:57:14.924730Z","shell.execute_reply.started":"2021-09-03T08:54:53.232521Z","shell.execute_reply":"2021-09-03T08:57:14.924002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict_classes(test)\npred","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:57:14.925775Z","iopub.execute_input":"2021-09-03T08:57:14.926194Z","iopub.status.idle":"2021-09-03T08:57:17.598749Z","shell.execute_reply.started":"2021-09-03T08:57:14.926164Z","shell.execute_reply":"2021-09-03T08:57:17.597697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageID' : np.arange(1,28001),\n    'Label' : pred\n})","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:57:17.599865Z","iopub.execute_input":"2021-09-03T08:57:17.600178Z","iopub.status.idle":"2021-09-03T08:57:17.607633Z","shell.execute_reply.started":"2021-09-03T08:57:17.600141Z","shell.execute_reply":"2021-09-03T08:57:17.606774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:57:17.610623Z","iopub.execute_input":"2021-09-03T08:57:17.611012Z","iopub.status.idle":"2021-09-03T08:57:17.627058Z","shell.execute_reply.started":"2021-09-03T08:57:17.610980Z","shell.execute_reply":"2021-09-03T08:57:17.625999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('new_output1.csv' , index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T08:57:17.628523Z","iopub.execute_input":"2021-09-03T08:57:17.629048Z","iopub.status.idle":"2021-09-03T08:57:17.692112Z","shell.execute_reply.started":"2021-09-03T08:57:17.629005Z","shell.execute_reply":"2021-09-03T08:57:17.691126Z"},"trusted":true},"execution_count":null,"outputs":[]}]}