{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:53:47.860356Z","iopub.execute_input":"2024-12-06T09:53:47.860818Z","iopub.status.idle":"2024-12-06T09:53:50.582029Z","shell.execute_reply.started":"2024-12-06T09:53:47.860772Z","shell.execute_reply":"2024-12-06T09:53:50.580904Z"}},"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-06T09:53:50.584321Z","iopub.execute_input":"2024-12-06T09:53:50.584943Z","iopub.status.idle":"2024-12-06T09:53:54.167466Z","shell.execute_reply.started":"2024-12-06T09:53:50.584895Z","shell.execute_reply":"2024-12-06T09:53:54.166483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:53:54.16872Z","iopub.execute_input":"2024-12-06T09:53:54.169174Z","iopub.status.idle":"2024-12-06T09:53:54.200126Z","shell.execute_reply.started":"2024-12-06T09:53:54.169127Z","shell.execute_reply":"2024-12-06T09:53:54.199057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:53:54.201937Z","iopub.execute_input":"2024-12-06T09:53:54.202393Z","iopub.status.idle":"2024-12-06T09:53:54.224078Z","shell.execute_reply.started":"2024-12-06T09:53:54.202346Z","shell.execute_reply":"2024-12-06T09:53:54.2229Z"}},"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-06T09:53:54.227463Z","iopub.execute_input":"2024-12-06T09:53:54.227864Z","iopub.status.idle":"2024-12-06T09:57:28.416035Z","shell.execute_reply.started":"2024-12-06T09:53:54.227831Z","shell.execute_reply":"2024-12-06T09:57:28.415033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:28.417583Z","iopub.execute_input":"2024-12-06T09:57:28.418041Z","iopub.status.idle":"2024-12-06T09:57:29.011189Z","shell.execute_reply.started":"2024-12-06T09:57:28.417972Z","shell.execute_reply":"2024-12-06T09:57:29.009771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:29.012505Z","iopub.execute_input":"2024-12-06T09:57:29.012843Z","iopub.status.idle":"2024-12-06T09:57:29.585805Z","shell.execute_reply.started":"2024-12-06T09:57:29.012805Z","shell.execute_reply":"2024-12-06T09:57:29.584666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = 'Premium Amount'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:29.587364Z","iopub.execute_input":"2024-12-06T09:57:29.588344Z","iopub.status.idle":"2024-12-06T09:57:29.593327Z","shell.execute_reply.started":"2024-12-06T09:57:29.588306Z","shell.execute_reply":"2024-12-06T09:57:29.592152Z"}},"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-06T09:57:29.594802Z","iopub.execute_input":"2024-12-06T09:57:29.595201Z","iopub.status.idle":"2024-12-06T09:57:29.866316Z","shell.execute_reply.started":"2024-12-06T09:57:29.595166Z","shell.execute_reply":"2024-12-06T09:57:29.865183Z"}},"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-06T09:57:29.867721Z","iopub.execute_input":"2024-12-06T09:57:29.868046Z","iopub.status.idle":"2024-12-06T09:57:30.497191Z","shell.execute_reply.started":"2024-12-06T09:57:29.868014Z","shell.execute_reply":"2024-12-06T09:57:30.496181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.duplicated().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:30.49852Z","iopub.execute_input":"2024-12-06T09:57:30.498957Z","iopub.status.idle":"2024-12-06T09:57:31.873569Z","shell.execute_reply.started":"2024-12-06T09:57:30.498911Z","shell.execute_reply":"2024-12-06T09:57:31.872436Z"}},"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-06T09:57:31.874943Z","iopub.execute_input":"2024-12-06T09:57:31.875339Z","iopub.status.idle":"2024-12-06T09:57:32.775604Z","shell.execute_reply.started":"2024-12-06T09:57:31.875308Z","shell.execute_reply":"2024-12-06T09:57:32.774572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe(include='O').T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:32.776867Z","iopub.execute_input":"2024-12-06T09:57:32.777202Z","iopub.status.idle":"2024-12-06T09:57:34.397189Z","shell.execute_reply.started":"2024-12-06T09:57:32.777169Z","shell.execute_reply":"2024-12-06T09:57:34.396149Z"}},"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-06T09:57:34.400224Z","iopub.execute_input":"2024-12-06T09:57:34.400552Z","iopub.status.idle":"2024-12-06T09:57:34.943493Z","shell.execute_reply.started":"2024-12-06T09:57:34.40052Z","shell.execute_reply":"2024-12-06T09:57:34.942426Z"}},"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-06T09:57:34.944871Z","iopub.execute_input":"2024-12-06T09:57:34.945518Z","iopub.status.idle":"2024-12-06T09:57:35.522281Z","shell.execute_reply.started":"2024-12-06T09:57:34.945468Z","shell.execute_reply":"2024-12-06T09:57:35.521245Z"}},"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-06T09:57:35.523641Z","iopub.execute_input":"2024-12-06T09:57:35.524102Z","iopub.status.idle":"2024-12-06T09:57:46.740136Z","shell.execute_reply.started":"2024-12-06T09:57:35.524059Z","shell.execute_reply":"2024-12-06T09:57:46.738331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = LinearRegression()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:46.741775Z","iopub.execute_input":"2024-12-06T09:57:46.742151Z","iopub.status.idle":"2024-12-06T09:57:46.747341Z","shell.execute_reply.started":"2024-12-06T09:57:46.742117Z","shell.execute_reply":"2024-12-06T09:57:46.746067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:46.748824Z","iopub.execute_input":"2024-12-06T09:57:46.74941Z","iopub.status.idle":"2024-12-06T09:57:48.388599Z","shell.execute_reply.started":"2024-12-06T09:57:46.749362Z","shell.execute_reply":"2024-12-06T09:57:48.386248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=model.predict(testProcessed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:48.390166Z","iopub.execute_input":"2024-12-06T09:57:48.390569Z","iopub.status.idle":"2024-12-06T09:57:48.421838Z","shell.execute_reply.started":"2024-12-06T09:57:48.390527Z","shell.execute_reply":"2024-12-06T09:57:48.420499Z"}},"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)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:57:48.423158Z","iopub.execute_input":"2024-12-06T09:57:48.423598Z","iopub.status.idle":"2024-12-06T09:57:50.372827Z","shell.execute_reply.started":"2024-12-06T09:57:48.423549Z","shell.execute_reply":"2024-12-06T09:57:50.371399Z"}},"outputs":[],"execution_count":null}]}