{"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-03-05T10:55:37.127735Z","iopub.execute_input":"2023-03-05T10:55:37.128208Z","iopub.status.idle":"2023-03-05T10:55:37.141798Z","shell.execute_reply.started":"2023-03-05T10:55:37.128174Z","shell.execute_reply":"2023-03-05T10:55:37.140343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport tensorflow as tf\ndata = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata.head()\nlabels = data.iloc[:,0]\npxlvals = data.iloc[:,1:]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:55:37.144440Z","iopub.execute_input":"2023-03-05T10:55:37.144817Z","iopub.status.idle":"2023-03-05T10:55:38.677104Z","shell.execute_reply.started":"2023-03-05T10:55:37.144781Z","shell.execute_reply":"2023-03-05T10:55:38.675516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain, xval, labelTrain, labelVal = train_test_split(pxlvals, labels, test_size = 0.3)\nxtrain = xtrain/255\nxval = xval/255","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:55:38.678378Z","iopub.execute_input":"2023-03-05T10:55:38.678778Z","iopub.status.idle":"2023-03-05T10:55:38.923815Z","shell.execute_reply.started":"2023-03-05T10:55:38.678737Z","shell.execute_reply":"2023-03-05T10:55:38.923078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([tf.keras.layers.Flatten(),\n                            tf.keras.layers.Dense(512, activation = tf.nn.relu),\n                            tf.keras.layers.Dense(256, activation = tf.nn.relu),\n                            tf.keras.layers.Dense(10, activation = tf.nn.softmax)])\n\nmodel.compile(optimizer = 'RMSprop',\n             loss = 'sparse_categorical_crossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:55:38.924591Z","iopub.execute_input":"2023-03-05T10:55:38.924821Z","iopub.status.idle":"2023-03-05T10:55:38.942700Z","shell.execute_reply.started":"2023-03-05T10:55:38.924796Z","shell.execute_reply":"2023-03-05T10:55:38.941013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(xtrain, labelTrain, epochs = 10)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:55:38.945139Z","iopub.execute_input":"2023-03-05T10:55:38.945455Z","iopub.status.idle":"2023-03-05T10:56:27.968241Z","shell.execute_reply.started":"2023-03-05T10:55:38.945425Z","shell.execute_reply":"2023-03-05T10:56:27.967281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(xval, labelVal)\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\npreds = np.argmax(model.predict(test_data.to_numpy()), axis = -1)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:56:27.969588Z","iopub.execute_input":"2023-03-05T10:56:27.970112Z","iopub.status.idle":"2023-03-05T10:56:32.886442Z","shell.execute_reply.started":"2023-03-05T10:56:27.970080Z","shell.execute_reply":"2023-03-05T10:56:32.884894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':pd.Series(list(range(1, len(preds)+1))),\n                           'Label':pd.Series(preds)})\nsubmission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T10:56:32.888422Z","iopub.execute_input":"2023-03-05T10:56:32.888793Z","iopub.status.idle":"2023-03-05T10:56:32.928352Z","shell.execute_reply.started":"2023-03-05T10:56:32.888755Z","shell.execute_reply":"2023-03-05T10:56:32.926902Z"},"trusted":true},"execution_count":null,"outputs":[]}]}