{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31089,"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 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":"2025-07-23T11:43:24.319603Z","iopub.execute_input":"2025-07-23T11:43:24.320052Z","iopub.status.idle":"2025-07-23T11:43:24.719867Z","shell.execute_reply.started":"2025-07-23T11:43:24.320025Z","shell.execute_reply":"2025-07-23T11:43:24.718845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras import layers\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T11:43:24.721389Z","iopub.execute_input":"2025-07-23T11:43:24.721778Z","iopub.status.idle":"2025-07-23T11:43:43.634356Z","shell.execute_reply.started":"2025-07-23T11:43:24.721750Z","shell.execute_reply":"2025-07-23T11:43:43.633304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.Sequential(\n    [\n        # Takes in the Input Image (MNIST size)\n        layers.InputLayer(input_shape = [28, 28, 1]),\n\n        # Data Augmentation\n        #layers.experimental.preprocessing.RandomContrast(factor=0.2),\n        #layers.experimental.preprocessing.RandomFlip(mode='horizontal'),\n        #layers.experimental.preprocessing.RandomRotation(factor=0.2),\n\n        # Convolutional Layers\n        layers.BatchNormalization(),\n        layers.Conv2D(filters=32, kernel_size=3, activation='relu', padding='same'),\n        layers.MaxPool2D(),\n\n        layers.BatchNormalization(),\n        layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),\n        layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),\n        layers.MaxPool2D(),\n\n        layers.BatchNormalization(),\n        layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding='same'),\n        layers.MaxPool2D(),\n\n        # Flatten and Fully Connected Layers\n        layers.BatchNormalization(),\n        layers.Flatten(),\n\n        layers.Dense(128, activation='relu'),\n        layers.Dropout(0.3),\n\n        layers.Dense(64, activation='relu'),\n        layers.Dropout(0.3),\n\n        # Output Layer with softmax for multi-class classification\n        layers.Dense(10, activation='softmax')\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T11:43:43.635334Z","iopub.execute_input":"2025-07-23T11:43:43.635880Z","iopub.status.idle":"2025-07-23T11:43:43.923209Z","shell.execute_reply.started":"2025-07-23T11:43:43.635855Z","shell.execute_reply":"2025-07-23T11:43:43.922073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Load the CSV\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\").values\nX = train_data[:, 1:]  # pixel values\ny = train_data[:, 0]   # labels\n\n# Normalize \nX = X / 255.0\n\nX_train, X_valid, y_train, y_valid = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\nX_train = X_train.reshape(-1, 28, 28, 1)\nX_valid = X_valid.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T11:43:43.925787Z","iopub.execute_input":"2025-07-23T11:43:43.926141Z","iopub.status.idle":"2025-07-23T11:43:48.563350Z","shell.execute_reply.started":"2025-07-23T11:43:43.926119Z","shell.execute_reply":"2025-07-23T11:43:48.562301Z"}},"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":"2025-07-23T11:43:48.564557Z","iopub.execute_input":"2025-07-23T11:43:48.564802Z","iopub.status.idle":"2025-07-23T11:43:48.580500Z","shell.execute_reply.started":"2025-07-23T11:43:48.564783Z","shell.execute_reply":"2025-07-23T11:43:48.579569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data = (X_valid, y_valid),\n    epochs = 20,\n    batch_size = 80\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T11:43:48.581736Z","iopub.execute_input":"2025-07-23T11:43:48.582113Z","iopub.status.idle":"2025-07-23T12:02:55.391846Z","shell.execute_reply.started":"2025-07-23T11:43:48.582083Z","shell.execute_reply":"2025-07-23T12:02:55.390692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['accuracy', 'val_accuracy']].plot();","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T12:02:55.393513Z","iopub.execute_input":"2025-07-23T12:02:55.393932Z","iopub.status.idle":"2025-07-23T12:02:56.054068Z","shell.execute_reply.started":"2025-07-23T12:02:55.393890Z","shell.execute_reply":"2025-07-23T12:02:56.053033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv').values\nX_test = X_test / 255\nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T12:02:56.055200Z","iopub.execute_input":"2025-07-23T12:02:56.055558Z","iopub.status.idle":"2025-07-23T12:02:58.459669Z","shell.execute_reply.started":"2025-07-23T12:02:56.055528Z","shell.execute_reply":"2025-07-23T12:02:58.458535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredicted_labels = np.argmax(predictions, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T12:02:58.460684Z","iopub.execute_input":"2025-07-23T12:02:58.460964Z","iopub.status.idle":"2025-07-23T12:03:13.499895Z","shell.execute_reply.started":"2025-07-23T12:02:58.460937Z","shell.execute_reply":"2025-07-23T12:03:13.498246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(predicted_labels) + 1),  # row numbers starting at 1\n    \"Label\": predicted_labels\n})\noutput.to_csv('/kaggle/working/submission.csv', index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-23T12:03:13.502565Z","iopub.execute_input":"2025-07-23T12:03:13.502922Z","iopub.status.idle":"2025-07-23T12:03:13.551625Z","shell.execute_reply.started":"2025-07-23T12:03:13.502896Z","shell.execute_reply":"2025-07-23T12:03:13.550585Z"}},"outputs":[],"execution_count":null}]}