{"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":[{"sourceId":127091,"databundleVersionId":15179667,"sourceType":"competition"}],"dockerImageVersionId":31234,"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":"2026-01-04T03:15:49.304002Z","iopub.execute_input":"2026-01-04T03:15:49.304289Z","iopub.status.idle":"2026-01-04T03:15:50.692487Z","shell.execute_reply.started":"2026-01-04T03:15:49.304250Z","shell.execute_reply":"2026-01-04T03:15:50.691518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport xgboost as xgb\nfrom sklearn.preprocessing import LabelEncoder\n\n# 1. Define Paths\ntrain_path = '/kaggle/input/cuhkml-2025-classification/dataframe_train.csv'\ntest_path = '/kaggle/input/cuhkml-2025-classification/dataframe_test.csv'\n\n# 2. Load Data\nprint(\"Loading data...\")\ntrain = pd.read_csv(train_path)\ntest = pd.read_csv(test_path)\n\n# 3. Preprocessing\ndrop_cols = ['expected_use_time', 'id', 'date', 'action_type']\n# Drop ID columns to prevent errors and overfitting\ndrop_ids = ['tracking_id', 'source_tracking_id', 'courier_id', 'shop_id', 'aoi_id'] \n\nfeatures = [col for col in train.columns if col not in drop_cols and col not in drop_ids]\nprint(f\"Using features: {features}\")\n\nX = train[features]\ny = train['action_type']\nX_test = test[features]\n\n# 4. Target Mapping (Delivery=1, Pickup=0)\nif y.dtype == 'object':\n    print(\"Mapping target labels...\")\n    y = y.map({'PICKUP': 0, 'DELIVERY': 1}).fillna(0).astype(int)\n\n# 5. Robust Categorical Encoding\ncombined = pd.concat([X, X_test], keys=['train', 'test'])\n\ncat_cols = [col for col in combined.columns if combined[col].dtype == 'object']\nprint(f\"Encoding categorical columns: {cat_cols}\")\n\nfor col in cat_cols:\n    le = LabelEncoder()\n    combined[col] = le.fit_transform(combined[col].astype(str))\n\nX = combined.xs('train')\nX_test = combined.xs('test')\n\n# 6. Train XGBoost Model\nprint(\"Training XGBoost model...\")\nmodel = xgb.XGBClassifier(\n    n_estimators=300,\n    learning_rate=0.05,\n    max_depth=9,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    n_jobs=-1,\n    random_state=42\n)\n\nmodel.fit(X, y)\n\n# 7. Generate Predictions\nprint(\"Predicting...\")\npredictions = model.predict(X_test)\n\n# 8. Create Submission with CORRECT Headers\nsubmission = pd.DataFrame()\n\n# ERROR FIX: The competition wants 'order', not 'ID'\n# We use the range 0 to len(test) as seen in the sample_submission file\nsubmission['order'] = range(len(test))\n\n# ERROR FIX: Use 'action_type_DELIVERY' to match the sample format exactly\nsubmission['action_type_DELIVERY'] = predictions\n\nsubmission.to_csv('submission.csv', index=False)\n\nprint(\"Success! 'submission.csv' saved with headers: ['order', 'action_type_DELIVERY']\")\nprint(submission.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-04T03:16:20.661771Z","iopub.execute_input":"2026-01-04T03:16:20.662109Z","iopub.status.idle":"2026-01-04T03:16:35.145678Z","shell.execute_reply.started":"2026-01-04T03:16:20.662079Z","shell.execute_reply":"2026-01-04T03:16:35.144630Z"}},"outputs":[],"execution_count":null}]}