{"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\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import accuracy_score, mean_squared_error\nimport warnings\n\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:49:52.67532Z","iopub.execute_input":"2024-12-09T03:49:52.675983Z","iopub.status.idle":"2024-12-09T03:49:55.432112Z","shell.execute_reply.started":"2024-12-09T03:49:52.675937Z","shell.execute_reply":"2024-12-09T03:49:55.430821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv', index_col='id', engine='pyarrow')\ntest_df = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv', index_col='id', engine='pyarrow')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:49:55.434167Z","iopub.execute_input":"2024-12-09T03:49:55.43481Z","iopub.status.idle":"2024-12-09T03:49:58.784216Z","shell.execute_reply.started":"2024-12-09T03:49:55.434758Z","shell.execute_reply":"2024-12-09T03:49:58.783259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:49:58.785394Z","iopub.execute_input":"2024-12-09T03:49:58.785763Z","iopub.status.idle":"2024-12-09T03:49:58.824549Z","shell.execute_reply.started":"2024-12-09T03:49:58.785731Z","shell.execute_reply":"2024-12-09T03:49:58.823315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:49:58.827341Z","iopub.execute_input":"2024-12-09T03:49:58.82781Z","iopub.status.idle":"2024-12-09T03:49:58.853204Z","shell.execute_reply.started":"2024-12-09T03:49:58.827765Z","shell.execute_reply":"2024-12-09T03:49:58.852135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def date_separator(x):\n    return pd.Series([x.day, x.month, x.year])\n\ntrain_df[['day', 'month', 'year']] = train_df['Policy Start Date'].apply(date_separator)\ntest_df[['day', 'month', 'year']] = test_df['Policy Start Date'].apply(date_separator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:49:58.854634Z","iopub.execute_input":"2024-12-09T03:49:58.854968Z","iopub.status.idle":"2024-12-09T03:53:33.657889Z","shell.execute_reply.started":"2024-12-09T03:49:58.854937Z","shell.execute_reply":"2024-12-09T03:53:33.656661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:33.659711Z","iopub.execute_input":"2024-12-09T03:53:33.660089Z","iopub.status.idle":"2024-12-09T03:53:34.253547Z","shell.execute_reply.started":"2024-12-09T03:53:33.660054Z","shell.execute_reply":"2024-12-09T03:53:34.252357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:34.255145Z","iopub.execute_input":"2024-12-09T03:53:34.255621Z","iopub.status.idle":"2024-12-09T03:53:34.869966Z","shell.execute_reply.started":"2024-12-09T03:53:34.255563Z","shell.execute_reply":"2024-12-09T03:53:34.868832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = 'Premium Amount'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:34.871098Z","iopub.execute_input":"2024-12-09T03:53:34.871378Z","iopub.status.idle":"2024-12-09T03:53:34.876271Z","shell.execute_reply.started":"2024-12-09T03:53:34.87135Z","shell.execute_reply":"2024-12-09T03:53:34.875126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_features = train_df.drop(target, axis=1).select_dtypes(include=np.number).columns.values\nnumerical_features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:34.877519Z","iopub.execute_input":"2024-12-09T03:53:34.87782Z","iopub.status.idle":"2024-12-09T03:53:35.139775Z","shell.execute_reply.started":"2024-12-09T03:53:34.877791Z","shell.execute_reply":"2024-12-09T03:53:35.138515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_features = train_df.drop(target, axis=1).select_dtypes(include='object').columns.values\ncategorical_features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:35.142893Z","iopub.execute_input":"2024-12-09T03:53:35.143222Z","iopub.status.idle":"2024-12-09T03:53:35.808061Z","shell.execute_reply.started":"2024-12-09T03:53:35.14319Z","shell.execute_reply":"2024-12-09T03:53:35.806858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.duplicated().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:35.809475Z","iopub.execute_input":"2024-12-09T03:53:35.809826Z","iopub.status.idle":"2024-12-09T03:53:37.179025Z","shell.execute_reply.started":"2024-12-09T03:53:35.809794Z","shell.execute_reply":"2024-12-09T03:53:37.177979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[numerical_features].astype(np.float_).describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:37.180266Z","iopub.execute_input":"2024-12-09T03:53:37.180603Z","iopub.status.idle":"2024-12-09T03:53:38.06954Z","shell.execute_reply.started":"2024-12-09T03:53:37.18057Z","shell.execute_reply":"2024-12-09T03:53:38.068487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe(include='O').T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:38.070897Z","iopub.execute_input":"2024-12-09T03:53:38.071323Z","iopub.status.idle":"2024-12-09T03:53:39.690073Z","shell.execute_reply.started":"2024-12-09T03:53:38.071277Z","shell.execute_reply":"2024-12-09T03:53:39.689024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler, FunctionTransformer, LabelEncoder, OneHotEncoder\nfrom sklearn.pipeline import make_pipeline, Pipeline\nfrom sklearn.compose import ColumnTransformer, make_column_selector, make_column_transformer\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import SimpleImputer, IterativeImputer\nimport category_encoders as ce\n\npreprocessing = ColumnTransformer([\n    ('num', make_pipeline(SimpleImputer(strategy='mean'), FunctionTransformer(), StandardScaler()), numerical_features),\n    ('cat', make_pipeline(SimpleImputer(strategy='most_frequent'), ce.cat_boost.CatBoostEncoder()), categorical_features)\n], remainder='drop')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:39.691514Z","iopub.execute_input":"2024-12-09T03:53:39.69196Z","iopub.status.idle":"2024-12-09T03:53:40.207838Z","shell.execute_reply.started":"2024-12-09T03:53:39.691913Z","shell.execute_reply":"2024-12-09T03:53:40.206797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df.copy()\ny = X.pop(target)\ny = np.log1p(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:40.208992Z","iopub.execute_input":"2024-12-09T03:53:40.209404Z","iopub.status.idle":"2024-12-09T03:53:40.792332Z","shell.execute_reply.started":"2024-12-09T03:53:40.209376Z","shell.execute_reply":"2024-12-09T03:53:40.791421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = preprocessing.fit_transform(X, y)\ntestProcessed = preprocessing.transform(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:40.79358Z","iopub.execute_input":"2024-12-09T03:53:40.793883Z","iopub.status.idle":"2024-12-09T03:53:52.150754Z","shell.execute_reply.started":"2024-12-09T03:53:40.793853Z","shell.execute_reply":"2024-12-09T03:53:52.149626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:52.152163Z","iopub.execute_input":"2024-12-09T03:53:52.152542Z","iopub.status.idle":"2024-12-09T03:53:53.099066Z","shell.execute_reply.started":"2024-12-09T03:53:52.152495Z","shell.execute_reply":"2024-12-09T03:53:53.097913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgb_params = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'num_leaves': 31,\n    'max_depth': -1,\n    'learning_rate': 0.05,\n    'n_estimators': 1000,\n    'subsample': 0.8,  \n    'colsample_bytree': 0.8,\n    'reg_alpha': 0.1,\n    'reg_lambda': 0.1\n}\n\nlgb_model = LGBMRegressor(**lgb_params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:53.100396Z","iopub.execute_input":"2024-12-09T03:53:53.101025Z","iopub.status.idle":"2024-12-09T03:53:53.107357Z","shell.execute_reply.started":"2024-12-09T03:53:53.100981Z","shell.execute_reply":"2024-12-09T03:53:53.105883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from xgboost import XGBRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:53:53.10895Z","iopub.execute_input":"2024-12-09T03:53:53.109294Z","iopub.status.idle":"2024-12-09T03:53:53.292982Z","shell.execute_reply.started":"2024-12-09T03:53:53.109263Z","shell.execute_reply":"2024-12-09T03:53:53.291592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_model = XGBRegressor(\n    n_estimators=1000,\n    learning_rate=0.05,\n    max_depth=6,\n    random_state=42,\n    n_jobs=-1,\n    verbose=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:04.545846Z","iopub.execute_input":"2024-12-09T05:23:04.546376Z","iopub.status.idle":"2024-12-09T05:23:04.553821Z","shell.execute_reply.started":"2024-12-09T05:23:04.54633Z","shell.execute_reply":"2024-12-09T05:23:04.552367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neural_network import MLPRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:06.094133Z","iopub.execute_input":"2024-12-09T05:23:06.094584Z","iopub.status.idle":"2024-12-09T05:23:06.0999Z","shell.execute_reply.started":"2024-12-09T05:23:06.094545Z","shell.execute_reply":"2024-12-09T05:23:06.098611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mlp = MLPRegressor(   \n    hidden_layer_sizes=(100, 100),\n    activation='relu',\n    solver='adam',\n    learning_rate_init=0.001,\n    max_iter= 1000,\n    batch_size='auto',\n    early_stopping=True,\n    tol=1e-4,\n    verbose=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:06.438633Z","iopub.execute_input":"2024-12-09T05:23:06.439056Z","iopub.status.idle":"2024-12-09T05:23:06.445064Z","shell.execute_reply.started":"2024-12-09T05:23:06.439022Z","shell.execute_reply":"2024-12-09T05:23:06.443727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:06.814862Z","iopub.execute_input":"2024-12-09T05:23:06.815244Z","iopub.status.idle":"2024-12-09T05:23:06.820694Z","shell.execute_reply.started":"2024-12-09T05:23:06.815215Z","shell.execute_reply":"2024-12-09T05:23:06.819219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf_model = RandomForestRegressor(\n    n_estimators=100, \n    max_depth=6, \n    n_jobs=-1,\n    verbose=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:08.582406Z","iopub.execute_input":"2024-12-09T05:23:08.582846Z","iopub.status.idle":"2024-12-09T05:23:08.588884Z","shell.execute_reply.started":"2024-12-09T05:23:08.582809Z","shell.execute_reply":"2024-12-09T05:23:08.587506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import StackingRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:09.283166Z","iopub.execute_input":"2024-12-09T05:23:09.283606Z","iopub.status.idle":"2024-12-09T05:23:09.289034Z","shell.execute_reply.started":"2024-12-09T05:23:09.283569Z","shell.execute_reply":"2024-12-09T05:23:09.287721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"meta_model = LinearRegression()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:11.213553Z","iopub.execute_input":"2024-12-09T05:23:11.213967Z","iopub.status.idle":"2024-12-09T05:23:11.219521Z","shell.execute_reply.started":"2024-12-09T05:23:11.213934Z","shell.execute_reply":"2024-12-09T05:23:11.218104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostRegressor\n\ncatboost_params = {\n    'iterations': 1000,\n    'learning_rate': 0.05,\n    'depth': 6,\n    'l2_leaf_reg': 3,\n    'loss_function': 'RMSE',\n    'border_count': 32,\n    'thread_count': -1,\n    'early_stopping_rounds': 50,\n    'verbose': True\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:13.717068Z","iopub.execute_input":"2024-12-09T05:23:13.718004Z","iopub.status.idle":"2024-12-09T05:23:13.723602Z","shell.execute_reply.started":"2024-12-09T05:23:13.717956Z","shell.execute_reply":"2024-12-09T05:23:13.722286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_models = [\n    ('xgb', xgb_model), \n    ('lgb', lgb_model),\n    # ('rf', rf_model),\n    ('mlp', mlp)\n    # ('catboost', CatBoostRegressor(**catboost_params))\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:16.624707Z","iopub.execute_input":"2024-12-09T05:23:16.625117Z","iopub.status.idle":"2024-12-09T05:23:16.63071Z","shell.execute_reply.started":"2024-12-09T05:23:16.625083Z","shell.execute_reply":"2024-12-09T05:23:16.62949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = StackingRegressor(estimators=base_models, final_estimator=meta_model, n_jobs=-1, verbose=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:27.760861Z","iopub.execute_input":"2024-12-09T05:23:27.761289Z","iopub.status.idle":"2024-12-09T05:23:27.768007Z","shell.execute_reply.started":"2024-12-09T05:23:27.761255Z","shell.execute_reply":"2024-12-09T05:23:27.766576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:23:29.72332Z","iopub.execute_input":"2024-12-09T05:23:29.724751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(testProcessed)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/playground-series-s4e12/sample_submission.csv\")\nsub[target] = np.expm1(y_pred)\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}