{"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":112509,"databundleVersionId":14254895,"sourceType":"competition"}],"dockerImageVersionId":31192,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\n\n# ✅ Correct file paths for Kaggle\ntrain = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")\nsample_sub = pd.read_csv(\"/kaggle/input/csiro-biomass/sample_submission.csv\")\n\nprint(\"Train shape:\", train.shape)\nprint(\"Test shape:\", test.shape)\nprint(\"Sample submission shape:\", sample_sub.shape)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-10T14:08:33.432051Z","iopub.execute_input":"2025-11-10T14:08:33.432455Z","iopub.status.idle":"2025-11-10T14:08:33.855275Z","shell.execute_reply.started":"2025-11-10T14:08:33.432429Z","shell.execute_reply":"2025-11-10T14:08:33.854274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error\n\n# 🗂 Load the data from Kaggle competition directory\ntrain = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")\nsample_sub = pd.read_csv(\"/kaggle/input/csiro-biomass/sample_submission.csv\")\n\nprint(\"✅ Data loaded\")\nprint(\"Train shape:\", train.shape)\nprint(\"Test shape:\", test.shape)\n\n# Predict each target_name separately\ntargets = train[\"target_name\"].unique()\nall_preds = []\n\nfor target_type in targets:\n    print(f\"\\nTraining model for: {target_type}\")\n    \n    # Subset data for this target\n    train_sub = train[train[\"target_name\"] == target_type].copy()\n    test_sub = test[test[\"target_name\"] == target_type].copy()\n    \n    # Select possible features\n    possible_features = [\"Pre_GSHH_NDVI\", \"Height_Ave_cm\"]\n    features = [f for f in possible_features if f in train_sub.columns and f in test_sub.columns]\n    \n    # Add categorical features if they exist in both train & test\n    for col in [\"State\", \"Species\"]:\n        if col in train_sub.columns and col in test_sub.columns:\n            le = LabelEncoder()\n            train_sub[col] = le.fit_transform(train_sub[col].astype(str))\n            test_sub[col] = test_sub[col].astype(str).map(lambda x: le.transform([x])[0] if x in le.classes_ else -1)\n            features.append(col)\n    \n    print(\"Using features:\", features)\n    \n    if len(features) == 0:\n        # No usable features — fallback to mean\n        mean_val = train_sub[\"target\"].mean()\n        print(f\"⚠️ No features found. Using mean = {mean_val:.2f}\")\n        test_sub[\"target\"] = mean_val\n        all_preds.append(test_sub[[\"sample_id\", \"target\"]])\n        continue\n    \n    # Train-validation split\n    X = train_sub[features]\n    y = train_sub[\"target\"]\n    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n    \n    # 🌳 Train Decision Tree model\n    model = DecisionTreeRegressor(max_depth=8, random_state=42)\n    model.fit(X_train, y_train)\n    \n    # Evaluate\n    val_pred = model.predict(X_val)\n    mae = mean_absolute_error(y_val, val_pred)\n    print(f\"Validation MAE: {mae:.3f}\")\n    \n    # Predict for test subset\n    test_preds = model.predict(test_sub[features])\n    test_sub[\"target\"] = test_preds\n    all_preds.append(test_sub[[\"sample_id\", \"target\"]])\nsubmission = pd.concat(all_preds, axis=0)\nsubmission[\"target\"] = submission[\"target\"].fillna(submission[\"target\"].mean())\n\n# 💾 Save submission file\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)\nprint(\"\\n✅ Done! Submission file saved as /kaggle/working/submission.csv\")\n\n# Preview\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-10T14:12:48.317770Z","iopub.execute_input":"2025-11-10T14:12:48.318873Z","iopub.status.idle":"2025-11-10T14:12:48.389757Z","shell.execute_reply.started":"2025-11-10T14:12:48.318833Z","shell.execute_reply":"2025-11-10T14:12:48.388675Z"}},"outputs":[],"execution_count":null}]}