{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-24T06:53:01.231029Z","iopub.execute_input":"2026-08-24T06:53:01.232017Z","iopub.status.idle":"2026-08-24T06:53:01.240415Z","shell.execute_reply.started":"2026-08-24T06:53:01.231980Z","shell.execute_reply":"2026-08-24T06:53:01.239701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")\nsample_submission = pd.read_csv(\n    \"/kaggle/input/competitions/digit-recognizer/sample_submission.csv\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T06:53:01.242147Z","iopub.execute_input":"2026-08-24T06:53:01.242551Z","iopub.status.idle":"2026-08-24T06:53:04.559173Z","shell.execute_reply.started":"2026-08-24T06:53:01.242521Z","shell.execute_reply":"2026-08-24T06:53:04.558478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:00:22.852672Z","iopub.execute_input":"2026-08-24T07:00:22.853517Z","iopub.status.idle":"2026-08-24T07:00:22.873016Z","shell.execute_reply.started":"2026-08-24T07:00:22.853481Z","shell.execute_reply":"2026-08-24T07:00:22.872054Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(\"label\", axis=1)\ny = train[\"label\"]\n\nprint(X.shape)\nprint(y.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T06:55:36.575509Z","iopub.execute_input":"2026-08-24T06:55:36.575887Z","iopub.status.idle":"2026-08-24T06:55:36.654269Z","shell.execute_reply.started":"2026-08-24T06:55:36.575848Z","shell.execute_reply":"2026-08-24T06:55:36.653528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T06:56:08.005065Z","iopub.execute_input":"2026-08-24T06:56:08.005594Z","iopub.status.idle":"2026-08-24T06:56:08.077419Z","shell.execute_reply.started":"2026-08-24T06:56:08.005545Z","shell.execute_reply":"2026-08-24T06:56:08.076606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X,\n    y,\n    test_size=0.2,\n    random_state=42,\n    stratify=y\n)\n\nprint(X_train.shape)\nprint(X_val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T06:56:51.174953Z","iopub.execute_input":"2026-08-24T06:56:51.175280Z","iopub.status.idle":"2026-08-24T06:56:52.239777Z","shell.execute_reply.started":"2026-08-24T06:56:51.175251Z","shell.execute_reply":"2026-08-24T06:56:52.238991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:01:44.729905Z","iopub.execute_input":"2026-08-24T07:01:44.730242Z","iopub.status.idle":"2026-08-24T07:02:00.882924Z","shell.execute_reply.started":"2026-08-24T07:01:44.730216Z","shell.execute_reply":"2026-08-24T07:02:00.881883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    \n    Dense(512, activation=\"relu\", input_shape=(784,)),\n    Dropout(0.2),\n    \n    Dense(256, activation=\"relu\"),\n    Dropout(0.2),\n    \n    Dense(128, activation=\"relu\"),\n    \n    Dense(10, activation=\"softmax\")\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:02:04.645249Z","iopub.execute_input":"2026-08-24T07:02:04.645843Z","iopub.status.idle":"2026-08-24T07:02:04.753201Z","shell.execute_reply.started":"2026-08-24T07:02:04.645811Z","shell.execute_reply":"2026-08-24T07:02:04.752345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:02:16.457884Z","iopub.execute_input":"2026-08-24T07:02:16.458202Z","iopub.status.idle":"2026-08-24T07:02:16.474624Z","shell.execute_reply.started":"2026-08-24T07:02:16.458176Z","shell.execute_reply":"2026-08-24T07:02:16.473737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    validation_data=(X_val, y_val),\n    epochs=10,\n    batch_size=64\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:02:46.499677Z","iopub.execute_input":"2026-08-24T07:02:46.499998Z","iopub.status.idle":"2026-08-24T07:03:33.116878Z","shell.execute_reply.started":"2026-08-24T07:02:46.499969Z","shell.execute_reply":"2026-08-24T07:03:33.115873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val)\n\nprint(\"Validation Accuracy:\", accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:04:55.666424Z","iopub.execute_input":"2026-08-24T07:04:55.667295Z","iopub.status.idle":"2026-08-24T07:04:56.546022Z","shell.execute_reply.started":"2026-08-24T07:04:55.667262Z","shell.execute_reply":"2026-08-24T07:04:56.545000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = test / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:05:11.723037Z","iopub.execute_input":"2026-08-24T07:05:11.723370Z","iopub.status.idle":"2026-08-24T07:05:11.766220Z","shell.execute_reply.started":"2026-08-24T07:05:11.723342Z","shell.execute_reply":"2026-08-24T07:05:11.765332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(X_test)\n\npredicted_labels = predictions.argmax(axis=1)\n\nprint(predicted_labels[:10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:05:29.345106Z","iopub.execute_input":"2026-08-24T07:05:29.345461Z","iopub.status.idle":"2026-08-24T07:05:31.714313Z","shell.execute_reply.started":"2026-08-24T07:05:29.345428Z","shell.execute_reply":"2026-08-24T07:05:31.713436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:05:46.559742Z","iopub.execute_input":"2026-08-24T07:05:46.560306Z","iopub.status.idle":"2026-08-24T07:05:46.571757Z","shell.execute_reply.started":"2026-08-24T07:05:46.560275Z","shell.execute_reply":"2026-08-24T07:05:46.570853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"ImageId\": range(1, len(predicted_labels) + 1),\n    \"Label\": predicted_labels\n})\n\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:06:05.034921Z","iopub.execute_input":"2026-08-24T07:06:05.035756Z","iopub.status.idle":"2026-08-24T07:06:05.045442Z","shell.execute_reply.started":"2026-08-24T07:06:05.035724Z","shell.execute_reply":"2026-08-24T07:06:05.044647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-24T07:06:13.818390Z","iopub.execute_input":"2026-08-24T07:06:13.818753Z","iopub.status.idle":"2026-08-24T07:06:13.852312Z","shell.execute_reply.started":"2026-08-24T07:06:13.818723Z","shell.execute_reply":"2026-08-24T07:06:13.851631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}