【机器学习与R语言】4-决策树

2020-09-02  本文已影响0人  生物信息与育种

1.决策树原理

curve(-x*log2(x)-(1-x)*log2(1-x),
      col='red',xlab = 'x',ylab = 'Entropy',lwd=4)
0.5时最大熵值

以上假定的是名义特征,对于数值特征同样可用信息增益。即通过设置阈值来划分不同组。

除了信息增益可用于特征分割标准,其他常用的标准还有:基尼系数,卡方统计量,增益比等。

②修剪决策树

2.决策树应用示例

采用C5.0决策树来识别高风险银行贷款

2.1)收集数据

信贷数据集:包含1000个贷款案例,贷款特征和贷款者特征的数值特征和名义特征的组合。其中一个类变量表示贷款是否陷入违约。
数据下载:

链接: https://pan.baidu.com/s/1p6eTQvUZEAHd9GaFQN0BKA 提取码: ph8u

2.2)探索和准备数据

## Example: Identifying Risky Bank Loans ----
## Step 2: Exploring and preparing the data ----
credit <- read.csv("credit.csv")
str(credit)

# look at two characteristics of the applicant
table(credit$checking_balance)
table(credit$savings_balance)

# look at two characteristics of the loan
summary(credit$months_loan_duration)
summary(credit$amount)

# look at the class variable
table(credit$default)

# create a random sample for training and test data
# use set.seed to use the same random number sequence as the tutorial
set.seed(123)
train_sample <- sample(1000, 900)

str(train_sample)

# split the data frames
credit_train <- credit[train_sample, ]
credit_test  <- credit[-train_sample, ]

# check the proportion of class variable
prop.table(table(credit_train$default))
prop.table(table(credit_test$default))

2.3)训练模型

## Step 3: Training a model on the data ----
# build the simplest decision tree
library(C50)
credit_model <- C5.0(credit_train[-17], credit_train$default)
# trial可选数值用于控制自助法循环次数(默认1)
# costs可选矩阵用于给出各类型错误对应的成本

# display simple facts about the tree
credit_model

# display detailed information about the tree
summary(credit_model)
决策树大小 决策树的结构
决策树生成的混淆矩阵

2.4)评估模型性能

依然使用混淆矩阵来评价模型。

## Step 4: Evaluating model performance ----
# create a factor vector of predictions on test data
credit_pred <- predict(credit_model, credit_test)

# cross tabulation of predicted versus actual classes
library(gmodels)
CrossTable(credit_test$default, credit_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))
image.png

错误率30%,且只正确预测了15个贷款违约者。需要提升性能。

2.5)提高模型性能

通过自适应增强算法(boosting)

## Step 5: Improving model performance ----

## Boosting the accuracy of decision trees
# boosted decision tree with 10 trials
credit_boost10 <- C5.0(credit_train[-17], credit_train$default,
                       trials = 10)
credit_boost10
summary(credit_boost10)

credit_boost_pred10 <- predict(credit_boost10, credit_test)
CrossTable(credit_test$default, credit_boost_pred10,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))
image.png

总的错误率仍为30%。缺乏更大的提高可能是本身使用了一个相对较小的训练集。

将惩罚因子分配到不同类型的错误上

假阴性付出的代价比较大(给有违约的申请者放贷),可以通过拒绝大量处于边界线的申请者来规避风险。

将惩罚因子设定在一个代价矩阵中,用来指定每种错误相对于其他任何错误有多少倍的严重性。如错放一个贷款违约者的损失是错失一次基于损失的4倍:

# create dimensions for a cost matrix
matrix_dimensions <- list(c("no", "yes"), c("no", "yes"))
names(matrix_dimensions) <- c("predicted", "actual")
matrix_dimensions

# build the matrix
error_cost <- matrix(c(0, 1, 4, 0), nrow = 2, dimnames = matrix_dimensions)
error_cost
image.png

可通过costs参数来指定代价矩阵:

# apply the cost matrix to the tree
credit_cost <- C5.0(credit_train[-17], credit_train$default,
                          costs = error_cost)
credit_cost_pred <- predict(credit_cost, credit_test)

CrossTable(credit_test$default, credit_cost_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))
image.png

虽然总的错误率增加到了33%,但假阴性率降低到了7%。以增加错误肯定为代价,减少错误否定的这种折中的方案是可以接受的。


机器学习与R语言系列推文汇总:
【机器学习与R语言】1-机器学习简介
【机器学习与R语言】2-K近邻(kNN)
【机器学习与R语言】3-朴素贝叶斯(NB)
【机器学习与R语言】4-决策树
【机器学习与R语言】5-规则学习
【机器学习与R语言】6-线性回归
【机器学习与R语言】7-回归树和模型树
【机器学习与R语言】8-神经网络
【机器学习与R语言】9-支持向量机
【机器学习与R语言】10-关联规则
【机器学习与R语言】11-Kmeans聚类
【机器学习与R语言】12-如何评估模型的性能?
【机器学习与R语言】13-如何提高模型的性能?

上一篇 下一篇

猜你喜欢

热点阅读