{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30822,"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:37.274370Z","iopub.execute_input":"2024-12-29T19:54:37.274829Z","iopub.status.idle":"2024-12-29T19:54:37.735300Z","shell.execute_reply.started":"2024-12-29T19:54:37.274790Z","shell.execute_reply":"2024-12-29T19:54:37.734040Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 1. Import Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.utils import to_categorical","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:37.736866Z","iopub.execute_input":"2024-12-29T19:54:37.737580Z","iopub.status.idle":"2024-12-29T19:54:47.447939Z","shell.execute_reply.started":"2024-12-29T19:54:37.737537Z","shell.execute_reply":"2024-12-29T19:54:47.446638Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 2. Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:47.450181Z","iopub.execute_input":"2024-12-29T19:54:47.451097Z","iopub.status.idle":"2024-12-29T19:54:52.641459Z","shell.execute_reply.started":"2024-12-29T19:54:47.451055Z","shell.execute_reply":"2024-12-29T19:54:52.640084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.info()\nprint('----------------------------------------')\ntest_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:52.643234Z","iopub.execute_input":"2024-12-29T19:54:52.643645Z","iopub.status.idle":"2024-12-29T19:54:53.030918Z","shell.execute_reply.started":"2024-12-29T19:54:52.643597Z","shell.execute_reply":"2024-12-29T19:54:53.029563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:53.032079Z","iopub.execute_input":"2024-12-29T19:54:53.032440Z","iopub.status.idle":"2024-12-29T19:54:53.057779Z","shell.execute_reply.started":"2024-12-29T19:54:53.032409Z","shell.execute_reply":"2024-12-29T19:54:53.056618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:53.059052Z","iopub.execute_input":"2024-12-29T19:54:53.059504Z","iopub.status.idle":"2024-12-29T19:54:53.076556Z","shell.execute_reply.started":"2024-12-29T19:54:53.059450Z","shell.execute_reply":"2024-12-29T19:54:53.075324Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 3. Preprocess the Data","metadata":{}},{"cell_type":"code","source":"# Extract features and labels\nX = train_data.drop(\"label\", axis=1).values / 255.0  # Normalize pixel values to [0, 1]\ny = train_data[\"label\"].values\n\n# One-hot encode the labels\ny = to_categorical(y, num_classes=10)\n\n# Split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:53.077932Z","iopub.execute_input":"2024-12-29T19:54:53.078321Z","iopub.status.idle":"2024-12-29T19:54:53.795148Z","shell.execute_reply.started":"2024-12-29T19:54:53.078273Z","shell.execute_reply":"2024-12-29T19:54:53.793994Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Load and Prepare Test Data:","metadata":{}},{"cell_type":"code","source":"X_test = test_data.values / 255.0  # Normalize test data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:53.797602Z","iopub.execute_input":"2024-12-29T19:54:53.797965Z","iopub.status.idle":"2024-12-29T19:54:53.896304Z","shell.execute_reply.started":"2024-12-29T19:54:53.797931Z","shell.execute_reply":"2024-12-29T19:54:53.895155Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 4. Visualize the Train Data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nfor i in range(10):\n    plt.subplot(2, 5, i + 1)\n    plt.imshow(X_train[i].reshape(28, 28), cmap=\"gray\")\n    plt.title(f\"Label: {np.argmax(y_train[i])}\")\n    plt.axis(\"off\")\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:53.897979Z","iopub.execute_input":"2024-12-29T19:54:53.898291Z","iopub.status.idle":"2024-12-29T19:54:54.700379Z","shell.execute_reply.started":"2024-12-29T19:54:53.898262Z","shell.execute_reply":"2024-12-29T19:54:54.699361Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 5. Build the Neural Network","metadata":{}},{"cell_type":"code","source":"model = Sequential(\n    [\n        Dense(128, activation=\"relu\", input_shape=(784,)),\n        Dropout(0.2),  # Add dropout to prevent overfitting\n        Dense(64, activation=\"relu\"),\n        Dense(10, activation=\"softmax\"),  # Output layer for 10 classes\n    ]\n)\n\n# Compile the model\nmodel.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:54.701347Z","iopub.execute_input":"2024-12-29T19:54:54.701644Z","iopub.status.idle":"2024-12-29T19:54:54.804784Z","shell.execute_reply.started":"2024-12-29T19:54:54.701616Z","shell.execute_reply":"2024-12-29T19:54:54.803652Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 6. Train the Neural Network","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    validation_data=(X_val, y_val),\n    epochs=10,  # 10 epochs\n    batch_size=32,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:54:54.805664Z","iopub.execute_input":"2024-12-29T19:54:54.805975Z","iopub.status.idle":"2024-12-29T19:55:25.594170Z","shell.execute_reply.started":"2024-12-29T19:54:54.805940Z","shell.execute_reply":"2024-12-29T19:55:25.593169Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Visualize the training and validation accuracy/loss to understand model behavior:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\n\n# Plot accuracy\nplt.subplot(1, 2, 1)\nplt.plot(history.history[\"accuracy\"], label=\"Train Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], label=\"Validation Accuracy\")\nplt.title(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\n\n# Plot loss\nplt.subplot(1, 2, 2)\nplt.plot(history.history[\"loss\"], label=\"Train Loss\")\nplt.plot(history.history[\"val_loss\"], label=\"Validation Loss\")\nplt.title(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:25.595502Z","iopub.execute_input":"2024-12-29T19:55:25.595828Z","iopub.status.idle":"2024-12-29T19:55:26.036022Z","shell.execute_reply.started":"2024-12-29T19:55:25.595797Z","shell.execute_reply":"2024-12-29T19:55:26.034555Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 7. Evaluate the Model","metadata":{}},{"cell_type":"code","source":"val_loss, val_accuracy = model.evaluate(X_val, y_val)\nprint(f\"Validation Accuracy: {val_accuracy * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:26.037262Z","iopub.execute_input":"2024-12-29T19:55:26.037664Z","iopub.status.idle":"2024-12-29T19:55:26.498815Z","shell.execute_reply.started":"2024-12-29T19:55:26.037619Z","shell.execute_reply":"2024-12-29T19:55:26.497811Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 8. Make Predictions","metadata":{}},{"cell_type":"code","source":"predictions = np.argmax(\n    model.predict(X_test), axis=1\n)  # Get the class with highest probability","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:26.499873Z","iopub.execute_input":"2024-12-29T19:55:26.500258Z","iopub.status.idle":"2024-12-29T19:55:28.187959Z","shell.execute_reply.started":"2024-12-29T19:55:26.500218Z","shell.execute_reply":"2024-12-29T19:55:28.186937Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Visualize Some Test Images and Predictions","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\n\nfor i in range(12):\n    plt.subplot(3, 4, i + 1)\n    plt.imshow(X_test[i].reshape(28, 28), cmap=\"gray\")\n    plt.title(f\"Prediction: {predictions[i]}\")\n    plt.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:28.188828Z","iopub.execute_input":"2024-12-29T19:55:28.189144Z","iopub.status.idle":"2024-12-29T19:55:29.161567Z","shell.execute_reply.started":"2024-12-29T19:55:28.189117Z","shell.execute_reply":"2024-12-29T19:55:29.160673Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 9. Prepare the Submission File","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(\n    {\"ImageId\": np.arange(1, len(predictions) + 1), \"Label\": predictions}\n)\n\n# Save to CSV\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:29.162358Z","iopub.execute_input":"2024-12-29T19:55:29.162675Z","iopub.status.idle":"2024-12-29T19:55:29.191831Z","shell.execute_reply.started":"2024-12-29T19:55:29.162647Z","shell.execute_reply":"2024-12-29T19:55:29.190983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_data = pd.read_csv(\"/kaggle/working/submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:29.192646Z","iopub.execute_input":"2024-12-29T19:55:29.192926Z","iopub.status.idle":"2024-12-29T19:55:29.203139Z","shell.execute_reply.started":"2024-12-29T19:55:29.192902Z","shell.execute_reply":"2024-12-29T19:55:29.202129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T19:55:29.204079Z","iopub.execute_input":"2024-12-29T19:55:29.204345Z","iopub.status.idle":"2024-12-29T19:55:29.217843Z","shell.execute_reply.started":"2024-12-29T19:55:29.204320Z","shell.execute_reply":"2024-12-29T19:55:29.216874Z"}},"outputs":[],"execution_count":null}]}