{"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-04-15T17:50:42.658036Z","iopub.execute_input":"2023-04-15T17:50:42.658628Z","iopub.status.idle":"2023-04-15T17:50:42.667454Z","shell.execute_reply.started":"2023-04-15T17:50:42.658589Z","shell.execute_reply":"2023-04-15T17:50:42.666237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:50:45.124669Z","iopub.execute_input":"2023-04-15T17:50:45.125356Z","iopub.status.idle":"2023-04-15T17:50:47.546674Z","shell.execute_reply.started":"2023-04-15T17:50:45.125316Z","shell.execute_reply":"2023-04-15T17:50:47.545681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = train_data.pop('label')","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:50:50.535458Z","iopub.execute_input":"2023-04-15T17:50:50.535864Z","iopub.status.idle":"2023-04-15T17:50:50.545636Z","shell.execute_reply.started":"2023-04-15T17:50:50.535826Z","shell.execute_reply":"2023-04-15T17:50:50.544606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:50:52.590235Z","iopub.execute_input":"2023-04-15T17:50:52.593073Z","iopub.status.idle":"2023-04-15T17:50:54.199845Z","shell.execute_reply.started":"2023-04-15T17:50:52.593022Z","shell.execute_reply":"2023-04-15T17:50:54.198753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\n\n# Separate out image and label data\ntrain_images = train_data.iloc[:, :].to_numpy().astype('float32')\ntrain_labels = train_labels.to_numpy().astype('int32')\ntest_images = test_data.to_numpy().astype('float32')\n\n# Normalize pixel values to be between 0 and 1\ntrain_images = train_images / 255.0\ntest_images = test_images / 255.0\n\n# Reshape image data to 28x28 grayscale images\ntrain_images = train_images.reshape((-1, 28, 28, 1))\ntest_images = test_images.reshape((-1, 28, 28, 1))","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:13.274507Z","iopub.execute_input":"2023-04-15T17:51:13.274918Z","iopub.status.idle":"2023-04-15T17:51:13.579457Z","shell.execute_reply.started":"2023-04-15T17:51:13.274882Z","shell.execute_reply":"2023-04-15T17:51:13.578153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\nmodel = keras.Sequential([\n    keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),\n    keras.layers.MaxPooling2D((2,2)),\n    keras.layers.Conv2D(64, (3,3), activation='relu'),\n    keras.layers.MaxPooling2D((2,2)),\n    keras.layers.Conv2D(128, (3,3), activation='relu'),\n    keras.layers.Flatten(),\n    keras.layers.Dense(128, activation='relu'),\n    keras.layers.Dropout(0.5),\n    keras.layers.Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Train the model\nmodel.fit(train_images, train_labels, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:01:03.256436Z","iopub.execute_input":"2023-04-15T18:01:03.257154Z","iopub.status.idle":"2023-04-15T18:02:26.426201Z","shell.execute_reply.started":"2023-04-15T18:01:03.257112Z","shell.execute_reply":"2023-04-15T18:02:26.425062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set\npredictions = model.predict(test_images)\npredictions = np.argmax(predictions, axis=1)\n\n# Save the predictions to a CSV file\nsubmission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission[\"Label\"] = predictions.reshape(-1, 1)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:03:28.772072Z","iopub.execute_input":"2023-04-15T18:03:28.772674Z","iopub.status.idle":"2023-04-15T18:03:31.604938Z","shell.execute_reply.started":"2023-04-15T18:03:28.772635Z","shell.execute_reply":"2023-04-15T18:03:31.603813Z"},"trusted":true},"execution_count":null,"outputs":[]}]}