{"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":127090,"databundleVersionId":15179656,"sourceType":"competition"}],"dockerImageVersionId":31239,"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:20:46.205536Z","iopub.execute_input":"2026-01-04T03:20:46.205847Z","iopub.status.idle":"2026-01-04T03:20:49.004925Z","shell.execute_reply.started":"2026-01-04T03:20:46.205816Z","shell.execute_reply":"2026-01-04T03:20:49.003564Z"}},"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\n# Using the specific paths you provided\ntrain_path = '/kaggle/input/cuhkml-2025-regression/dataframe_train.csv'\ntest_path = '/kaggle/input/cuhkml-2025-regression/dataframe_test.csv'\nsample_sub_path = '/kaggle/input/cuhkml-2025-regression/regression_example.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\n# We need to predict 'expected_use_time'\ntarget = 'expected_use_time'\n\n# Columns to drop:\n# - 'action_type': Classification target (not needed for regression)\n# - 'id', 'order': Identifiers\n# - 'date': We can rely on 'hour' or other features\n# - target itself from features\ndrop_cols = ['action_type', 'id', 'date', 'order', 'expected_use_time']\n\n# Drop High Cardinality IDs to prevent \"Object/Int\" type errors and overfitting\n# These are unique IDs that confuse the model\ndrop_ids = ['tracking_id', 'source_tracking_id', 'courier_id', 'shop_id', 'aoi_id', 'wave_index']\n\nfeatures = [col for col in train.columns if col not in drop_cols and col not in drop_ids]\nprint(f\"Training on features: {features}\")\n\nX = train[features]\ny = train[target]\nX_test = test[features]\n\n# 4. Robust Categorical Encoding\n# Combine Train and Test to ensure categories are encoded consistently\ncombined = pd.concat([X, X_test], keys=['train', 'test'])\n\n# Identify object (text) columns\ncat_cols = [col for col in combined.columns if combined[col].dtype == 'object']\nprint(f\"Encoding categorical columns: {cat_cols}\")\n\n# Encode using LabelEncoder\nfor col in cat_cols:\n    le = LabelEncoder()\n    # Convert to string to handle mixed types\n    combined[col] = le.fit_transform(combined[col].astype(str))\n\n# Split back into X and X_test\nX = combined.xs('train')\nX_test = combined.xs('test')\n\n# 5. Train XGBoost Regressor\n# The competition evaluates on MAE (Mean Absolute Error)\nprint(\"Training XGBoost Regressor...\")\nmodel = xgb.XGBRegressor(\n    n_estimators=500,\n    learning_rate=0.05,\n    max_depth=8,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    objective='reg:absoluteerror',  # Optimizes for the competition metric directly\n    n_jobs=-1,\n    random_state=42\n)\n\nmodel.fit(X, y)\n\n# 6. Generate Predictions\nprint(\"Predicting...\")\npredictions = model.predict(X_test)\n\n# Ensure no negative predictions (time can't be negative)\npredictions = np.maximum(predictions, 0)\n\n# 7. Create Submission File\nsubmission = pd.DataFrame()\n\n# The format in 'regression_example.csv' requires 'order' and 'expected_use_time'\n# 'order' corresponds to the row index of the test set\nsubmission['order'] = range(len(test))\nsubmission['expected_use_time'] = predictions\n\n# Save to CSV\nsubmission.to_csv('submission.csv', index=False)\n\nprint(\"Success! 'submission.csv' has been saved with headers: ['order', 'expected_use_time']\")\nprint(submission.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-04T03:23:49.684988Z","iopub.execute_input":"2026-01-04T03:23:49.685409Z","iopub.status.idle":"2026-01-04T03:25:11.910264Z","shell.execute_reply.started":"2026-01-04T03:23:49.685380Z","shell.execute_reply":"2026-01-04T03:25:11.909028Z"}},"outputs":[],"execution_count":null}]}