{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30804,"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\nfrom tensorflow.keras.utils import to_categorical\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","trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:14:07.625488Z","iopub.execute_input":"2024-12-02T22:14:07.625857Z","iopub.status.idle":"2024-12-02T22:14:07.641055Z","shell.execute_reply.started":"2024-12-02T22:14:07.625826Z","shell.execute_reply":"2024-12-02T22:14:07.639862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nX_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:14:38.817504Z","iopub.execute_input":"2024-12-02T22:14:38.818546Z","iopub.status.idle":"2024-12-02T22:14:42.747330Z","shell.execute_reply.started":"2024-12-02T22:14:38.818488Z","shell.execute_reply":"2024-12-02T22:14:42.746163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_df.drop('label', axis=1).values\ny_train = train_df['label'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:14:48.307107Z","iopub.execute_input":"2024-12-02T22:14:48.307581Z","iopub.status.idle":"2024-12-02T22:14:48.427054Z","shell.execute_reply.started":"2024-12-02T22:14:48.307543Z","shell.execute_reply":"2024-12-02T22:14:48.425769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255.00\ny_train = to_categorical(y_train, 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:14:57.415434Z","iopub.execute_input":"2024-12-02T22:14:57.416457Z","iopub.status.idle":"2024-12-02T22:14:57.635436Z","shell.execute_reply.started":"2024-12-02T22:14:57.416397Z","shell.execute_reply":"2024-12-02T22:14:57.634322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test=X_test.values.reshape(-1, 28, 28, 1).astype('float32') / 255.00","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:15:03.992326Z","iopub.execute_input":"2024-12-02T22:15:03.992758Z","iopub.status.idle":"2024-12-02T22:15:04.048277Z","shell.execute_reply.started":"2024-12-02T22:15:03.992721Z","shell.execute_reply":"2024-12-02T22:15:04.046888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(6, (5, 5), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(2,2),\n    Conv2D(16, (5, 5), activation='relu'),\n    MaxPooling2D(2,2),\n    Flatten(),\n    Dense(120, activation=\"relu\"),\n    Dense(80, activation=\"relu\"),\n    Dense(10, activation=\"softmax\")\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:15:09.743586Z","iopub.execute_input":"2024-12-02T22:15:09.744014Z","iopub.status.idle":"2024-12-02T22:15:09.863893Z","shell.execute_reply.started":"2024-12-02T22:15:09.743977Z","shell.execute_reply":"2024-12-02T22:15:09.862701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:15:24.938817Z","iopub.execute_input":"2024-12-02T22:15:24.939300Z","iopub.status.idle":"2024-12-02T22:15:24.975818Z","shell.execute_reply.started":"2024-12-02T22:15:24.939254Z","shell.execute_reply":"2024-12-02T22:15:24.974339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(X_train, y_train, epochs=10, batch_size=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:15:34.362292Z","iopub.execute_input":"2024-12-02T22:15:34.362775Z","iopub.status.idle":"2024-12-02T22:17:14.805998Z","shell.execute_reply.started":"2024-12-02T22:15:34.362735Z","shell.execute_reply":"2024-12-02T22:17:14.804783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nplt.subplot(2,1,1)\nplt.plot(history.history[\"loss\"], label=\"Training Loss\")\nplt.title(\"Loss during Training\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:17:29.131184Z","iopub.execute_input":"2024-12-02T22:17:29.131641Z","iopub.status.idle":"2024-12-02T22:17:29.459518Z","shell.execute_reply.started":"2024-12-02T22:17:29.131603Z","shell.execute_reply":"2024-12-02T22:17:29.458298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nplt.subplot(2,1,1)\nplt.plot(history.history[\"accuracy\"], label=\"Training Accuracy\")\nplt.title(\"Accuracy during Training\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:17:32.302596Z","iopub.execute_input":"2024-12-02T22:17:32.303016Z","iopub.status.idle":"2024-12-02T22:17:32.588561Z","shell.execute_reply.started":"2024-12-02T22:17:32.302980Z","shell.execute_reply":"2024-12-02T22:17:32.587161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions=model.predict(X_test)\npredictions=np.argmax(predictions, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:17:36.142744Z","iopub.execute_input":"2024-12-02T22:17:36.143161Z","iopub.status.idle":"2024-12-02T22:17:40.076864Z","shell.execute_reply.started":"2024-12-02T22:17:36.143124Z","shell.execute_reply":"2024-12-02T22:17:40.075677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample.head()\noutput = pd.DataFrame({'ImageId': sample.ImageId, 'Label': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T22:18:04.270317Z","iopub.execute_input":"2024-12-02T22:18:04.270760Z","iopub.status.idle":"2024-12-02T22:18:04.316080Z","shell.execute_reply.started":"2024-12-02T22:18:04.270721Z","shell.execute_reply":"2024-12-02T22:18:04.315028Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Done!","metadata":{}},{"cell_type":"markdown","source":"Note: You should split the training into training and validation! I have done it locally.","metadata":{}}]}