{"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":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30804,"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\nfrom sklearn.model_selection import train_test_split, cross_val_score\nfrom sklearn.metrics import mean_squared_error, mean_squared_log_error\n\nimport xgboost as xgb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:08.922312Z","iopub.execute_input":"2024-12-09T05:31:08.923273Z","iopub.status.idle":"2024-12-09T05:31:11.097805Z","shell.execute_reply.started":"2024-12-09T05:31:08.923219Z","shell.execute_reply":"2024-12-09T05:31:11.096639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\ntry:\n    for root,dirs,files in os.walk('/kaggle/input'):\n        print('root:', root)\n        print('dirs:', dirs)\n        for file in files:\n            print(file)\nexcept Exception as e:\n    print(f'Exception:{e}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:18:50.126621Z","iopub.execute_input":"2024-12-09T05:18:50.127012Z","iopub.status.idle":"2024-12-09T05:18:50.136285Z","shell.execute_reply.started":"2024-12-09T05:18:50.126975Z","shell.execute_reply":"2024-12-09T05:18:50.135037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest_df = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:13.768731Z","iopub.execute_input":"2024-12-09T05:31:13.76978Z","iopub.status.idle":"2024-12-09T05:31:24.094943Z","shell.execute_reply.started":"2024-12-09T05:31:13.769742Z","shell.execute_reply":"2024-12-09T05:31:24.093425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 开始数据探索\ntrain_df.head()\ntrain_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:26.434519Z","iopub.execute_input":"2024-12-09T05:31:26.434907Z","iopub.status.idle":"2024-12-09T05:31:26.443663Z","shell.execute_reply.started":"2024-12-09T05:31:26.434874Z","shell.execute_reply":"2024-12-09T05:31:26.442377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:36.361256Z","iopub.execute_input":"2024-12-09T05:31:36.361674Z","iopub.status.idle":"2024-12-09T05:31:36.447388Z","shell.execute_reply.started":"2024-12-09T05:31:36.361639Z","shell.execute_reply":"2024-12-09T05:31:36.446188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 计算每列数据缺失值总数\nprint(train_df.shape)\ntrain_df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:42.799746Z","iopub.execute_input":"2024-12-09T05:31:42.80013Z","iopub.status.idle":"2024-12-09T05:31:43.440664Z","shell.execute_reply.started":"2024-12-09T05:31:42.800098Z","shell.execute_reply":"2024-12-09T05:31:43.439402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(train_df.columns))\nprint(train_df.dtypes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:29:04.029273Z","iopub.execute_input":"2024-12-09T05:29:04.029741Z","iopub.status.idle":"2024-12-09T05:29:04.037721Z","shell.execute_reply.started":"2024-12-09T05:29:04.029701Z","shell.execute_reply":"2024-12-09T05:29:04.036484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols_train = [c for c in train_df.columns \n                  if (train_df[c].dtype == 'float64' \n                      or train_df[c].dtype == 'int64' \n                      or train_df[c].dtype == 'category' \n                      or train_df[c].dtype == 'bool')]\nprint(len(num_cols_train))\ntrain_df = train_df[num_cols_train]\nprint(len(train_df.columns))\nprint(train_df.dtypes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:31:44.863087Z","iopub.execute_input":"2024-12-09T05:31:44.863507Z","iopub.status.idle":"2024-12-09T05:31:44.966366Z","shell.execute_reply.started":"2024-12-09T05:31:44.863474Z","shell.execute_reply":"2024-12-09T05:31:44.965184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 将Gender列转为category类型：注意，需要用=号赋值一次，否则报错。\n#train_df['Gender'] = train_df['Gender'].astype('category')\n#print(train_df['Gender'].cat.categories)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:25:48.497583Z","iopub.execute_input":"2024-12-09T05:25:48.498092Z","iopub.status.idle":"2024-12-09T05:25:48.50342Z","shell.execute_reply.started":"2024-12-09T05:25:48.498039Z","shell.execute_reply":"2024-12-09T05:25:48.502093Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 独特编码处理 Gender\n#one_hot_cols = ['Gender', 'Marital Status', 'Occupation', 'Location', 'Policy Type', 'Smoking Status', 'Property Type']\n#train_df = pd.get_dummies(train_df, columns=one_hot_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:36:40.76483Z","iopub.execute_input":"2024-12-09T05:36:40.765216Z","iopub.status.idle":"2024-12-09T05:36:40.769912Z","shell.execute_reply.started":"2024-12-09T05:36:40.765185Z","shell.execute_reply":"2024-12-09T05:36:40.768763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:32:35.868632Z","iopub.execute_input":"2024-12-09T05:32:35.8696Z","iopub.status.idle":"2024-12-09T05:32:35.876646Z","shell.execute_reply.started":"2024-12-09T05:32:35.869545Z","shell.execute_reply":"2024-12-09T05:32:35.875669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:32:38.450672Z","iopub.execute_input":"2024-12-09T05:32:38.451169Z","iopub.status.idle":"2024-12-09T05:32:38.474486Z","shell.execute_reply.started":"2024-12-09T05:32:38.451115Z","shell.execute_reply":"2024-12-09T05:32:38.473381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 对测试集做独特编码\n#test_df = pd.get_dummies(test_df, columns=one_hot_cols)\n#test_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:36:59.338585Z","iopub.execute_input":"2024-12-09T05:36:59.338972Z","iopub.status.idle":"2024-12-09T05:36:59.343897Z","shell.execute_reply.started":"2024-12-09T05:36:59.338942Z","shell.execute_reply":"2024-12-09T05:36:59.342763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols_test = [c for c in test_df.columns \n                  if (test_df[c].dtype == 'float64' \n                      or test_df[c].dtype == 'int64' \n                      or test_df[c].dtype == 'category' \n                      or test_df[c].dtype == 'bool')]\ntest_df = test_df[num_cols_test]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:32.788067Z","iopub.execute_input":"2024-12-09T05:34:32.788664Z","iopub.status.idle":"2024-12-09T05:34:32.819394Z","shell.execute_reply.started":"2024-12-09T05:34:32.788603Z","shell.execute_reply":"2024-12-09T05:34:32.818545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df.drop(['Premium Amount'], axis=1)\ny = train_df['Premium Amount']\nprint(X.shape, y.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:37.902867Z","iopub.execute_input":"2024-12-09T05:34:37.903254Z","iopub.status.idle":"2024-12-09T05:34:37.938816Z","shell.execute_reply.started":"2024-12-09T05:34:37.903221Z","shell.execute_reply":"2024-12-09T05:34:37.937716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42 )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:40.53586Z","iopub.execute_input":"2024-12-09T05:34:40.53625Z","iopub.status.idle":"2024-12-09T05:34:40.72364Z","shell.execute_reply.started":"2024-12-09T05:34:40.536215Z","shell.execute_reply":"2024-12-09T05:34:40.72252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape, X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:42.842064Z","iopub.execute_input":"2024-12-09T05:34:42.84249Z","iopub.status.idle":"2024-12-09T05:34:42.848756Z","shell.execute_reply.started":"2024-12-09T05:34:42.842454Z","shell.execute_reply":"2024-12-09T05:34:42.847501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.columns\n#drop_cols = ['Education Level','Policy Start Date','Customer Feedback','Exercise Frequency']\ndrop_cols = []\nX_train = X_train.drop(columns=drop_cols, axis=1)\nX_train.shape\n#X_train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:37:19.950198Z","iopub.execute_input":"2024-12-09T05:37:19.951301Z","iopub.status.idle":"2024-12-09T05:37:19.989138Z","shell.execute_reply.started":"2024-12-09T05:37:19.951243Z","shell.execute_reply":"2024-12-09T05:37:19.988073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dMatrix_train = xgb.DMatrix(X_train, label=y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:47.930572Z","iopub.execute_input":"2024-12-09T05:34:47.931006Z","iopub.status.idle":"2024-12-09T05:34:48.146643Z","shell.execute_reply.started":"2024-12-09T05:34:47.93097Z","shell.execute_reply":"2024-12-09T05:34:48.145799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {\n    'eta': 0.01,\n    'max_depth': 5,\n    'objective': 'reg:squarederror',\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:50.362609Z","iopub.execute_input":"2024-12-09T05:34:50.363481Z","iopub.status.idle":"2024-12-09T05:34:50.368251Z","shell.execute_reply.started":"2024-12-09T05:34:50.363435Z","shell.execute_reply":"2024-12-09T05:34:50.366969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_round = 20\nmodel = xgb.train(params, dMatrix_train, num_round)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:34:52.629482Z","iopub.execute_input":"2024-12-09T05:34:52.629852Z","iopub.status.idle":"2024-12-09T05:34:54.161198Z","shell.execute_reply.started":"2024-12-09T05:34:52.62982Z","shell.execute_reply":"2024-12-09T05:34:54.160277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.columns\n#drop_cols\n#drop_cols = ['Education Level','Policy Start Date','Customer Feedback','Exercise Frequency']\n#test_df = test_df.drop(columns=drop_cols, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:36:32.515652Z","iopub.execute_input":"2024-12-09T05:36:32.516545Z","iopub.status.idle":"2024-12-09T05:36:32.523294Z","shell.execute_reply.started":"2024-12-09T05:36:32.516506Z","shell.execute_reply":"2024-12-09T05:36:32.522131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.shape\ntest_df.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:01.595076Z","iopub.execute_input":"2024-12-09T05:35:01.595504Z","iopub.status.idle":"2024-12-09T05:35:01.603674Z","shell.execute_reply.started":"2024-12-09T05:35:01.595468Z","shell.execute_reply":"2024-12-09T05:35:01.602624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dMatrix_test = xgb.DMatrix(test_df)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:08.418892Z","iopub.execute_input":"2024-12-09T05:35:08.419279Z","iopub.status.idle":"2024-12-09T05:35:08.591848Z","shell.execute_reply.started":"2024-12-09T05:35:08.419245Z","shell.execute_reply":"2024-12-09T05:35:08.591006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(dMatrix_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:11.820878Z","iopub.execute_input":"2024-12-09T05:35:11.82125Z","iopub.status.idle":"2024-12-09T05:35:11.968391Z","shell.execute_reply.started":"2024-12-09T05:35:11.821219Z","shell.execute_reply":"2024-12-09T05:35:11.96754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.index\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:14.363072Z","iopub.execute_input":"2024-12-09T05:35:14.363452Z","iopub.status.idle":"2024-12-09T05:35:14.370731Z","shell.execute_reply.started":"2024-12-09T05:35:14.363418Z","shell.execute_reply":"2024-12-09T05:35:14.36958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")\n\nsubmission = pd.DataFrame({\n    \"Id\": sub.id,\n    \"Premium Amount\": y_pred\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:17.456641Z","iopub.execute_input":"2024-12-09T05:35:17.45705Z","iopub.status.idle":"2024-12-09T05:35:18.927133Z","shell.execute_reply.started":"2024-12-09T05:35:17.457015Z","shell.execute_reply":"2024-12-09T05:35:18.926019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:35:21.737537Z","iopub.execute_input":"2024-12-09T05:35:21.73808Z","iopub.status.idle":"2024-12-09T05:35:21.747961Z","shell.execute_reply.started":"2024-12-09T05:35:21.737918Z","shell.execute_reply":"2024-12-09T05:35:21.746934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}