{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"machine_shape":"hm","gpuType":"V28"},"accelerator":"TPU","kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"0101a9fb-9c19-4973-a63a-8676e0798193","cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n!pip install scikit-learn\n!pip install xgboost\n!pip install lightgbm\n!pip install catboost\nfrom sklearn.metrics import mean_absolute_error, r2_score\nfrom xgboost import XGBRegressor\nfrom sklearn.model_selection import RandomizedSearchCV\nimport sklearn\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nimport lightgbm as lgb\nfrom sklearn.linear_model import Ridge, Lasso\nfrom catboost import CatBoostRegressor\n","metadata":{"id":"0101a9fb-9c19-4973-a63a-8676e0798193","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:00.97423Z","iopub.execute_input":"2024-12-31T01:53:00.974584Z","iopub.status.idle":"2024-12-31T01:53:15.784973Z","shell.execute_reply.started":"2024-12-31T01:53:00.974555Z","shell.execute_reply":"2024-12-31T01:53:15.783965Z"}},"outputs":[],"execution_count":null},{"id":"08845edd-7a2d-47aa-a713-c7e7475ee102","cell_type":"markdown","source":"# Read Data","metadata":{"id":"08845edd-7a2d-47aa-a713-c7e7475ee102"}},{"id":"1d957323-a4fb-495b-b114-90575df8b800","cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ndf_test = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"id":"1d957323-a4fb-495b-b114-90575df8b800","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:15.786279Z","iopub.execute_input":"2024-12-31T01:53:15.787Z","iopub.status.idle":"2024-12-31T01:53:21.618199Z","shell.execute_reply.started":"2024-12-31T01:53:15.786963Z","shell.execute_reply":"2024-12-31T01:53:21.617416Z"}},"outputs":[],"execution_count":null},{"id":"b275df5f-add6-440d-8388-00d93929ed1d","cell_type":"code","source":"df_train.shape","metadata":{"id":"b275df5f-add6-440d-8388-00d93929ed1d","outputId":"33474019-e965-4613-a9e4-5a2523abe988","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:21.619427Z","iopub.execute_input":"2024-12-31T01:53:21.619679Z","iopub.status.idle":"2024-12-31T01:53:21.625768Z","shell.execute_reply.started":"2024-12-31T01:53:21.619659Z","shell.execute_reply":"2024-12-31T01:53:21.624878Z"}},"outputs":[],"execution_count":null},{"id":"9487e510-3938-465f-9c63-6107f3552218","cell_type":"code","source":"df_test.shape","metadata":{"id":"9487e510-3938-465f-9c63-6107f3552218","outputId":"ed2353a9-7c45-4c5f-f2a9-d411dd9c4d18","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:21.626981Z","iopub.execute_input":"2024-12-31T01:53:21.627231Z","iopub.status.idle":"2024-12-31T01:53:21.646698Z","shell.execute_reply.started":"2024-12-31T01:53:21.627213Z","shell.execute_reply":"2024-12-31T01:53:21.645929Z"}},"outputs":[],"execution_count":null},{"id":"76922b32-e7e1-4f7c-a013-8ecc992e5eb4","cell_type":"code","source":"df_train.head()","metadata":{"id":"76922b32-e7e1-4f7c-a013-8ecc992e5eb4","outputId":"900439e2-1888-4d99-bfdf-bfb15fe2b4b2","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:21.647555Z","iopub.execute_input":"2024-12-31T01:53:21.647862Z","iopub.status.idle":"2024-12-31T01:53:21.680045Z","shell.execute_reply.started":"2024-12-31T01:53:21.647799Z","shell.execute_reply":"2024-12-31T01:53:21.679428Z"}},"outputs":[],"execution_count":null},{"id":"c7ed04f4-f2ca-4c87-8091-884fbf21b099","cell_type":"code","source":"df_test.head()","metadata":{"id":"c7ed04f4-f2ca-4c87-8091-884fbf21b099","outputId":"0c639e33-9aaa-4a73-967b-5fdee0dbb2b5","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:21.68078Z","iopub.execute_input":"2024-12-31T01:53:21.680968Z","iopub.status.idle":"2024-12-31T01:53:21.698108Z","shell.execute_reply.started":"2024-12-31T01:53:21.680953Z","shell.execute_reply":"2024-12-31T01:53:21.697278Z"}},"outputs":[],"execution_count":null},{"id":"653c532c-fc5c-423d-86fa-c703be1c7229","cell_type":"code","source":"df_train.info()","metadata":{"id":"653c532c-fc5c-423d-86fa-c703be1c7229","outputId":"d3209a17-094f-4210-dfd0-767901a90d4c","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:21.698907Z","iopub.execute_input":"2024-12-31T01:53:21.699122Z","iopub.status.idle":"2024-12-31T01:53:22.250074Z","shell.execute_reply.started":"2024-12-31T01:53:21.699104Z","shell.execute_reply":"2024-12-31T01:53:22.249136Z"}},"outputs":[],"execution_count":null},{"id":"2ab5ca85-59a6-44fa-9f24-54b9debf7bb2","cell_type":"markdown","source":"# object cols\n\nGenger\nMarital Status\nEducation Level\nOccupation\nLocation\nPolicy Type\nPolicy Start Date\nCustomer Feedback\nSmoking Status\nExercise Frequency\nProperty Type\n\n# num cols\n\nid\nAge\nAnnual Income\nNumber of Dependents\nHealth Score\nPrevious Claims\nVehicle Age\nCredit Score\nInsurance Duration\nPremium Amount","metadata":{"id":"2ab5ca85-59a6-44fa-9f24-54b9debf7bb2"}},{"id":"85afc10b-70d4-43fc-8ae5-35c6551b6900","cell_type":"code","source":"num_cols = df_train.select_dtypes(include = [np.number])\nobj_cols = df_train.select_dtypes(exclude = [np.number])","metadata":{"id":"85afc10b-70d4-43fc-8ae5-35c6551b6900","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:22.252636Z","iopub.execute_input":"2024-12-31T01:53:22.252873Z","iopub.status.idle":"2024-12-31T01:53:22.386508Z","shell.execute_reply.started":"2024-12-31T01:53:22.252855Z","shell.execute_reply":"2024-12-31T01:53:22.385829Z"}},"outputs":[],"execution_count":null},{"id":"c922398b-d987-46dd-be65-ba7a4b8657e4","cell_type":"code","source":"num_cols","metadata":{"id":"c922398b-d987-46dd-be65-ba7a4b8657e4","outputId":"8bcc79a5-9dc6-4613-9c46-2a6a7ad83760","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:22.387866Z","iopub.execute_input":"2024-12-31T01:53:22.388177Z","iopub.status.idle":"2024-12-31T01:53:22.406488Z","shell.execute_reply.started":"2024-12-31T01:53:22.388154Z","shell.execute_reply":"2024-12-31T01:53:22.405759Z"}},"outputs":[],"execution_count":null},{"id":"60672f48-185e-4d2a-802c-8767bb011026","cell_type":"code","source":"obj_cols","metadata":{"id":"60672f48-185e-4d2a-802c-8767bb011026","outputId":"85d18a11-f97a-4115-8046-3f6e366a7b71","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:22.407485Z","iopub.execute_input":"2024-12-31T01:53:22.407774Z","iopub.status.idle":"2024-12-31T01:53:22.431051Z","shell.execute_reply.started":"2024-12-31T01:53:22.407753Z","shell.execute_reply":"2024-12-31T01:53:22.430324Z"}},"outputs":[],"execution_count":null},{"id":"7cf1a12d-c672-4621-90a9-d1ed8f4f33e7","cell_type":"markdown","source":"# Corr","metadata":{"id":"7cf1a12d-c672-4621-90a9-d1ed8f4f33e7"}},{"id":"f7b00add-9eb1-4ddb-945a-f7c33ab639eb","cell_type":"code","source":"corr = num_cols.corr()\n\nplt.figure(figsize=(6,4))\nsns.heatmap(corr, annot=True, cmap='coolwarm', fmt=\".2f\")","metadata":{"id":"f7b00add-9eb1-4ddb-945a-f7c33ab639eb","outputId":"26158a16-0967-4c50-c1d9-04671b616fa6","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:22.431775Z","iopub.execute_input":"2024-12-31T01:53:22.431982Z","iopub.status.idle":"2024-12-31T01:53:23.461271Z","shell.execute_reply.started":"2024-12-31T01:53:22.431957Z","shell.execute_reply":"2024-12-31T01:53:23.460331Z"}},"outputs":[],"execution_count":null},{"id":"b67130d6-88bc-40fd-a45c-8b75d5694e86","cell_type":"markdown","source":"# nulls","metadata":{"id":"b67130d6-88bc-40fd-a45c-8b75d5694e86"}},{"id":"8a29b567-02f6-46b7-88c8-c80c2c9bc0c0","cell_type":"code","source":"df_test.isna().sum()","metadata":{"id":"8a29b567-02f6-46b7-88c8-c80c2c9bc0c0","outputId":"8dd356dc-e92f-46c7-ae5e-0b6b8597e66f","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:23.46229Z","iopub.execute_input":"2024-12-31T01:53:23.462638Z","iopub.status.idle":"2024-12-31T01:53:23.820074Z","shell.execute_reply.started":"2024-12-31T01:53:23.462604Z","shell.execute_reply":"2024-12-31T01:53:23.819284Z"}},"outputs":[],"execution_count":null},{"id":"dce7262b-b622-4e2d-b362-a75d0549f392","cell_type":"code","source":"df_train.isna().sum()","metadata":{"id":"dce7262b-b622-4e2d-b362-a75d0549f392","outputId":"84ee2bae-433f-47c1-8acd-53a9071a8ddc","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:23.820938Z","iopub.execute_input":"2024-12-31T01:53:23.82124Z","iopub.status.idle":"2024-12-31T01:53:24.365568Z","shell.execute_reply.started":"2024-12-31T01:53:23.821204Z","shell.execute_reply":"2024-12-31T01:53:24.364504Z"}},"outputs":[],"execution_count":null},{"id":"d6834863-ef9c-4775-9e9a-89ec439e2e4d","cell_type":"code","source":"","metadata":{"id":"d6834863-ef9c-4775-9e9a-89ec439e2e4d","trusted":true},"outputs":[],"execution_count":null},{"id":"3128a49c-f97c-4fa9-90cb-d6e7213eb3ca","cell_type":"code","source":"# histgram for Age\nplt.figure(figsize=(6,3))\nsns.histplot(df_train['Age'], kde=True)\nplt.show()","metadata":{"id":"3128a49c-f97c-4fa9-90cb-d6e7213eb3ca","outputId":"b2953e36-3936-46ac-dbd4-8909583a96d1","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:24.366672Z","iopub.execute_input":"2024-12-31T01:53:24.366948Z","iopub.status.idle":"2024-12-31T01:53:29.106255Z","shell.execute_reply.started":"2024-12-31T01:53:24.366926Z","shell.execute_reply":"2024-12-31T01:53:29.105398Z"}},"outputs":[],"execution_count":null},{"id":"65580e04-2ca1-49ab-a3e8-61677ce0e3d7","cell_type":"code","source":"# fillna with mean\n# define cols to fillna with mean\nmean_fill_cols = ['Age', 'Health Score', 'Vehicle Age', 'Credit Score', 'Annual Income']\n\n# fillna with mean\nfor col in mean_fill_cols:\n    df_train[col] = df_train[col].fillna(df_train[col].mean())\n    if col in df_test.columns:\n        df_test[col] = df_test[col].fillna(df_test[col].mean())\n# fillna with Unknown\ndf_train['Occupation'] = df_train['Occupation'].fillna('Unknown')\ndf_test['Occupation'] = df_test['Occupation'].fillna('Unknown')\n","metadata":{"id":"65580e04-2ca1-49ab-a3e8-61677ce0e3d7","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:29.107154Z","iopub.execute_input":"2024-12-31T01:53:29.10744Z","iopub.status.idle":"2024-12-31T01:53:29.332274Z","shell.execute_reply.started":"2024-12-31T01:53:29.107403Z","shell.execute_reply":"2024-12-31T01:53:29.331621Z"}},"outputs":[],"execution_count":null},{"id":"1120bbe9-f04f-498f-8f77-b0c21ac5affb","cell_type":"code","source":"","metadata":{"id":"1120bbe9-f04f-498f-8f77-b0c21ac5affb","trusted":true},"outputs":[],"execution_count":null},{"id":"c2cf2661-1b8a-4694-8016-ab310043010b","cell_type":"code","source":"# define mode cols\nmode_cols = [\n    'Marital Status',\n    'Number of Dependents',\n    'Previous Claims',\n    'Insurance Duration',\n    'Customer Feedback'\n]\n\n# fillna with mode for all mode_cols\nfor col in mode_cols:\n    if not df_train[col].mode().empty:\n        mode_value = df_train[col].mode()[0]\n\n        df_train[col] = df_train[col].fillna(mode_value)\n        df_test[col] = df_test[col].fillna(mode_value)\n\n# check results\nprint(\"df_train missing values:\")\nprint(df_train[mode_cols].isnull().sum())\n\nprint(\"\\ndf_test missing values:\")\nprint(df_test[mode_cols].isnull().sum())","metadata":{"id":"c2cf2661-1b8a-4694-8016-ab310043010b","outputId":"78fcf2e0-0c7d-45c3-dbb2-4852813ce366","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:29.333025Z","iopub.execute_input":"2024-12-31T01:53:29.33323Z","iopub.status.idle":"2024-12-31T01:53:30.210033Z","shell.execute_reply.started":"2024-12-31T01:53:29.333213Z","shell.execute_reply":"2024-12-31T01:53:30.209106Z"}},"outputs":[],"execution_count":null},{"id":"a7062009-8ee1-453f-9f8d-ab9d30896f06","cell_type":"markdown","source":"# Outliers","metadata":{"id":"a7062009-8ee1-453f-9f8d-ab9d30896f06"}},{"id":"5b576f33-3d61-4d3e-95d3-f1aa9f4ea312","cell_type":"code","source":"plt.figure(figsize=(6,3))\nsns.boxplot(x=df_train['Premium Amount'])\nplt.show","metadata":{"id":"5b576f33-3d61-4d3e-95d3-f1aa9f4ea312","outputId":"cc5263bf-f1d0-46dd-d554-dc096d327653","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:30.210967Z","iopub.execute_input":"2024-12-31T01:53:30.211256Z","iopub.status.idle":"2024-12-31T01:53:30.594527Z","shell.execute_reply.started":"2024-12-31T01:53:30.211224Z","shell.execute_reply":"2024-12-31T01:53:30.593711Z"}},"outputs":[],"execution_count":null},{"id":"d073f79b-e5e5-4516-ada7-9a0b6f311c24","cell_type":"code","source":"plt.figure(figsize=(6,3))\nsns.boxplot(x=df_train['Annual Income'])\nplt.show","metadata":{"id":"d073f79b-e5e5-4516-ada7-9a0b6f311c24","outputId":"0be5ec48-3e01-4911-947d-2c698084c10d","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:30.595272Z","iopub.execute_input":"2024-12-31T01:53:30.595514Z","iopub.status.idle":"2024-12-31T01:53:31.043402Z","shell.execute_reply.started":"2024-12-31T01:53:30.595496Z","shell.execute_reply":"2024-12-31T01:53:31.042466Z"}},"outputs":[],"execution_count":null},{"id":"dAi5M5tZ0p45","cell_type":"code","source":"# remove outliers\ndef wisker(col):\n\n    q1,q3 = np.percentile(col, [25,75])\n    iqr = q3 - q1\n    lw = q1 - 1.5 * iqr\n    uw = q3 + 1.5 * iqr\n    return lw, uw\n\nfor i in ['Premium Amount', 'Annual Income']:\n    lw,uw = wisker(df_train[i]) # 获取下界和上界\n    df_train[i] = np.where(df_train[i]<lw,lw,df_train[i]) # 将低于下界的值替换为下界\n    df_train[i] = np.where(df_train[i]>uw,uw,df_train[i]) # 将高于上界的值替换为上界","metadata":{"id":"dAi5M5tZ0p45","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:31.044346Z","iopub.execute_input":"2024-12-31T01:53:31.044605Z","iopub.status.idle":"2024-12-31T01:53:31.108682Z","shell.execute_reply.started":"2024-12-31T01:53:31.044584Z","shell.execute_reply":"2024-12-31T01:53:31.10777Z"}},"outputs":[],"execution_count":null},{"id":"9b30c3e3-5518-48b7-8596-6d0d13e06e22","cell_type":"markdown","source":"# duplicates","metadata":{"id":"9b30c3e3-5518-48b7-8596-6d0d13e06e22"}},{"id":"b21c8df0-5632-448a-bcf3-e289336d39db","cell_type":"code","source":"df_train.duplicated().any()","metadata":{"id":"b21c8df0-5632-448a-bcf3-e289336d39db","outputId":"ed68a907-c272-47e2-a9ad-e6f87097c37a","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:31.10963Z","iopub.execute_input":"2024-12-31T01:53:31.109926Z","iopub.status.idle":"2024-12-31T01:53:32.342243Z","shell.execute_reply.started":"2024-12-31T01:53:31.109896Z","shell.execute_reply":"2024-12-31T01:53:32.341444Z"}},"outputs":[],"execution_count":null},{"id":"60184cb8-265c-4aaf-abc8-631c8e9d9221","cell_type":"code","source":"df_test.duplicated().any()","metadata":{"id":"60184cb8-265c-4aaf-abc8-631c8e9d9221","outputId":"000778f5-3c28-4ee4-db33-1026fd217fdf","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:32.343028Z","iopub.execute_input":"2024-12-31T01:53:32.34325Z","iopub.status.idle":"2024-12-31T01:53:33.126815Z","shell.execute_reply.started":"2024-12-31T01:53:32.343231Z","shell.execute_reply":"2024-12-31T01:53:33.125964Z"}},"outputs":[],"execution_count":null},{"id":"f267720e-d625-4205-827f-99677689088b","cell_type":"markdown","source":"# Encoder","metadata":{"id":"f267720e-d625-4205-827f-99677689088b"}},{"id":"e5883be5-274f-4444-b2e2-2473d1bb8a35","cell_type":"code","source":"obj_cols.columns","metadata":{"id":"e5883be5-274f-4444-b2e2-2473d1bb8a35","outputId":"892903f3-8fd9-4648-f322-15c9902c0ec6","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:33.127669Z","iopub.execute_input":"2024-12-31T01:53:33.127931Z","iopub.status.idle":"2024-12-31T01:53:33.133135Z","shell.execute_reply.started":"2024-12-31T01:53:33.127912Z","shell.execute_reply":"2024-12-31T01:53:33.13241Z"}},"outputs":[],"execution_count":null},{"id":"f2fb5449-5e39-4934-a78a-da9f9487292a","cell_type":"code","source":"df_train['Property Type'].value_counts()","metadata":{"id":"f2fb5449-5e39-4934-a78a-da9f9487292a","outputId":"dca7f3f3-2b3c-4a9c-fe23-068db91f74d6","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:33.13629Z","iopub.execute_input":"2024-12-31T01:53:33.136519Z","iopub.status.idle":"2024-12-31T01:53:33.224431Z","shell.execute_reply.started":"2024-12-31T01:53:33.136501Z","shell.execute_reply":"2024-12-31T01:53:33.223602Z"}},"outputs":[],"execution_count":null},{"id":"ec90a4f7-7784-451d-88ea-aa8a42134cee","cell_type":"code","source":"# define mapping\n\ngender_mapping = {\n    'Male':1,\n    'Female':2\n}\n\nms_mapping = {\n    'Single':1,\n    'Married':2,\n    'Divorced':3\n}\n\nel_mapping = {\n    'High School':1,\n    \"Bachelor's\":2,\n    \"Master's\":3,\n    'PhD':4\n}\n\nocc_mapping = {\n    'Unknown':0,\n    'Employed':1,\n    'Self-Employed':2,\n    'Unemployed':3\n}\n\nlocation_mapping = {\n    'Suburban':1,\n    'Rural':2,\n    'Urban':3\n}\n\npolicy_mapping = {\n    'Basic':1,\n    'Comprehensive':2,\n    'Premium':3\n}\n\nfeedback_mapping = {\n    'Poor':1,\n    'Average':2,\n    'Good':3\n}\n\nsmoke_mapping = {\n    'No':0,\n    'Yes':1\n}\n\nexcerise_mapping = {\n    'Rarely':1,\n    'Monthly':2,\n    'Weekly':3,\n    'Daily':4\n}\n\nproperty_mapping = {\n    'House':1,\n    'Apartment':2,\n    'Condo':3\n}\n\nmappings = {\n    'Gender':gender_mapping,\n    'Marital Status':ms_mapping,\n    'Education Level':el_mapping,\n    'Occupation':occ_mapping,\n    'Location':location_mapping,\n    'Policy Type':policy_mapping,\n    'Customer Feedback':feedback_mapping,\n    'Smoking Status':smoke_mapping,\n    'Exercise Frequency':excerise_mapping,\n    'Property Type':property_mapping\n}\n\n# replace column values with mapping\nfor col, mapping in mappings.items():\n    if col in df_train.columns:  # make sure in the df_train\n        df_train[col] = df_train[col].map(mapping)\n    if col in df_test.columns:  # make sure in the df_test\n        df_test[col] = df_test[col].map(mapping)","metadata":{"id":"ec90a4f7-7784-451d-88ea-aa8a42134cee","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:33.225637Z","iopub.execute_input":"2024-12-31T01:53:33.225899Z","iopub.status.idle":"2024-12-31T01:53:34.337396Z","shell.execute_reply.started":"2024-12-31T01:53:33.225878Z","shell.execute_reply":"2024-12-31T01:53:34.336199Z"}},"outputs":[],"execution_count":null},{"id":"a4320014-eef9-4c31-b0b5-42c5ba7bffd6","cell_type":"markdown","source":"# Feature Enineering","metadata":{"id":"a4320014-eef9-4c31-b0b5-42c5ba7bffd6"}},{"id":"46b250da-ff58-4e82-ba40-2d8f9855e332","cell_type":"code","source":"# convert 'Policy Start Date' to date dtype\n# only keep year\ndf_train['Policy Start Date'] = pd.to_datetime(df_train['Policy Start Date']).dt.year\ndf_test['Policy Start Date'] = pd.to_datetime(df_test['Policy Start Date']).dt.year","metadata":{"id":"46b250da-ff58-4e82-ba40-2d8f9855e332","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:34.338641Z","iopub.execute_input":"2024-12-31T01:53:34.338983Z","iopub.status.idle":"2024-12-31T01:53:35.035396Z","shell.execute_reply.started":"2024-12-31T01:53:34.338951Z","shell.execute_reply":"2024-12-31T01:53:35.034731Z"}},"outputs":[],"execution_count":null},{"id":"ziWE882L4xRF","cell_type":"code","source":"# create groups for age\ndf_train['Age Group'] = pd.cut(df_train['Age'], bins=[0, 18, 30, 50, 65], labels=['0', '1', '2', '3'])\ndf_test['Age Group'] = pd.cut(df_test['Age'], bins=[0, 18, 30, 50, 65], labels=['0', '1', '2', '3'])","metadata":{"id":"ziWE882L4xRF","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:35.036154Z","iopub.execute_input":"2024-12-31T01:53:35.036406Z","iopub.status.idle":"2024-12-31T01:53:35.0881Z","shell.execute_reply.started":"2024-12-31T01:53:35.036385Z","shell.execute_reply":"2024-12-31T01:53:35.087084Z"}},"outputs":[],"execution_count":null},{"id":"Z5ljSTU_5W4I","cell_type":"code","source":"# create income level\ndf_train['Income Level'] = pd.qcut(df_train['Annual Income'], q=4, labels=['1', '2', '3', '4'])\ndf_test['Income Level'] = pd.qcut(df_test['Annual Income'], q=4, labels=['1', '2', '3', '4'])","metadata":{"id":"Z5ljSTU_5W4I","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:35.089161Z","iopub.execute_input":"2024-12-31T01:53:35.089507Z","iopub.status.idle":"2024-12-31T01:53:35.19441Z","shell.execute_reply.started":"2024-12-31T01:53:35.089475Z","shell.execute_reply":"2024-12-31T01:53:35.193487Z"}},"outputs":[],"execution_count":null},{"id":"Wdn5i_Mz5l8_","cell_type":"code","source":"# define vehicle age ratio\ndf_train['Vehicle Age Ratio'] = df_train['Vehicle Age'] / df_train['Age']\ndf_test['Vehicle Age Ratio'] = df_test['Vehicle Age'] / df_test['Age']\n#\ndf_train['Income_Credit Score'] = df_train['Annual Income'] * df_train['Credit Score']\ndf_test['Income_Credit Score'] = df_test['Annual Income'] * df_test['Credit Score']","metadata":{"id":"Wdn5i_Mz5l8_","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:35.195264Z","iopub.execute_input":"2024-12-31T01:53:35.195614Z","iopub.status.idle":"2024-12-31T01:53:35.216896Z","shell.execute_reply.started":"2024-12-31T01:53:35.195581Z","shell.execute_reply":"2024-12-31T01:53:35.216132Z"}},"outputs":[],"execution_count":null},{"id":"wtzNP3Wpt5Vp","cell_type":"markdown","source":"# standarlization","metadata":{"id":"wtzNP3Wpt5Vp"}},{"id":"VipnQ3xwubip","cell_type":"code","source":"# split data to train and test 2:8\n\nX = df_train.drop(['Premium Amount', 'id'],axis=1)\ny = df_train['Premium Amount']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"id":"VipnQ3xwubip","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:35.217746Z","iopub.execute_input":"2024-12-31T01:53:35.218052Z","iopub.status.idle":"2024-12-31T01:53:35.746308Z","shell.execute_reply.started":"2024-12-31T01:53:35.218022Z","shell.execute_reply":"2024-12-31T01:53:35.745601Z"}},"outputs":[],"execution_count":null},{"id":"R162DwKjt9PV","cell_type":"code","source":"# standardscaler data\nscaler = StandardScaler()\n\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"id":"R162DwKjt9PV","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:35.74717Z","iopub.execute_input":"2024-12-31T01:53:35.747413Z","iopub.status.idle":"2024-12-31T01:53:40.447949Z","shell.execute_reply.started":"2024-12-31T01:53:35.747392Z","shell.execute_reply":"2024-12-31T01:53:40.446973Z"}},"outputs":[],"execution_count":null},{"id":"cIkx5OiwtzaJ","cell_type":"markdown","source":"# Modeling","metadata":{"id":"cIkx5OiwtzaJ"}},{"id":"KplAf3sQUaix","cell_type":"code","source":"pd.set_option('display.max_columns', None)\ndf_train.head()","metadata":{"id":"KplAf3sQUaix","outputId":"475ab1fc-9ad2-4250-d44d-dce443d0318d","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:40.448839Z","iopub.execute_input":"2024-12-31T01:53:40.449105Z","iopub.status.idle":"2024-12-31T01:53:40.468961Z","shell.execute_reply.started":"2024-12-31T01:53:40.449082Z","shell.execute_reply":"2024-12-31T01:53:40.468005Z"}},"outputs":[],"execution_count":null},{"id":"WnWEWXx9v4vO","cell_type":"code","source":"# 模型列表\nmodels = {\n    'Linear Regression': LinearRegression(),\n    'Ridge': Ridge(alpha=1.0, random_state=42),\n    'XGBoost': XGBRegressor(random_state=42),\n    'LightGBM': lgb.LGBMRegressor(random_state=42),\n    'Decision Tree': DecisionTreeRegressor(max_depth=5),\n    'Cat Boost': CatBoostRegressor(verbose=0, task_type='GPU')\n}\n\nfor model_name, model in models.items():\n    print(f\"Training and evaluating model: {model_name}\")\n\n    # 训练模型\n    model.fit(X_train, y_train)\n\n    # 验证集预测\n    y_test_pred = model.predict(X_test)\n\n    # 验证集评估\n    test_MAE = mean_absolute_error(y_test, y_test_pred)\n    test_R2 = r2_score(y_test, y_test_pred)\n\n    # 打印验证集指标\n    print(f\"{model_name} - Test MAE: {test_MAE:.4f}\")\n    print(f\"{model_name} - Test R^2: {test_R2:.4f}\\n\")\n","metadata":{"id":"WnWEWXx9v4vO","outputId":"c7371cc6-b78e-4604-9234-577a60a29d7d","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:53:40.470064Z","iopub.execute_input":"2024-12-31T01:53:40.47033Z","iopub.status.idle":"2024-12-31T01:54:05.038811Z","shell.execute_reply.started":"2024-12-31T01:53:40.470296Z","shell.execute_reply":"2024-12-31T01:54:05.037861Z"}},"outputs":[],"execution_count":null},{"id":"cbe0322e-8475-4b96-b9f0-6fcf7be7b782","cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n\n# 超参数网格\nparam_grid = {\n    'iterations': [500, 1000, 1500],\n    'learning_rate': [0.01, 0.1, 0.2],\n    'depth': [4, 6, 8],\n    'l2_leaf_reg': [1, 3, 5]\n}\n\ncat_model = CatBoostRegressor(verbose=0, task_type='GPU')\n\n# 网格搜索\ngrid = GridSearchCV(cat_model, param_grid, scoring='neg_mean_absolute_error', cv=10)\ngrid.fit(X_train, y_train)\n\n# 最佳参数\nprint(\"Best Parameters:\", grid.best_params_)\n\n# 使用最佳参数训练模型\nbest_cat_model = grid.best_estimator_\n\n# 验证集预测\ny_val_pred = best_cat_model.predict(X_val)\n\n# 模型评估\nval_MAE = mean_absolute_error(y_val, y_val_pred)\nval_R2 = r2_score(y_val, y_val_pred)\n\nprint(f\"Validation MAE: {val_MAE:.4f}\")\nprint(f\"Validation R^2: {val_R2:.4f}\")","metadata":{"id":"cbe0322e-8475-4b96-b9f0-6fcf7be7b782","trusted":true,"execution":{"iopub.status.busy":"2024-12-31T01:54:05.039776Z","iopub.execute_input":"2024-12-31T01:54:05.040129Z"}},"outputs":[],"execution_count":null},{"id":"62d38ecb-9bd1-4969-a716-a1fdeaa2950e","cell_type":"code","source":"\n# 获取特征重要性\nfeature_importances = best_cat_model.get_feature_importance(prettified=True)\n\n# 打印特征重要性\nprint(feature_importances)\n\n# 可视化\nplt.figure(figsize=(10, 6))\nplt.barh(feature_importances['Feature Id'], feature_importances['Importances'])\nplt.xlabel(\"Importance\")\nplt.ylabel(\"Feature\")\nplt.title(\"Feature Importance\")\nplt.show()","metadata":{"id":"62d38ecb-9bd1-4969-a716-a1fdeaa2950e","trusted":true},"outputs":[],"execution_count":null},{"id":"G-4MrniMS_38","cell_type":"markdown","source":"# submission","metadata":{"id":"G-4MrniMS_38"}},{"id":"7de6dc87-fe25-4f8a-918c-c601a83bfe58","cell_type":"code","source":"X_test_final = df_test.drop('id', axis=1)\n\nscaler = StandardScaler()\nscaler.fit(df_test)\nX_test_final = scaler.transform(df_test)\n\ndf_test['Premium Amount'] = best_cat_model.predict(X_test_final)\n\nsubmission = df_test[['id', 'Premium Amount']]\n# 保存结果到 CSV 文件\nsubmission.to_csv('submission.csv', index=False)\n\nprint(submission.head())","metadata":{"id":"7de6dc87-fe25-4f8a-918c-c601a83bfe58","trusted":true},"outputs":[],"execution_count":null},{"id":"3e6e922f-a72e-4b00-8c4a-dbbaa6282e72","cell_type":"code","source":"","metadata":{"id":"3e6e922f-a72e-4b00-8c4a-dbbaa6282e72","trusted":true},"outputs":[],"execution_count":null},{"id":"3b3c6e36-ed6f-4e29-b82d-c1eb412f0e58","cell_type":"code","source":"","metadata":{"id":"3b3c6e36-ed6f-4e29-b82d-c1eb412f0e58","trusted":true},"outputs":[],"execution_count":null},{"id":"cc94a1b3-2b8b-4320-9be9-c1d58df23cd6","cell_type":"code","source":"","metadata":{"id":"cc94a1b3-2b8b-4320-9be9-c1d58df23cd6","trusted":true},"outputs":[],"execution_count":null},{"id":"b4342105-c2f9-49bc-9073-d81f292f2de9","cell_type":"code","source":"","metadata":{"id":"b4342105-c2f9-49bc-9073-d81f292f2de9","trusted":true},"outputs":[],"execution_count":null},{"id":"c43a8ca2-a88a-4d24-bcdd-0f3bd77d5e7a","cell_type":"code","source":"","metadata":{"id":"c43a8ca2-a88a-4d24-bcdd-0f3bd77d5e7a","trusted":true},"outputs":[],"execution_count":null}]}