{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"},{"sourceId":34041,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":28496}],"dockerImageVersionId":30635,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ROUS Submission\n\nThe model in this notebook corresponds to this run id in our MLflow experiment: `2a59e4770de244caa34b815a22a027d3`","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport lightgbm as lgb\nimport xgboost as xgb\n\ndataPath = \"/kaggle/input/home-credit-credit-risk-model-stability/\"","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:04.780935Z","iopub.execute_input":"2024-04-19T21:56:04.781445Z","iopub.status.idle":"2024-04-19T21:56:06.899667Z","shell.execute_reply.started":"2024-04-19T21:56:04.781404Z","shell.execute_reply":"2024-04-19T21:56:06.898386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_table_dtypes_and_convert_strings(df: pd.DataFrame) -> pd.DataFrame:\n    # Set the data types for numeric columns based on their suffix\n    for col in df.columns:\n        if col[-1] in (\"P\", \"A\"):\n            df[col] = pd.to_numeric(df[col], errors='coerce')  # Coerce errors to NaNs\n\n    # Convert string/object columns to categorical, add \"Unknown\" category, and handle NaNs\n    for col in df.columns:  \n        if df[col].dtype.name in ['object', 'string']:\n            try:\n                df[col] = pd.to_datetime(df[col], errors='raise')  # Attempt to convert to datetime\n            except (ValueError, TypeError):\n                # If conversion fails, handle as a string category\n                df[col] = df[col].fillna(\"Unknown\")\n                current_categories = df[col].unique().tolist()\n                if \"Unknown\" not in current_categories:\n                    current_categories.append(\"Unknown\")\n                new_dtype = pd.CategoricalDtype(categories=current_categories, ordered=True)\n                df[col] = df[col].astype(new_dtype)\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:06.902821Z","iopub.execute_input":"2024-04-19T21:56:06.903370Z","iopub.status.idle":"2024-04-19T21:56:06.917035Z","shell.execute_reply.started":"2024-04-19T21:56:06.903319Z","shell.execute_reply":"2024-04-19T21:56:06.915351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming 'dataPath' is a string with the path to your data, including \"parquet_files/test/\"\n\ntest_path = \"parquet_files/test/\"\n\n# Read the parquet files using pandas and apply the set_table_dtypes function\ntest_basetable = pd.read_parquet(dataPath + test_path + \"test_base.parquet\")\n\ntest_static_0_0 = pd.read_parquet(dataPath + test_path + \"test_static_0_0.parquet\")\ntest_static_0_0 = set_table_dtypes_and_convert_strings(test_static_0_0)\n\ntest_static_0_1 = pd.read_parquet(dataPath + test_path + \"test_static_0_1.parquet\")\ntest_static_0_1 = set_table_dtypes_and_convert_strings(test_static_0_1)\n\n# Concatenate using pandas (note that 'ignore_index=True' is equivalent to 'vertical_relaxed' in Polars)\ntest_static = pd.concat([test_static_0_0, test_static_0_1], ignore_index=True)\n\ntest_static_cb = pd.read_parquet(dataPath + test_path + \"test_static_cb_0.parquet\")\ntest_static_cb = set_table_dtypes_and_convert_strings(test_static_cb)\n\ntest_person_1 = pd.read_parquet(dataPath + test_path + \"test_person_1.parquet\")\ntest_person_1 = set_table_dtypes_and_convert_strings(test_person_1)\n\ntest_credit_bureau_b_2 = pd.read_parquet(dataPath + test_path + \"test_credit_bureau_b_2.parquet\")\ntest_credit_bureau_b_2 = set_table_dtypes_and_convert_strings(test_credit_bureau_b_2)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:06.918903Z","iopub.execute_input":"2024-04-19T21:56:06.919781Z","iopub.status.idle":"2024-04-19T21:56:07.489801Z","shell.execute_reply.started":"2024-04-19T21:56:06.919723Z","shell.execute_reply":"2024-04-19T21:56:07.488105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Aggregations for test_person_1 with conditions\ntest_person_1_feats_1 = test_person_1.groupby(\"case_id\").agg(\n    mainoccupationinc_384A_max=pd.NamedAgg(column=\"mainoccupationinc_384A\", aggfunc=\"max\"),\n    mainoccupationinc_384A_any_selfemployed=pd.NamedAgg(column=\"incometype_1044T\", aggfunc=lambda x: (x == \"SELFEMPLOYED\").max())\n)\n\n# Filter rows where num_group1=0, then drop the num_group1 column, and rename housetype_905L\ntest_person_1_feats_2 = test_person_1.loc[test_person_1[\"num_group1\"] == 0, [\"case_id\", \"housetype_905L\"]].rename(\n    columns={\"housetype_905L\": \"person_housetype\"}\n)\n\n# Aggregations for test_credit_bureau_b_2 with conditional checks\ntest_credit_bureau_b_2_feats = test_credit_bureau_b_2.groupby(\"case_id\").agg(\n    pmts_pmtsoverdue_635A_max=pd.NamedAgg(column=\"pmts_pmtsoverdue_635A\", aggfunc=\"max\"),\n    pmts_dpdvalue_108P_over31=pd.NamedAgg(column=\"pmts_dpdvalue_108P\", aggfunc=lambda x: (x > 31).max())\n)\n\n# Selecting specific columns based on their ending character in test_static and test_static_cb\nselected_static_cols = [col for col in test_static.columns if col[-1] in (\"A\", \"M\")]\nprint(selected_static_cols)\n\nselected_static_cb_cols = [col for col in test_static_cb.columns if col[-1] in (\"A\", \"M\")]\nprint(selected_static_cb_cols)\n\n# Join all tables together on 'case_id' with left joins\ndata = test_basetable.merge(\n    test_static[[\"case_id\"] + selected_static_cols], on=\"case_id\", how=\"left\"\n).merge(\n    test_static_cb[[\"case_id\"] + selected_static_cb_cols], on=\"case_id\", how=\"left\"\n).merge(\n    test_person_1_feats_1, on=\"case_id\", how=\"left\"\n).merge(\n    test_person_1_feats_2, on=\"case_id\", how=\"left\"\n).merge(\n    test_credit_bureau_b_2_feats, on=\"case_id\", how=\"left\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:07.492061Z","iopub.execute_input":"2024-04-19T21:56:07.492581Z","iopub.status.idle":"2024-04-19T21:56:07.556116Z","shell.execute_reply.started":"2024-04-19T21:56:07.492532Z","shell.execute_reply":"2024-04-19T21:56:07.554617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:07.559506Z","iopub.execute_input":"2024-04-19T21:56:07.560042Z","iopub.status.idle":"2024-04-19T21:56:07.605232Z","shell.execute_reply.started":"2024-04-19T21:56:07.559995Z","shell.execute_reply":"2024-04-19T21:56:07.603706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:07.606899Z","iopub.execute_input":"2024-04-19T21:56:07.607506Z","iopub.status.idle":"2024-04-19T21:56:07.640123Z","shell.execute_reply.started":"2024-04-19T21:56:07.607445Z","shell.execute_reply":"2024-04-19T21:56:07.638176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data[(data['WEEK_NUM'] < 54) | (data['WEEK_NUM'] > 64)]\ndata = data[data['maininc_215A'] != 0]","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:07.885914Z","iopub.execute_input":"2024-04-19T21:56:07.887767Z","iopub.status.idle":"2024-04-19T21:56:07.898683Z","shell.execute_reply.started":"2024-04-19T21:56:07.887708Z","shell.execute_reply":"2024-04-19T21:56:07.897228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Encode column using dummy variables\ndummy_df = pd.get_dummies(data['lastrejectreason_759M'], prefix='lastrejectreason')\n\n# # Concatenate the original DataFrame with the dummy variables\ndata = pd.concat([data, dummy_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:08.818138Z","iopub.execute_input":"2024-04-19T21:56:08.819152Z","iopub.status.idle":"2024-04-19T21:56:08.831978Z","shell.execute_reply.started":"2024-04-19T21:56:08.819093Z","shell.execute_reply":"2024-04-19T21:56:08.830142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# instead of splitting the data, I just need to apply the feature engineering to the whole dataset","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"median_value_data = data['maininc_215A'].median()\ndata['maininc_215A'] = data['maininc_215A'].fillna(median_value_data)\n\ndata['loan_to_income_ratio'] = data['credamount_770A']/data['maininc_215A']\n\ndata['debt_to_income_ratio'] = data['totaldebt_9A']/data['maininc_215A']","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:12.332866Z","iopub.execute_input":"2024-04-19T21:56:12.333316Z","iopub.status.idle":"2024-04-19T21:56:12.343991Z","shell.execute_reply.started":"2024-04-19T21:56:12.333279Z","shell.execute_reply":"2024-04-19T21:56:12.342897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:56:14.730257Z","iopub.execute_input":"2024-04-19T21:56:14.731320Z","iopub.status.idle":"2024-04-19T21:56:14.765817Z","shell.execute_reply.started":"2024-04-19T21:56:14.731264Z","shell.execute_reply":"2024-04-19T21:56:14.764050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.select_dtypes(exclude=['object', 'category'])","metadata":{"execution":{"iopub.status.busy":"2024-04-19T22:01:13.206214Z","iopub.execute_input":"2024-04-19T22:01:13.206715Z","iopub.status.idle":"2024-04-19T22:01:13.220005Z","shell.execute_reply.started":"2024-04-19T22:01:13.206679Z","shell.execute_reply":"2024-04-19T22:01:13.218253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n#import seaborn as sns\n#import lightgbm as lgb\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import make_scorer\n#from sklearn.metrics import roc_auc_score\n\npath_data = \"/kaggle/input/home-credit-credit-risk-model-stability/\"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\n\nbst = xgb.Booster({'nthread': 4})  # init model\nbst.load_model('/kaggle/input/xgb_rous/other/model.xgb/1/model.xgb')  # load model data","metadata":{"execution":{"iopub.status.busy":"2024-04-19T21:59:32.271495Z","iopub.execute_input":"2024-04-19T21:59:32.272085Z","iopub.status.idle":"2024-04-19T21:59:32.318682Z","shell.execute_reply.started":"2024-04-19T21:59:32.272039Z","shell.execute_reply":"2024-04-19T21:59:32.317431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_dmatrix = xgb.DMatrix(data.values)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T22:01:21.619436Z","iopub.execute_input":"2024-04-19T22:01:21.620015Z","iopub.status.idle":"2024-04-19T22:01:21.678492Z","shell.execute_reply.started":"2024-04-19T22:01:21.619971Z","shell.execute_reply":"2024-04-19T22:01:21.677125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = bst.predict(test_data_dmatrix)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T22:02:54.494479Z","iopub.execute_input":"2024-04-19T22:02:54.495027Z","iopub.status.idle":"2024-04-19T22:02:54.511203Z","shell.execute_reply.started":"2024-04-19T22:02:54.494986Z","shell.execute_reply":"2024-04-19T22:02:54.509460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"case_id\": data[\"case_id\"].to_numpy(),\n    \"score\": predictions\n}) #.set_index('case_id')\nsubmission.to_csv(\"./submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-19T22:21:07.939264Z","iopub.execute_input":"2024-04-19T22:21:07.939772Z","iopub.status.idle":"2024-04-19T22:21:07.951218Z","shell.execute_reply.started":"2024-04-19T22:21:07.939733Z","shell.execute_reply":"2024-04-19T22:21:07.949446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-04-19T22:21:14.668225Z","iopub.execute_input":"2024-04-19T22:21:14.668760Z","iopub.status.idle":"2024-04-19T22:21:14.683898Z","shell.execute_reply.started":"2024-04-19T22:21:14.668720Z","shell.execute_reply":"2024-04-19T22:21:14.682338Z"},"trusted":true},"execution_count":null,"outputs":[]}]}