{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31328,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:25:58.231352Z","iopub.execute_input":"2026-03-25T20:25:58.231701Z","iopub.status.idle":"2026-03-25T20:26:08.229071Z","shell.execute_reply.started":"2026-03-25T20:25:58.231664Z","shell.execute_reply":"2026-03-25T20:26:08.227800Z"}},"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')\nX = train.drop('label', axis=1)\ny = train['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:27:23.717170Z","iopub.execute_input":"2026-03-25T20:27:23.718206Z","iopub.status.idle":"2026-03-25T20:27:28.878136Z","shell.execute_reply.started":"2026-03-25T20:27:23.718163Z","shell.execute_reply":"2026-03-25T20:27:28.876676Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X / 255.0\nX = X.values.reshape(-1, 28, 28)\nX = X.reshape(-1, 28*28)\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:28:18.591014Z","iopub.execute_input":"2026-03-25T20:28:18.592129Z","iopub.status.idle":"2026-03-25T20:28:19.298184Z","shell.execute_reply.started":"2026-03-25T20:28:18.592052Z","shell.execute_reply":"2026-03-25T20:28:19.296896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Dense(256, activation='relu', input_shape=(784,)),\n    layers.Dropout(0.3),\n\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.3),\n\n    layers.Dense(10, activation='softmax')  \n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:31:18.755842Z","iopub.execute_input":"2026-03-25T20:31:18.756503Z","iopub.status.idle":"2026-03-25T20:31:18.813682Z","shell.execute_reply.started":"2026-03-25T20:31:18.756450Z","shell.execute_reply":"2026-03-25T20:31:18.812538Z"}},"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-03-25T20:32:04.951776Z","iopub.execute_input":"2026-03-25T20:32:04.952080Z","iopub.status.idle":"2026-03-25T20:32:04.962731Z","shell.execute_reply.started":"2026-03-25T20:32:04.952052Z","shell.execute_reply":"2026-03-25T20:32:04.961326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data=(X_val, y_val),\n    epochs=15,\n    batch_size=64\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:32:17.441289Z","iopub.execute_input":"2026-03-25T20:32:17.442101Z","iopub.status.idle":"2026-03-25T20:33:15.293141Z","shell.execute_reply.started":"2026-03-25T20:32:17.442066Z","shell.execute_reply":"2026-03-25T20:33:15.291851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_loss, val_acc = model.evaluate(X_val, y_val)\nprint(\"Validation Accuracy:\", val_acc)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:33:22.458231Z","iopub.execute_input":"2026-03-25T20:33:22.458613Z","iopub.status.idle":"2026-03-25T20:33:22.883520Z","shell.execute_reply.started":"2026-03-25T20:33:22.458582Z","shell.execute_reply":"2026-03-25T20:33:22.882578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(X_val)\npredicted_labels = np.argmax(predictions, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:33:37.600660Z","iopub.execute_input":"2026-03-25T20:33:37.601176Z","iopub.status.idle":"2026-03-25T20:33:38.081096Z","shell.execute_reply.started":"2026-03-25T20:33:37.601141Z","shell.execute_reply":"2026-03-25T20:33:38.079720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.legend(['Train', 'Validation'])\nplt.title('Model Accuracy')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T20:33:49.838807Z","iopub.execute_input":"2026-03-25T20:33:49.839755Z","iopub.status.idle":"2026-03-25T20:33:50.083241Z","shell.execute_reply.started":"2026-03-25T20:33:49.839716Z","shell.execute_reply":"2026-03-25T20:33:50.082048Z"}},"outputs":[],"execution_count":null}]}