{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","execution":{"iopub.status.busy":"2025-10-13T12:32:09.904837Z","iopub.execute_input":"2025-10-13T12:32:09.905105Z","iopub.status.idle":"2025-10-13T12:32:11.526838Z","shell.execute_reply.started":"2025-10-13T12:32:09.905080Z","shell.execute_reply":"2025-10-13T12:32:11.525933Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Install required packages\n# pip install numpy pandas matplotlib seaborn scikit-learn tensorflow\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T12:32:49.998970Z","iopub.execute_input":"2025-10-13T12:32:49.999483Z","iopub.status.idle":"2025-10-13T12:32:50.003561Z","shell.execute_reply.started":"2025-10-13T12:32:49.999457Z","shell.execute_reply":"2025-10-13T12:32:50.002707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load data\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Separate features and labels\nX = train_df.drop('label', axis=1).values\ny = train_df['label'].values\nX_test = test_df.values\n\n# Normalize pixel values to [0, 1]\nX = X / 255.0\nX_test = X_test / 255.0\n\n# Reshape for CNN (samples, height, width, channels)\nX = X.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)\n\n# Split training data\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":"2025-10-13T12:33:12.469801Z","iopub.execute_input":"2025-10-13T12:33:12.470107Z","iopub.status.idle":"2025-10-13T12:33:17.609735Z","shell.execute_reply.started":"2025-10-13T12:33:12.470087Z","shell.execute_reply":"2025-10-13T12:33:17.608891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build CNN model\nmodel = keras.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(10, activation='softmax')\n])\n\n# Compile model\nmodel.compile(optimizer='adam',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy'])\n\n# Train model\nhistory = model.fit(X_train, y_train,\n                    epochs=10,\n                    batch_size=128,\n                    validation_data=(X_val, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T12:33:31.579767Z","iopub.execute_input":"2025-10-13T12:33:31.580061Z","iopub.status.idle":"2025-10-13T12:33:52.052384Z","shell.execute_reply.started":"2025-10-13T12:33:31.580040Z","shell.execute_reply":"2025-10-13T12:33:52.051783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions\npredictions = model.predict(X_test)\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create submission file\nsubmission = pd.DataFrame({\n    'ImageId': range(1, len(predicted_labels) + 1),\n    'Label': predicted_labels\n})\nsubmission.to_csv('submission.csv', index=False)\n\n# Plot training history\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Train')\nplt.plot(history.history['val_accuracy'], label='Validation')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Train')\nplt.plot(history.history['val_loss'], label='Validation')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.savefig('training_history.png')\n\nprint(f\"Validation Accuracy: {max(history.history['val_accuracy']):.4f}\")\nprint(f\"Submission file created: submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T12:34:05.714714Z","iopub.execute_input":"2025-10-13T12:34:05.714989Z","iopub.status.idle":"2025-10-13T12:34:08.706649Z","shell.execute_reply.started":"2025-10-13T12:34:05.714970Z","shell.execute_reply":"2025-10-13T12:34:08.705930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Alternative: Random Forest (faster, no GPU needed)\nfrom sklearn.ensemble import RandomForestClassifier\n\n# Load and prepare data\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nX = train_df.drop('label', axis=1).values / 255.0\ny = train_df['label'].values\nX_test = test_df.values / 255.0\n\n# Train Random Forest\nrf_model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)\nrf_model.fit(X, y)\n\n# Predict and submit\npredictions = rf_model.predict(X_test)\nsubmission = pd.DataFrame({\n    'ImageId': range(1, len(predictions) + 1),\n    'Label': predictions\n})\nsubmission.to_csv('submission_rf.csv', index=False)\nprint(\"Random Forest submission created: submission_rf.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T12:34:17.922982Z","iopub.execute_input":"2025-10-13T12:34:17.923657Z","iopub.status.idle":"2025-10-13T12:34:32.141179Z","shell.execute_reply.started":"2025-10-13T12:34:17.923635Z","shell.execute_reply":"2025-10-13T12:34:32.140380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}