{"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":"# 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:12.235657Z","iopub.execute_input":"2024-12-09T07:03:12.236083Z","iopub.status.idle":"2024-12-09T07:03:12.61842Z","shell.execute_reply.started":"2024-12-09T07:03:12.23602Z","shell.execute_reply":"2024-12-09T07:03:12.617386Z"}},"outputs":[],"execution_count":null},{"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')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:13.482677Z","iopub.execute_input":"2024-12-09T07:03:13.483232Z","iopub.status.idle":"2024-12-09T07:03:23.67359Z","shell.execute_reply.started":"2024-12-09T07:03:13.483191Z","shell.execute_reply":"2024-12-09T07:03:23.672547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:33.182284Z","iopub.execute_input":"2024-12-09T07:03:33.182634Z","iopub.status.idle":"2024-12-09T07:03:33.222672Z","shell.execute_reply.started":"2024-12-09T07:03:33.182607Z","shell.execute_reply":"2024-12-09T07:03:33.221597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:36.527462Z","iopub.execute_input":"2024-12-09T07:03:36.527869Z","iopub.status.idle":"2024-12-09T07:03:36.549447Z","shell.execute_reply.started":"2024-12-09T07:03:36.527835Z","shell.execute_reply":"2024-12-09T07:03:36.548107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:36.915548Z","iopub.execute_input":"2024-12-09T07:03:36.915946Z","iopub.status.idle":"2024-12-09T07:03:37.597693Z","shell.execute_reply.started":"2024-12-09T07:03:36.915912Z","shell.execute_reply":"2024-12-09T07:03:37.596637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:37.599502Z","iopub.execute_input":"2024-12-09T07:03:37.59982Z","iopub.status.idle":"2024-12-09T07:03:38.308965Z","shell.execute_reply.started":"2024-12-09T07:03:37.599791Z","shell.execute_reply":"2024-12-09T07:03:38.307938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Policy Start Date'] = pd.to_datetime(train['Policy Start Date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:38.310311Z","iopub.execute_input":"2024-12-09T07:03:38.310682Z","iopub.status.idle":"2024-12-09T07:03:38.78858Z","shell.execute_reply.started":"2024-12-09T07:03:38.310649Z","shell.execute_reply":"2024-12-09T07:03:38.787385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:38.791326Z","iopub.execute_input":"2024-12-09T07:03:38.79176Z","iopub.status.idle":"2024-12-09T07:03:39.375925Z","shell.execute_reply.started":"2024-12-09T07:03:38.791713Z","shell.execute_reply":"2024-12-09T07:03:39.374928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Smoking Status'].unique","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:39.377311Z","iopub.execute_input":"2024-12-09T07:03:39.377739Z","iopub.status.idle":"2024-12-09T07:03:39.38627Z","shell.execute_reply.started":"2024-12-09T07:03:39.377694Z","shell.execute_reply":"2024-12-09T07:03:39.385127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Smoking Status'] = train['Smoking Status'].map({'Yes': 1, 'No': 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:39.387801Z","iopub.execute_input":"2024-12-09T07:03:39.388187Z","iopub.status.idle":"2024-12-09T07:03:39.472024Z","shell.execute_reply.started":"2024-12-09T07:03:39.388156Z","shell.execute_reply":"2024-12-09T07:03:39.470988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:39.473613Z","iopub.execute_input":"2024-12-09T07:03:39.473914Z","iopub.status.idle":"2024-12-09T07:03:40.007287Z","shell.execute_reply.started":"2024-12-09T07:03:39.473871Z","shell.execute_reply":"2024-12-09T07:03:40.006326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Exercise Frequency'].unique","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.008642Z","iopub.execute_input":"2024-12-09T07:03:40.008966Z","iopub.status.idle":"2024-12-09T07:03:40.017063Z","shell.execute_reply.started":"2024-12-09T07:03:40.008936Z","shell.execute_reply":"2024-12-09T07:03:40.015973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Property Type'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.018658Z","iopub.execute_input":"2024-12-09T07:03:40.019071Z","iopub.status.idle":"2024-12-09T07:03:40.125244Z","shell.execute_reply.started":"2024-12-09T07:03:40.019008Z","shell.execute_reply":"2024-12-09T07:03:40.12423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Policy Age'] = (pd.Timestamp.now() - pd.to_datetime(train['Policy Start Date'])).dt.days","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.129258Z","iopub.execute_input":"2024-12-09T07:03:40.129549Z","iopub.status.idle":"2024-12-09T07:03:40.191615Z","shell.execute_reply.started":"2024-12-09T07:03:40.129522Z","shell.execute_reply":"2024-12-09T07:03:40.190513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(['id', 'Premium Amount', 'Policy Start Date'], axis=1)\ny = train['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.19288Z","iopub.execute_input":"2024-12-09T07:03:40.193202Z","iopub.status.idle":"2024-12-09T07:03:40.294977Z","shell.execute_reply.started":"2024-12-09T07:03:40.193172Z","shell.execute_reply":"2024-12-09T07:03:40.293941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Identify numerical and categorical columns\nnumerical_cols = X.select_dtypes(include=['float64', 'int64']).columns\ncategorical_cols = X.select_dtypes(include=['object']).columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.296162Z","iopub.execute_input":"2024-12-09T07:03:40.296521Z","iopub.status.idle":"2024-12-09T07:03:40.840238Z","shell.execute_reply.started":"2024-12-09T07:03:40.296483Z","shell.execute_reply":"2024-12-09T07:03:40.838984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define preprocessing for numerical data\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nnumerical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='mean')),\n    ('scaler', StandardScaler())\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:40.841578Z","iopub.execute_input":"2024-12-09T07:03:40.841963Z","iopub.status.idle":"2024-12-09T07:03:41.661704Z","shell.execute_reply.started":"2024-12-09T07:03:40.841923Z","shell.execute_reply":"2024-12-09T07:03:41.660688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define preprocessing for categorical data\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.662927Z","iopub.execute_input":"2024-12-09T07:03:41.663406Z","iopub.status.idle":"2024-12-09T07:03:41.668762Z","shell.execute_reply.started":"2024-12-09T07:03:41.663374Z","shell.execute_reply":"2024-12-09T07:03:41.667565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.670186Z","iopub.execute_input":"2024-12-09T07:03:41.6706Z","iopub.status.idle":"2024-12-09T07:03:41.698106Z","shell.execute_reply.started":"2024-12-09T07:03:41.670556Z","shell.execute_reply":"2024-12-09T07:03:41.696974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Let's import necessary modules \n\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.699465Z","iopub.execute_input":"2024-12-09T07:03:41.70027Z","iopub.status.idle":"2024-12-09T07:03:41.800065Z","shell.execute_reply.started":"2024-12-09T07:03:41.700222Z","shell.execute_reply":"2024-12-09T07:03:41.799202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = RandomForestRegressor(n_estimators=100, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.801669Z","iopub.execute_input":"2024-12-09T07:03:41.80207Z","iopub.status.idle":"2024-12-09T07:03:41.807104Z","shell.execute_reply.started":"2024-12-09T07:03:41.802006Z","shell.execute_reply":"2024-12-09T07:03:41.806067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', model)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.808382Z","iopub.execute_input":"2024-12-09T07:03:41.808677Z","iopub.status.idle":"2024-12-09T07:03:41.818768Z","shell.execute_reply.started":"2024-12-09T07:03:41.80865Z","shell.execute_reply":"2024-12-09T07:03:41.81787Z"}},"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)\npipeline.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:03:41.820098Z","iopub.execute_input":"2024-12-09T07:03:41.820465Z","iopub.status.idle":"2024-12-09T07:35:50.827307Z","shell.execute_reply.started":"2024-12-09T07:03:41.820431Z","shell.execute_reply":"2024-12-09T07:35:50.826171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = pipeline.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:35:50.82863Z","iopub.execute_input":"2024-12-09T07:35:50.828943Z","iopub.status.idle":"2024-12-09T07:36:15.85063Z","shell.execute_reply.started":"2024-12-09T07:35:50.82891Z","shell.execute_reply":"2024-12-09T07:36:15.849468Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mae = mean_absolute_error(y_test, y_pred)\nmse = mean_squared_error(y_test, y_pred)\nr2 = r2_score(y_test, y_pred)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:36:15.8527Z","iopub.execute_input":"2024-12-09T07:36:15.853164Z","iopub.status.idle":"2024-12-09T07:36:15.864568Z","shell.execute_reply.started":"2024-12-09T07:36:15.853116Z","shell.execute_reply":"2024-12-09T07:36:15.863432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Model Evaluation:\")\nprint(f\"Mean Absolute Error (MAE): {mae}\")\nprint(f\"Mean Squared Error (MSE): {mse}\")\nprint(f\"R-squared (R2): {r2}\")\nr2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T08:25:12.255418Z","iopub.execute_input":"2024-12-09T08:25:12.256371Z","iopub.status.idle":"2024-12-09T08:25:12.266944Z","shell.execute_reply.started":"2024-12-09T08:25:12.256317Z","shell.execute_reply":"2024-12-09T08:25:12.265703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:24:36.438414Z","iopub.execute_input":"2024-12-09T09:24:36.439665Z","iopub.status.idle":"2024-12-09T09:24:39.844923Z","shell.execute_reply.started":"2024-12-09T09:24:36.439607Z","shell.execute_reply":"2024-12-09T09:24:39.844001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:24:42.375653Z","iopub.execute_input":"2024-12-09T09:24:42.376025Z","iopub.status.idle":"2024-12-09T09:24:42.382399Z","shell.execute_reply.started":"2024-12-09T09:24:42.375994Z","shell.execute_reply":"2024-12-09T09:24:42.381365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"default_start_date = \"2020-01-01\"  \ntest['Policy Start Date'] = default_start_date\ntest['Policy Start Date'] = pd.to_datetime(test['Policy Start Date'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:24:46.91416Z","iopub.execute_input":"2024-12-09T09:24:46.915135Z","iopub.status.idle":"2024-12-09T09:24:46.99795Z","shell.execute_reply.started":"2024-12-09T09:24:46.915091Z","shell.execute_reply":"2024-12-09T09:24:46.997013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Policy Start Date'] = pd.to_datetime(test['Policy Start Date'], errors='coerce')  \ntest['Policy Age'] = (pd.Timestamp.now() - test['Policy Start Date']).dt.days\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:24:49.844028Z","iopub.execute_input":"2024-12-09T09:24:49.84488Z","iopub.status.idle":"2024-12-09T09:24:49.903387Z","shell.execute_reply.started":"2024-12-09T09:24:49.844839Z","shell.execute_reply":"2024-12-09T09:24:49.902289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:24:57.292392Z","iopub.execute_input":"2024-12-09T09:24:57.293147Z","iopub.status.idle":"2024-12-09T09:24:57.316987Z","shell.execute_reply.started":"2024-12-09T09:24:57.293107Z","shell.execute_reply":"2024-12-09T09:24:57.315877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Smoking Status'] = test['Smoking Status'].map({'Yes': 1, 'No': 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:25:03.922373Z","iopub.execute_input":"2024-12-09T09:25:03.923221Z","iopub.status.idle":"2024-12-09T09:25:03.976805Z","shell.execute_reply.started":"2024-12-09T09:25:03.92318Z","shell.execute_reply":"2024-12-09T09:25:03.975638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:25:11.000961Z","iopub.execute_input":"2024-12-09T09:25:11.00141Z","iopub.status.idle":"2024-12-09T09:25:11.029454Z","shell.execute_reply.started":"2024-12-09T09:25:11.001374Z","shell.execute_reply":"2024-12-09T09:25:11.02821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_cols = [col for col in train.columns if col not in test.columns]\nif missing_cols:\n    print(f\"Columns missing in test data: {missing_cols}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:25:35.004197Z","iopub.execute_input":"2024-12-09T09:25:35.00463Z","iopub.status.idle":"2024-12-09T09:25:35.01155Z","shell.execute_reply.started":"2024-12-09T09:25:35.004588Z","shell.execute_reply":"2024-12-09T09:25:35.010329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = pipeline.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:26:07.175768Z","iopub.execute_input":"2024-12-09T09:26:07.176343Z","iopub.status.idle":"2024-12-09T09:27:17.306154Z","shell.execute_reply.started":"2024-12-09T09:26:07.176306Z","shell.execute_reply":"2024-12-09T09:27:17.304968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Predicted Premium Amount'] = test_predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:27:36.174318Z","iopub.execute_input":"2024-12-09T09:27:36.174778Z","iopub.status.idle":"2024-12-09T09:27:36.18138Z","shell.execute_reply.started":"2024-12-09T09:27:36.174739Z","shell.execute_reply":"2024-12-09T09:27:36.180219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test[['id', 'Predicted Premium Amount']].to_csv('test_predictions.csv', index=False)\n\nprint(\"Predictions saved to 'test_predictions.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:27:41.902329Z","iopub.execute_input":"2024-12-09T09:27:41.902735Z","iopub.status.idle":"2024-12-09T09:27:43.028941Z","shell.execute_reply.started":"2024-12-09T09:27:41.902699Z","shell.execute_reply":"2024-12-09T09:27:43.027817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = pd.read_csv('test_predictions.csv')\noutput","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:35:21.732004Z","iopub.execute_input":"2024-12-09T09:35:21.732444Z","iopub.status.idle":"2024-12-09T09:35:21.909142Z","shell.execute_reply.started":"2024-12-09T09:35:21.732409Z","shell.execute_reply":"2024-12-09T09:35:21.908099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')\nsample","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T09:34:05.346405Z","iopub.execute_input":"2024-12-09T09:34:05.346817Z","iopub.status.idle":"2024-12-09T09:34:05.533228Z","shell.execute_reply.started":"2024-12-09T09:34:05.34678Z","shell.execute_reply":"2024-12-09T09:34:05.532105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}