{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.metrics import mean_squared_log_error\n\nimport optuna\n\nimport lightgbm as lgb\nimport xgboost as xgb\nimport catboost as cat\n\nimport torch\n\nfrom sklearn.pipeline import Pipeline\n\npd.set_option(\"display.max_columns\",None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:53.01819Z","iopub.execute_input":"2024-12-18T08:24:53.01853Z","iopub.status.idle":"2024-12-18T08:24:53.024567Z","shell.execute_reply.started":"2024-12-18T08:24:53.018502Z","shell.execute_reply":"2024-12-18T08:24:53.023656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:53.027443Z","iopub.execute_input":"2024-12-18T08:24:53.027757Z","iopub.status.idle":"2024-12-18T08:24:58.696044Z","shell.execute_reply.started":"2024-12-18T08:24:53.02773Z","shell.execute_reply":"2024-12-18T08:24:58.6953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Dataset contains {df.shape[0]} rows and contain {df.shape[1]} columns.\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:58.697722Z","iopub.execute_input":"2024-12-18T08:24:58.698003Z","iopub.status.idle":"2024-12-18T08:24:58.719039Z","shell.execute_reply.started":"2024-12-18T08:24:58.697972Z","shell.execute_reply":"2024-12-18T08:24:58.718162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"查看数据集 Columns 数据类型以及非空值数量等等\")\ndf.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:58.719978Z","iopub.execute_input":"2024-12-18T08:24:58.720238Z","iopub.status.idle":"2024-12-18T08:24:59.288936Z","shell.execute_reply.started":"2024-12-18T08:24:58.720213Z","shell.execute_reply":"2024-12-18T08:24:59.288133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_column = 'Premium Amount'\ncategorical_columns = df.select_dtypes(include=['object']).columns\nnumerical_columns = df.select_dtypes(exclude=['object']).columns\n\n# Print out column information\nprint(\"Target Column:\", target_column)\nprint(\"\\nCategorical Columns:\", categorical_columns.tolist())\nprint(\"\\nNumerical Columns:\", numerical_columns.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:59.290239Z","iopub.execute_input":"2024-12-18T08:24:59.29107Z","iopub.status.idle":"2024-12-18T08:24:59.479783Z","shell.execute_reply.started":"2024-12-18T08:24:59.291022Z","shell.execute_reply":"2024-12-18T08:24:59.478958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_columns = numerical_columns.drop(['id',target_column])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:59.482012Z","iopub.execute_input":"2024-12-18T08:24:59.482311Z","iopub.status.idle":"2024-12-18T08:24:59.486786Z","shell.execute_reply.started":"2024-12-18T08:24:59.482282Z","shell.execute_reply":"2024-12-18T08:24:59.485943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:24:59.487809Z","iopub.execute_input":"2024-12-18T08:24:59.488089Z","iopub.status.idle":"2024-12-18T08:25:00.0707Z","shell.execute_reply.started":"2024-12-18T08:24:59.488063Z","shell.execute_reply":"2024-12-18T08:25:00.06982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def data(df):\n    df['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'])\n    df['year'] = df['Policy Start Date'].dt.year\n    df['month'] = df['Policy Start Date'].dt.month\n    df['day'] = df['Policy Start Date'].dt.day\n    df['hour'] = df['Policy Start Date'].dt.hour\n\n    df.drop('Policy Start Date',axis = 1,inplace=True)\n\ndata(df)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.071655Z","iopub.execute_input":"2024-12-18T08:25:00.071929Z","iopub.status.idle":"2024-12-18T08:25:00.781521Z","shell.execute_reply.started":"2024-12-18T08:25:00.071887Z","shell.execute_reply":"2024-12-18T08:25:00.780521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.782643Z","iopub.execute_input":"2024-12-18T08:25:00.782954Z","iopub.status.idle":"2024-12-18T08:25:00.800895Z","shell.execute_reply.started":"2024-12-18T08:25:00.782923Z","shell.execute_reply":"2024-12-18T08:25:00.799931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop([target_column,'id'],axis = 1)\ny = df[target_column]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.801996Z","iopub.execute_input":"2024-12-18T08:25:00.802314Z","iopub.status.idle":"2024-12-18T08:25:00.953696Z","shell.execute_reply.started":"2024-12-18T08:25:00.802285Z","shell.execute_reply":"2024-12-18T08:25:00.952925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_columns = categorical_columns.drop('Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.954643Z","iopub.execute_input":"2024-12-18T08:25:00.954915Z","iopub.status.idle":"2024-12-18T08:25:00.959284Z","shell.execute_reply.started":"2024-12-18T08:25:00.954888Z","shell.execute_reply":"2024-12-18T08:25:00.95841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_columns,numerical_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.960082Z","iopub.execute_input":"2024-12-18T08:25:00.960314Z","iopub.status.idle":"2024-12-18T08:25:00.970888Z","shell.execute_reply.started":"2024-12-18T08:25:00.960291Z","shell.execute_reply":"2024-12-18T08:25:00.970124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:00.971901Z","iopub.execute_input":"2024-12-18T08:25:00.972184Z","iopub.status.idle":"2024-12-18T08:25:01.491245Z","shell.execute_reply.started":"2024-12-18T08:25:00.972157Z","shell.execute_reply":"2024-12-18T08:25:01.490379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:01.492754Z","iopub.execute_input":"2024-12-18T08:25:01.49318Z","iopub.status.idle":"2024-12-18T08:25:01.51224Z","shell.execute_reply.started":"2024-12-18T08:25:01.493133Z","shell.execute_reply":"2024-12-18T08:25:01.511423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='median')),\n    #('scaler', StandardScaler())                       # Scale numerical features\n])\n\ncat_pipeline = Pipeline(\n    steps =[\n        ('imputer',SimpleImputer(strategy='constant', fill_value='Unknown')),\n        ('onehot', OneHotEncoder(handle_unknown='ignore'))\n    ]\n)\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', num_pipeline, numerical_columns),\n        ('cat', cat_pipeline, categorical_columns)\n    ]\n)\n\nX_processed = preprocessor.fit_transform(X)\ntest = preprocessor.transform(df_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:01.515305Z","iopub.execute_input":"2024-12-18T08:25:01.515622Z","iopub.status.idle":"2024-12-18T08:25:09.594581Z","shell.execute_reply.started":"2024-12-18T08:25:01.515588Z","shell.execute_reply":"2024-12-18T08:25:09.593578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_processed,y,test_size = 0.2)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:09.595769Z","iopub.execute_input":"2024-12-18T08:25:09.596099Z","iopub.status.idle":"2024-12-18T08:25:09.823016Z","shell.execute_reply.started":"2024-12-18T08:25:09.59607Z","shell.execute_reply":"2024-12-18T08:25:09.822206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_processed","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:09.824398Z","iopub.execute_input":"2024-12-18T08:25:09.824779Z","iopub.status.idle":"2024-12-18T08:25:09.831958Z","shell.execute_reply.started":"2024-12-18T08:25:09.824739Z","shell.execute_reply":"2024-12-18T08:25:09.831056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_params ={\n        \"objective\": \"reg:squarederror\",\n        \"device\":\"gpu\",\n        \"booster\": \"gbtree\", # 'hist'\n        \"n_estimators\": 500,\n        \"learning_rate\": 0.01,\n        \"max_depth\": 12,\n        \"min_child_weight\": 10,\n        \"subsample\": 0.6,\n        \"colsample_bytree\": 0.6,\n        \"random_state\": 1,\n            }\nlgb_params={\n       'verbose': -1,\n       'boosting_type': 'gbdt',\n       'n_estimators': 3000,\n       'eval_metric': 'rmse',\n\n       'objective': 'regression_l2',\n       'learning_rate': 0.01,\n       'max_bin': 8000,\n       'max_depth': 8, \n       'num_leaves': 796,\n       'min_child_samples': 21,\n       'min_child_weight': 11,\n       'colsample_bytree': 0.48551477424501904, \n       'reg_alpha': 0.44307695050069845, \n       'reg_lambda': 0.9310703189038081, \n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:02:11.273215Z","iopub.execute_input":"2024-12-18T09:02:11.273564Z","iopub.status.idle":"2024-12-18T09:02:11.279398Z","shell.execute_reply.started":"2024-12-18T09:02:11.273537Z","shell.execute_reply":"2024-12-18T09:02:11.278284Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_re = xgb.XGBRegressor(**xgb_params,absenable_categorical=True, eval_metric='rmsle')\nlgb_re = lgb.LGBMRegressor(**lgb_params)\ncat_re = cat.CatBoostRegressor()\nxgb_re.fit(X_train, y_train)\nlgb_re.fit(X_train, y_train)\ncat_re.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:02:13.106762Z","iopub.execute_input":"2024-12-18T09:02:13.107138Z","iopub.status.idle":"2024-12-18T09:06:21.540941Z","shell.execute_reply.started":"2024-12-18T09:02:13.107107Z","shell.execute_reply":"2024-12-18T09:06:21.540069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:11:47.730762Z","iopub.execute_input":"2024-12-18T09:11:47.731153Z","iopub.status.idle":"2024-12-18T09:11:47.738223Z","shell.execute_reply.started":"2024-12-18T09:11:47.731123Z","shell.execute_reply":"2024-12-18T09:11:47.737171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = xgb_re.predict(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:28.325195Z","iopub.execute_input":"2024-12-18T09:12:28.325955Z","iopub.status.idle":"2024-12-18T09:12:28.55764Z","shell.execute_reply.started":"2024-12-18T09:12:28.325922Z","shell.execute_reply":"2024-12-18T09:12:28.556905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:30.405692Z","iopub.execute_input":"2024-12-18T09:12:30.406292Z","iopub.status.idle":"2024-12-18T09:12:30.412118Z","shell.execute_reply.started":"2024-12-18T09:12:30.406259Z","shell.execute_reply":"2024-12-18T09:12:30.411259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:32.231705Z","iopub.execute_input":"2024-12-18T09:12:32.232051Z","iopub.status.idle":"2024-12-18T09:12:32.237446Z","shell.execute_reply.started":"2024-12-18T09:12:32.232019Z","shell.execute_reply":"2024-12-18T09:12:32.236644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_val = np.nan_to_num(y_val)\ny_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:37.796464Z","iopub.execute_input":"2024-12-18T09:12:37.796791Z","iopub.status.idle":"2024-12-18T09:12:37.803891Z","shell.execute_reply.started":"2024-12-18T09:12:37.796762Z","shell.execute_reply":"2024-12-18T09:12:37.803015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmsle= np.sqrt(mean_squared_log_error(y_val,pred))\nprint(f\"RMSLE : {rmsle} \" )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:39.850945Z","iopub.execute_input":"2024-12-18T09:12:39.851301Z","iopub.status.idle":"2024-12-18T09:12:39.859625Z","shell.execute_reply.started":"2024-12-18T09:12:39.851271Z","shell.execute_reply":"2024-12-18T09:12:39.858816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from xgboost import plot_importance","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:37.356894Z","iopub.status.idle":"2024-12-18T08:25:37.357348Z","shell.execute_reply.started":"2024-12-18T08:25:37.357125Z","shell.execute_reply":"2024-12-18T08:25:37.357149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_importance(xgb_re,importance_type='gain', title='XGB Feature Importance', max_num_features=10, color='purple')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T08:25:37.358462Z","iopub.status.idle":"2024-12-18T08:25:37.358913Z","shell.execute_reply.started":"2024-12-18T08:25:37.35866Z","shell.execute_reply":"2024-12-18T08:25:37.358684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t_pred = xgb_re.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:45.758225Z","iopub.execute_input":"2024-12-18T09:12:45.759109Z","iopub.status.idle":"2024-12-18T09:12:46.667764Z","shell.execute_reply.started":"2024-12-18T09:12:45.759065Z","shell.execute_reply":"2024-12-18T09:12:46.666896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output= pd.DataFrame(df_test['id'])\noutput[target_column] = t_pred\noutput.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T09:12:47.980681Z","iopub.execute_input":"2024-12-18T09:12:47.981539Z","iopub.status.idle":"2024-12-18T09:12:48.975642Z","shell.execute_reply.started":"2024-12-18T09:12:47.9815Z","shell.execute_reply":"2024-12-18T09:12:48.974951Z"}},"outputs":[],"execution_count":null}]}