{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:30.812866Z","iopub.execute_input":"2024-12-16T16:41:30.813372Z","iopub.status.idle":"2024-12-16T16:41:32.054739Z","shell.execute_reply.started":"2024-12-16T16:41:30.813327Z","shell.execute_reply":"2024-12-16T16:41:32.05325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def split_numerical_categorical(df):\n    \"\"\"\n    Splits the columns of a DataFrame into numerical and categorical features.\n\n    Parameters:\n    df (pandas.DataFrame): The DataFrame to split.\n\n    Returns:\n    tuple: A tuple containing two lists - numerical columns and categorical columns.\n    \"\"\"\n    numerical_cols = df.select_dtypes(include=['number']).columns.tolist()\n    categorical_cols = df.select_dtypes(exclude=['number']).columns.tolist()\n    return numerical_cols, categorical_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:32.05713Z","iopub.execute_input":"2024-12-16T16:41:32.057761Z","iopub.status.idle":"2024-12-16T16:41:32.065025Z","shell.execute_reply.started":"2024-12-16T16:41:32.057709Z","shell.execute_reply":"2024-12-16T16:41:32.063582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def nums_combinations(df_, numerical_cols):\n    for col1 in numerical_cols:\n        for col2 in numerical_cols:\n            if col1 != col2:\n                df_[f'{col1}__{col2}__dzielenie'] = df_[col1]/df_[col2]\n                df_[f'{col1}__{col2}__mnozenie'] = df_[col1]*df_[col2]\n                df_[f'{col1}__{col2}__dodawanie'] = df_[col1]+df_[col2]\n                df_[f'{col1}__{col2}__odejmowanie'] = df_[col1]-df_[col2]\n    return df_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:32.066359Z","iopub.execute_input":"2024-12-16T16:41:32.066737Z","iopub.status.idle":"2024-12-16T16:41:32.078831Z","shell.execute_reply.started":"2024-12-16T16:41:32.066695Z","shell.execute_reply":"2024-12-16T16:41:32.077703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ndf_test = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:32.080899Z","iopub.execute_input":"2024-12-16T16:41:32.081231Z","iopub.status.idle":"2024-12-16T16:41:42.981909Z","shell.execute_reply.started":"2024-12-16T16:41:32.081199Z","shell.execute_reply":"2024-12-16T16:41:42.980735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:42.983225Z","iopub.execute_input":"2024-12-16T16:41:42.983528Z","iopub.status.idle":"2024-12-16T16:41:43.024484Z","shell.execute_reply.started":"2024-12-16T16:41:42.983499Z","shell.execute_reply":"2024-12-16T16:41:43.023354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.drop('id', axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:43.026809Z","iopub.execute_input":"2024-12-16T16:41:43.027132Z","iopub.status.idle":"2024-12-16T16:41:43.192101Z","shell.execute_reply.started":"2024-12-16T16:41:43.027101Z","shell.execute_reply":"2024-12-16T16:41:43.190758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols, categorical_cols = split_numerical_categorical(df_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:43.193847Z","iopub.execute_input":"2024-12-16T16:41:43.194249Z","iopub.status.idle":"2024-12-16T16:41:43.456297Z","shell.execute_reply.started":"2024-12-16T16:41:43.19421Z","shell.execute_reply":"2024-12-16T16:41:43.454034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:43.459626Z","iopub.execute_input":"2024-12-16T16:41:43.460449Z","iopub.status.idle":"2024-12-16T16:41:43.472311Z","shell.execute_reply.started":"2024-12-16T16:41:43.460376Z","shell.execute_reply":"2024-12-16T16:41:43.470238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:43.474412Z","iopub.execute_input":"2024-12-16T16:41:43.475042Z","iopub.status.idle":"2024-12-16T16:41:43.487537Z","shell.execute_reply.started":"2024-12-16T16:41:43.474906Z","shell.execute_reply":"2024-12-16T16:41:43.485053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_column = 'Premium Amount'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:41:43.489577Z","iopub.execute_input":"2024-12-16T16:41:43.490177Z","iopub.status.idle":"2024-12-16T16:41:43.503256Z","shell.execute_reply.started":"2024-12-16T16:41:43.490117Z","shell.execute_reply":"2024-12-16T16:41:43.500044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols.remove(target_column)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:43:29.049598Z","iopub.execute_input":"2024-12-16T16:43:29.050042Z","iopub.status.idle":"2024-12-16T16:43:29.072927Z","shell.execute_reply.started":"2024-12-16T16:43:29.050004Z","shell.execute_reply":"2024-12-16T16:43:29.071491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def transform_categoricals(df_, categorical_cols):\n    return pd.get_dummies(df_, columns=categorical_cols) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:43:29.424611Z","iopub.execute_input":"2024-12-16T16:43:29.425018Z","iopub.status.idle":"2024-12-16T16:43:29.430763Z","shell.execute_reply.started":"2024-12-16T16:43:29.424987Z","shell.execute_reply":"2024-12-16T16:43:29.429637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = df_train[target_column]\ndf_train = df_train.drop([target_column], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:43:31.244966Z","iopub.execute_input":"2024-12-16T16:43:31.245376Z","iopub.status.idle":"2024-12-16T16:43:31.512928Z","shell.execute_reply.started":"2024-12-16T16:43:31.245336Z","shell.execute_reply":"2024-12-16T16:43:31.510707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all = pd.concat([df_train, df_test], axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:43:32.450422Z","iopub.execute_input":"2024-12-16T16:43:32.450881Z","iopub.status.idle":"2024-12-16T16:43:32.862758Z","shell.execute_reply.started":"2024-12-16T16:43:32.450836Z","shell.execute_reply":"2024-12-16T16:43:32.861334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for c in categorical_cols:\n    print(c, \"n unique:\",df_all[c].nunique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:43:34.507202Z","iopub.execute_input":"2024-12-16T16:43:34.507685Z","iopub.status.idle":"2024-12-16T16:43:36.491951Z","shell.execute_reply.started":"2024-12-16T16:43:34.507615Z","shell.execute_reply":"2024-12-16T16:43:36.49069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all['Policy Start Date']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:44:39.771296Z","iopub.execute_input":"2024-12-16T16:44:39.771863Z","iopub.status.idle":"2024-12-16T16:44:39.782422Z","shell.execute_reply.started":"2024-12-16T16:44:39.771821Z","shell.execute_reply":"2024-12-16T16:44:39.78121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datetime import datetime","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:50:24.389251Z","iopub.execute_input":"2024-12-16T16:50:24.390052Z","iopub.status.idle":"2024-12-16T16:50:24.394644Z","shell.execute_reply.started":"2024-12-16T16:50:24.390012Z","shell.execute_reply":"2024-12-16T16:50:24.393475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_date_features(df_all, column_name):\n    df_all[column_name] = pd.to_datetime(df_all[column_name], errors='coerce')\n    df_all['Year'] = df_all[column_name].dt.year\n    df_all['Month'] = df_all[column_name].dt.month\n    df_all['Day'] = df_all[column_name].dt.day\n    df_all['Weekday'] = df_all[column_name].dt.weekday\n    df_all['Week'] = df_all[column_name].dt.isocalendar().week\n    df_all['Quarter'] = df_all[column_name].dt.quarter\n    df_all['Day of Year'] = df_all[column_name].dt.dayofyear\n    df_all['Is Month Start'] = df_all[column_name].dt.is_month_start\n    df_all['Is Month End'] = df_all[column_name].dt.is_month_end\n    df_all['Is Leap Year'] = df_all[column_name].dt.is_leap_year\n    df_all['Days Since Start'] = (datetime.now() - df_all[column_name]).dt.days\n    df_all.drop(column_name, inplace=True, axis = 1)\n    return df_all","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:26.323865Z","iopub.execute_input":"2024-12-16T16:54:26.324552Z","iopub.status.idle":"2024-12-16T16:54:26.337895Z","shell.execute_reply.started":"2024-12-16T16:54:26.324479Z","shell.execute_reply":"2024-12-16T16:54:26.336183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all = extract_date_features(df_all, 'Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:50:26.499798Z","iopub.execute_input":"2024-12-16T16:50:26.500482Z","iopub.status.idle":"2024-12-16T16:50:27.755237Z","shell.execute_reply.started":"2024-12-16T16:50:26.500446Z","shell.execute_reply":"2024-12-16T16:50:27.753213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all.drop('Policy Start Date', inplace=True, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:40.446229Z","iopub.execute_input":"2024-12-16T16:54:40.446956Z","iopub.status.idle":"2024-12-16T16:54:40.606592Z","shell.execute_reply.started":"2024-12-16T16:54:40.446915Z","shell.execute_reply":"2024-12-16T16:54:40.605721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all.drop('id', inplace=True, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:40.446229Z","iopub.execute_input":"2024-12-16T16:54:40.446956Z","iopub.status.idle":"2024-12-16T16:54:40.606592Z","shell.execute_reply.started":"2024-12-16T16:54:40.446915Z","shell.execute_reply":"2024-12-16T16:54:40.605721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols.remove('Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:51:34.52247Z","iopub.execute_input":"2024-12-16T16:51:34.524428Z","iopub.status.idle":"2024-12-16T16:51:34.533161Z","shell.execute_reply.started":"2024-12-16T16:51:34.524342Z","shell.execute_reply":"2024-12-16T16:51:34.531653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all = transform_categoricals(df_all, categorical_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:51:46.480581Z","iopub.execute_input":"2024-12-16T16:51:46.481197Z","iopub.status.idle":"2024-12-16T16:51:49.677121Z","shell.execute_reply.started":"2024-12-16T16:51:46.481147Z","shell.execute_reply":"2024-12-16T16:51:49.675913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_all.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:43.876502Z","iopub.execute_input":"2024-12-16T16:54:43.877167Z","iopub.status.idle":"2024-12-16T16:54:43.893059Z","shell.execute_reply.started":"2024-12-16T16:54:43.877103Z","shell.execute_reply":"2024-12-16T16:54:43.891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nimport numpy as np\nfrom lightgbm import LGBMClassifier\n\n# Assuming df_all, y_train, and df_train are already defined\nX_train = df_all[:df_train.shape[0]]\nX_test = df_all[df_train.shape[0]:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:53.720211Z","iopub.execute_input":"2024-12-16T16:54:53.7207Z","iopub.status.idle":"2024-12-16T16:54:53.728997Z","shell.execute_reply.started":"2024-12-16T16:54:53.720646Z","shell.execute_reply":"2024-12-16T16:54:53.727619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare arrays to store out-of-fold predictions and test set predictions\noof_preds = np.zeros(X_train.shape[0])\ntest_preds = np.zeros(X_test.shape[0])\n\n# Initialize 5-fold cross-validation\nkf = KFold(n_splits=5, shuffle=True, random_state=42)\n\n# Loop over each fold\nfor fold, (train_idx, val_idx) in enumerate(kf.split(X_train)):\n    print(f\"Fold {fold + 1}\")\n    \n    # Split data into train and validation sets\n    X_tr, X_val = X_train.iloc[train_idx], X_train.iloc[val_idx]\n    y_tr, y_val = y_train.iloc[train_idx], y_train.iloc[val_idx]\n    \n    # Initialize and train the model\n    model = LGBMClassifier()\n    model.fit(X_tr, y_tr)\n    \n    # Predict on validation set and test set\n    oof_preds[val_idx] = model.predict_proba(X_val)[:, 1]\n    test_preds += model.predict_proba(X_test)[:, 1] / kf.n_splits\n\n# Final averaged predictions for the test set\ny_pred = test_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T16:54:54.080067Z","iopub.execute_input":"2024-12-16T16:54:54.080499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}