
决策树
经验熵是针对所有样本的分类结果而言
经验条件熵是针对每个特征里每个特征样本分类结果之特征样本比例和
基尼不纯度
简单地说就是从一个数据集中随机选取子项,度量其被错误分类到其他分组里的概率
决策树算法使用轴平行分割来表现具体一定的局限性
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
2025 年,数据如同数字时代的 DNA,编码着人类社会的未来图景,驱动着商业时代的运转。从全球互联网用户每天产生的2.5亿TB数据, ...
2025-06-052025 年,数据如同数字时代的 DNA,编码着人类社会的未来图景,驱动着商业时代的运转。从全球互联网用户每天产生的2.5亿TB数据, ...
2025-05-27CDA数据分析师证书考试体系(更新于2025年05月22日)
2025-05-26解码数据基因:从数字敏感度到逻辑思维 每当看到超市货架上商品的排列变化,你是否会联想到背后的销售数据波动?三年前在零售行 ...
2025-05-23在本文中,我们将探讨 AI 为何能够加速数据分析、如何在每个步骤中实现数据分析自动化以及使用哪些工具。 数据分析中的AI是什么 ...
2025-05-20当数据遇见人生:我的第一个分析项目 记得三年前接手第一个数据分析项目时,我面对Excel里密密麻麻的销售数据手足无措。那些跳动 ...
2025-05-20在数字化运营的时代,企业每天都在产生海量数据:用户点击行为、商品销售记录、广告投放反馈…… 这些数据就像散落的拼图,而相 ...
2025-05-19在当今数字化营销时代,小红书作为国内领先的社交电商平台,其销售数据蕴含着巨大的商业价值。通过对小红书销售数据的深入分析, ...
2025-05-16Excel作为最常用的数据分析工具,有没有什么工具可以帮助我们快速地使用excel表格,只要轻松几步甚至输入几项指令就能搞定呢? ...
2025-05-15数据,如同无形的燃料,驱动着现代社会的运转。从全球互联网用户每天产生的2.5亿TB数据,到制造业的传感器、金融交易 ...
2025-05-15大数据是什么_数据分析师培训 其实,现在的大数据指的并不仅仅是海量数据,更准确而言是对大数据分析的方法。传统的数 ...
2025-05-14CDA持证人简介: 万木,CDA L1持证人,某电商中厂BI工程师 ,5年数据经验1年BI内训师,高级数据分析师,拥有丰富的行业经验。 ...
2025-05-13CDA持证人简介: 王明月 ,CDA 数据分析师二级持证人,2年数据产品工作经验,管理学博士在读。 学习入口:https://edu.cda.cn/g ...
2025-05-12CDA持证人简介: 杨贞玺 ,CDA一级持证人,郑州大学情报学硕士研究生,某上市公司数据分析师。 学习入口:https://edu.cda.cn/g ...
2025-05-09CDA持证人简介 程靖 CDA会员大咖,畅销书《小白学产品》作者,13年顶级互联网公司产品经理相关经验,曾在百度、美团、阿里等 ...
2025-05-07相信很多做数据分析的小伙伴,都接到过一些高阶的数据分析需求,实现的过程需要用到一些数据获取,数据清洗转换,建模方法等,这 ...
2025-05-06以下的文章内容来源于刘静老师的专栏,如果您想阅读专栏《10大业务分析模型突破业务瓶颈》,点击下方链接 https://edu.cda.cn/g ...
2025-04-30CDA持证人简介: 邱立峰 CDA 数据分析师二级持证人,数字化转型专家,数据治理专家,高级数据分析师,拥有丰富的行业经验。 ...
2025-04-29CDA持证人简介: 程靖 CDA会员大咖,畅销书《小白学产品》作者,13年顶级互联网公司产品经理相关经验,曾在百度,美团,阿里等 ...
2025-04-28CDA持证人简介: 居瑜 ,CDA一级持证人国企财务经理,13年财务管理运营经验,在数据分析就业和实践经验方面有着丰富的积累和经 ...
2025-04-27