
决策树
经验熵是针对所有样本的分类结果而言
经验条件熵是针对每个特征里每个特征样本分类结果之特征样本比例和
基尼不纯度
简单地说就是从一个数据集中随机选取子项,度量其被错误分类到其他分组里的概率
决策树算法使用轴平行分割来表现具体一定的局限性
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
用 Power BI 制作地图热力图:基于经纬度数据的实践指南 在数据可视化领域,地图热力图凭借直观呈现地理数据分布密度的优势,成 ...
2025-07-24解析 insert into select 是否会锁表:原理、场景与应对策略 在数据库操作中,insert into select 是一种常用的批量数据插入语句 ...
2025-07-24CDA 数据分析师的工作范围解析 在数字化时代的浪潮下,数据已成为企业发展的核心资产之一。CDA(Certified Data Analyst)数据分 ...
2025-07-24从 CDA LEVEL II 考试题型看 Python 数据分析要点 在数据科学领域蓬勃发展的当下,CDA(Certified Data Analyst)认证成为众多从 ...
2025-07-23用 Python 开启数据分析之旅:从基础到实践的完整指南 在数据驱动决策的时代,数据分析已成为各行业不可或缺的核心能力。而 Pyt ...
2025-07-23鸢尾花判别分析:机器学习中的经典实践案例 在机器学习的世界里,有一个经典的数据集如同引路明灯,为无数初学者打开了模式识别 ...
2025-07-23解析 response.text 与 response.content 的核心区别 在网络数据请求与处理的场景中,开发者经常需要从服务器返回的响应中提取数 ...
2025-07-22解析神经网络中 Softmax 函数的核心作用 在神经网络的发展历程中,激活函数扮演着至关重要的角色,它们为网络赋予了非线性能力, ...
2025-07-22CDA数据分析师证书考取全攻略 一、了解 CDA 数据分析师认证 CDA 数据分析师认证是一套科学化、专业化、国际化的人才考核标准, ...
2025-07-22左偏态分布转正态分布:方法、原理与实践 左偏态分布转正态分布:方法、原理与实践 在统计分析、数据建模和科学研究中,正态分 ...
2025-07-22你是不是也经常刷到别人涨粉百万、带货千万,心里痒痒的,想着“我也试试”,结果三个月过去,粉丝不到1000,播放量惨不忍睹? ...
2025-07-21我是陈辉,一个创业十多年的企业主,前半段人生和“文字”紧紧绑在一起。从广告公司文案到品牌策划,再到自己开策划机构,我靠 ...
2025-07-21CDA 数据分析师的职业生涯规划:从入门到卓越的成长之路 在数字经济蓬勃发展的当下,数据已成为企业核心竞争力的重要来源,而 CD ...
2025-07-21MySQL执行计划中rows的计算逻辑:从原理到实践 MySQL 执行计划中 rows 的计算逻辑:从原理到实践 在 MySQL 数据库的查询优化中 ...
2025-07-21在AI渗透率超85%的2025年,企业生存之战就是数据之战,CDA认证已成为决定企业存续的生死线!据麦肯锡全球研究院数据显示,AI驱 ...
2025-07-2035岁焦虑像一把高悬的利刃,裁员潮、晋升无望、技能过时……当职场中年危机与数字化浪潮正面交锋,你是否发现: 简历投了10 ...
2025-07-20CDA 数据分析师报考条件详解与准备指南 在数据驱动决策的时代浪潮下,CDA 数据分析师认证愈发受到瞩目,成为众多有志投身数 ...
2025-07-18刚入职场或是在职场正面临岗位替代、技能更新、人机协作等焦虑的打工人,想要找到一条破解职场焦虑和升职瓶颈的系统化学习提升 ...
2025-07-182025被称为“AI元年”,而AI,与数据密不可分。网易公司创始人丁磊在《AI思维:从数据中创造价值的炼金术 ...
2025-07-18CDA 数据分析师:数据时代的价值挖掘者 在大数据席卷全球的今天,数据已成为企业核心竞争力的重要组成部分。从海量数据中提取有 ...
2025-07-18