{"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":30918,"isInternetEnabled":true,"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 tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\n# Load Data\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# Prepare Data\nX = train_data.drop(\"label\", axis=1).values.reshape(-1, 28, 28, 1) / 255.0\ny = to_categorical(train_data[\"label\"].values, num_classes=10)\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Define Model\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D((2,2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile Model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train Model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=64)\n\n# Plot Training History\nplt.plot(history.history['accuracy'], label='train accuracy')\nplt.plot(history.history['val_accuracy'], label='val accuracy')\nplt.legend()\nplt.show()\n\n# Make Predictions\nX_test = test_data.values.reshape(-1, 28, 28, 1) / 255.0\npredictions = model.predict(X_test)\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create Submission File\nsubmission = pd.DataFrame({\"ImageId\": np.arange(1, len(predicted_labels) + 1), \"Label\": predicted_labels})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Submission file saved!\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-08T06:41:25.622239Z","iopub.execute_input":"2025-03-08T06:41:25.622492Z","iopub.status.idle":"2025-03-08T06:43:55.695006Z","shell.execute_reply.started":"2025-03-08T06:41:25.622466Z","shell.execute_reply":"2025-03-08T06:43:55.694257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}