{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tensorflow.keras.datasets import mnist\n(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-17T19:36:48.478473Z","iopub.execute_input":"2023-04-17T19:36:48.478967Z","iopub.status.idle":"2023-04-17T19:36:48.788492Z","shell.execute_reply.started":"2023-04-17T19:36:48.478919Z","shell.execute_reply":"2023-04-17T19:36:48.787185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'train_images shape: {train_images.shape}')\nprint(f'test_images shape: {test_images.shape}')\nprint(f'label values: {train_labels}')","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:36:48.790733Z","iopub.execute_input":"2023-04-17T19:36:48.791213Z","iopub.status.idle":"2023-04-17T19:36:48.797375Z","shell.execute_reply.started":"2023-04-17T19:36:48.791176Z","shell.execute_reply":"2023-04-17T19:36:48.795982Z"},"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='relu')\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:36:48.798843Z","iopub.execute_input":"2023-04-17T19:36:48.799722Z","iopub.status.idle":"2023-04-17T19:36:48.818347Z","shell.execute_reply.started":"2023-04-17T19:36:48.799682Z","shell.execute_reply":"2023-04-17T19:36:48.817157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\",\n loss=\"sparse_categorical_crossentropy\",\n metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:36:48.821088Z","iopub.execute_input":"2023-04-17T19:36:48.821573Z","iopub.status.idle":"2023-04-17T19:36:48.835600Z","shell.execute_reply.started":"2023-04-17T19:36:48.821533Z","shell.execute_reply":"2023-04-17T19:36:48.834423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_images.reshape((60000, 28 * 28))\ntrain_images = train_images.astype(\"float32\") / 255\ntest_images = test_images.reshape((10000, 28 * 28))\ntest_images = test_images.astype(\"float32\") / 255","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:36:48.837054Z","iopub.execute_input":"2023-04-17T19:36:48.837966Z","iopub.status.idle":"2023-04-17T19:36:48.922382Z","shell.execute_reply.started":"2023-04-17T19:36:48.837924Z","shell.execute_reply":"2023-04-17T19:36:48.920939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_images, train_labels, epochs=10, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:36:48.923839Z","iopub.execute_input":"2023-04-17T19:36:48.924241Z","iopub.status.idle":"2023-04-17T19:37:24.648801Z","shell.execute_reply.started":"2023-04-17T19:36:48.924204Z","shell.execute_reply":"2023-04-17T19:37:24.647709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_digits = test_images[0:10]\npredictions = model.predict(test_digits)\npredictions[0]","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:24.650181Z","iopub.execute_input":"2023-04-17T19:37:24.650526Z","iopub.status.idle":"2023-04-17T19:37:24.771340Z","shell.execute_reply.started":"2023-04-17T19:37:24.650492Z","shell.execute_reply":"2023-04-17T19:37:24.770320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0].argmax()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:24.772494Z","iopub.execute_input":"2023-04-17T19:37:24.772827Z","iopub.status.idle":"2023-04-17T19:37:24.781116Z","shell.execute_reply.started":"2023-04-17T19:37:24.772797Z","shell.execute_reply":"2023-04-17T19:37:24.779860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(test_images, test_labels)\nprint(f\"test_acc: {test_acc}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:24.783009Z","iopub.execute_input":"2023-04-17T19:37:24.783464Z","iopub.status.idle":"2023-04-17T19:37:26.251596Z","shell.execute_reply.started":"2023-04-17T19:37:24.783414Z","shell.execute_reply":"2023-04-17T19:37:26.250555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:26.255140Z","iopub.execute_input":"2023-04-17T19:37:26.256243Z","iopub.status.idle":"2023-04-17T19:37:27.909929Z","shell.execute_reply.started":"2023-04-17T19:37:26.256199Z","shell.execute_reply":"2023-04-17T19:37:27.908494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.astype(\"float32\")\ntest = test.reshape((28000, 28 * 28))\ntest","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:27.911344Z","iopub.execute_input":"2023-04-17T19:37:27.911693Z","iopub.status.idle":"2023-04-17T19:37:27.973169Z","shell.execute_reply.started":"2023-04-17T19:37:27.911661Z","shell.execute_reply":"2023-04-17T19:37:27.971747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:27.974737Z","iopub.execute_input":"2023-04-17T19:37:27.975240Z","iopub.status.idle":"2023-04-17T19:37:30.616146Z","shell.execute_reply.started":"2023-04-17T19:37:27.975177Z","shell.execute_reply":"2023-04-17T19:37:30.614700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0].argmax()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:30.617637Z","iopub.execute_input":"2023-04-17T19:37:30.618057Z","iopub.status.idle":"2023-04-17T19:37:30.625694Z","shell.execute_reply.started":"2023-04-17T19:37:30.618018Z","shell.execute_reply":"2023-04-17T19:37:30.624432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = []\nLabel = []\nfor i in range(len(predictions)):\n    ImageId.append(i+1)\n    Label.append(predictions[i].argmax())\n    \nsubmissions=pd.DataFrame({\"ImageId\": ImageId,\n                         \"Label\": Label})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:30.627499Z","iopub.execute_input":"2023-04-17T19:37:30.628008Z","iopub.status.idle":"2023-04-17T19:37:30.708251Z","shell.execute_reply.started":"2023-04-17T19:37:30.627958Z","shell.execute_reply":"2023-04-17T19:37:30.706872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/working/submission.csv')\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T19:37:30.709732Z","iopub.execute_input":"2023-04-17T19:37:30.710759Z","iopub.status.idle":"2023-04-17T19:37:30.727691Z","shell.execute_reply.started":"2023-04-17T19:37:30.710717Z","shell.execute_reply":"2023-04-17T19:37:30.726346Z"},"trusted":true},"execution_count":null,"outputs":[]}]}