{"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-27T18:18:25.992899Z","iopub.execute_input":"2026-08-27T18:18:25.993195Z","iopub.status.idle":"2026-08-27T18:18:28.833370Z","shell.execute_reply.started":"2026-08-27T18:18:25.993166Z","shell.execute_reply":"2026-08-27T18:18:28.832410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:20:17.470288Z","iopub.execute_input":"2026-08-27T18:20:17.470739Z","iopub.status.idle":"2026-08-27T18:20:19.107054Z","shell.execute_reply.started":"2026-08-27T18:20:17.470691Z","shell.execute_reply":"2026-08-27T18:20:19.106036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/competitions/digit-recognizer/test.csv')\n\nprint(\"Training data shape:\", train.shape)\nprint(\"Testing data shape:\", test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:20:28.234760Z","iopub.execute_input":"2026-08-27T18:20:28.235249Z","iopub.status.idle":"2026-08-27T18:20:32.751619Z","shell.execute_reply.started":"2026-08-27T18:20:28.235220Z","shell.execute_reply":"2026-08-27T18:20:32.750704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:21:17.460437Z","iopub.execute_input":"2026-08-27T18:21:17.460824Z","iopub.status.idle":"2026-08-27T18:21:17.502372Z","shell.execute_reply.started":"2026-08-27T18:21:17.460789Z","shell.execute_reply":"2026-08-27T18:21:17.501517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Missing values in train:\", train.isnull().sum().sum())\nprint(\"Missing values in test:\", test.isnull().sum().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:21:29.367856Z","iopub.execute_input":"2026-08-27T18:21:29.368286Z","iopub.status.idle":"2026-08-27T18:21:29.427542Z","shell.execute_reply.started":"2026-08-27T18:21:29.368229Z","shell.execute_reply":"2026-08-27T18:21:29.426480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop('label', axis=1)\ny = train['label']\n\nX_test = test\n\nprint(\"X shape:\", X.shape)\nprint(\"y shape:\", y.shape)\nprint(\"X_test shape:\", X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:21:38.497902Z","iopub.execute_input":"2026-08-27T18:21:38.498278Z","iopub.status.idle":"2026-08-27T18:21:38.609764Z","shell.execute_reply.started":"2026-08-27T18:21:38.498244Z","shell.execute_reply":"2026-08-27T18:21:38.608821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X / 255.0\nX_test = X_test / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:21:45.440613Z","iopub.execute_input":"2026-08-27T18:21:45.441007Z","iopub.status.idle":"2026-08-27T18:21:45.550536Z","shell.execute_reply.started":"2026-08-27T18:21:45.440930Z","shell.execute_reply":"2026-08-27T18:21:45.549834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X.values.reshape(-1, 28, 28, 1)\nX_test = X_test.values.reshape(-1, 28, 28, 1)\n\nprint(\"X shape:\", X.shape)\nprint(\"X_test shape:\", X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:21:52.446693Z","iopub.execute_input":"2026-08-27T18:21:52.447058Z","iopub.status.idle":"2026-08-27T18:21:52.454544Z","shell.execute_reply.started":"2026-08-27T18:21:52.447026Z","shell.execute_reply":"2026-08-27T18:21:52.453499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:22:00.802067Z","iopub.execute_input":"2026-08-27T18:22:00.802373Z","iopub.status.idle":"2026-08-27T18:22:00.806645Z","shell.execute_reply.started":"2026-08-27T18:22:00.802346Z","shell.execute_reply":"2026-08-27T18:22:00.805816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\n\nfor i in range(10):\n    plt.subplot(2, 5, i + 1)\n    plt.imshow(X[i].reshape(28, 28), cmap='gray')\n    plt.title(\"Label: \" + str(y.iloc[i]))\n    plt.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:22:06.725779Z","iopub.execute_input":"2026-08-27T18:22:06.726120Z","iopub.status.idle":"2026-08-27T18:22:07.156882Z","shell.execute_reply.started":"2026-08-27T18:22:06.726075Z","shell.execute_reply":"2026-08-27T18:22:07.155768Z"}},"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.1,\n    random_state=42,\n    stratify=y\n)\n\nprint(\"Training data:\", X_train.shape)\nprint(\"Validation data:\", X_val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:22:15.186021Z","iopub.execute_input":"2026-08-27T18:22:15.186365Z","iopub.status.idle":"2026-08-27T18:22:15.653410Z","shell.execute_reply.started":"2026-08-27T18:22:15.186335Z","shell.execute_reply":"2026-08-27T18:22:15.652604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:23:17.611923Z","iopub.execute_input":"2026-08-27T18:23:17.612358Z","iopub.status.idle":"2026-08-27T18:23:17.616911Z","shell.execute_reply.started":"2026-08-27T18:23:17.612322Z","shell.execute_reply":"2026-08-27T18:23:17.616020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D((2, 2)),\n\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n\n    Flatten(),\n\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n\n    Dense(10, activation='softmax')\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:23:11.736350Z","iopub.execute_input":"2026-08-27T18:23:11.737632Z","iopub.status.idle":"2026-08-27T18:23:11.863896Z","shell.execute_reply.started":"2026-08-27T18:23:11.737582Z","shell.execute_reply":"2026-08-27T18:23:11.863122Z"}},"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-27T18:23:29.178321Z","iopub.execute_input":"2026-08-27T18:23:29.178985Z","iopub.status.idle":"2026-08-27T18:23:29.193666Z","shell.execute_reply.started":"2026-08-27T18:23:29.178949Z","shell.execute_reply":"2026-08-27T18:23:29.192701Z"}},"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-27T18:23:37.459253Z","iopub.execute_input":"2026-08-27T18:23:37.459604Z","iopub.status.idle":"2026-08-27T18:25:58.295589Z","shell.execute_reply.started":"2026-08-27T18:23:37.459570Z","shell.execute_reply":"2026-08-27T18:25:58.294388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val)\n\nprint(\"Validation Loss:\", loss)\nprint(\"Validation Accuracy:\", accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:26:04.766749Z","iopub.execute_input":"2026-08-27T18:26:04.767090Z","iopub.status.idle":"2026-08-27T18:26:05.559348Z","shell.execute_reply.started":"2026-08-27T18:26:04.767057Z","shell.execute_reply":"2026-08-27T18:26:05.558517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\n\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training vs Validation Accuracy')\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:26:14.055213Z","iopub.execute_input":"2026-08-27T18:26:14.056077Z","iopub.status.idle":"2026-08-27T18:26:14.218893Z","shell.execute_reply.started":"2026-08-27T18:26:14.056041Z","shell.execute_reply":"2026-08-27T18:26:14.217762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\n\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training vs Validation Accuracy')\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:26:25.787168Z","iopub.execute_input":"2026-08-27T18:26:25.788071Z","iopub.status.idle":"2026-08-27T18:26:25.938512Z","shell.execute_reply.started":"2026-08-27T18:26:25.788032Z","shell.execute_reply":"2026-08-27T18:26:25.937768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(X_test)\n\npredicted_labels = np.argmax(predictions, axis=1)\n\nprint(predicted_labels[:20])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:26:59.744383Z","iopub.execute_input":"2026-08-27T18:26:59.745163Z","iopub.status.idle":"2026-08-27T18:27:04.615600Z","shell.execute_reply.started":"2026-08-27T18:26:59.745126Z","shell.execute_reply":"2026-08-27T18:27:04.614636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': np.arange(1, len(predicted_labels) + 1),\n    'Label': predicted_labels\n})\n\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:27:13.692428Z","iopub.execute_input":"2026-08-27T18:27:13.693317Z","iopub.status.idle":"2026-08-27T18:27:13.704397Z","shell.execute_reply.started":"2026-08-27T18:27:13.693278Z","shell.execute_reply":"2026-08-27T18:27:13.703683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Submission shape:\", submission.shape)\nprint(\"Expected rows:\", len(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:27:31.523926Z","iopub.execute_input":"2026-08-27T18:27:31.524217Z","iopub.status.idle":"2026-08-27T18:27:31.528962Z","shell.execute_reply.started":"2026-08-27T18:27:31.524190Z","shell.execute_reply":"2026-08-27T18:27:31.528110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\n    '/kaggle/working/submission.csv',\n    index=False\n)\n\nprint(\"submission.csv created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:27:51.497220Z","iopub.execute_input":"2026-08-27T18:27:51.497556Z","iopub.status.idle":"2026-08-27T18:27:51.531503Z","shell.execute_reply.started":"2026-08-27T18:27:51.497526Z","shell.execute_reply":"2026-08-27T18:27:51.530586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_check = pd.read_csv('/kaggle/working/submission.csv')\n\nprint(submission_check.head())\nprint(submission_check.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T18:28:08.426327Z","iopub.execute_input":"2026-08-27T18:28:08.427128Z","iopub.status.idle":"2026-08-27T18:28:08.439653Z","shell.execute_reply.started":"2026-08-27T18:28:08.427092Z","shell.execute_reply":"2026-08-27T18:28:08.438471Z"}},"outputs":[],"execution_count":null}]}