{"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":"markdown","source":"# Import Libraries\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom xgboost import XGBRFRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OrdinalEncoder\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.preprocessing import OneHotEncoder\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:18:43.927558Z","iopub.execute_input":"2024-12-01T01:18:43.927967Z","iopub.status.idle":"2024-12-01T01:18:43.934291Z","shell.execute_reply.started":"2024-12-01T01:18:43.927932Z","shell.execute_reply":"2024-12-01T01:18:43.933123Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Import Data\n","metadata":{}},{"cell_type":"code","source":"train = 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')\nsample = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')\n\n\nprint(\"******Train*****\"*6)\ndisplay(train.head(), train.shape, train.dtypes, \"****Null****\"*10, train.isnull().sum(), \"****Unique****\"*10, train.nunique())\nprint(\"******Test*****\"*6)\ndisplay(test.head(), test.shape, test.dtypes, \"****Null****\"*10, test.isnull().sum(), \"****Unique****\"*10, test.nunique())\nprint(\"******Sample*****\"*6)\ndisplay(sample.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:13:19.871654Z","iopub.execute_input":"2024-12-01T01:13:19.872029Z","iopub.status.idle":"2024-12-01T01:13:31.802782Z","shell.execute_reply.started":"2024-12-01T01:13:19.871998Z","shell.execute_reply":"2024-12-01T01:13:31.801445Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols = [cname for cname in test.columns if test[cname].dtype == \"object\"]\ncategorical_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:13:31.805325Z","iopub.execute_input":"2024-12-01T01:13:31.805807Z","iopub.status.idle":"2024-12-01T01:13:31.816443Z","shell.execute_reply.started":"2024-12-01T01:13:31.805757Z","shell.execute_reply":"2024-12-01T01:13:31.814902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols = [cname for cname in test.columns if test[cname].dtype == \"float64\"]\nnumerical_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:13:31.818684Z","iopub.execute_input":"2024-12-01T01:13:31.819169Z","iopub.status.idle":"2024-12-01T01:13:31.835709Z","shell.execute_reply.started":"2024-12-01T01:13:31.819119Z","shell.execute_reply":"2024-12-01T01:13:31.834483Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"xgb = XGBRFRegressor(n_jobs = -1, random_state = 8)\nskf = StratifiedKFold(n_splits=5, shuffle = True, random_state = 9)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:13:31.838192Z","iopub.execute_input":"2024-12-01T01:13:31.838795Z","iopub.status.idle":"2024-12-01T01:13:31.854189Z","shell.execute_reply.started":"2024-12-01T01:13:31.838743Z","shell.execute_reply":"2024-12-01T01:13:31.852851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = train.pop('Premium Amount')\nX = train ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:13:51.224215Z","iopub.execute_input":"2024-12-01T01:13:51.224744Z","iopub.status.idle":"2024-12-01T01:13:51.230725Z","shell.execute_reply.started":"2024-12-01T01:13:51.224708Z","shell.execute_reply":"2024-12-01T01:13:51.229403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preprocessing for numerical data\nnumerical_transformer = SimpleImputer(strategy='constant')\n\n# Preprocessing for categorical data\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\n# Bundle preprocessing for numerical and categorical data\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ], remainder='passthrough')\n\nmy_pipeline = Pipeline(steps=[('preprocessor', preprocessor),\n                              ('model', xgb)\n                             ])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:18:52.790295Z","iopub.execute_input":"2024-12-01T01:18:52.790714Z","iopub.status.idle":"2024-12-01T01:18:52.797774Z","shell.execute_reply.started":"2024-12-01T01:18:52.790671Z","shell.execute_reply":"2024-12-01T01:18:52.796425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nscore = cross_validate(my_pipeline, X, y, cv = skf, n_jobs = -1, scoring = 'neg_mean_squared_log_error', return_train_score = True)\n\nprint(-score['test_score'].mean())\nprint(-score['train_score'].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:20:15.562533Z","iopub.execute_input":"2024-12-01T01:20:15.562969Z","iopub.status.idle":"2024-12-01T01:29:54.760923Z","shell.execute_reply.started":"2024-12-01T01:20:15.562932Z","shell.execute_reply":"2024-12-01T01:29:54.759513Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"my_pipeline.fit(X,y)\npredictions = my_pipeline.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T01:40:51.887396Z","iopub.execute_input":"2024-12-01T01:40:51.889963Z","iopub.status.idle":"2024-12-01T01:43:49.506536Z","shell.execute_reply.started":"2024-12-01T01:40:51.88989Z","shell.execute_reply":"2024-12-01T01:43:49.505347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample['Premium Amount'] = predictions\nsample.head()\nsample.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T03:01:42.452902Z","iopub.execute_input":"2024-12-01T03:01:42.453448Z","iopub.status.idle":"2024-12-01T03:01:42.738859Z","shell.execute_reply.started":"2024-12-01T03:01:42.453391Z","shell.execute_reply":"2024-12-01T03:01:42.73754Z"}},"outputs":[],"execution_count":null}]}