{"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"},{"sourceId":9178166,"sourceType":"datasetVersion","datasetId":5547076}],"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\nfrom scipy.stats import kruskal\n\nimport matplotlib \nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom lightgbm import LGBMRegressor\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.metrics import mean_squared_log_error\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\ncmap = matplotlib.colormaps.get_cmap('rocket_r')","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-12-22T14:55:35.520899Z","iopub.execute_input":"2024-12-22T14:55:35.521278Z","iopub.status.idle":"2024-12-22T14:55:39.561285Z","shell.execute_reply.started":"2024-12-22T14:55:35.521223Z","shell.execute_reply":"2024-12-22T14:55:39.56046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv',index_col='id')\ntest_data = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv',index_col='id')\ncombined_data  = pd.concat([train_data,test_data])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:23.649869Z","iopub.execute_input":"2024-12-22T14:56:23.650884Z","iopub.status.idle":"2024-12-22T14:56:34.109474Z","shell.execute_reply.started":"2024-12-22T14:56:23.650848Z","shell.execute_reply":"2024-12-22T14:56:34.108751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"combined_data['Policy Start Date'] = pd.DatetimeIndex(combined_data['Policy Start Date'])\ncombined_data['year'] = combined_data['Policy Start Date'].dt.year\ncombined_data['month'] = combined_data['Policy Start Date'].dt.month\ncombined_data['date'] = combined_data['Policy Start Date'].dt.day\ncombined_data.drop('Policy Start Date',axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:34.111194Z","iopub.execute_input":"2024-12-22T14:56:34.111979Z","iopub.status.idle":"2024-12-22T14:56:35.74367Z","shell.execute_reply.started":"2024-12-22T14:56:34.111931Z","shell.execute_reply":"2024-12-22T14:56:35.742805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"discrete_feat = [feat for feat in combined_data.select_dtypes(exclude='O') if combined_data[feat].nunique()<35]\ncontinuous_feat = [feat for feat in combined_data.select_dtypes(include='number') if feat not in discrete_feat]\ncategorical_feat = [feat for feat in combined_data.select_dtypes(include='O')]\ncontinuous_feat.remove('Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:35.744842Z","iopub.execute_input":"2024-12-22T14:56:35.745137Z","iopub.status.idle":"2024-12-22T14:56:37.248152Z","shell.execute_reply.started":"2024-12-22T14:56:35.745108Z","shell.execute_reply":"2024-12-22T14:56:37.247233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for feat in discrete_feat: \n    if combined_data[feat].isna().sum()>0:\n        combined_data[feat+'_is_na'] = combined_data[feat].isna().astype(int)\n        impute = np.exp( np.median( np.log1p(combined_data[feat]) ) )-1\n        combined_data[feat] = combined_data[feat].fillna(impute)\n\nfor feat in continuous_feat: \n    if combined_data[feat].isna().sum()>0:\n        combined_data[feat+'_is_na'] = combined_data[feat].isna().astype(int)\n        impute = np.exp( np.median( np.log1p(combined_data[feat]) ) )-1\n        combined_data[feat] = combined_data[feat].fillna(impute)\n        # combined_data[feat+'_bin'] = pd.cut(combined_data[feat],bins=50,labels=False,duplicates='drop')\n\ncombined_data[categorical_feat] = combined_data[categorical_feat].fillna('NA')\ncombined_data = pd.get_dummies(combined_data,columns=categorical_feat,dtype=int) \ncombined_data['Annual Income'] = np.log1p(combined_data['Annual Income'])\n#combined_data['Annual_Income_bin'] = pd.cut(combined_data['Annual Income'],bins=100,labels=False,duplicates='drop')\n#combined_data.drop('Annual Income',axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:37.250047Z","iopub.execute_input":"2024-12-22T14:56:37.25039Z","iopub.status.idle":"2024-12-22T14:56:43.397914Z","shell.execute_reply.started":"2024-12-22T14:56:37.250355Z","shell.execute_reply":"2024-12-22T14:56:43.396769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = combined_data[:len(test_data)].drop('Premium Amount',axis=1) \ny = combined_data[:len(test_data)]['Premium Amount']\ntest = combined_data[-len(test_data):].drop('Premium Amount',axis=1)\n\nmodel_log = LGBMRegressor(n_estimators=5000,learning_rate=0.01,lambda_l2=0.9,max_depth=7,verbose=-1,n_jobs=4)\nmodel_nonlog = LGBMRegressor(n_estimators=5000,learning_rate=0.01,lambda_l2=0.9,max_depth=7,verbose=-1,n_jobs=4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:43.399415Z","iopub.execute_input":"2024-12-22T14:56:43.399851Z","iopub.status.idle":"2024-12-22T14:56:43.540826Z","shell.execute_reply.started":"2024-12-22T14:56:43.399803Z","shell.execute_reply":"2024-12-22T14:56:43.53975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def validation_scores(score):\n    mean_scores = {metric: np.mean(values) for metric, values in score.items()}\n    return pd.DataFrame(mean_scores, index=['Score'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:43.541949Z","iopub.execute_input":"2024-12-22T14:56:43.542345Z","iopub.status.idle":"2024-12-22T14:56:43.548048Z","shell.execute_reply.started":"2024-12-22T14:56:43.542311Z","shell.execute_reply":"2024-12-22T14:56:43.54684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score = cross_validate(model_nonlog,X,y,cv=10,\n                       return_train_score=True,\n                       return_estimator=True,\n                       scoring=['r2','neg_mean_squared_log_error']\n                    )\nestimators_0 = score.pop('estimator')\nprint('RMSLE:',np.mean([np.sqrt(mean_squared_log_error(y,np.maximum(0,i.predict(X)))) for i in estimators_0]))\n\nvalidation_scores(score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:56:43.549569Z","iopub.execute_input":"2024-12-22T14:56:43.550093Z","iopub.status.idle":"2024-12-22T14:57:52.384377Z","shell.execute_reply.started":"2024-12-22T14:56:43.550049Z","shell.execute_reply":"2024-12-22T14:57:52.383314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oofs = []\nfor estimator in estimators_0:\n    oofs.append(np.maximum(0,estimator.predict(X)))\n\nfor i, oof in enumerate(oofs):\n    X[f'OOF_{i}'] = oof\n\nX['non_log'] = np.mean(oofs,axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:57:52.385783Z","iopub.execute_input":"2024-12-22T14:57:52.386191Z","iopub.status.idle":"2024-12-22T14:58:00.775512Z","shell.execute_reply.started":"2024-12-22T14:57:52.38613Z","shell.execute_reply":"2024-12-22T14:58:00.774705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oofs = []\nfor estimator in estimators_0:\n    oofs.append(np.maximum(0,estimator.predict(test)))\n\nfor i, oof in enumerate(oofs):\n    test[f'OOF_{i}'] = oof\n\ntest['non_log'] = np.mean(oofs,axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:58:00.776741Z","iopub.execute_input":"2024-12-22T14:58:00.777062Z","iopub.status.idle":"2024-12-22T14:58:09.682939Z","shell.execute_reply.started":"2024-12-22T14:58:00.777032Z","shell.execute_reply":"2024-12-22T14:58:09.682019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# using log1p because its beter performing than sqrt and cbrt\nscore = cross_validate(model_log,X,np.log1p(y),cv=10,\n                       return_train_score=True,\n                       return_estimator=True,\n                       scoring=['r2','neg_mean_squared_log_error']\n                    )\nestimators_1 = score.pop('estimator')\nprint('RMSLE:',np.mean([np.sqrt(mean_squared_log_error(y,np.exp(i.predict(X))-1)) for i in estimators_1]))\n\nvalidation_scores(score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:58:09.685076Z","iopub.execute_input":"2024-12-22T14:58:09.685399Z","iopub.status.idle":"2024-12-22T14:59:14.183738Z","shell.execute_reply.started":"2024-12-22T14:58:09.685368Z","shell.execute_reply":"2024-12-22T14:59:14.182743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = [np.exp(i.predict(test))-1 for i in estimators_1]\npred = np.mean(pred,axis=0)\ntest_data['Premium Amount'] = pred\ntest_data['Premium Amount'].to_csv(f'submission_1.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:59:50.535003Z","iopub.execute_input":"2024-12-22T14:59:50.535349Z","iopub.status.idle":"2024-12-22T15:00:02.278321Z","shell.execute_reply.started":"2024-12-22T14:59:50.535319Z","shell.execute_reply":"2024-12-22T15:00:02.277425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,10))\nsns.barplot(x=np.mean([e.feature_importances_ for e in estimators_1],axis=0),y=X.columns)\n# plt.xscale('log')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:00:05.261656Z","iopub.execute_input":"2024-12-22T15:00:05.262112Z","iopub.status.idle":"2024-12-22T15:00:06.203859Z","shell.execute_reply.started":"2024-12-22T15:00:05.262078Z","shell.execute_reply":"2024-12-22T15:00:06.202622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}