{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.11"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10445,"databundleVersionId":200747,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder\nfrom xgboost import XGBRegressor\nimport matplotlib.pyplot as plt\n\nrseed = 71","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:30.815812Z","iopub.execute_input":"2025-06-29T12:23:30.816231Z","iopub.status.idle":"2025-06-29T12:23:30.822363Z","shell.execute_reply.started":"2025-06-29T12:23:30.816202Z","shell.execute_reply":"2025-06-29T12:23:30.821186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ----------------------------------------------\n# データ読み込み\n# ----------------------------------------------\nDATA_DIR = \"/kaggle/input/elo-merchant-category-recommendation\"\ntrain = pd.read_csv(f\"{DATA_DIR}/train.csv\")\ntest  = pd.read_csv(f\"{DATA_DIR}/test.csv\")\n\n# 元テーブルからID列と目的変数をコピー\ntrain_card_id = train['card_id'].copy()\ntest_card_id  = test['card_id'].copy()\ntrain_y       = train['target'].copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:30.824288Z","iopub.execute_input":"2025-06-29T12:23:30.824592Z","iopub.status.idle":"2025-06-29T12:23:31.146507Z","shell.execute_reply.started":"2025-06-29T12:23:30.824561Z","shell.execute_reply":"2025-06-29T12:23:31.145486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ----------------------------------------------\n# メインテーブルの特徴量作成\n# ・年月文字列は欠損補完・Label Encoding\n# ・他はそのまま\n# ----------------------------------------------\n\n# 元テーブルから特徴量をコピー（目的変数は削除）\ntrain_x = train.copy().drop(columns=[\"target\"])\ntest_x  = test.copy()\n\n# 年月文字列に欠損があるので補完\ntrain_x[\"first_active_month\"] = train_x[\"first_active_month\"].fillna(\"Unknown\")\ntest_x[\"first_active_month\"]  = test_x[\"first_active_month\"].fillna(\"Unknown\")\n\n# 年月文字列をLabel Encoding\nle = LabelEncoder()\nall_months = pd.concat([train_x[\"first_active_month\"], test_x[\"first_active_month\"]])\nle.fit(all_months)\ntrain_x[\"fam_label\"] = le.transform(train_x[\"first_active_month\"])\ntest_x[\"fam_label\"]  = le.transform(test_x[\"first_active_month\"])\n\n# 変換前の年月文字列を削除\ntrain_x = train_x.drop(columns=[\"first_active_month\"])\ntest_x  = test_x.drop(columns=[\"first_active_month\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:31.147353Z","iopub.execute_input":"2025-06-29T12:23:31.147634Z","iopub.status.idle":"2025-06-29T12:23:31.269904Z","shell.execute_reply.started":"2025-06-29T12:23:31.147611Z","shell.execute_reply":"2025-06-29T12:23:31.269099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ----------------------------------------------\n# new_merchant_transactions.csv 的特征工程（优化版）\n# ----------------------------------------------\n\n# 读取追加交易数据\nnew_trans = pd.read_csv(f\"{DATA_DIR}/new_merchant_transactions.csv\")\n\n# 自定义函数：最大值 - 最小值（range）\ndef range_func(x):\n    return x.max() - x.min()\n\n# 定义聚合方式（加入 mean, var, skew, 自定义 range）\nagg_funcs = {\n    \"purchase_amount\": [\"min\", \"max\", \"mean\", \"var\", \"skew\", range_func],\n    \"installments\":    [\"min\", \"max\", \"mean\", \"nunique\", range_func],\n    \"month_lag\":       [\"min\", \"max\", \"mean\", \"skew\"],\n    \"purchase_date\":   [\"count\"]\n}\n\n# 分组聚合\nagg_new = new_trans.groupby(\"card_id\").agg(agg_funcs)\n\n# 展平列名并处理非法字符\nflat_cols = []\nfor col_name, func in agg_new.columns.to_flat_index():\n    if isinstance(func, str):\n        func_name = func\n    else:\n        func_name = \"range\"  # lambda函数用自定义名字\n    new_col = f\"new_{col_name}_{func_name}\"\n    # 清除非法字符\n    new_col = new_col.replace(\"[\", \"\").replace(\"]\", \"\") \\\n                     .replace(\"<\", \"\").replace(\">\", \"\") \\\n                     .replace(\"(\", \"\").replace(\")\", \"\") \\\n                     .replace(\",\", \"_\").replace(\" \", \"\")\n    flat_cols.append(new_col)\n\n# 更新列名\nagg_new.columns = flat_cols\n\n# 重置索引\nagg_new = agg_new.reset_index()\n\n# 合并进主表\ntrain_x = train_x.merge(agg_new, on=\"card_id\", how=\"left\")\ntest_x  = test_x.merge(agg_new, on=\"card_id\", how=\"left\")\n\n# 填补缺失值\ntrain_x = train_x.fillna(-1)\ntest_x = test_x.fillna(-1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:31.271027Z","iopub.execute_input":"2025-06-29T12:23:31.271348Z","iopub.status.idle":"2025-06-29T12:23:57.767608Z","shell.execute_reply.started":"2025-06-29T12:23:31.271324Z","shell.execute_reply":"2025-06-29T12:23:57.766818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ID列を削除\ntrain_x = train_x.drop(columns=[\"card_id\"])\ntest_x  = test_x.drop(columns=[\"card_id\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:57.769685Z","iopub.execute_input":"2025-06-29T12:23:57.770000Z","iopub.status.idle":"2025-06-29T12:23:57.788202Z","shell.execute_reply.started":"2025-06-29T12:23:57.769979Z","shell.execute_reply":"2025-06-29T12:23:57.787307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ----------------------------------------------\n# 使用する特徴量の可視化\n# ----------------------------------------------\n\n# 各特徴の統計量\nprint(pd.concat([train_x, train_y], axis=1).describe())\n\n# 各特徴のヒストグラム\ntrain_x.hist(bins=100, color=\"blue\", grid=True, label='pandas')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:23:57.789032Z","iopub.execute_input":"2025-06-29T12:23:57.789311Z","iopub.status.idle":"2025-06-29T12:24:02.194645Z","shell.execute_reply.started":"2025-06-29T12:23:57.789292Z","shell.execute_reply":"2025-06-29T12:24:02.193735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------------------\n# XGBoostの学習・推論・submit\n# ------------------------------\nmodel = XGBRegressor(random_state=rseed)\nmodel.fit(train_x, train_y)\npreds = model.predict(test_x)\n\nsubmission = pd.DataFrame({\n    \"card_id\": test_card_id,\n    \"target\":  preds\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true,"execution":{"iopub.status.busy":"2025-06-29T12:24:02.195721Z","iopub.execute_input":"2025-06-29T12:24:02.196062Z","iopub.status.idle":"2025-06-29T12:24:03.665891Z","shell.execute_reply.started":"2025-06-29T12:24:02.196036Z","shell.execute_reply":"2025-06-29T12:24:03.664757Z"}},"outputs":[],"execution_count":null}]}