{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:00:50.608311Z","iopub.execute_input":"2024-12-09T12:00:50.608606Z","iopub.status.idle":"2024-12-09T12:00:50.615491Z","shell.execute_reply.started":"2024-12-09T12:00:50.608578Z","shell.execute_reply":"2024-12-09T12:00:50.614663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install xgbtune","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:00:39.112336Z","iopub.execute_input":"2024-12-09T12:00:39.112662Z","iopub.status.idle":"2024-12-09T12:00:50.606111Z","shell.execute_reply.started":"2024-12-09T12:00:39.112604Z","shell.execute_reply":"2024-12-09T12:00:50.605219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler, FunctionTransformer,OneHotEncoder\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.utils.validation import check_array\nimport xgboost as xgb\nfrom xgbtune import tune_xgb_model\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:01:12.396603Z","iopub.execute_input":"2024-12-09T12:01:12.397559Z","iopub.status.idle":"2024-12-09T12:01:13.573347Z","shell.execute_reply.started":"2024-12-09T12:01:12.397514Z","shell.execute_reply":"2024-12-09T12:01:13.572665Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Loading <!--  -->","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:01:56.447893Z","iopub.execute_input":"2024-12-09T12:01:56.448987Z","iopub.status.idle":"2024-12-09T12:02:04.460803Z","shell.execute_reply.started":"2024-12-09T12:01:56.448955Z","shell.execute_reply":"2024-12-09T12:02:04.460066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:02:18.801584Z","iopub.execute_input":"2024-12-09T12:02:18.802412Z","iopub.status.idle":"2024-12-09T12:02:18.838952Z","shell.execute_reply.started":"2024-12-09T12:02:18.802364Z","shell.execute_reply":"2024-12-09T12:02:18.838043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:02:50.642784Z","iopub.execute_input":"2024-12-09T12:02:50.643127Z","iopub.status.idle":"2024-12-09T12:02:50.665683Z","shell.execute_reply.started":"2024-12-09T12:02:50.643099Z","shell.execute_reply":"2024-12-09T12:02:50.664655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop('id', axis=1, inplace=True)\ntest.drop('id', axis=1, inplace=True) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:02:55.056909Z","iopub.execute_input":"2024-12-09T12:02:55.057257Z","iopub.status.idle":"2024-12-09T12:02:55.319393Z","shell.execute_reply.started":"2024-12-09T12:02:55.057228Z","shell.execute_reply":"2024-12-09T12:02:55.318731Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reduce memory usage","metadata":{}},{"cell_type":"code","source":"def reduce_memory_usage(df):\n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type == 'float64':\n            df[col] = df[col].astype('float32')\n        elif col_type == 'int64':\n            df[col] = df[col].astype('int32')\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:26.004679Z","iopub.execute_input":"2024-12-09T12:33:26.005008Z","iopub.status.idle":"2024-12-09T12:33:26.009924Z","shell.execute_reply.started":"2024-12-09T12:33:26.004982Z","shell.execute_reply":"2024-12-09T12:33:26.009025Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Extract date features","metadata":{}},{"cell_type":"code","source":"def extract_date_features(df, date_column='Policy Start Date'):\n    df[date_column] = pd.to_datetime(df[date_column])\n    df['Year'] = df[date_column].dt.year\n    df['Month'] = df[date_column].dt.month\n    df['Day'] = df[date_column].dt.day\n    df['Quarter'] = df[date_column].dt.quarter\n    df['Day of Week'] = df[date_column].dt.dayofweek\n    df.drop(columns=[date_column], inplace=True)\n    return df\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:35.854558Z","iopub.execute_input":"2024-12-09T12:33:35.855491Z","iopub.status.idle":"2024-12-09T12:33:35.86172Z","shell.execute_reply.started":"2024-12-09T12:33:35.855446Z","shell.execute_reply":"2024-12-09T12:33:35.86083Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Summarize columns","metadata":{}},{"cell_type":"code","source":"def column_summary(df):\n    summary = []\n    for col in df.columns:\n        summary.append({\n            'col_name': col,\n            'dtype': df[col].dtype,\n            'nulls': df[col].isnull().sum(),\n            'unique_values': df[col].nunique()\n        })\n    return pd.DataFrame(summary)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:39.094783Z","iopub.execute_input":"2024-12-09T12:33:39.095361Z","iopub.status.idle":"2024-12-09T12:33:39.099902Z","shell.execute_reply.started":"2024-12-09T12:33:39.095331Z","shell.execute_reply":"2024-12-09T12:33:39.098988Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Function to calculate RMSLE","metadata":{}},{"cell_type":"code","source":"def rmsle(y_true, y_pred):\n    y_true = check_array(y_true, ensure_2d=False)\n    y_pred = check_array(y_pred, ensure_2d=False)\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:46.254432Z","iopub.execute_input":"2024-12-09T12:33:46.255198Z","iopub.status.idle":"2024-12-09T12:33:46.259072Z","shell.execute_reply.started":"2024-12-09T12:33:46.255166Z","shell.execute_reply":"2024-12-09T12:33:46.258253Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"code","source":"# Date-based features\ntrain = extract_date_features(train)\ntest = extract_date_features(test)\n\n# Derived ratios\ntrain['Annual_Income_Health_Score_Ratio'] = train['Health Score'] / train['Annual Income']\ntest['Annual_Income_Health_Score_Ratio'] = test['Health Score'] / test['Annual Income']\n\ntrain['Annual_Income_Age_Ratio'] = train['Annual Income'] / train['Age']\ntest['Annual_Income_Age_Ratio'] = test['Annual Income'] / test['Age']\n\ntrain['Credit_Age'] = train['Credit Score'] / train['Age']\ntest['Credit_Age'] = test['Credit Score'] / test['Age']\n\ntrain['Vehicle_Age_Insurance_Duration'] = train['Vehicle Age'] / train['Insurance Duration']\ntest['Vehicle_Age_Insurance_Duration'] = test['Vehicle Age'] / test['Insurance Duration']\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:50.199994Z","iopub.execute_input":"2024-12-09T12:33:50.201041Z","iopub.status.idle":"2024-12-09T12:33:51.566892Z","shell.execute_reply.started":"2024-12-09T12:33:50.201005Z","shell.execute_reply":"2024-12-09T12:33:51.56619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# High-income indicator\nincome_threshold = train['Annual Income'].mean()\ntrain['Is High Income'] = (train['Annual Income'] > income_threshold).astype(int)\ntest['Is High Income'] = (test['Annual Income'] > income_threshold).astype(int)\n\n# Combined categorical feature\ntrain['Property_Location_Type'] = train['Location'] + '_' + train['Property Type']\ntest['Property_Location_Type'] = test['Location'] + '_' + test['Property Type']\n\n# Drop unused columns\ntrain.drop(columns=['Property Type'], inplace=True)\ntest.drop(columns=['Property Type'], inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:54.674545Z","iopub.execute_input":"2024-12-09T12:33:54.675396Z","iopub.status.idle":"2024-12-09T12:33:55.492597Z","shell.execute_reply.started":"2024-12-09T12:33:54.675362Z","shell.execute_reply":"2024-12-09T12:33:55.491671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preview transformed train data\ndisplay(train.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:33:58.534541Z","iopub.execute_input":"2024-12-09T12:33:58.535364Z","iopub.status.idle":"2024-12-09T12:33:58.55726Z","shell.execute_reply.started":"2024-12-09T12:33:58.53533Z","shell.execute_reply":"2024-12-09T12:33:58.556337Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# Reduce memory usage\ntrain = reduce_memory_usage(train)\ntest = reduce_memory_usage(test)\n\n# Target variable\ntarget = 'Premium Amount'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:34:14.174389Z","iopub.execute_input":"2024-12-09T12:34:14.175046Z","iopub.status.idle":"2024-12-09T12:34:14.229816Z","shell.execute_reply.started":"2024-12-09T12:34:14.175011Z","shell.execute_reply":"2024-12-09T12:34:14.229112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Separate numerical and categorical features\nnumerical_cols = train.select_dtypes(include=['float32', 'int32']).columns.tolist()\nif target in numerical_cols:\n    numerical_cols.remove(target)\n\ncategorical_cols = train.select_dtypes(include=['object']).columns.tolist()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:34:17.999121Z","iopub.execute_input":"2024-12-09T12:34:17.999763Z","iopub.status.idle":"2024-12-09T12:34:18.578186Z","shell.execute_reply.started":"2024-12-09T12:34:17.99973Z","shell.execute_reply":"2024-12-09T12:34:18.577257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preprocessing pipeline\npreprocessing = ColumnTransformer([\n    ('num', make_pipeline(SimpleImputer(strategy='mean'), StandardScaler()), numerical_cols),\n    ('cat', make_pipeline(SimpleImputer(strategy='constant', fill_value='unknown'), \n                          OneHotEncoder(handle_unknown='ignore')), categorical_cols)\n])\n\n# Separate features and target for training\nX_train = train.drop(columns=[target])\ny_train = np.log1p(train[target])  # Log-transform the target for skew handling\n\nX_test = test.copy()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:34:21.434406Z","iopub.execute_input":"2024-12-09T12:34:21.434854Z","iopub.status.idle":"2024-12-09T12:34:22.024763Z","shell.execute_reply.started":"2024-12-09T12:34:21.434817Z","shell.execute_reply":"2024-12-09T12:34:22.024034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Apply preprocessing\nX_train_preprocessed = preprocessing.fit_transform(X_train)\nX_test_preprocessed = preprocessing.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:35:07.435017Z","iopub.execute_input":"2024-12-09T12:35:07.435683Z","iopub.status.idle":"2024-12-09T12:35:15.389301Z","shell.execute_reply.started":"2024-12-09T12:35:07.43565Z","shell.execute_reply":"2024-12-09T12:35:15.388575Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"# Initial XGBoost parameters\nparams = {'eval_metric': 'rmsle', 'tree_method': 'hist', 'device': 'cuda'}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:35:18.914977Z","iopub.execute_input":"2024-12-09T12:35:18.915878Z","iopub.status.idle":"2024-12-09T12:35:18.920088Z","shell.execute_reply.started":"2024-12-09T12:35:18.915827Z","shell.execute_reply":"2024-12-09T12:35:18.919091Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Hyperparameter tuning\nparams, round_count = tune_xgb_model(params, X_train_preprocessed, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T12:35:22.694639Z","iopub.execute_input":"2024-12-09T12:35:22.694953Z","iopub.status.idle":"2024-12-09T13:00:08.270495Z","shell.execute_reply.started":"2024-12-09T12:35:22.694927Z","shell.execute_reply":"2024-12-09T13:00:08.269689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train final model\ndtrain = xgb.DMatrix(X_train_preprocessed, label=y_train)\nfinal_model = xgb.train(params, dtrain, num_boost_round=round_count)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T13:01:58.730753Z","iopub.execute_input":"2024-12-09T13:01:58.731076Z","iopub.status.idle":"2024-12-09T13:02:00.34407Z","shell.execute_reply.started":"2024-12-09T13:01:58.731052Z","shell.execute_reply":"2024-12-09T13:02:00.343332Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predictions and Submission","metadata":{}},{"cell_type":"code","source":"# Generate predictions\ndtest = xgb.DMatrix(X_test_preprocessed)\ny_pred = final_model.predict(dtest)\n\n# Transform predictions back from log scale\ny_pred_final = np.expm1(y_pred)\n\n# Create submission file\nsubmission = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")\nsubmission['Premium Amount'] = y_pred_final\nsubmission.to_csv('submission.csv', index=False)\n\n# Preview submission\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T13:02:17.630592Z","iopub.execute_input":"2024-12-09T13:02:17.631427Z","iopub.status.idle":"2024-12-09T13:02:19.480627Z","shell.execute_reply.started":"2024-12-09T13:02:17.631397Z","shell.execute_reply":"2024-12-09T13:02:19.479636Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Analyse and visualize","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# Visualize feature importance\nxgb.plot_importance(final_model, max_num_features=10)\nplt.show()\n\n# Check distribution of predictions\nsns.histplot(y_pred_final, kde=True)\nplt.title(\"Distribution of Predictions\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T13:02:55.250534Z","iopub.execute_input":"2024-12-09T13:02:55.250911Z","iopub.status.idle":"2024-12-09T13:03:00.355511Z","shell.execute_reply.started":"2024-12-09T13:02:55.25088Z","shell.execute_reply":"2024-12-09T13:03:00.35454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}