{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":false,"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:08.945384Z","iopub.execute_input":"2024-12-28T14:11:08.948255Z","iopub.status.idle":"2024-12-28T14:11:08.966901Z","shell.execute_reply.started":"2024-12-28T14:11:08.948144Z","shell.execute_reply":"2024-12-28T14:11:08.965131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv', index_col='id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:09.097223Z","iopub.execute_input":"2024-12-28T14:11:09.097617Z","iopub.status.idle":"2024-12-28T14:11:14.669566Z","shell.execute_reply.started":"2024-12-28T14:11:09.09758Z","shell.execute_reply":"2024-12-28T14:11:14.668349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:14.671269Z","iopub.execute_input":"2024-12-28T14:11:14.671619Z","iopub.status.idle":"2024-12-28T14:11:14.706073Z","shell.execute_reply.started":"2024-12-28T14:11:14.67158Z","shell.execute_reply":"2024-12-28T14:11:14.704704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:14.708452Z","iopub.execute_input":"2024-12-28T14:11:14.708869Z","iopub.status.idle":"2024-12-28T14:11:15.359599Z","shell.execute_reply.started":"2024-12-28T14:11:14.708828Z","shell.execute_reply":"2024-12-28T14:11:15.357557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:15.361941Z","iopub.execute_input":"2024-12-28T14:11:15.362416Z","iopub.status.idle":"2024-12-28T14:11:15.370262Z","shell.execute_reply.started":"2024-12-28T14:11:15.362365Z","shell.execute_reply":"2024-12-28T14:11:15.369037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:15.371379Z","iopub.execute_input":"2024-12-28T14:11:15.371868Z","iopub.status.idle":"2024-12-28T14:11:16.109303Z","shell.execute_reply.started":"2024-12-28T14:11:15.371824Z","shell.execute_reply":"2024-12-28T14:11:16.107985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:16.11063Z","iopub.execute_input":"2024-12-28T14:11:16.111001Z","iopub.status.idle":"2024-12-28T14:11:16.118203Z","shell.execute_reply.started":"2024-12-28T14:11:16.110968Z","shell.execute_reply":"2024-12-28T14:11:16.116826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nsns.scatterplot(data = df, x='Credit Score', y='Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:16.119551Z","iopub.execute_input":"2024-12-28T14:11:16.119921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.scatterplot(data = df, x='Health Score', y='Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.idle":"2024-12-28T14:11:25.080741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.kdeplot(x=df['Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:25.083947Z","iopub.execute_input":"2024-12-28T14:11:25.084333Z","iopub.status.idle":"2024-12-28T14:11:30.154129Z","shell.execute_reply.started":"2024-12-28T14:11:25.084298Z","shell.execute_reply":"2024-12-28T14:11:30.152544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col=df.drop(\"Premium Amount\",axis=1).select_dtypes(include=['float64']).columns\ncat_col=df.select_dtypes(include=['object']).columns\n\nfig, axes = plt.subplots(nrows=len(num_col), ncols=1, figsize=(8, len(num_col) * 4))  # Create subplots\n\n# Loop through numerical columns and plot each on a separate subplot\nfor ax, col in zip(axes, num_col):\n    sns.boxplot(x=df[col], ax=ax)  # Plot on the corresponding axis\n    ax.set_title(f\"Boxplot of {col}\")  # Set title for each subplot\n\nplt.tight_layout()  # Adjust layout to avoid overlap\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:30.155324Z","iopub.execute_input":"2024-12-28T14:11:30.155671Z","iopub.status.idle":"2024-12-28T14:11:32.319175Z","shell.execute_reply.started":"2024-12-28T14:11:30.155638Z","shell.execute_reply":"2024-12-28T14:11:32.317849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Q1 = df['Annual Income'].quantile(0.25)\nQ3 = df['Annual Income'].quantile(0.75)\nIQR = Q3 - Q1\nlower_bound = Q1 - 1.5 * IQR\nupper_bound = Q3 + 1.5 * IQR","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.320485Z","iopub.execute_input":"2024-12-28T14:11:32.320898Z","iopub.status.idle":"2024-12-28T14:11:32.37658Z","shell.execute_reply.started":"2024-12-28T14:11:32.320865Z","shell.execute_reply":"2024-12-28T14:11:32.375188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df[df['Annual Income'] < upper_bound]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.377862Z","iopub.execute_input":"2024-12-28T14:11:32.378287Z","iopub.status.idle":"2024-12-28T14:11:32.561924Z","shell.execute_reply.started":"2024-12-28T14:11:32.378252Z","shell.execute_reply":"2024-12-28T14:11:32.560691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Q1 = df['Previous Claims'].quantile(0.25)\nQ3 = df['Previous Claims'].quantile(0.75)\nIQR = Q3 - Q1\nlower_bound = Q1 - 1.5 * IQR\nupper_bound = Q3 + 1.5 * IQR","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.563112Z","iopub.execute_input":"2024-12-28T14:11:32.563564Z","iopub.status.idle":"2024-12-28T14:11:32.609433Z","shell.execute_reply.started":"2024-12-28T14:11:32.563504Z","shell.execute_reply":"2024-12-28T14:11:32.608069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df[df['Previous Claims'] < upper_bound]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.61079Z","iopub.execute_input":"2024-12-28T14:11:32.611153Z","iopub.status.idle":"2024-12-28T14:11:32.808875Z","shell.execute_reply.started":"2024-12-28T14:11:32.611122Z","shell.execute_reply":"2024-12-28T14:11:32.807504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.810012Z","iopub.execute_input":"2024-12-28T14:11:32.81032Z","iopub.status.idle":"2024-12-28T14:11:32.843545Z","shell.execute_reply.started":"2024-12-28T14:11:32.810292Z","shell.execute_reply":"2024-12-28T14:11:32.842185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(data=df, x='Annual Income')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:32.845182Z","iopub.execute_input":"2024-12-28T14:11:32.845535Z","iopub.status.idle":"2024-12-28T14:11:33.135671Z","shell.execute_reply.started":"2024-12-28T14:11:32.845482Z","shell.execute_reply":"2024-12-28T14:11:33.13435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.136862Z","iopub.execute_input":"2024-12-28T14:11:33.137205Z","iopub.status.idle":"2024-12-28T14:11:33.544337Z","shell.execute_reply.started":"2024-12-28T14:11:33.137173Z","shell.execute_reply":"2024-12-28T14:11:33.543209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(df['Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.54533Z","iopub.execute_input":"2024-12-28T14:11:33.545722Z","iopub.status.idle":"2024-12-28T14:11:33.552548Z","shell.execute_reply.started":"2024-12-28T14:11:33.545679Z","shell.execute_reply":"2024-12-28T14:11:33.551315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df['Number of Dependents'].isna()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.553761Z","iopub.execute_input":"2024-12-28T14:11:33.554097Z","iopub.status.idle":"2024-12-28T14:11:33.620356Z","shell.execute_reply.started":"2024-12-28T14:11:33.554067Z","shell.execute_reply":"2024-12-28T14:11:33.619084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"109672/len(df['Premium Amount']) * 100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.621564Z","iopub.execute_input":"2024-12-28T14:11:33.621862Z","iopub.status.idle":"2024-12-28T14:11:33.629131Z","shell.execute_reply.started":"2024-12-28T14:11:33.621836Z","shell.execute_reply":"2024-12-28T14:11:33.628053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df['Occupation'].isna()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.630659Z","iopub.execute_input":"2024-12-28T14:11:33.631009Z","iopub.status.idle":"2024-12-28T14:11:33.747329Z","shell.execute_reply.started":"2024-12-28T14:11:33.630974Z","shell.execute_reply":"2024-12-28T14:11:33.745992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Education Level'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.74875Z","iopub.execute_input":"2024-12-28T14:11:33.749064Z","iopub.status.idle":"2024-12-28T14:11:33.801654Z","shell.execute_reply.started":"2024-12-28T14:11:33.749035Z","shell.execute_reply":"2024-12-28T14:11:33.800212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Marital Status'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:33.808532Z","iopub.execute_input":"2024-12-28T14:11:33.808912Z","iopub.status.idle":"2024-12-28T14:11:33.848194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Occupation'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:34.604834Z","iopub.execute_input":"2024-12-28T14:11:34.605299Z","iopub.status.idle":"2024-12-28T14:11:34.644292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Location'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:34.665941Z","iopub.execute_input":"2024-12-28T14:11:34.666274Z","iopub.status.idle":"2024-12-28T14:11:34.727908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Customer Feedback'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:34.73796Z","iopub.execute_input":"2024-12-28T14:11:34.738419Z","iopub.status.idle":"2024-12-28T14:11:34.793948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Property Type'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:34.81801Z","iopub.execute_input":"2024-12-28T14:11:34.818327Z","iopub.status.idle":"2024-12-28T14:11:34.879165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Policy Type'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:34.910175Z","iopub.execute_input":"2024-12-28T14:11:34.910471Z","iopub.status.idle":"2024-12-28T14:11:34.973712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:35.21224Z","iopub.execute_input":"2024-12-28T14:11:35.212593Z","iopub.status.idle":"2024-12-28T14:11:35.615845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col_cont = [cols for cols in df.columns.values if df[cols].dtype == 'float64']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:35.647398Z","iopub.execute_input":"2024-12-28T14:11:35.647867Z","iopub.status.idle":"2024-12-28T14:11:35.668244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col_cont","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:35.673298Z","iopub.execute_input":"2024-12-28T14:11:35.673696Z","iopub.status.idle":"2024-12-28T14:11:35.693335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_col = ['Age',\n 'Annual Income',\n 'Number of Dependents',\n 'Health Score',\n 'Previous Claims',\n 'Vehicle Age',\n 'Credit Score',\n 'Insurance Duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:35.698872Z","iopub.execute_input":"2024-12-28T14:11:35.699289Z","iopub.status.idle":"2024-12-28T14:11:35.715857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[num_col].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:35.72132Z","iopub.execute_input":"2024-12-28T14:11:35.721881Z","iopub.status.idle":"2024-12-28T14:11:35.793305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df['Health Score'].isna()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:36.194919Z","iopub.execute_input":"2024-12-28T14:11:36.195253Z","iopub.status.idle":"2024-12-28T14:11:36.296862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.Age = df.Age.fillna(df.Age.mean())\ndf['Annual Income'] = df['Annual Income'].fillna(0)\ndf['Number of Dependents'] = df['Number of Dependents'].fillna(0)\ndf[['Health Score', 'Previous Claims', 'Vehicle Age', 'Credit Score', 'Insurance Duration']] = df[['Health Score', 'Previous Claims', 'Vehicle Age', 'Credit Score', 'Insurance Duration']].fillna(-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:36.305433Z","iopub.execute_input":"2024-12-28T14:11:36.305807Z","iopub.status.idle":"2024-12-28T14:11:36.395769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:37.231969Z","iopub.execute_input":"2024-12-28T14:11:37.232269Z","iopub.status.idle":"2024-12-28T14:11:37.258118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[num_col].isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:39.940472Z","iopub.execute_input":"2024-12-28T14:11:39.940922Z","iopub.status.idle":"2024-12-28T14:11:39.979149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.Gender = df.Gender.map({'Male': 0, 'Female' : 1})\ndf['Smoking Status'] = df['Smoking Status'].map({'Yes' : 1, 'No' : 0})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:40.010009Z","iopub.execute_input":"2024-12-28T14:11:40.01056Z","iopub.status.idle":"2024-12-28T14:11:40.116024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"Policy Start Date\"]=pd.to_datetime(df['Policy Start Date'])\ndf[\"day\"]=df[\"Policy Start Date\"].dt.day_name()\ndf[\"month\"] = df[\"Policy Start Date\"].dt.month\ndf[\"year\"] = df[\"Policy Start Date\"].dt.year\ndf.drop(\"Policy Start Date\",axis=1,inplace =True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:35.603598Z","iopub.execute_input":"2024-12-28T14:12:35.604026Z","iopub.status.idle":"2024-12-28T14:12:35.66779Z","shell.execute_reply.started":"2024-12-28T14:12:35.603994Z","shell.execute_reply":"2024-12-28T14:12:35.665384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = [cols for cols in df.columns.values if df[cols].dtype == 'object']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:37.83732Z","iopub.execute_input":"2024-12-28T14:12:37.83773Z","iopub.status.idle":"2024-12-28T14:12:37.844546Z","shell.execute_reply.started":"2024-12-28T14:12:37.837696Z","shell.execute_reply":"2024-12-28T14:12:37.842842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:39.427474Z","iopub.execute_input":"2024-12-28T14:12:39.428031Z","iopub.status.idle":"2024-12-28T14:12:39.436038Z","shell.execute_reply.started":"2024-12-28T14:12:39.427974Z","shell.execute_reply":"2024-12-28T14:12:39.433699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = ['Marital Status',\n 'Education Level',\n 'Occupation',\n 'Location',\n 'Policy Type',\n 'Customer Feedback',\n 'Exercise Frequency',\n 'Property Type',\n 'day']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:40.938115Z","iopub.execute_input":"2024-12-28T14:12:40.938457Z","iopub.status.idle":"2024-12-28T14:12:40.943576Z","shell.execute_reply.started":"2024-12-28T14:12:40.938431Z","shell.execute_reply":"2024-12-28T14:12:40.942171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:42.471498Z","iopub.execute_input":"2024-12-28T14:12:42.471916Z","iopub.status.idle":"2024-12-28T14:12:42.498838Z","shell.execute_reply.started":"2024-12-28T14:12:42.47188Z","shell.execute_reply":"2024-12-28T14:12:42.497362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder, StandardScaler\n\nscaler = StandardScaler()\n\ndf[num_col] = scaler.fit_transform(df[num_col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:44.104616Z","iopub.execute_input":"2024-12-28T14:12:44.105022Z","iopub.status.idle":"2024-12-28T14:12:44.261307Z","shell.execute_reply.started":"2024-12-28T14:12:44.10499Z","shell.execute_reply":"2024-12-28T14:12:44.260024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:45.945007Z","iopub.execute_input":"2024-12-28T14:12:45.945494Z","iopub.status.idle":"2024-12-28T14:12:45.980063Z","shell.execute_reply.started":"2024-12-28T14:12:45.945448Z","shell.execute_reply":"2024-12-28T14:12:45.978423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:47.519213Z","iopub.execute_input":"2024-12-28T14:12:47.519634Z","iopub.status.idle":"2024-12-28T14:12:47.876222Z","shell.execute_reply.started":"2024-12-28T14:12:47.51959Z","shell.execute_reply":"2024-12-28T14:12:47.874608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.reset_index(drop=True, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:50.399462Z","iopub.execute_input":"2024-12-28T14:12:50.399849Z","iopub.status.idle":"2024-12-28T14:12:50.404793Z","shell.execute_reply.started":"2024-12-28T14:12:50.399821Z","shell.execute_reply":"2024-12-28T14:12:50.403334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:51.38496Z","iopub.execute_input":"2024-12-28T14:12:51.385336Z","iopub.status.idle":"2024-12-28T14:12:52.154752Z","shell.execute_reply.started":"2024-12-28T14:12:51.38531Z","shell.execute_reply":"2024-12-28T14:12:52.15339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder = OneHotEncoder(sparse_output=False)\n\none_hot_encoded = encoder.fit_transform(df[cat_cols])\n\none_hot_df = pd.DataFrame(one_hot_encoded, columns=encoder.get_feature_names_out(cat_cols))\n\ndf_encoded = pd.concat([df[num_col], one_hot_df, df['Premium Amount']], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:53.816791Z","iopub.execute_input":"2024-12-28T14:12:53.817156Z","iopub.status.idle":"2024-12-28T14:12:56.536648Z","shell.execute_reply.started":"2024-12-28T14:12:53.817127Z","shell.execute_reply":"2024-12-28T14:12:56.535497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_encoded.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:40.721613Z","iopub.status.idle":"2024-12-28T14:11:40.722056Z","shell.execute_reply":"2024-12-28T14:11:40.721894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_encoded.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:40.722836Z","iopub.status.idle":"2024-12-28T14:11:40.723175Z","shell.execute_reply":"2024-12-28T14:11:40.723047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:11:40.723802Z","iopub.status.idle":"2024-12-28T14:11:40.724145Z","shell.execute_reply":"2024-12-28T14:11:40.724015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\nimport xgboost as xgb\n\nmodel = xgb.XGBRegressor(verbosity = 0, device='gpu', n_estimators = 300, \n                             learning_rate=0.01, \n                             objective = 'reg:squarederror', gamma=0.3)\n\nx = df_encoded.drop('Premium Amount', axis=1)\ny = df_encoded['Premium Amount']\n\nscore = cross_val_score(model, x, y, cv=5, scoring='r2')\n\nprint(f'R2: {score}')\nprint(f'R2 mean: {score.mean()}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:12:57.898777Z","iopub.execute_input":"2024-12-28T14:12:57.899145Z","iopub.status.idle":"2024-12-28T14:14:13.538873Z","shell.execute_reply.started":"2024-12-28T14:12:57.899118Z","shell.execute_reply":"2024-12-28T14:14:13.537535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import make_scorer\n\ndef rmsle(y_true, y_pred):\n    # Avoid log(0) by clipping predictions to a minimum value\n    y_pred = np.clip(y_pred, 1e-10, None)\n    return np.sqrt(np.mean((np.log1p(y_pred) - np.log1p(y_true)) ** 2))\nrmsle_scorer=make_scorer(rmsle,greater_is_better=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:14:13.540322Z","iopub.execute_input":"2024-12-28T14:14:13.540714Z","iopub.status.idle":"2024-12-28T14:14:13.546683Z","shell.execute_reply.started":"2024-12-28T14:14:13.540675Z","shell.execute_reply":"2024-12-28T14:14:13.545221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score = cross_val_score(model, x, y, cv=5, scoring=rmsle_scorer)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:17:36.527301Z","iopub.execute_input":"2024-12-28T14:17:36.527848Z","iopub.status.idle":"2024-12-28T14:18:51.920775Z","shell.execute_reply.started":"2024-12-28T14:17:36.527806Z","shell.execute_reply":"2024-12-28T14:18:51.919393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:19:43.833226Z","iopub.execute_input":"2024-12-28T14:19:43.833634Z","iopub.status.idle":"2024-12-28T14:19:43.842108Z","shell.execute_reply.started":"2024-12-28T14:19:43.833602Z","shell.execute_reply":"2024-12-28T14:19:43.840743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\n\nx_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42, test_size=0.2)\n\nmodel.fit(x_train, y_train)\ny_pred = model.predict(x_test)\n\nrmsle(y_test,y_pred)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:24:07.596827Z","iopub.execute_input":"2024-12-28T14:24:07.597324Z","iopub.status.idle":"2024-12-28T14:24:23.7282Z","shell.execute_reply.started":"2024-12-28T14:24:07.597287Z","shell.execute_reply":"2024-12-28T14:24:23.726855Z"}},"outputs":[],"execution_count":null}]}