{"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-03T09:01:30.970363Z","iopub.execute_input":"2021-09-03T09:01:30.971006Z","iopub.status.idle":"2021-09-03T09:01:30.987557Z","shell.execute_reply.started":"2021-09-03T09:01:30.970895Z","shell.execute_reply":"2021-09-03T09:01:30.986617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:06:42.793284Z","iopub.execute_input":"2021-09-03T09:06:42.793733Z","iopub.status.idle":"2021-09-03T09:06:48.881923Z","shell.execute_reply.started":"2021-09-03T09:06:42.793697Z","shell.execute_reply":"2021-09-03T09:06:48.880628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:07:04.081342Z","iopub.execute_input":"2021-09-03T09:07:04.081718Z","iopub.status.idle":"2021-09-03T09:07:04.087008Z","shell.execute_reply.started":"2021-09-03T09:07:04.081687Z","shell.execute_reply":"2021-09-03T09:07:04.085875Z"},"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-03T09:07:16.647306Z","iopub.execute_input":"2021-09-03T09:07:16.647699Z","iopub.status.idle":"2021-09-03T09:07:16.855776Z","shell.execute_reply.started":"2021-09-03T09:07:16.647668Z","shell.execute_reply":"2021-09-03T09:07:16.854659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(label.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:07:31.076116Z","iopub.execute_input":"2021-09-03T09:07:31.076560Z","iopub.status.idle":"2021-09-03T09:07:31.082546Z","shell.execute_reply.started":"2021-09-03T09:07:31.076520Z","shell.execute_reply":"2021-09-03T09:07:31.081527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train / 255\ntest = test / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:07:42.480408Z","iopub.execute_input":"2021-09-03T09:07:42.481096Z","iopub.status.idle":"2021-09-03T09:07:42.632700Z","shell.execute_reply.started":"2021-09-03T09:07:42.481045Z","shell.execute_reply":"2021-09-03T09:07:42.631946Z"},"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-03T09:07:54.225609Z","iopub.execute_input":"2021-09-03T09:07:54.226212Z","iopub.status.idle":"2021-09-03T09:08:00.892675Z","shell.execute_reply.started":"2021-09-03T09:07:54.226156Z","shell.execute_reply":"2021-09-03T09:08:00.891719Z"},"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-03T09:08:52.446370Z","iopub.execute_input":"2021-09-03T09:08:52.446747Z","iopub.status.idle":"2021-09-03T09:08:52.453546Z","shell.execute_reply.started":"2021-09-03T09:08:52.446717Z","shell.execute_reply":"2021-09-03T09:08:52.452048Z"},"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-03T09:09:17.568985Z","iopub.execute_input":"2021-09-03T09:09:17.569534Z","iopub.status.idle":"2021-09-03T09:09:17.591508Z","shell.execute_reply.started":"2021-09-03T09:09:17.569493Z","shell.execute_reply":"2021-09-03T09:09:17.589925Z"},"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-03T09:09:31.678471Z","iopub.execute_input":"2021-09-03T09:09:31.678908Z","iopub.status.idle":"2021-09-03T09:09:31.684889Z","shell.execute_reply.started":"2021-09-03T09:09:31.678872Z","shell.execute_reply":"2021-09-03T09:09:31.683407Z"},"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-03T09:09:48.569954Z","iopub.execute_input":"2021-09-03T09:09:48.570334Z","iopub.status.idle":"2021-09-03T09:12:49.576601Z","shell.execute_reply.started":"2021-09-03T09:09:48.570304Z","shell.execute_reply":"2021-09-03T09:12:49.575633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict_classes(test)\npred","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:13:41.813982Z","iopub.execute_input":"2021-09-03T09:13:41.814684Z","iopub.status.idle":"2021-09-03T09:13:44.779822Z","shell.execute_reply.started":"2021-09-03T09:13:41.814623Z","shell.execute_reply":"2021-09-03T09:13:44.778742Z"},"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-03T09:14:29.209849Z","iopub.execute_input":"2021-09-03T09:14:29.210254Z","iopub.status.idle":"2021-09-03T09:14:29.217635Z","shell.execute_reply.started":"2021-09-03T09:14:29.210220Z","shell.execute_reply":"2021-09-03T09:14:29.216522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-03T09:14:44.603052Z","iopub.execute_input":"2021-09-03T09:14:44.603421Z","iopub.status.idle":"2021-09-03T09:14:44.610279Z","shell.execute_reply.started":"2021-09-03T09:14:44.603389Z","shell.execute_reply":"2021-09-03T09:14:44.609046Z"},"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-03T09:15:05.440609Z","iopub.execute_input":"2021-09-03T09:15:05.441004Z","iopub.status.idle":"2021-09-03T09:15:05.502487Z","shell.execute_reply.started":"2021-09-03T09:15:05.440969Z","shell.execute_reply":"2021-09-03T09:15:05.501473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}