{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\n\nimport matplotlib.pyplot as plt\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":"2024-03-11T14:44:33.477368Z","iopub.execute_input":"2024-03-11T14:44:33.477969Z","iopub.status.idle":"2024-03-11T14:44:33.497890Z","shell.execute_reply.started":"2024-03-11T14:44:33.477926Z","shell.execute_reply":"2024-03-11T14:44:33.496440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Each image is 28x28px\ndf = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\n# Convert values to range 0 - 1","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:33.500252Z","iopub.execute_input":"2024-03-11T14:44:33.500769Z","iopub.status.idle":"2024-03-11T14:44:36.252870Z","shell.execute_reply.started":"2024-03-11T14:44:33.500725Z","shell.execute_reply":"2024-03-11T14:44:36.251634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['label'].to_frame()\ny.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:36.254818Z","iopub.execute_input":"2024-03-11T14:44:36.255283Z","iopub.status.idle":"2024-03-11T14:44:36.266068Z","shell.execute_reply.started":"2024-03-11T14:44:36.255241Z","shell.execute_reply":"2024-03-11T14:44:36.264950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.drop('label', axis=1).values / 255\nX = X.reshape((42000, 28, 28))\nX.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:36.268613Z","iopub.execute_input":"2024-03-11T14:44:36.268998Z","iopub.status.idle":"2024-03-11T14:44:36.696589Z","shell.execute_reply.started":"2024-03-11T14:44:36.268964Z","shell.execute_reply":"2024-03-11T14:44:36.695454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential(\n    [\n        #Convolutional layer creating 32 filters, with a shape of 3x3\n        Conv2D(32, (3,3), activation=\"relu\", input_shape=(28,28,1)),\n        \n        #Pooling layer using a 2x2 pool size\n        MaxPooling2D(pool_size=(2,2)),\n        \n        Flatten(),\n        \n        \n        #Hidden Layers\n        Dense(units=20, activation='relu'),\n        Dense(units=15, activation='relu'),\n        Dropout(0.5),\n        \n        #Output Layer\n        Dense(units=10, activation='linear'),     \n        \n    ], name=\"digit_recogniser\"\n)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:53.967321Z","iopub.execute_input":"2024-03-11T14:44:53.967810Z","iopub.status.idle":"2024-03-11T14:44:54.074698Z","shell.execute_reply.started":"2024-03-11T14:44:53.967767Z","shell.execute_reply":"2024-03-11T14:44:54.073669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:56.443502Z","iopub.execute_input":"2024-03-11T14:44:56.443981Z","iopub.status.idle":"2024-03-11T14:44:56.457612Z","shell.execute_reply.started":"2024-03-11T14:44:56.443945Z","shell.execute_reply":"2024-03-11T14:44:56.456582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X,y,epochs=10\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:58.151766Z","iopub.execute_input":"2024-03-11T14:44:58.152875Z","iopub.status.idle":"2024-03-11T14:47:08.707551Z","shell.execute_reply.started":"2024-03-11T14:44:58.152832Z","shell.execute_reply":"2024-03-11T14:47:08.706678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df = test_df / 255\ntest_df = test_df.values.reshape(len(test_df),28,28)\np = model.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:48:22.777107Z","iopub.execute_input":"2024-03-11T14:48:22.777713Z","iopub.status.idle":"2024-03-11T14:48:29.989660Z","shell.execute_reply.started":"2024-03-11T14:48:22.777666Z","shell.execute_reply":"2024-03-11T14:48:29.988299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_df = pd.DataFrame(p)\npredictions = p_df.idxmax(axis=1)\nres = pd.DataFrame({\n    \"ImageId\": range(1, len(predictions) + 1),\n    \"Label\": predictions\n})\n\nres.head(4)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:48:32.294626Z","iopub.execute_input":"2024-03-11T14:48:32.295093Z","iopub.status.idle":"2024-03-11T14:48:32.315068Z","shell.execute_reply.started":"2024-03-11T14:48:32.295057Z","shell.execute_reply":"2024-03-11T14:48:32.314020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T14:44:36.716395Z","iopub.status.idle":"2024-03-11T14:44:36.716817Z","shell.execute_reply.started":"2024-03-11T14:44:36.716628Z","shell.execute_reply":"2024-03-11T14:44:36.716645Z"},"trusted":true},"execution_count":null,"outputs":[]}]}