{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"}],"dockerImageVersionId":30618,"isInternetEnabled":false,"language":"r","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"I only share my code here. I can't find out what casued my submit errors.\n\nThrough this competition, I learnt how to use tidymodels to build lightgbm model.\n\nI investegated the importance variables that are really meanful.\n\nThese important variables are\n   1. avgdpdtolclosure24_3658938P: Average DPD (days past due) with tolerance within the past 24 months from the maximum closure date, assuming that the contract is finished. If the contract is ongoing, the calculation is based on the current date.\n   2. numrejects9m_859L: Number of credit applications that were rejected in the last 9 months.\n   3. price_1097A: Credit price.\n   4. incometype_1044T: Type of income of the person.\n   5. pmtnum_254L: Total number of loan payments made by the client.\n   6. mobilephncnt_593L: Number of persons with the same mobile phone number.\n   7. pctinstlsallpaidlate1d_3546856L: Percentage of installments that are paid 1 or more days after the due date.\n   8. numinstpaidearly3d_3546850L: Number of instalments paid more than three days before the due date.\n   9. maxdpdlast24m_143P: Maximal days past due in the last 24 months.\n   10. maxdpdlast12m_727P: Maximum days past due in the past 12 months.\n\nWhat do you think? I feel these variables can explain a default probability.","metadata":{}},{"cell_type":"code","source":"library(tidyverse) # metapackage of all tidyverse packages\nlibrary(tidymodels)\nlibrary(bonsai)\nlibrary(recipes)\nlibrary(rsample)\n#Training data\ntrain_path<-\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train\"\n\ntrain_base<- read_csv(file.path(train_path,\"/train_base.csv\",fsep=\"\"),col_select=c(\"target\",\"case_id\"))\n\ntrain_static<- bind_rows(read_csv(file.path(train_path,\"/train_static_0_0.csv\",fsep=\"\")),\n                         read_csv(file.path(train_path,\"/train_static_0_1.csv\",fsep=\"\"))\n                        )\ntrain_static<-train_static%>%select(where(is.numeric) | where(is.logical))\n\nselect_static <- names(train_static)\n\ntrain_person_1<- read_csv(file.path(train_path,\"/train_person_1.csv\",fsep=\"\"),\n                          col_select=c(\"case_id\",\"mainoccupationinc_384A\",\"incometype_1044T\",\"housetype_905L\",starts_with(\"num_group\")))\ntrain_person_1<- train_person_1%>%mutate(incometype_1044T=as.factor(incometype_1044T),housetype_905L=as.factor(housetype_905L))%>%group_by(case_id)%>%mutate(mainoccupationinc_384A=max(mainoccupationinc_384A,na.rm=TRUE))%>%filter(num_group1==0)%>%select(-num_group1)\n\nlevel1<-train_person_1%>%pull(incometype_1044T)%>%levels()\nlevel2<-train_person_1%>%pull(housetype_905L)%>%levels()\n\ntrain_other_1<- read_csv(file.path(train_path,\"/train_other_1.csv\",fsep=\"\"),\n                        col_select=c(-num_group1))\n\ntrain_deposit_1<- read_csv(file.path(train_path,\"/train_deposit_1.csv\",fsep=\"\"),\n                          col_select=c(\"case_id\",\"amount_416A\"))\ntrain_deposit_1<-train_deposit_1%>%group_by(case_id)%>%mutate(min_deposit=min(amount_416A,na.rm=TRUE),max_deposit=max(amount_416A,na.rm=TRUE),deposit_num=n(),sd_deposit=sd(amount_416A,na.rm=TRUE))%>%\n                                   slice(n())%>%select(-c(amount_416A))\n\ntrain_debitcard_1<- read_csv(file.path(train_path,\"/train_debitcard_1.csv\",fsep=\"\"))\n\n\ntrain_debitcard_1<- train_debitcard_1%>%group_by(case_id)%>%mutate(max_last180dayaveragebalance_704A=ifelse(is.na(last180dayaveragebalance_704A),NA,max(last180dayaveragebalance_704A,na.rm=TRUE)),\n                                               max_last180dayturnover_1134A=ifelse(is.na(last180dayturnover_1134A),NA,max(last180dayturnover_1134A,na.rm=TRUE)),\n                                               max_last30dayturnover_651A=ifelse(is.na(last30dayturnover_651A),NA,max(last30dayturnover_651A,na.rm=TRUE)),\n                                               num_opening=n())%>%dplyr::slice(n())%>%\n                                               select(case_id,starts_with(\"max_\"),num_opening)","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all<- left_join(train_base,train_static,by=\"case_id\")%>%#left_join(train_person_1,by=\"case_id\")%>%\n            left_join(train_other_1,by=\"case_id\")%>%left_join(train_deposit_1,by=\"case_id\")%>%left_join(train_debitcard_1,by=\"case_id\")\ntrain_all<- train_all%>%select(-case_id)%>%mutate(target=as.factor(target))\n\n#remove unused data\nrm(train_base,train_static,train_person_1,train_other_1,train_deposit_1,train_debitcard_1)\ngc()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:35:42.677952Z","iopub.execute_input":"2024-05-13T11:35:42.679351Z","iopub.status.idle":"2024-05-13T11:35:49.568655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model building\nI chose gradient boost tree, because the data have too many missing. I need the tree model to help me handle that. ","metadata":{}},{"cell_type":"markdown","source":"# Train/validation/test\nBefore this parameter setting, I use grid search to help me narrow down the good parameter values. I want to use manual tuning by myself to save my time. ","metadata":{}},{"cell_type":"code","source":"#data split\ntrain_split<-initial_validation_split(train_all,strata=target)\ntrain_trn<-training(train_split)\ntrain_test<-testing(train_split)\ntrain_val <- validation(train_split)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:35:49.571063Z","iopub.execute_input":"2024-05-13T11:35:49.572334Z","iopub.status.idle":"2024-05-13T11:35:51.585009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm <-\n  boost_tree(\n    mode = \"classification\",\n    mtry = as.integer(round(ncol(train_trn)*0.8,0)),\n    trees = 500,\n    tree_depth = 10,\n    min_n = 30,\n    learn_rate = 0.001,\n    loss_reduction= 0.0001,\n    sample_size=0.75,\n    stop_iter=100\n  ) %>%\n  set_engine(\"lightgbm\", lambda_l1 = 0.1 ,num_leaves = 100)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:35:51.588950Z","iopub.execute_input":"2024-05-13T11:35:51.590406Z","iopub.status.idle":"2024-05-13T11:35:51.604901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_wf <-  workflow()%>%add_model(lgbm)%>%\n       add_formula(target~.)\n\nfit <- parsnip::fit(lgbm_wf,train_trn)\n\nlgbm_imp <- extract_fit_engine(fit) %>% \n  lightgbm::lgb.importance() %>% \n  lightgbm::lgb.plot.importance(top_n = 10)\n\ncat(\"Validation\\n\")\ncheck_perf_val<-fit%>%predict(train_val,type=\"prob\")%>%bind_cols(train_val%>%select(target))\nroc_auc(check_perf_val,target,.pred_0)\n\ncat(\"Testing\\n\")\ncheck_perf_test<-fit%>%predict(train_test,type=\"prob\")%>%bind_cols(train_test%>%select(target))\nroc_auc(check_perf_test,target,.pred_0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm(train_all,train_split,train_trn,train_test,train_val,check_perf_val,check_perf_test,lgbm)\ngc()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:36:10.423298Z","iopub.execute_input":"2024-05-13T11:36:10.424647Z","iopub.status.idle":"2024-05-13T11:36:10.722759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"test_path<-\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/test\"\n\ntest_base<- read_csv(file.path(test_path,\"/test_base.csv\",fsep=\"\"),col_select=c(\"case_id\"))\n\ntest_static<- bind_rows(read_csv(file.path(test_path,\"/test_static_0_0.csv\",fsep=\"\"),col_select=all_of(select_static)),\n                        read_csv(file.path(test_path,\"/test_static_0_1.csv\",fsep=\"\"),col_select=all_of(select_static)),\n                        read_csv(file.path(test_path,\"/test_static_0_2.csv\",fsep=\"\"),col_select=all_of(select_static))\n                        )\n\ntest_person_1<- read_csv(file.path(test_path,\"/test_person_1.csv\",fsep=\"\"),\n                          col_select=c(\"case_id\",\"mainoccupationinc_384A\",\"incometype_1044T\",\"housetype_905L\",starts_with(\"num_group\")))\n\ntest_person_1<- test_person_1%>%mutate(incometype_1044T=ifelse((incometype_1044T %in% c(level1,NA)),incometype_1044T,\"OTHER\"),\n                                       housetype_905L=ifelse(housetype_905L %in% c(level2,NA),housetype_905L,\"OTHER\"))\ntest_person_1<- test_person_1%>%mutate(incometype_1044T=as.factor(incometype_1044T),housetype_905L=as.factor(housetype_905L))%>%group_by(case_id)%>%mutate(mainoccupationinc_384A=max(mainoccupationinc_384A,na.rm=TRUE))%>%filter(num_group1==0)%>%select(-num_group1)\n\ntest_other_1<- read_csv(file.path(test_path,\"/test_other_1.csv\",fsep=\"\"),\n                        col_select=c(-num_group1))\n\ntest_deposit_1<- read_csv(file.path(test_path,\"/test_deposit_1.csv\",fsep=\"\"),\n                          col_select=c(\"case_id\",\"amount_416A\"))\ntest_deposit_1<-test_deposit_1%>%group_by(case_id)%>%mutate(min_deposit=min(amount_416A,na.rm=TRUE),max_deposit=max(amount_416A,na.rm=TRUE),deposit_num=n(),sd_deposit=sd(amount_416A,na.rm=TRUE))%>%\n                                   dplyr::slice(n())%>%select(-c(amount_416A))\n\ntest_debitcard_1<- read_csv(file.path(test_path,\"/test_debitcard_1.csv\",fsep=\"\"))\n\n\ntest_debitcard_1<- test_debitcard_1%>%group_by(case_id)%>%mutate(max_last180dayaveragebalance_704A=ifelse(is.na(last180dayaveragebalance_704A),NA,max(last180dayaveragebalance_704A,na.rm=TRUE)),\n                                               max_last180dayturnover_1134A=ifelse(is.na(last180dayturnover_1134A),NA,max(last180dayturnover_1134A,na.rm=TRUE)),\n                                               max_last30dayturnover_651A=ifelse(is.na(last30dayturnover_651A),NA,max(last30dayturnover_651A,na.rm=TRUE)),\n                                               num_opening=n())%>%dplyr::slice(n())%>%\n                                               select(case_id,starts_with(\"max_\"),num_opening)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:36:10.725241Z","iopub.execute_input":"2024-05-13T11:36:10.726531Z","iopub.status.idle":"2024-05-13T11:36:11.327409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_all<- left_join(test_base,test_static,by=\"case_id\")%>%#left_join(test_person_1,by=\"case_id\")%>%\n            left_join(test_other_1,by=\"case_id\")%>%left_join(test_deposit_1,by=\"case_id\")%>%left_join(test_debitcard_1,by=\"case_id\")\n\n#remove unused data\nrm(test_base,test_static,test_person_1,test_other_1,test_deposit_1,test_debitcard_1)\ngc()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:39:45.624005Z","iopub.execute_input":"2024-05-13T11:39:45.625748Z","iopub.status.idle":"2024-05-13T11:39:45.645424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit%>%predict(test_all%>%select(-case_id),type = \"prob\")%>%select(.pred_1)%>%\n     bind_cols(test_all%>%select(case_id))%>%rename(score=.pred_1)%>%\n      select(case_id,score)%>%write_csv(\"./submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-13T11:39:38.578355Z","iopub.execute_input":"2024-05-13T11:39:38.580044Z","iopub.status.idle":"2024-05-13T11:39:38.640367Z"},"trusted":true},"execution_count":null,"outputs":[]}]}