{"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":30786,"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)\nfrom sklearn.preprocessing import LabelEncoder\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\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:04.826903Z","iopub.execute_input":"2024-12-03T13:20:04.827821Z","iopub.status.idle":"2024-12-03T13:20:07.391469Z","shell.execute_reply.started":"2024-12-03T13:20:04.82776Z","shell.execute_reply":"2024-12-03T13:20:07.390051Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Import Dataset**","metadata":{}},{"cell_type":"code","source":"df_tr=pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv').drop(columns='id')\ndf_tr.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:07.393775Z","iopub.execute_input":"2024-12-03T13:20:07.394451Z","iopub.status.idle":"2024-12-03T13:20:14.292371Z","shell.execute_reply.started":"2024-12-03T13:20:07.3944Z","shell.execute_reply":"2024-12-03T13:20:14.291318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_id=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')['id']\ndf_ts=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv').drop(columns='id')\ndf_ts.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:14.293555Z","iopub.execute_input":"2024-12-03T13:20:14.293845Z","iopub.status.idle":"2024-12-03T13:20:21.484683Z","shell.execute_reply.started":"2024-12-03T13:20:14.293818Z","shell.execute_reply":"2024-12-03T13:20:21.483575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tr.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:21.486821Z","iopub.execute_input":"2024-12-03T13:20:21.487156Z","iopub.status.idle":"2024-12-03T13:20:22.156918Z","shell.execute_reply.started":"2024-12-03T13:20:21.487125Z","shell.execute_reply":"2024-12-03T13:20:22.155821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ts.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:22.15838Z","iopub.execute_input":"2024-12-03T13:20:22.158865Z","iopub.status.idle":"2024-12-03T13:20:22.56584Z","shell.execute_reply.started":"2024-12-03T13:20:22.158817Z","shell.execute_reply":"2024-12-03T13:20:22.564747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tr.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:22.567323Z","iopub.execute_input":"2024-12-03T13:20:22.567826Z","iopub.status.idle":"2024-12-03T13:20:23.227943Z","shell.execute_reply.started":"2024-12-03T13:20:22.567781Z","shell.execute_reply":"2024-12-03T13:20:23.226822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ts.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:23.229114Z","iopub.execute_input":"2024-12-03T13:20:23.230064Z","iopub.status.idle":"2024-12-03T13:20:23.663148Z","shell.execute_reply.started":"2024-12-03T13:20:23.230031Z","shell.execute_reply":"2024-12-03T13:20:23.661824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tr.isnull().sum()/len(df_tr)*100 #percentage of null value ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:23.664824Z","iopub.execute_input":"2024-12-03T13:20:23.665313Z","iopub.status.idle":"2024-12-03T13:20:24.314125Z","shell.execute_reply.started":"2024-12-03T13:20:23.665264Z","shell.execute_reply":"2024-12-03T13:20:24.313116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ts.isnull().sum()/len(df_ts)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:24.315531Z","iopub.execute_input":"2024-12-03T13:20:24.315945Z","iopub.status.idle":"2024-12-03T13:20:24.748136Z","shell.execute_reply.started":"2024-12-03T13:20:24.315902Z","shell.execute_reply":"2024-12-03T13:20:24.74701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in df_tr.columns:\n    if df_tr[i].dtypes == 'object':  \n        df_tr[i].fillna(df_tr[i].mode()[0], inplace=True) \n    elif df_tr[i].dtypes=='int64':\n    \tdf_tr[i].fillna(df_tr[i].mean(),inplace=True)\n    elif  df_tr[i].dtypes=='float64':\n    \tdf_tr[i].fillna(df_tr[i].mean(),inplace=True)","metadata":{"trusted":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:24.751228Z","iopub.execute_input":"2024-12-03T13:20:24.751771Z","iopub.status.idle":"2024-12-03T13:20:26.912524Z","shell.execute_reply.started":"2024-12-03T13:20:24.751732Z","shell.execute_reply":"2024-12-03T13:20:26.911405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for k in df_ts.columns:\n    if df_ts[k].dtypes == 'object':  \n        df_ts[k].fillna(df_ts[k].mode()[0], inplace=True) \n    elif df_ts[k].dtypes=='int64':\n    \tdf_ts[k].fillna(df_ts[k].mean(),inplace=True)\n    elif  df_ts[k].dtypes=='float64':\n    \tdf_ts[k].fillna(df_ts[k].mean(),inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:26.913893Z","iopub.execute_input":"2024-12-03T13:20:26.914335Z","iopub.status.idle":"2024-12-03T13:20:28.382398Z","shell.execute_reply.started":"2024-12-03T13:20:26.914273Z","shell.execute_reply":"2024-12-03T13:20:28.381387Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tr.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:28.383767Z","iopub.execute_input":"2024-12-03T13:20:28.384187Z","iopub.status.idle":"2024-12-03T13:20:29.027241Z","shell.execute_reply.started":"2024-12-03T13:20:28.384143Z","shell.execute_reply":"2024-12-03T13:20:29.02603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ts.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:29.02865Z","iopub.execute_input":"2024-12-03T13:20:29.029058Z","iopub.status.idle":"2024-12-03T13:20:29.458132Z","shell.execute_reply.started":"2024-12-03T13:20:29.029026Z","shell.execute_reply":"2024-12-03T13:20:29.457057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le=LabelEncoder()\n\nfor j in df_tr.columns:\n    if df_tr[j].dtypes=='object':\n        df_tr[j]=le.fit_transform(df_tr[j])","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:29.459286Z","iopub.execute_input":"2024-12-03T13:20:29.459596Z","iopub.status.idle":"2024-12-03T13:20:33.194907Z","shell.execute_reply.started":"2024-12-03T13:20:29.459569Z","shell.execute_reply":"2024-12-03T13:20:33.193782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for l in df_ts.columns:\n    if df_ts[l].dtypes=='object':\n        df_ts[l]=le.fit_transform(df_ts[l])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:33.196828Z","iopub.execute_input":"2024-12-03T13:20:33.197295Z","iopub.status.idle":"2024-12-03T13:20:35.779107Z","shell.execute_reply.started":"2024-12-03T13:20:33.197239Z","shell.execute_reply":"2024-12-03T13:20:35.777701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tr.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:35.780715Z","iopub.execute_input":"2024-12-03T13:20:35.78108Z","iopub.status.idle":"2024-12-03T13:20:35.799548Z","shell.execute_reply.started":"2024-12-03T13:20:35.781045Z","shell.execute_reply":"2024-12-03T13:20:35.798272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ts.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:35.80106Z","iopub.execute_input":"2024-12-03T13:20:35.801521Z","iopub.status.idle":"2024-12-03T13:20:35.822414Z","shell.execute_reply.started":"2024-12-03T13:20:35.801459Z","shell.execute_reply":"2024-12-03T13:20:35.821239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df_tr.iloc[:,:-1]\ny=df_tr['Premium Amount']\nx","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:35.823945Z","iopub.execute_input":"2024-12-03T13:20:35.824381Z","iopub.status.idle":"2024-12-03T13:20:35.956156Z","shell.execute_reply.started":"2024-12-03T13:20:35.824334Z","shell.execute_reply":"2024-12-03T13:20:35.955024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:35.957375Z","iopub.execute_input":"2024-12-03T13:20:35.957838Z","iopub.status.idle":"2024-12-03T13:20:36.64823Z","shell.execute_reply.started":"2024-12-03T13:20:35.957806Z","shell.execute_reply":"2024-12-03T13:20:36.647123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from xgboost import XGBRegressor\n\n# Create the model\nxgb_reg = XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=3)\n\n# Train the model\nxgb_reg.fit(xtrain, ytrain)\npred=xgb_reg.predict(xtest)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:36.649636Z","iopub.execute_input":"2024-12-03T13:20:36.649991Z","iopub.status.idle":"2024-12-03T13:20:40.439364Z","shell.execute_reply.started":"2024-12-03T13:20:36.64996Z","shell.execute_reply":"2024-12-03T13:20:40.43855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef rmsle(y_true, y_pred):\n    # Ensure no negative values by adding 1 to predictions and true values\n    y_true = np.array(y_true)\n    y_pred = np.array(y_pred)\n    \n    # Calculate RMSLE\n    log_true = np.log1p(y_true)\n    log_pred = np.log1p(y_pred)\n    squared_log_error = np.square(log_true - log_pred)\n    rmsle_value = np.sqrt(np.mean(squared_log_error))\n    \n    return rmsle_value\n\n\nrmsle_value = rmsle(ytest, pred)\nprint(f\"RMSLE: {rmsle_value:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:20:40.440344Z","iopub.execute_input":"2024-12-03T13:20:40.440667Z","iopub.status.idle":"2024-12-03T13:20:40.461889Z","shell.execute_reply.started":"2024-12-03T13:20:40.440634Z","shell.execute_reply":"2024-12-03T13:20:40.460787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor \nmodel=RandomForestRegressor()\nmodel.fit(xtrain,ytrain)\nmodel.score(xtest,ytest)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:21:12.421779Z","iopub.execute_input":"2024-12-03T13:21:12.422203Z","iopub.status.idle":"2024-12-03T13:24:32.14532Z","shell.execute_reply.started":"2024-12-03T13:21:12.422166Z","shell.execute_reply":"2024-12-03T13:24:32.143514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict=xgb_reg.predict(df_ts)\n\ndf=pd.DataFrame()\ndf['id']=test_id\ndf['Premium Amount']=predict\n\ndf.to_csv('ins_sub.csv',index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T13:26:48.765919Z","iopub.execute_input":"2024-12-03T13:26:48.76631Z","iopub.status.idle":"2024-12-03T13:26:50.375321Z","shell.execute_reply.started":"2024-12-03T13:26:48.766276Z","shell.execute_reply":"2024-12-03T13:26:50.374129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.to_csv('insu_sub.csv')","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}