{"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":"2023-07-12T05:02:41.639393Z","iopub.execute_input":"2023-07-12T05:02:41.639848Z","iopub.status.idle":"2023-07-12T05:02:41.649406Z","shell.execute_reply.started":"2023-07-12T05:02:41.639811Z","shell.execute_reply":"2023-07-12T05:02:41.647889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:02:41.651288Z","iopub.execute_input":"2023-07-12T05:02:41.652132Z","iopub.status.idle":"2023-07-12T05:02:41.662189Z","shell.execute_reply.started":"2023-07-12T05:02:41.652080Z","shell.execute_reply":"2023-07-12T05:02:41.660892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **>Import Training Data**","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_y = data['label']\ntrain_X = data.iloc[:,1:] / 255\ntrain_X.describe()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:05:55.465274Z","iopub.execute_input":"2023-07-12T05:05:55.465817Z","iopub.status.idle":"2023-07-12T05:06:02.508217Z","shell.execute_reply.started":"2023-07-12T05:05:55.465764Z","shell.execute_reply":"2023-07-12T05:06:02.506892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.array(train_X.iloc[0]).reshape(28, 28), cmap = plt.cm.binary)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:09.030162Z","iopub.execute_input":"2023-07-12T05:06:09.030629Z","iopub.status.idle":"2023-07-12T05:06:09.273775Z","shell.execute_reply.started":"2023-07-12T05:06:09.030594Z","shell.execute_reply":"2023-07-12T05:06:09.272487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **>Modeling with Keras/TensorFlow**","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:03:22.768347Z","iopub.execute_input":"2023-07-12T05:03:22.769383Z","iopub.status.idle":"2023-07-12T05:03:22.776840Z","shell.execute_reply.started":"2023-07-12T05:03:22.769328Z","shell.execute_reply":"2023-07-12T05:03:22.775322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\nmodel.add(layers.Dense(512, activation = 'relu'))\nmodel.add(layers.Dense(10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:12.837019Z","iopub.execute_input":"2023-07-12T05:06:12.837519Z","iopub.status.idle":"2023-07-12T05:06:12.852143Z","shell.execute_reply.started":"2023-07-12T05:06:12.837482Z","shell.execute_reply":"2023-07-12T05:06:12.850905Z"},"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":"2023-07-12T05:06:14.758321Z","iopub.execute_input":"2023-07-12T05:06:14.758792Z","iopub.status.idle":"2023-07-12T05:06:14.774662Z","shell.execute_reply.started":"2023-07-12T05:06:14.758756Z","shell.execute_reply":"2023-07-12T05:06:14.773327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_X, train_y, batch_size=200, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:16.862219Z","iopub.execute_input":"2023-07-12T05:06:16.863378Z","iopub.status.idle":"2023-07-12T05:06:27.745738Z","shell.execute_reply.started":"2023-07-12T05:06:16.863329Z","shell.execute_reply":"2023-07-12T05:06:27.744624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **>Import Test Data**","metadata":{}},{"cell_type":"code","source":"data2 = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')/255\ndata2.describe()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:30.521096Z","iopub.execute_input":"2023-07-12T05:06:30.522609Z","iopub.status.idle":"2023-07-12T05:06:36.264780Z","shell.execute_reply.started":"2023-07-12T05:06:30.522553Z","shell.execute_reply":"2023-07-12T05:06:36.263382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **>Prediction**","metadata":{}},{"cell_type":"code","source":"predict = model.predict(data2)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:39.597251Z","iopub.execute_input":"2023-07-12T05:06:39.597779Z","iopub.status.idle":"2023-07-12T05:06:42.591567Z","shell.execute_reply.started":"2023-07-12T05:06:39.597738Z","shell.execute_reply":"2023-07-12T05:06:42.590546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"answer = np.array(predict)\n\nlabels = np.argmax(answer, axis = 1)\nlabels","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:06:56.278194Z","iopub.execute_input":"2023-07-12T05:06:56.278692Z","iopub.status.idle":"2023-07-12T05:06:56.289139Z","shell.execute_reply.started":"2023-07-12T05:06:56.278652Z","shell.execute_reply":"2023-07-12T05:06:56.287899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **>Make Output**","metadata":{}},{"cell_type":"code","source":"Final = pd.DataFrame()\nFinal['Label'] = labels\nFinal['ImageId'] = [i for i in range(1, 28001)]\nFinal = Final.set_index('ImageId')\nFinal","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:07:06.140814Z","iopub.execute_input":"2023-07-12T05:07:06.141928Z","iopub.status.idle":"2023-07-12T05:07:06.183027Z","shell.execute_reply.started":"2023-07-12T05:07:06.141874Z","shell.execute_reply":"2023-07-12T05:07:06.181675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final.to_csv('answer.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-12T05:07:14.575566Z","iopub.execute_input":"2023-07-12T05:07:14.576036Z","iopub.status.idle":"2023-07-12T05:07:14.659477Z","shell.execute_reply.started":"2023-07-12T05:07:14.576002Z","shell.execute_reply":"2023-07-12T05:07:14.658239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}