{"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":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import FunctionTransformer\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn import set_config\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.compose import make_column_selector, make_column_transformer\nfrom sklearn.metrics import mean_squared_error\nimport category_encoders as ce","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:53:55.361191Z","iopub.execute_input":"2024-12-27T19:53:55.361835Z","iopub.status.idle":"2024-12-27T19:53:55.368758Z","shell.execute_reply.started":"2024-12-27T19:53:55.361789Z","shell.execute_reply":"2024-12-27T19:53:55.36741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\",index_col=\"id\")\ntest = pd.read_csv(\"/kaggle/input/playground-series-s4e12/test.csv\",index_col=\"id\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:53:55.3709Z","iopub.execute_input":"2024-12-27T19:53:55.371348Z","iopub.status.idle":"2024-12-27T19:54:02.824418Z","shell.execute_reply.started":"2024-12-27T19:53:55.371298Z","shell.execute_reply":"2024-12-27T19:54:02.823267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:02.826108Z","iopub.execute_input":"2024-12-27T19:54:02.82661Z","iopub.status.idle":"2024-12-27T19:54:03.514558Z","shell.execute_reply.started":"2024-12-27T19:54:02.826557Z","shell.execute_reply":"2024-12-27T19:54:03.51345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:03.517063Z","iopub.execute_input":"2024-12-27T19:54:03.517359Z","iopub.status.idle":"2024-12-27T19:54:04.17241Z","shell.execute_reply.started":"2024-12-27T19:54:03.517328Z","shell.execute_reply":"2024-12-27T19:54:04.171179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:04.173875Z","iopub.execute_input":"2024-12-27T19:54:04.174284Z","iopub.status.idle":"2024-12-27T19:54:04.200115Z","shell.execute_reply.started":"2024-12-27T19:54:04.174246Z","shell.execute_reply":"2024-12-27T19:54:04.198951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"object_cols = data.select_dtypes(include='object',exclude='float64')\nfor col in object_cols:\n    print(data[col].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:04.201443Z","iopub.execute_input":"2024-12-27T19:54:04.201779Z","iopub.status.idle":"2024-12-27T19:54:05.778406Z","shell.execute_reply.started":"2024-12-27T19:54:04.201738Z","shell.execute_reply":"2024-12-27T19:54:05.777322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.rc('font',size=14)\nplt.rc('axes',labelsize=14,titlesize=14)\nplt.rc('legend',fontsize=14)\nplt.rc('xtick',labelsize=10)\nplt.rc('ytick',labelsize=10)\n\ndata.hist(bins=50,figsize=(12,8))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:05.779944Z","iopub.execute_input":"2024-12-27T19:54:05.780301Z","iopub.status.idle":"2024-12-27T19:54:07.867017Z","shell.execute_reply.started":"2024-12-27T19:54:05.780265Z","shell.execute_reply":"2024-12-27T19:54:07.865843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr_matrix=data.corr(numeric_only=True)\ncorr_matrix['Premium Amount'].sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:07.868595Z","iopub.execute_input":"2024-12-27T19:54:07.869046Z","iopub.status.idle":"2024-12-27T19:54:08.193422Z","shell.execute_reply.started":"2024-12-27T19:54:07.869Z","shell.execute_reply":"2024-12-27T19:54:08.192315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"insurance_train = data.copy()\ninsurance_test = test.copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:08.196529Z","iopub.execute_input":"2024-12-27T19:54:08.196847Z","iopub.status.idle":"2024-12-27T19:54:08.547625Z","shell.execute_reply.started":"2024-12-27T19:54:08.196817Z","shell.execute_reply":"2024-12-27T19:54:08.546435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"insurance_premiums = np.log1p(data['Premium Amount'])\ninsurance_train.drop(['Premium Amount'],axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:54:08.549142Z","iopub.execute_input":"2024-12-27T19:54:08.549513Z","iopub.status.idle":"2024-12-27T19:54:08.783658Z","shell.execute_reply.started":"2024-12-27T19:54:08.549478Z","shell.execute_reply":"2024-12-27T19:54:08.782706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_attribs = [\"Age\", \"Annual Income\", \"Number of Dependents\", \"Health Score\",\n               \"Previous Claims\", \"Vehicle Age\", \"Credit Score\", \"Insurance Duration\"]\ncat_attribs = ['Gender', 'Marital Status', 'Education Level', 'Occupation',\n               'Location', 'Policy Type', 'Customer Feedback', 'Smoking Status',\n               'Exercise Frequency', 'Property Type']\n\ncat_pipeline = make_pipeline(\n    SimpleImputer(strategy=\"most_frequent\"),\n     ce.cat_boost.CatBoostEncoder())\nnum_pipeline = make_pipeline(\n    SimpleImputer(strategy=\"mean\"),\n    FunctionTransformer(), \n    StandardScaler())\nlog_pipeline = make_pipeline(\n    SimpleImputer(strategy=\"median\"),\n    FunctionTransformer(np.log1p, feature_names_out=\"one-to-one\"),\n    StandardScaler())\ndefault_num_pipeline = make_pipeline(SimpleImputer(strategy=\"mean\"),\n                                     StandardScaler())\npreprocessing = ColumnTransformer([\n        (\"log\", log_pipeline, [\"Annual Income\",\"Previous Claims\"]),\n        (\"cat\", cat_pipeline, cat_attribs),\n        (\"num\", num_pipeline, num_attribs)\n    ],\n    remainder='drop') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:23.319254Z","iopub.execute_input":"2024-12-27T19:59:23.319655Z","iopub.status.idle":"2024-12-27T19:59:23.326962Z","shell.execute_reply.started":"2024-12-27T19:59:23.319621Z","shell.execute_reply":"2024-12-27T19:59:23.325878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lin_reg = make_pipeline(preprocessing, LinearRegression())\nlin_reg.fit(insurance_train, insurance_premiums)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:25.898568Z","iopub.execute_input":"2024-12-27T19:59:25.899016Z","iopub.status.idle":"2024-12-27T19:59:37.128833Z","shell.execute_reply.started":"2024-12-27T19:59:25.898978Z","shell.execute_reply":"2024-12-27T19:59:37.127612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"insurance_preds = lin_reg.predict(insurance_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:40.208886Z","iopub.execute_input":"2024-12-27T19:59:40.209254Z","iopub.status.idle":"2024-12-27T19:59:43.69034Z","shell.execute_reply.started":"2024-12-27T19:59:40.209222Z","shell.execute_reply":"2024-12-27T19:59:43.688905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lin_rmse = mean_squared_error(insurance_premiums, insurance_preds,squared=False)\nlin_rmse","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:45.019994Z","iopub.execute_input":"2024-12-27T19:59:45.020423Z","iopub.status.idle":"2024-12-27T19:59:45.035906Z","shell.execute_reply.started":"2024-12-27T19:59:45.020349Z","shell.execute_reply":"2024-12-27T19:59:45.034821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = lin_reg.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:46.592396Z","iopub.execute_input":"2024-12-27T19:59:46.592789Z","iopub.status.idle":"2024-12-27T19:59:48.875304Z","shell.execute_reply.started":"2024-12-27T19:59:46.592753Z","shell.execute_reply":"2024-12-27T19:59:48.873842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\",index_col='id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:56.954598Z","iopub.execute_input":"2024-12-27T19:59:56.955656Z","iopub.status.idle":"2024-12-27T19:59:57.242974Z","shell.execute_reply.started":"2024-12-27T19:59:56.95561Z","shell.execute_reply":"2024-12-27T19:59:57.241544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample['Premium Amount'] = np.expm1(test_preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T19:59:58.987276Z","iopub.execute_input":"2024-12-27T19:59:58.987689Z","iopub.status.idle":"2024-12-27T19:59:59.013701Z","shell.execute_reply.started":"2024-12-27T19:59:58.987652Z","shell.execute_reply":"2024-12-27T19:59:59.012584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.to_csv(\"submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T20:00:00.416978Z","iopub.execute_input":"2024-12-27T20:00:00.417391Z","iopub.status.idle":"2024-12-27T20:00:01.795799Z","shell.execute_reply.started":"2024-12-27T20:00:00.417339Z","shell.execute_reply":"2024-12-27T20:00:01.794241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}