{"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# 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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:35.966397Z","iopub.execute_input":"2024-12-16T08:28:35.966831Z","iopub.status.idle":"2024-12-16T08:28:36.29743Z","shell.execute_reply.started":"2024-12-16T08:28:35.966794Z","shell.execute_reply":"2024-12-16T08:28:36.296361Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Imports**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nimport xgboost as xgb\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import OneHotEncoder\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:36.299239Z","iopub.execute_input":"2024-12-16T08:28:36.299612Z","iopub.status.idle":"2024-12-16T08:28:36.817145Z","shell.execute_reply.started":"2024-12-16T08:28:36.299589Z","shell.execute_reply":"2024-12-16T08:28:36.815862Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Read the Train data**","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(r\"/kaggle/input/playground-series-s4e12/train.csv\")\ntrain_data2 = pd.read_csv(r\"/kaggle/input/playground-series-s4e12/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:36.818294Z","iopub.execute_input":"2024-12-16T08:28:36.818792Z","iopub.status.idle":"2024-12-16T08:28:41.99196Z","shell.execute_reply.started":"2024-12-16T08:28:36.818756Z","shell.execute_reply":"2024-12-16T08:28:41.991009Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Read the Test data**","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(r\"/kaggle/input/playground-series-s4e12/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:41.994274Z","iopub.execute_input":"2024-12-16T08:28:41.994625Z","iopub.status.idle":"2024-12-16T08:28:43.513821Z","shell.execute_reply.started":"2024-12-16T08:28:41.994593Z","shell.execute_reply":"2024-12-16T08:28:43.513035Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **HEAD**","metadata":{}},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:43.514672Z","iopub.execute_input":"2024-12-16T08:28:43.514903Z","iopub.status.idle":"2024-12-16T08:28:43.538315Z","shell.execute_reply.started":"2024-12-16T08:28:43.514881Z","shell.execute_reply":"2024-12-16T08:28:43.537213Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Data Info**","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:43.539392Z","iopub.execute_input":"2024-12-16T08:28:43.539675Z","iopub.status.idle":"2024-12-16T08:28:43.994261Z","shell.execute_reply.started":"2024-12-16T08:28:43.539646Z","shell.execute_reply":"2024-12-16T08:28:43.993395Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Cat and Num Data**","metadata":{}},{"cell_type":"code","source":"num=[]\ncat=[]\n\nfor col in train_data.columns :\n    if train_data[col].dtype==\"object\" :\n        cat.append(col)\n    else :\n        num.append(col)\nnum.remove(\"Premium Amount\")\nnum.remove(\"id\")\nprint(\"numerical columns : \" + str(num))\nprint(\"categorical columns : \"+str(cat))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:43.995549Z","iopub.execute_input":"2024-12-16T08:28:43.995879Z","iopub.status.idle":"2024-12-16T08:28:44.003455Z","shell.execute_reply.started":"2024-12-16T08:28:43.995837Z","shell.execute_reply":"2024-12-16T08:28:44.001395Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Nulls**","metadata":{}},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:44.004981Z","iopub.execute_input":"2024-12-16T08:28:44.005315Z","iopub.status.idle":"2024-12-16T08:28:44.461755Z","shell.execute_reply.started":"2024-12-16T08:28:44.005284Z","shell.execute_reply":"2024-12-16T08:28:44.460983Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Unique values**","metadata":{}},{"cell_type":"code","source":"for col in cat : \n    print(train_data[col].value_counts())\n    print(\"-------------------\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:44.462974Z","iopub.execute_input":"2024-12-16T08:28:44.463203Z","iopub.status.idle":"2024-12-16T08:28:45.677468Z","shell.execute_reply.started":"2024-12-16T08:28:44.46317Z","shell.execute_reply":"2024-12-16T08:28:45.676238Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Date and time Dtype**","metadata":{}},{"cell_type":"code","source":"train_data[\"Policy Start Date\"]=pd.to_datetime(train_data[\"Policy Start Date\"])\ntest_data[\"Policy Start Date\"]=pd.to_datetime(test_data[\"Policy Start Date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:45.680975Z","iopub.execute_input":"2024-12-16T08:28:45.681743Z","iopub.status.idle":"2024-12-16T08:28:46.062306Z","shell.execute_reply.started":"2024-12-16T08:28:45.681713Z","shell.execute_reply":"2024-12-16T08:28:46.061461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"year\"]=train_data[\"Policy Start Date\"].dt.year\nnum.append(\"year\")\ntest_data[\"year\"]=test_data[\"Policy Start Date\"].dt.year","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.063307Z","iopub.execute_input":"2024-12-16T08:28:46.063615Z","iopub.status.idle":"2024-12-16T08:28:46.138517Z","shell.execute_reply.started":"2024-12-16T08:28:46.063585Z","shell.execute_reply":"2024-12-16T08:28:46.136972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"month\"]=train_data[\"Policy Start Date\"].dt.month\nnum.append(\"month\")\ntest_data[\"month\"]=test_data[\"Policy Start Date\"].dt.month","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.140129Z","iopub.execute_input":"2024-12-16T08:28:46.14052Z","iopub.status.idle":"2024-12-16T08:28:46.218456Z","shell.execute_reply.started":"2024-12-16T08:28:46.140484Z","shell.execute_reply":"2024-12-16T08:28:46.217235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"day\"]=train_data[\"Policy Start Date\"].dt.day\nnum.append(\"day\")\ntest_data[\"day\"]=test_data[\"Policy Start Date\"].dt.day","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.219816Z","iopub.execute_input":"2024-12-16T08:28:46.220219Z","iopub.status.idle":"2024-12-16T08:28:46.295069Z","shell.execute_reply.started":"2024-12-16T08:28:46.220185Z","shell.execute_reply":"2024-12-16T08:28:46.293492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data=train_data.drop(columns=[\"Policy Start Date\"])\ncat.remove(\"Policy Start Date\")\ntest_data=test_data.drop(columns=[\"Policy Start Date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.296287Z","iopub.execute_input":"2024-12-16T08:28:46.296687Z","iopub.status.idle":"2024-12-16T08:28:46.522494Z","shell.execute_reply.started":"2024-12-16T08:28:46.296654Z","shell.execute_reply":"2024-12-16T08:28:46.520734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.524291Z","iopub.execute_input":"2024-12-16T08:28:46.524983Z","iopub.status.idle":"2024-12-16T08:28:46.979204Z","shell.execute_reply.started":"2024-12-16T08:28:46.524859Z","shell.execute_reply":"2024-12-16T08:28:46.978251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class DateTimeFeaturesExtractor(BaseEstimator, TransformerMixin):\n    def fit(self, X, y=None):\n        return self\n    \n    def transform(self, X):\n        X_copy = X.copy()\n        # Ensure the date is in datetime format\n        X_copy[\"Policy Start Date\"] = pd.to_datetime(X_copy[\"Policy Start Date\"])\n        \n        # Extract year, month, and day\n        X_copy[\"year\"] = X_copy[\"Policy Start Date\"].dt.year\n        X_copy[\"month\"] = X_copy[\"Policy Start Date\"].dt.month\n        X_copy[\"day\"] = X_copy[\"Policy Start Date\"].dt.day\n        print(X_copy.head())\n        return X_copy.drop(columns=[\"Policy Start Date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.980407Z","iopub.execute_input":"2024-12-16T08:28:46.980695Z","iopub.status.idle":"2024-12-16T08:28:46.98695Z","shell.execute_reply.started":"2024-12-16T08:28:46.980671Z","shell.execute_reply":"2024-12-16T08:28:46.985878Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Filling Nulls**","metadata":{}},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:46.987905Z","iopub.execute_input":"2024-12-16T08:28:46.988196Z","iopub.status.idle":"2024-12-16T08:28:47.428707Z","shell.execute_reply.started":"2024-12-16T08:28:46.988171Z","shell.execute_reply":"2024-12-16T08:28:47.427873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_imputer=SimpleImputer(strategy='mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:47.429818Z","iopub.execute_input":"2024-12-16T08:28:47.430151Z","iopub.status.idle":"2024-12-16T08:28:47.434526Z","shell.execute_reply.started":"2024-12-16T08:28:47.430119Z","shell.execute_reply":"2024-12-16T08:28:47.433363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[num]=num_imputer.fit_transform(train_data[num])\ntest_data[num]=num_imputer.transform(test_data[num])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:47.435535Z","iopub.execute_input":"2024-12-16T08:28:47.435785Z","iopub.status.idle":"2024-12-16T08:28:47.82677Z","shell.execute_reply.started":"2024-12-16T08:28:47.435763Z","shell.execute_reply":"2024-12-16T08:28:47.825995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_imputer=SimpleImputer(strategy='most_frequent')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:47.827706Z","iopub.execute_input":"2024-12-16T08:28:47.827989Z","iopub.status.idle":"2024-12-16T08:28:47.832578Z","shell.execute_reply.started":"2024-12-16T08:28:47.827961Z","shell.execute_reply":"2024-12-16T08:28:47.831318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[cat]=cat_imputer.fit_transform(train_data[cat])\ntest_data[cat]=cat_imputer.transform(test_data[cat])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:47.833968Z","iopub.execute_input":"2024-12-16T08:28:47.834277Z","iopub.status.idle":"2024-12-16T08:28:50.20647Z","shell.execute_reply.started":"2024-12-16T08:28:47.834252Z","shell.execute_reply":"2024-12-16T08:28:50.205036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:50.208009Z","iopub.execute_input":"2024-12-16T08:28:50.208323Z","iopub.status.idle":"2024-12-16T08:28:50.640086Z","shell.execute_reply.started":"2024-12-16T08:28:50.208296Z","shell.execute_reply":"2024-12-16T08:28:50.639003Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Outliers**","metadata":{}},{"cell_type":"code","source":"train_data[\"Annual Income\"].plot(kind=\"box\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:50.641294Z","iopub.execute_input":"2024-12-16T08:28:50.64156Z","iopub.status.idle":"2024-12-16T08:28:50.87827Z","shell.execute_reply.started":"2024-12-16T08:28:50.641539Z","shell.execute_reply":"2024-12-16T08:28:50.877422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"q3=train_data[\"Annual Income\"].quantile(0.75)\nq1=train_data[\"Annual Income\"].quantile(0.25)\nupper=q3+(q3-q1)*1.5\nprint(q3)\nprint(q1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:50.879376Z","iopub.execute_input":"2024-12-16T08:28:50.879671Z","iopub.status.idle":"2024-12-16T08:28:50.924869Z","shell.execute_reply.started":"2024-12-16T08:28:50.879639Z","shell.execute_reply":"2024-12-16T08:28:50.923254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[train_data[\"Annual Income\"]>upper,\"Annual Income\"]=upper","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:50.925872Z","iopub.execute_input":"2024-12-16T08:28:50.926205Z","iopub.status.idle":"2024-12-16T08:28:50.935893Z","shell.execute_reply.started":"2024-12-16T08:28:50.926169Z","shell.execute_reply":"2024-12-16T08:28:50.934685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"Previous Claims\"].plot(kind=\"box\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:50.937429Z","iopub.execute_input":"2024-12-16T08:28:50.937792Z","iopub.status.idle":"2024-12-16T08:28:51.129634Z","shell.execute_reply.started":"2024-12-16T08:28:50.93776Z","shell.execute_reply":"2024-12-16T08:28:51.128908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"q3=train_data[\"Previous Claims\"].quantile(0.75)\nq1=train_data[\"Previous Claims\"].quantile(0.25)\nupper=q3+(q3-q1)*1.5\nprint(q3)\nprint(q1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:51.130509Z","iopub.execute_input":"2024-12-16T08:28:51.131012Z","iopub.status.idle":"2024-12-16T08:28:51.1725Z","shell.execute_reply.started":"2024-12-16T08:28:51.13098Z","shell.execute_reply":"2024-12-16T08:28:51.171702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.loc[train_data[\"Previous Claims\"]>upper,\"Previous Claims\"]=upper","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:51.17636Z","iopub.execute_input":"2024-12-16T08:28:51.17678Z","iopub.status.idle":"2024-12-16T08:28:51.186473Z","shell.execute_reply.started":"2024-12-16T08:28:51.176753Z","shell.execute_reply":"2024-12-16T08:28:51.185173Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"Annual Income\"].plot(kind=\"box\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:51.187698Z","iopub.execute_input":"2024-12-16T08:28:51.187969Z","iopub.status.idle":"2024-12-16T08:28:51.32509Z","shell.execute_reply.started":"2024-12-16T08:28:51.187935Z","shell.execute_reply":"2024-12-16T08:28:51.324303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"Previous Claims\"].plot(kind=\"box\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:51.32608Z","iopub.execute_input":"2024-12-16T08:28:51.326351Z","iopub.status.idle":"2024-12-16T08:28:51.454824Z","shell.execute_reply.started":"2024-12-16T08:28:51.326327Z","shell.execute_reply":"2024-12-16T08:28:51.453953Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Label Encoders**","metadata":{}},{"cell_type":"code","source":"# for col in cat :\n#     temp=LabelEncoder()\n#     train_data[col]=temp.fit_transform(train_data[col])\n\n\nencoder = OneHotEncoder(sparse_output=False, handle_unknown='ignore')  # sparse=False to return dense numpy array\n\n# Loop through categorical columns\nfor col in cat:\n    # Fit and transform the data for each categorical column\n    encoded_values = encoder.fit_transform(train_data[[col]])  # Use double brackets to pass as DataFrame\n    \n    # Create new column names based on the original column and unique categories\n    encoded_df = pd.DataFrame(encoded_values, columns=encoder.get_feature_names_out([col]))\n    \n    # Drop the original categorical column and concatenate the encoded columns\n    train_data = pd.concat([train_data.drop(col, axis=1), encoded_df], axis=1)\n    \n    encoded_values = encoder.transform(test_data[[col]])  \n    \n    # Create new column names based on the original column and unique categories\n    encoded_df = pd.DataFrame(encoded_values, columns=encoder.get_feature_names_out([col]))\n    \n    # Drop the original categorical column and concatenate the encoded columns\n    test_data = pd.concat([test_data.drop(col, axis=1), encoded_df], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:51.45588Z","iopub.execute_input":"2024-12-16T08:28:51.456414Z","iopub.status.idle":"2024-12-16T08:28:58.769749Z","shell.execute_reply.started":"2024-12-16T08:28:51.45639Z","shell.execute_reply":"2024-12-16T08:28:58.768961Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **StandardScaler**","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\n\ntrain_data[num] = scaler.fit_transform(train_data[num])\ntest_data[num] = scaler.transform(test_data[num])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:58.771239Z","iopub.execute_input":"2024-12-16T08:28:58.771574Z","iopub.status.idle":"2024-12-16T08:28:58.983767Z","shell.execute_reply.started":"2024-12-16T08:28:58.771542Z","shell.execute_reply":"2024-12-16T08:28:58.982253Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Info after preprocessing**","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:58.985265Z","iopub.execute_input":"2024-12-16T08:28:58.985593Z","iopub.status.idle":"2024-12-16T08:28:59.066901Z","shell.execute_reply.started":"2024-12-16T08:28:58.985567Z","shell.execute_reply":"2024-12-16T08:28:59.065861Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Correlation**","metadata":{}},{"cell_type":"code","source":"correlation_matrix = train_data.corr()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:28:59.067769Z","iopub.execute_input":"2024-12-16T08:28:59.068074Z","iopub.status.idle":"2024-12-16T08:29:03.059317Z","shell.execute_reply.started":"2024-12-16T08:28:59.068045Z","shell.execute_reply":"2024-12-16T08:29:03.057773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(correlation_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.060762Z","iopub.execute_input":"2024-12-16T08:29:03.061266Z","iopub.status.idle":"2024-12-16T08:29:03.079764Z","shell.execute_reply.started":"2024-12-16T08:29:03.061228Z","shell.execute_reply":"2024-12-16T08:29:03.078782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(correlation_matrix[\"Premium Amount\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.080984Z","iopub.execute_input":"2024-12-16T08:29:03.081315Z","iopub.status.idle":"2024-12-16T08:29:03.098745Z","shell.execute_reply.started":"2024-12-16T08:29:03.081282Z","shell.execute_reply":"2024-12-16T08:29:03.097047Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Drop columns**","metadata":{}},{"cell_type":"code","source":"best_columns_train=[\"id\",\"Premium Amount\",\"Annual Income\",\"Previous Claims\",\"Credit Score\",\"Health Score\",\"year\",\"Customer Feedback_Average\",\"Customer Feedback_Good\",\"Customer Feedback_Poor\"]\ntrain_data=train_data[best_columns_train]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.099825Z","iopub.execute_input":"2024-12-16T08:29:03.100101Z","iopub.status.idle":"2024-12-16T08:29:03.155139Z","shell.execute_reply.started":"2024-12-16T08:29:03.100078Z","shell.execute_reply":"2024-12-16T08:29:03.15401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_columns_test = best_columns_train.copy()\nbest_columns_test.remove(\"Premium Amount\")\n\n# Filter test_data with the updated list\ntest_data = test_data[best_columns_test]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.156239Z","iopub.execute_input":"2024-12-16T08:29:03.15651Z","iopub.status.idle":"2024-12-16T08:29:03.179424Z","shell.execute_reply.started":"2024-12-16T08:29:03.156486Z","shell.execute_reply":"2024-12-16T08:29:03.178007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data=train_data.drop(columns=[\"id\"])\nid_test=test_data[\"id\"]\ntest_data=test_data.drop(columns=[\"id\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.180445Z","iopub.execute_input":"2024-12-16T08:29:03.1807Z","iopub.status.idle":"2024-12-16T08:29:03.219327Z","shell.execute_reply.started":"2024-12-16T08:29:03.180676Z","shell.execute_reply":"2024-12-16T08:29:03.218243Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Splitting data**","metadata":{}},{"cell_type":"code","source":"y=np.log1p(train_data[\"Premium Amount\"])\nX=train_data.drop(columns=[\"Premium Amount\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.220508Z","iopub.execute_input":"2024-12-16T08:29:03.220795Z","iopub.status.idle":"2024-12-16T08:29:03.26164Z","shell.execute_reply.started":"2024-12-16T08:29:03.220772Z","shell.execute_reply":"2024-12-16T08:29:03.260717Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Model**","metadata":{}},{"cell_type":"code","source":"best_params = {\n    'colsample_bytree': 0.9,\n    'learning_rate': 0.01,\n    'max_depth': 7,\n    'n_estimators': 500,\n    'subsample': 0.8\n}\n\n# Create the XGBRegressor model with the correct parameters\nmodel = xgb.XGBRegressor(**best_params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.262644Z","iopub.execute_input":"2024-12-16T08:29:03.262901Z","iopub.status.idle":"2024-12-16T08:29:03.268965Z","shell.execute_reply.started":"2024-12-16T08:29:03.262874Z","shell.execute_reply":"2024-12-16T08:29:03.268001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.270711Z","iopub.execute_input":"2024-12-16T08:29:03.271516Z","iopub.status.idle":"2024-12-16T08:29:03.302134Z","shell.execute_reply.started":"2024-12-16T08:29:03.271476Z","shell.execute_reply":"2024-12-16T08:29:03.30097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:03.30343Z","iopub.execute_input":"2024-12-16T08:29:03.303737Z","iopub.status.idle":"2024-12-16T08:29:24.18922Z","shell.execute_reply.started":"2024-12-16T08:29:03.303712Z","shell.execute_reply":"2024-12-16T08:29:24.188241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Prediction**","metadata":{}},{"cell_type":"code","source":"predicts=model.predict(test_data)\npredicts=np.expm1(predicts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:24.190351Z","iopub.execute_input":"2024-12-16T08:29:24.190684Z","iopub.status.idle":"2024-12-16T08:29:28.044616Z","shell.execute_reply.started":"2024-12-16T08:29:24.190648Z","shell.execute_reply":"2024-12-16T08:29:28.043242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results_df = pd.DataFrame({'id': id_test, 'Premium Amount': predicts})\n\n# Save the DataFrame to a CSV file\nresults_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T08:29:28.045738Z","iopub.execute_input":"2024-12-16T08:29:28.046323Z","iopub.status.idle":"2024-12-16T08:29:28.768404Z","shell.execute_reply.started":"2024-12-16T08:29:28.046267Z","shell.execute_reply":"2024-12-16T08:29:28.766875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}