{"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-06-28T20:16:21.341409Z","iopub.execute_input":"2022-06-28T20:16:21.341799Z","iopub.status.idle":"2022-06-28T20:16:21.354898Z","shell.execute_reply.started":"2022-06-28T20:16:21.341770Z","shell.execute_reply":"2022-06-28T20:16:21.353783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:17:05.218973Z","iopub.execute_input":"2022-06-28T20:17:05.219354Z","iopub.status.idle":"2022-06-28T20:17:07.933591Z","shell.execute_reply.started":"2022-06-28T20:17:05.219323Z","shell.execute_reply":"2022-06-28T20:17:07.932416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:17:30.059391Z","iopub.execute_input":"2022-06-28T20:17:30.059801Z","iopub.status.idle":"2022-06-28T20:17:31.747915Z","shell.execute_reply.started":"2022-06-28T20:17:30.059768Z","shell.execute_reply":"2022-06-28T20:17:31.747213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:18:02.982008Z","iopub.execute_input":"2022-06-28T20:18:02.982464Z","iopub.status.idle":"2022-06-28T20:18:02.987929Z","shell.execute_reply.started":"2022-06-28T20:18:02.982430Z","shell.execute_reply":"2022-06-28T20:18:02.987011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.drop('label', inplace=True, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:18:15.752751Z","iopub.execute_input":"2022-06-28T20:18:15.753151Z","iopub.status.idle":"2022-06-28T20:18:15.881588Z","shell.execute_reply.started":"2022-06-28T20:18:15.753118Z","shell.execute_reply":"2022-06-28T20:18:15.880442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:19:07.839596Z","iopub.execute_input":"2022-06-28T20:19:07.840298Z","iopub.status.idle":"2022-06-28T20:19:07.846799Z","shell.execute_reply.started":"2022-06-28T20:19:07.840255Z","shell.execute_reply":"2022-06-28T20:19:07.845753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:19:43.090469Z","iopub.execute_input":"2022-06-28T20:19:43.090935Z","iopub.status.idle":"2022-06-28T20:19:43.098061Z","shell.execute_reply.started":"2022-06-28T20:19:43.090897Z","shell.execute_reply":"2022-06-28T20:19:43.096881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:20:42.191719Z","iopub.execute_input":"2022-06-28T20:20:42.192178Z","iopub.status.idle":"2022-06-28T20:20:42.198748Z","shell.execute_reply.started":"2022-06-28T20:20:42.192141Z","shell.execute_reply":"2022-06-28T20:20:42.198013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\nmodel = keras.Sequential([\n    layers.Dense(512, activation=\"relu\"),\n    layers.Dense(10, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:21:27.272498Z","iopub.execute_input":"2022-06-28T20:21:27.272929Z","iopub.status.idle":"2022-06-28T20:21:34.502184Z","shell.execute_reply.started":"2022-06-28T20:21:27.272895Z","shell.execute_reply":"2022-06-28T20:21:34.501400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"rmsprop\",\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:22:12.963003Z","iopub.execute_input":"2022-06-28T20:22:12.963738Z","iopub.status.idle":"2022-06-28T20:22:12.992281Z","shell.execute_reply.started":"2022-06-28T20:22:12.963697Z","shell.execute_reply":"2022-06-28T20:22:12.991179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.astype(\"float32\") / 255\ntest_data = test_data.astype(\"float32\") / 255","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:23:43.907847Z","iopub.execute_input":"2022-06-28T20:23:43.908264Z","iopub.status.idle":"2022-06-28T20:23:44.120162Z","shell.execute_reply.started":"2022-06-28T20:23:43.908230Z","shell.execute_reply":"2022-06-28T20:23:44.118983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_data, y_train, epochs=5, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:24:31.527723Z","iopub.execute_input":"2022-06-28T20:24:31.528190Z","iopub.status.idle":"2022-06-28T20:24:52.932441Z","shell.execute_reply.started":"2022-06-28T20:24:31.528154Z","shell.execute_reply":"2022-06-28T20:24:52.931327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_digits = test_data[0:10]\npredictions = model.predict(test_digits)\npredictions[0]","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:26:50.711643Z","iopub.execute_input":"2022-06-28T20:26:50.712098Z","iopub.status.idle":"2022-06-28T20:26:50.869515Z","shell.execute_reply.started":"2022-06-28T20:26:50.712057Z","shell.execute_reply":"2022-06-28T20:26:50.868345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0].argmax()","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:27:06.923204Z","iopub.execute_input":"2022-06-28T20:27:06.923612Z","iopub.status.idle":"2022-06-28T20:27:06.930426Z","shell.execute_reply.started":"2022-06-28T20:27:06.923578Z","shell.execute_reply":"2022-06-28T20:27:06.929515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions  = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:31:29.399907Z","iopub.execute_input":"2022-06-28T20:31:29.400372Z","iopub.status.idle":"2022-06-28T20:31:31.044060Z","shell.execute_reply.started":"2022-06-28T20:31:29.400335Z","shell.execute_reply":"2022-06-28T20:31:31.042997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:31:41.542476Z","iopub.execute_input":"2022-06-28T20:31:41.542863Z","iopub.status.idle":"2022-06-28T20:31:41.550717Z","shell.execute_reply.started":"2022-06-28T20:31:41.542831Z","shell.execute_reply":"2022-06-28T20:31:41.549646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:33:06.273929Z","iopub.execute_input":"2022-06-28T20:33:06.274390Z","iopub.status.idle":"2022-06-28T20:33:06.281469Z","shell.execute_reply.started":"2022-06-28T20:33:06.274346Z","shell.execute_reply":"2022-06-28T20:33:06.280483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Id = []\nsub_pred = []\ncount=1\nfor pred in final_predictions:\n    Id.append(count)\n    sub_pred.append(np.argmax(pred))\n    count+=1","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:39:00.074483Z","iopub.execute_input":"2022-06-28T20:39:00.074887Z","iopub.status.idle":"2022-06-28T20:39:00.159872Z","shell.execute_reply.started":"2022-06-28T20:39:00.074854Z","shell.execute_reply":"2022-06-28T20:39:00.159065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_pred","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:40:03.473556Z","iopub.execute_input":"2022-06-28T20:40:03.474054Z","iopub.status.idle":"2022-06-28T20:40:03.504491Z","shell.execute_reply.started":"2022-06-28T20:40:03.474018Z","shell.execute_reply":"2022-06-28T20:40:03.503527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': Id, 'Label': sub_pred})\noutput.to_csv('final_predictions.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-06-28T20:42:58.374179Z","iopub.execute_input":"2022-06-28T20:42:58.374577Z","iopub.status.idle":"2022-06-28T20:42:58.460368Z","shell.execute_reply.started":"2022-06-28T20:42:58.374547Z","shell.execute_reply":"2022-06-28T20:42:58.459307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}