R中C5.0传入代价矩阵报错c50 code called exit with value 1如何解决
问题描述
- 运行环境:macOS平台 RStudio 2021.09.0 "Ghost Orchid"版本
- 学习内容:R语言C5.0算法,参考资料为Brett Lantz所著*《Machine Learning in R》*,使用修改版的德国信贷公开贷款数据集
- 数据集情况:无缺失值、无空因子水平,通过rsample包
initial_split()函数拆分为训练集、测试集tibble对象,训练集credit_train结构如下:
str(credit_train) tibble [900 × 21] (S3: tbl_df/tbl/data.frame) $ checking_balance : Factor w/ 4 levels "< 0 DM","> 200 DM",..: 4 1 4 3 3 4 3 4 1 1 ... $ months_loan_duration: Factor w/ 33 levels "4","5","6","7",..: 18 22 18 16 30 18 9 9 14 9 ... $ credit_history : Factor w/ 5 levels "critical","delayed",..: 1 1 1 5 4 2 5 5 5 5 ... $ purpose : Factor w/ 10 levels "business","car (new)",..: 8 3 3 1 1 1 8 1 2 2 ... $ amount : num [1:900] 2611 6187 2197 2767 6416 ... $ savings_balance : Factor w/ 5 levels "< 100 DM","> 1000 DM",..: 1 3 5 3 1 1 1 1 3 1 ... $ employment_length : Factor w/ 5 levels "> 7 yrs","0 - 1 yrs",..: 1 4 4 1 1 3 1 4 4 3 ... $ installment_rate : Factor w/ 4 levels "1","2","3","4": 4 1 4 4 4 1 3 2 4 4 ... $ personal_status : Factor w/ 4 levels "divorced male",..: 3 3 4 1 2 4 3 4 4 2 ... $ other_debtors : Factor w/ 3 levels "co-applicant",..: 1 3 3 3 3 3 2 3 3 2 ... $ residence_history : Factor w/ 4 levels "1","2","3","4": 3 4 4 2 3 2 3 4 3 4 ... $ property : Factor w/ 4 levels "building society savings",..: 3 2 2 2 4 4 3 2 2 1 ... $ age : num [1:900] 46 24 43 61 59 32 40 36 30 29 ... $ installment_plan : Factor w/ 3 levels "bank","none",..: 2 2 2 1 2 2 1 2 2 2 ... $ housing : Factor w/ 3 levels "for free","own",..: 2 3 2 3 3 1 2 2 2 2 ... $ existing_credits : Factor w/ 4 levels "1","2","3","4": 2 2 2 2 1 1 2 1 1 1 ... $ default : Factor w/ 2 levels "paid","default": 1 1 1 2 2 1 1 1 1 1 ... $ dependents : Factor w/ 2 levels "1","2": 1 1 2 1 1 1 1 1 2 1 ... $ telephone : Factor w/ 2 levels "none","yes": 1 1 2 1 1 1 1 2 2 2 ... $ foreign_worker : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ... $ job : Factor w/ 4 levels "management self-employed",..: 2 2 2 4 2 2 4 2 1 2 ...
- 报错触发条件:仅传入代价矩阵拟合模型时出现,不传入代价矩阵模型可正常运行。原代价矩阵构建代码:
error_cost <- matrix(nrow = 2, ncol = 2, dimnames = list(c('predict_paid','predict_default'), #rows c('actual_paid','actual_default')), #columns data = c(0, 1, 4, 0))
尝试过多种代价矩阵写法(包括照搬参考书籍示例)、更换非公式接口传参,均触发相同错误,报错信息:
c50 code called exit with value 1
- 附训练集前6行dput结果:
structure(list(checking_balance = structure(c(4L, 1L, 4L, 3L, 3L, 4L), .Label = c("< 0 DM", "> 200 DM", "1 - 200 DM", "unknown" ), class = "factor"), months_loan_duration = structure(c(18L, 22L, 18L, 16L, 30L, 18L), .Label = c("4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "18", "20", "21", "22", "24", "26", "27", "28", "30", "33", "36", "39", "40", "42", "45", "47", "48", "54", "60", "72"), class = "factor"), credit_history = structure(c(1L, 1L, 1L, 5L, 4L, 2L), .Label = c("critical", "delayed", "fully repaid", "fully repaid this bank", "repaid"), class = "factor"), purpose = structure(c(8L, 3L, 3L, 1L, 1L, 1L), .Label = c("business", "car (new)", "car (used)", "domestic appliances", "education", "furniture", "others", "radio/tv", "repairs", "retraining"), class = "factor"), amount = c(2611, 6187, 2197, 2767, 6416, 3863), savings_balance = structure(c(1L, 3L, 5L, 3L, 1L, 1L), .Label = c("< 100 DM", "> 1000 DM", "101 - 500 DM", "501 - 1000 DM", "unknown"), class = "factor"), employment_length = structure(c(1L, 4L, 4L, 1L, 1L, 3L), .Label = c("> 7 yrs", "0 - 1 yrs", "1 - 4 yrs", "4 - 7 yrs", "unemployed"), class = "factor"), installment_rate = structure(c(4L, 1L, 4L, 4L, 4L, 1L), .Label = c("1", "2", "3", "4"), class = "factor"), personal_status = structure(c(3L, 3L, 4L, 1L, 2L, 4L), .Label = c("divorced male", "female", "married male", "single male"), class = "factor"), other_debtors = structure(c(1L, 3L, 3L, 3L, 3L, 3L), .Label = c("co-applicant", "guarantor", "none"), class = "factor"), residence_history = structure(c(3L, 4L, 4L, 2L, 3L, 2L), .Label = c("1", "2", "3", "4"), class = "factor"), property = structure(c(3L, 2L, 2L, 2L, 4L, 4L), .Label = c("building society savings", "other", "real estate", "unknown/none"), class = "factor"), age = c(46, 24, 43, 61, 59, 32), installment_plan = structure(c(2L, 2L, 2L, 1L, 2L, 2L), .Label = c("bank", "none", "stores"), class = "factor"), housing = structure(c(2L, 3L, 2L, 3L, 3L, 1L), .Label = c("for free", "own", "rent"), class = "factor"), existing_credits = structure(c(2L, 2L, 2L, 2L, 1L, 1L), .Label = c("1", "2", "3", "4"), class = "factor"), default = structure(c(1L, 1L, 1L, 2L, 2L, 1L), .Label = c("paid", "default"), class = "factor"), dependents = structure(c(1L, 1L, 2L, 1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), telephone = structure(c(1L, 1L, 2L, 1L, 1L, 1L), .Label = c("none", "yes"), class = "factor"), foreign_worker = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no", "yes"), class = "factor"), job = structure(c(2L, 2L, 2L, 4L, 2L, 2L), .Label = c("management self-employed", "skilled employee", "unemployed non-resident", "unskilled resident" ), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
报错原因
报错由两个问题共同导致:
- 代价矩阵的行列维度名不符合C5.0包的要求。C5.0的
costs参数要求矩阵的行名、列名必须和响应变量(此处为default)的因子水平完全一致,不能自定义predict_paid、actual_paid这类名称,且行列顺序要对应预测值、真实值的因子水平顺序。 - C5.0包对tibble类型的兼容存在已知问题,传入tbl_df格式的训练集时,带代价矩阵的拟合流程会触发底层C代码退出。
解决方案
按以下步骤修改代码即可正常运行:
- 将训练集转为普通data.frame格式,不要使用tibble传入
- 重构代价矩阵,行列名直接使用
default变量的因子水平,对角线(预测正确)的代价设为0
修正后的代码如下:
# 1. 转换训练集为普通data.frame credit_train_df <- as.data.frame(credit_train) # 2. 构建符合要求的代价矩阵,行列名与default因子水平完全一致 error_cost <- matrix( data = c( 0, 1, # 真实值为paid时,预测为paid代价0,预测为default代价1 4, 0 # 真实值为default时,预测为paid代价4,预测为default代价0 ), nrow = 2, ncol = 2, dimnames = list( c("paid", "default"), # 行对应预测结果的因子水平 c("paid", "default") # 列对应真实结果的因子水平 ) ) # 3. 拟合模型 c5_boostTree <- C5.0( default ~ ., data = credit_train_df, trials = 3, costs = error_cost )
注意:代价矩阵的误分类成本数值可以根据实际业务需求调整,只要维度名和因子水平完全匹配即可。
内容的提问来源于stack exchange,提问作者for_the_love_of_cod
相关产品推荐
相关产品推荐

