{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":10445,"databundleVersionId":200747,"sourceType":"competition"},{"sourceId":8834080,"sourceType":"datasetVersion","datasetId":5305385}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\ntest_data = pd.read_csv(\"/kaggle/input/elo-merchant-category-recommendation-train-test/test_data.csv\")\ntrain_data = pd.read_csv(\"/kaggle/input/elo-merchant-category-recommendation-train-test/train_data.csv\")\n\ntest_data = test_data.fillna(0)\ntrain_data = train_data.fillna(0)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-02T14:23:36.301569Z","iopub.execute_input":"2024-07-02T14:23:36.301994Z","iopub.status.idle":"2024-07-02T14:23:51.764808Z","shell.execute_reply.started":"2024-07-02T14:23:36.301962Z","shell.execute_reply":"2024-07-02T14:23:51.763539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import KFold\nfrom numpy.random import RandomState\nfrom hyperopt import hp,fmin,tpe\n","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.766823Z","iopub.execute_input":"2024-07-02T14:23:51.767176Z","iopub.status.idle":"2024-07-02T14:23:51.772167Z","shell.execute_reply.started":"2024-07-02T14:23:51.767147Z","shell.execute_reply":"2024-07-02T14:23:51.771153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_select_warpper(train,feature,label):\n    params = {\n    'objective': 'regression',  # 对于二分类问题\n    'metric': 'rmse',  # 评估指标\n    'boosting_type': 'gbdt',\n    'num_leaves': 31,\n    'learning_rate': 0.05,\n    'feature_fraction': 0.9,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 1,\n    'bagging_seed':2024,\n    }\n    callbacks = [lgb.early_stopping(stopping_rounds=30)\n                 ,lgb.log_evaluation(-1)\n                ]\n    \n    kf = KFold(n_splits=5,random_state=2024,shuffle=True)\n    fse = pd.Series(0,index = feature)\n    \n    for train_index,val_index in kf.split(train[feature],train[label]):\n        train_d = lgb.Dataset(train[feature].iloc[train_index],train[label].iloc[train_index])\n        val_d = lgb.Dataset(train[feature].iloc[val_index],train[label].iloc[val_index])\n        \n        model = lgb.train(\n            params,\n            train_set=train_d,\n            valid_sets=[train_d,val_d],\n            valid_names=[\"train\",\"valid\"],\n            num_boost_round=1000,\n            callbacks = callbacks\n        )\n        fse += pd.Series(model.feature_importance(),index = feature)\n    return fse.sort_values(ascending=False).index","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.773470Z","iopub.execute_input":"2024-07-02T14:23:51.773778Z","iopub.status.idle":"2024-07-02T14:23:51.786150Z","shell.execute_reply.started":"2024-07-02T14:23:51.773752Z","shell.execute_reply":"2024-07-02T14:23:51.785071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def params_init(params):\n    params[\"objective\"] = 'regression'\n    params[\"metric\"] = 'rmse'\n    params['boosting_type'] =  'gbdt'\n    params['bagging_seed'] = 2024\n#     params['bagging_fraction'] = 0.774238469115141\n#     params['bagging_freq'] =  6\n#     params['feature_fraction'] = 0.5808889237092238\n#     params['learning_rate'] =  0.01025021127575689\n    return params","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.788200Z","iopub.execute_input":"2024-07-02T14:23:51.788530Z","iopub.status.idle":"2024-07-02T14:23:51.798252Z","shell.execute_reply.started":"2024-07-02T14:23:51.788496Z","shell.execute_reply":"2024-07-02T14:23:51.797148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def param_hyperopt(train,feature,label):\n    train_data = lgb.Dataset(train[feature],train[label])\n    \n    def hyperopt_obj(params):\n        params = params_init(params)\n        callbacks = [lgb.early_stopping(stopping_rounds=30)]\n        res = lgb.cv(params,train_data,1000,\n                    nfold=2,\n                    stratified=False,\n                    shuffle=True,\n                    metrics=\"rmse\",\n                    seed=2024,\n#                     verbose_eval=False,\n                    callbacks=callbacks\n                    )\n        return min(res[\"valid rmse-mean\"])\n    params_space = {\n        \"learning_rate\":hp.uniform(\"learning_rate\",1e-2,5e-1),\n        'feature_fraction': hp.uniform('feature_fraction',0.5,1),\n        'bagging_fraction': hp.uniform('bagging_fraction',0.5,1),\n        'reg_alpha': hp.uniform('reg_alpha',0,10),\n        'reg_lambda': hp.randint('reg_lambda',0,10),\n        'bagging_freq': hp.randint('bagging_freq',1,10),\n        'num_leaves': hp.choice('num_leaves',list(range(10,200,10))),\n        'min_child_samples':hp.choice('min_child_samples',list(range(5,50,5)))\n    }\n    params_best = fmin(\n        hyperopt_obj,\n        params_space,\n        algo=tpe.suggest,\n        max_evals=100,\n    )\n    return params_best","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.799548Z","iopub.execute_input":"2024-07-02T14:23:51.799935Z","iopub.status.idle":"2024-07-02T14:23:51.811539Z","shell.execute_reply.started":"2024-07-02T14:23:51.799900Z","shell.execute_reply":"2024-07-02T14:23:51.810252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def  kfold(k,best_params,train,test,feature,label): \n    kf = KFold(n_splits=k, shuffle=True, random_state=2024)  \n    res = pd.DataFrame(0,index = test['card_id'],columns=[label])\n\n    scores = []\n    kfold_pred = pd.DataFrame()\n    callbacks = [lgb.early_stopping(stopping_rounds=30)]\n    # 进行k折交叉验证\n    for train_index,val_index in kf.split(train[feature],train[label]):\n        train_d = lgb.Dataset(train[feature].loc[train_index],train[label].loc[train_index])\n        val_d = lgb.Dataset(train[feature].loc[val_index],train[label].loc[val_index])\n\n        model = lgb.train(\n            best_params,\n            train_set=train_d,\n            valid_sets=[train_d,val_d],\n            valid_names=[\"train\",\"valid\"],\n            num_boost_round=1000,\n            callbacks = callbacks,\n        )\n        res[\"target\"] += model.predict(test[feature])\n        val_pred = pd.Series(model.predict(train[feature].loc[val_index]),index=train[\"card_id\"].loc[val_index])\n        kfold_pred = pd.concat((kfold_pred,val_pred),axis=0)\n        \n        mse = mean_squared_error(val_pred.values,train[label].loc[val_index].values)  \n        scores.append(mse)  \n        print(f\"第{len(scores)}折mse: {mse}\")\n        \n\n    res['target'] /= k\n    kfold_pred.columns=[\"target\"]\n    \n    return res,kfold_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.812847Z","iopub.execute_input":"2024-07-02T14:23:51.813202Z","iopub.status.idle":"2024-07-02T14:23:51.827048Z","shell.execute_reply.started":"2024-07-02T14:23:51.813171Z","shell.execute_reply":"2024-07-02T14:23:51.826021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature = test_data.columns[test_data.columns!=\"card_id\"]\nfse = feature_select_warpper(train_data,feature,\"target\")\nfeature = fse[:200].tolist()\nlabel = \"target\"\nbest_params = {\n    'bagging_fraction': 0.907151993924132, \n    'bagging_freq': 5, \n    'feature_fraction': 0.690262120992561,\n    'learning_rate': 0.011198989977034331,\n    'min_child_samples': 7, \n    'num_leaves': 256, \n    'reg_alpha': 7.157770333065401,\n    'reg_lambda': 8\n}\n# best_params = param_hyperopt(train_data,feature,label)\nprint(best_params)\ntest_pred,train_pred = kfold(5,best_params,train_data,test_data,feature,label)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:23:51.830847Z","iopub.execute_input":"2024-07-02T14:23:51.831250Z","iopub.status.idle":"2024-07-02T14:27:36.773646Z","shell.execute_reply.started":"2024-07-02T14:23:51.831212Z","shell.execute_reply":"2024-07-02T14:27:36.772616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred.to_csv(\"/kaggle/working/submission.csv\")\ntrain_pred.to_csv(\"/kaggle/working/rf_train_pred.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-02T14:28:30.357457Z","iopub.execute_input":"2024-07-02T14:28:30.358404Z","iopub.status.idle":"2024-07-02T14:28:31.200202Z","shell.execute_reply.started":"2024-07-02T14:28:30.358366Z","shell.execute_reply":"2024-07-02T14:28:31.199128Z"},"trusted":true},"execution_count":null,"outputs":[]}]}