{"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":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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:02:24.33226Z","iopub.execute_input":"2025-01-01T04:02:24.332518Z","iopub.status.idle":"2025-01-01T04:02:24.727131Z","shell.execute_reply.started":"2025-01-01T04:02:24.332494Z","shell.execute_reply":"2025-01-01T04:02:24.725828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:03:02.615864Z","iopub.execute_input":"2025-01-01T04:03:02.616263Z","iopub.status.idle":"2025-01-01T04:03:03.501353Z","shell.execute_reply.started":"2025-01-01T04:03:02.616222Z","shell.execute_reply":"2025-01-01T04:03:03.500235Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:11:13.966247Z","iopub.execute_input":"2025-01-01T04:11:13.966592Z","iopub.status.idle":"2025-01-01T04:11:21.607531Z","shell.execute_reply.started":"2025-01-01T04:11:13.966562Z","shell.execute_reply":"2025-01-01T04:11:21.606669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isnull().sum().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:11:21.608759Z","iopub.execute_input":"2025-01-01T04:11:21.609044Z","iopub.status.idle":"2025-01-01T04:11:22.240476Z","shell.execute_reply.started":"2025-01-01T04:11:21.609018Z","shell.execute_reply":"2025-01-01T04:11:22.239393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.isnull().sum().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:11:22.805084Z","iopub.execute_input":"2025-01-01T04:11:22.805402Z","iopub.status.idle":"2025-01-01T04:11:23.219072Z","shell.execute_reply.started":"2025-01-01T04:11:22.805376Z","shell.execute_reply":"2025-01-01T04:11:23.218034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:11:48.160047Z","iopub.execute_input":"2025-01-01T04:11:48.160374Z","iopub.status.idle":"2025-01-01T04:11:48.79268Z","shell.execute_reply.started":"2025-01-01T04:11:48.160349Z","shell.execute_reply":"2025-01-01T04:11:48.791807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:12:01.839575Z","iopub.execute_input":"2025-01-01T04:12:01.840015Z","iopub.status.idle":"2025-01-01T04:12:02.281054Z","shell.execute_reply.started":"2025-01-01T04:12:01.83998Z","shell.execute_reply":"2025-01-01T04:12:02.279971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop unwanted columns\ndf_train.drop(['Policy Start Date'],axis=1,inplace=True)\ndf_test.drop(['Policy Start Date'],axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:11.676774Z","iopub.execute_input":"2025-01-01T04:05:11.677129Z","iopub.status.idle":"2025-01-01T04:05:12.032343Z","shell.execute_reply.started":"2025-01-01T04:05:11.677104Z","shell.execute_reply":"2025-01-01T04:05:12.03118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:12:24.730358Z","iopub.execute_input":"2025-01-01T04:12:24.730762Z","iopub.status.idle":"2025-01-01T04:12:24.737159Z","shell.execute_reply.started":"2025-01-01T04:12:24.730723Z","shell.execute_reply":"2025-01-01T04:12:24.736118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_col = ['Gender','Marital Status','Education Level', 'Occupation','Location', 'Policy Type','Customer Feedback',\n       'Smoking Status', 'Exercise Frequency', 'Property Type']\nnumerical_col = ['Age','Annual Income','Number of Dependents', 'Health Score','Previous Claims', 'Vehicle Age',\n       'Credit Score', 'Insurance Duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:12.990735Z","iopub.execute_input":"2025-01-01T04:05:12.991058Z","iopub.status.idle":"2025-01-01T04:05:12.995797Z","shell.execute_reply.started":"2025-01-01T04:05:12.991034Z","shell.execute_reply":"2025-01-01T04:05:12.994631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handling Null Values - Categorical\ndf_train['Occupation'] = df_train['Occupation'].fillna(df_train['Occupation'].mode()[0])\ndf_train['Customer Feedback'] = df_train['Customer Feedback'].fillna(df_train['Customer Feedback'].mode()[0])\ndf_train['Marital Status'] = df_train['Marital Status'].fillna(df_train['Marital Status'].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:13.825282Z","iopub.execute_input":"2025-01-01T04:05:13.825624Z","iopub.status.idle":"2025-01-01T04:05:14.311712Z","shell.execute_reply.started":"2025-01-01T04:05:13.825594Z","shell.execute_reply":"2025-01-01T04:05:14.310793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handling Null Values - Categorical\ndf_test['Occupation'] = df_test['Occupation'].fillna(df_test['Occupation'].mode()[0])\ndf_test['Customer Feedback'] = df_test['Customer Feedback'].fillna(df_test['Customer Feedback'].mode()[0])\ndf_test['Marital Status'] = df_test['Marital Status'].fillna(df_test['Marital Status'].mode()[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:14.524228Z","iopub.execute_input":"2025-01-01T04:05:14.524541Z","iopub.status.idle":"2025-01-01T04:05:14.853328Z","shell.execute_reply.started":"2025-01-01T04:05:14.524515Z","shell.execute_reply":"2025-01-01T04:05:14.852395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check for Outliers\nplt.figure(figsize=(10,10))\nfor i, col in enumerate(numerical_col):\n    plt.subplot(4,3, i+1)\n    sns.boxplot(y=df_train[col])\n    plt.title(f'{col}')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:13:07.014606Z","iopub.execute_input":"2025-01-01T04:13:07.01509Z","iopub.status.idle":"2025-01-01T04:13:08.733057Z","shell.execute_reply.started":"2025-01-01T04:13:07.015056Z","shell.execute_reply":"2025-01-01T04:13:08.73178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handling Null Values - Numerical\ndf_train['Age'] = df_train['Age'].fillna(df_train['Age'].mean())\ndf_train['Annual Income'] = df_train['Annual Income'].fillna(df_train['Annual Income'].median())\ndf_train['Number of Dependents'] = df_train['Number of Dependents'].fillna(df_train['Number of Dependents'].mean())\ndf_train['Health Score'] = df_train['Health Score'].fillna(df_train['Health Score'].mean())\ndf_train['Previous Claims'] = df_train['Previous Claims'].fillna(df_train['Previous Claims'].median())\ndf_train['Credit Score'] = df_train['Credit Score'].fillna(df_train['Credit Score'].mean()) \ndf_train['Vehicle Age'] = df_train['Vehicle Age'].fillna(df_train['Vehicle Age'].mean())\ndf_train['Insurance Duration'] = df_train['Insurance Duration'].fillna(df_train['Insurance Duration'].mean())\n\ndf_train.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:22.194378Z","iopub.execute_input":"2025-01-01T04:05:22.194759Z","iopub.status.idle":"2025-01-01T04:05:22.932692Z","shell.execute_reply.started":"2025-01-01T04:05:22.194728Z","shell.execute_reply":"2025-01-01T04:05:22.931803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check for Outliers\nplt.figure(figsize=(10,10))\nfor i, col in enumerate(numerical_col):\n    plt.subplot(4,3, i+1)\n    sns.boxplot(y=df_test[col])\n    plt.title(f'{col}')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:13:12.150536Z","iopub.execute_input":"2025-01-01T04:13:12.150977Z","iopub.status.idle":"2025-01-01T04:13:13.812828Z","shell.execute_reply.started":"2025-01-01T04:13:12.150943Z","shell.execute_reply":"2025-01-01T04:13:13.811739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handling Null Values - Numerical\ndf_test['Age'] = df_test['Age'].fillna(df_test['Age'].mean())\ndf_test['Annual Income'] = df_test['Annual Income'].fillna(df_test['Annual Income'].median())\ndf_test['Number of Dependents'] = df_test['Number of Dependents'].fillna(df_test['Number of Dependents'].mean())\ndf_test['Health Score'] = df_test['Health Score'].fillna(df_test['Health Score'].mean())\ndf_test['Previous Claims'] = df_test['Previous Claims'].fillna(df_test['Previous Claims'].median())\ndf_test['Credit Score'] = df_test['Credit Score'].fillna(df_test['Credit Score'].mean()) \ndf_test['Vehicle Age'] = df_test['Vehicle Age'].fillna(df_test['Vehicle Age'].mean())\ndf_test['Insurance Duration'] = df_test['Insurance Duration'].fillna(df_test['Insurance Duration'].mean())\n\ndf_test.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:27.263273Z","iopub.execute_input":"2025-01-01T04:05:27.263604Z","iopub.status.idle":"2025-01-01T04:05:27.763987Z","shell.execute_reply.started":"2025-01-01T04:05:27.263574Z","shell.execute_reply":"2025-01-01T04:05:27.763086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# OneHot Encoding of Categorical Columns\nfrom sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\n\nfor col in categorical_col:\n    df_train[col] = encoder.fit_transform(df_train[col])\n    df_test[col] = encoder.transform(df_test[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:28.209981Z","iopub.execute_input":"2025-01-01T04:05:28.210305Z","iopub.status.idle":"2025-01-01T04:05:31.719859Z","shell.execute_reply.started":"2025-01-01T04:05:28.210279Z","shell.execute_reply":"2025-01-01T04:05:31.718783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Correlation\nplt.figure(figsize=(15,10))\n\nsns.heatmap(df_train.corr(),annot=True, cmap='coolwarm',fmt='.0f')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:31.721285Z","iopub.execute_input":"2025-01-01T04:05:31.721683Z","iopub.status.idle":"2025-01-01T04:05:34.602587Z","shell.execute_reply.started":"2025-01-01T04:05:31.721629Z","shell.execute_reply":"2025-01-01T04:05:34.601546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train Test Split\nfrom sklearn.model_selection import train_test_split\n\nX = df_train.iloc[:,:-1]\ny = df_train.iloc[:,-1:]\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:34.604085Z","iopub.execute_input":"2025-01-01T04:05:34.604366Z","iopub.status.idle":"2025-01-01T04:05:35.357946Z","shell.execute_reply.started":"2025-01-01T04:05:34.60434Z","shell.execute_reply":"2025-01-01T04:05:35.356912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model Fitting\nfrom sklearn.linear_model import LinearRegression\n\nmodel = LinearRegression()\n\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:42.236889Z","iopub.execute_input":"2025-01-01T04:05:42.237215Z","iopub.status.idle":"2025-01-01T04:05:43.375169Z","shell.execute_reply.started":"2025-01-01T04:05:42.237189Z","shell.execute_reply":"2025-01-01T04:05:43.374027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)\nmodel.score(X_test,y_test)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:45.97945Z","iopub.execute_input":"2025-01-01T04:05:45.979858Z","iopub.status.idle":"2025-01-01T04:05:46.073066Z","shell.execute_reply.started":"2025-01-01T04:05:45.979823Z","shell.execute_reply":"2025-01-01T04:05:46.071729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Metrics: Root Mean Squared Logarithmic Error (RMSLE) (square root of MSLE)\nfrom sklearn.metrics import mean_squared_log_error\n\nnp.sqrt(mean_squared_log_error(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:47.065492Z","iopub.execute_input":"2025-01-01T04:05:47.065878Z","iopub.status.idle":"2025-01-01T04:05:47.088702Z","shell.execute_reply.started":"2025-01-01T04:05:47.065845Z","shell.execute_reply":"2025-01-01T04:05:47.087763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(df_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:47.900455Z","iopub.execute_input":"2025-01-01T04:05:47.900827Z","iopub.status.idle":"2025-01-01T04:05:47.968114Z","shell.execute_reply.started":"2025-01-01T04:05:47.900794Z","shell.execute_reply":"2025-01-01T04:05:47.966713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test['Premium Amount'] = predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:05:51.390766Z","iopub.execute_input":"2025-01-01T04:05:51.391099Z","iopub.status.idle":"2025-01-01T04:05:51.396999Z","shell.execute_reply.started":"2025-01-01T04:05:51.391075Z","shell.execute_reply":"2025-01-01T04:05:51.395794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:10:18.299686Z","iopub.execute_input":"2025-01-01T04:10:18.300209Z","iopub.status.idle":"2025-01-01T04:10:18.330646Z","shell.execute_reply.started":"2025-01-01T04:10:18.300164Z","shell.execute_reply":"2025-01-01T04:10:18.329435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test[['id','Premium Amount']].to_csv('submission_linear_reg.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T04:06:12.829296Z","iopub.execute_input":"2025-01-01T04:06:12.82982Z","iopub.status.idle":"2025-01-01T04:06:14.487493Z","shell.execute_reply.started":"2025-01-01T04:06:12.829773Z","shell.execute_reply":"2025-01-01T04:06:14.486452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}