京公网安备 11010802034615号
经营许可证编号:京B2-20210330
决策树
经验熵是针对所有样本的分类结果而言
经验条件熵是针对每个特征里每个特征样本分类结果之特征样本比例和
基尼不纯度
简单地说就是从一个数据集中随机选取子项,度量其被错误分类到其他分组里的概率

决策树算法使用轴平行分割来表现具体一定的局限性
C5.0算法--可以处理数值型和缺失 只使用最重要的特征--使用的熵度量-可以自动修剪枝
划分数据集
set.seed(123) #设置随机种子
train_sample <- sample(1000, 900)#从1000里随机900个数值
credit_train <- credit[train_sample, ]
credit_test <- credit[-train_sample, ]
library(C50)
credit_model <- C5.0(credit_train[-17], credit_train$default) #特征数据框-标签
C5.0(train,labers,trials = 1,costs = NULL)
trials控制自动法循环次数多迭代效果更好 costs可选矩阵 与各类型错误项对应的成本-代价矩阵
summary(credit_model)#查看模型
credit_pred <- predict(credit_model, credit_test)#预测
predict(model,test,type="class") type取class分类结果或者prob分类概率
单规则算法(1R算法)--单一规则直观,但大数据底下,对噪声预测不准
library(RWeka)
mushroom_1R <- OneR(type ~ ., data = mushrooms)
重复增量修建算法(RIPPER) 基于1R进一步提取规则
library(RWeka)
mushroom_JRip <- JRip(type ~ ., data = mushrooms)
[plain] view plain copy
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)
#从1000里随机900个数值
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))
## 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)
# display simple facts about the tree
credit_model
# display detailed information about the tree
summary(credit_model)
## 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'))
## Step 5: Improving model performance ----
## Boosting the accuracy of decision trees
# boosted decision tree with 10 trials提高模型性能 利用boosting提升
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'))
## Making some mistakes more costly than others
# 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
# 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'))
#### Part 2: Rule Learners -------------------
## Example: Identifying Poisonous Mushrooms ----
## Step 2: Exploring and preparing the data ---- 自动因子转换--将字符标记为因子减少存储
mushrooms <- read.csv("mushrooms.csv", stringsAsFactors = TRUE)
# examine the structure of the data frame
str(mushrooms)
# drop the veil_type feature
mushrooms$veil_type <- NULL
# examine the class distribution
table(mushrooms$type)
## Step 3: Training a model on the data ----
library(RWeka)
# train OneR() on the data
mushroom_1R <- OneR(type ~ ., data = mushrooms)
## Step 4: Evaluating model performance ----
mushroom_1R
summary(mushroom_1R)
## Step 5: Improving model performance ----
mushroom_JRip <- JRip(type ~ ., data = mushrooms)
mushroom_JRip
summary(mushroom_JRip)
# Rule Learner Using C5.0 Decision Trees (not in text)
library(C50)
mushroom_c5rules <- C5.0(type ~ odor + gill_size, data = mushrooms, rules = TRUE)
summary(mushroom_c5rules)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
大部分人用 AI 写东西,是"一次一份"。但办公室里真正耗时间的,往往是"同一份东西做 N 遍"。 这篇讲的就是这类活:一家做企业内 ...
2026-11-30对账这件事,最能看出一个人会不会用 AI。 因为它不是"生成内容",是"把两份不一样的东西对上"。这类活没有标准答案,只有一堆例 ...
2026-11-30让优质知识跨越语言,被更多人看见 世界各地不缺好课,缺的是能读懂它的语言。原版英文,术语密集,字幕能救句子,救不了概念— ...
2026-11-30 面对一堆数据,新手最常见的状态是:指标都在眼前,却不知道从哪下手。其实数据分析没有那么多玄学,绝大多数业务问题,都可 ...
2026-11-27一句话答案:把文件路径和问题一起给它,它自己读表、清洗、算数、出图,最后给你一个能直接发出去的文件。你负责提问题和验收, ...
2026-11-27这三个名字经常被放在一起问,但它们根本不在同一层。搞混了,选型一定出错。 点击链接领取WorkBuddy体验课:https://edu.cda.cn ...
2026-11-27 在做有序分组的趋势分析时,经常被问到一个问题:线性趋势检验(linear trend test)的自由度是 1 吗?答案是:在单因素方差 ...
2026-11-26很多人把这两个放在一起比,其实它们解决的不是同一层问题。 点击链接领取WorkBuddy体验课:https://edu.cda.cn/goods/show/4341 ...
2026-11-26直接回答:有免费额度,但免费额度不够日常重度使用。值不值得付费,取决于你每个月的"文档活儿"有多少。 点击链接领取WorkBuddy ...
2026-11-26 在质量分析和数据报表里,趋势图和柏拉图(帕累托图)都是常用图形,也都可能是"柱子加折线"的组合,很容易被混用。但它们回 ...
2026-11-25这篇只讲一件事:让你在 20 分钟内真正跑通一个任务,而不是装完就放着。 点击链接领取WorkBuddy体验课:https://edu.cda.cn/goo ...
2026-11-25一句话答案:WorkBuddy 是腾讯做的桌面 AI 办公助手。你用一句话说清想要什么结果,它自己拆步骤、写文件、做表格、做 PPT,最后 ...
2026-11-25 营销里最常见的两种状态:一种是凭经验拍板,"我觉得这个渠道有效";另一种是报表堆了一堆,流量、曝光、点赞都在涨,却不知 ...
2026-11-24 "你还没点购买,货已经在你家几公里外的快递车上了。"这句话听起来像科幻,但它正是亚马逊在 2013 年拿到的一项专利里写的构 ...
2026-11-24月度经营会上,一张饼图被切成十一块。 颜色相近的两块挨在一起,投影仪上几乎分不出来。老板看了三秒,问了一句:到底谁在涨? ...
2026-11-24 很多人把预处理理解成"建模前随手洗一下数据":丢进工具,填个空、删个重复,就开始跑分析。但真正做过数据项目的人都知道, ...
2026-11-23 SQL 不是数据库工程师的专利,而是数据分析、运营、产品做业务取数和统计的基本功。绝大多数经营报表、用户分析、漏斗留存、 ...
2026-11-23这周你大概又经历过一次:报表自己跑了,SQL 让 AI 写了,连周报初稿都不用自己动手。你一边松口气,一边心里发慌——那我还剩什 ...
2026-11-23CDA数据分析师 出品 作者:李诗怡 多表进阶与 DDL/DML 连接 · 子查询 · 合并 · 建表与书写规范 ” 三、DQL 多表进阶:连 ...
2026-11-20 前面讲了怎么搭指标体系,但很多团队卡在最后一步:高层定了一个漂亮的目标,往下一层层写进周报,最后却落不到任何人的具体 ...
2026-11-20