R与数据挖掘(学习决策树和随机森林的R语句)(2)
printcp(breast.part); # 查看这棵树的复杂性参量表
CP nsplit rel error xerror
交叉验证错误率 叶节点数减一
预测误差
xstd 0.052142
0.031415 0.026924 0.026071 0.026071
1 2 3 4 5 0.780083 0.053942 0.024896 0.012448 0.010000 0 1 2 3 6 1.00000 0.21992 0.16598 0.14108 0.10373 1.00000 0.26141 0.18672 0.17427 0.17427
误差原则:
参考文献
6
# 剪枝
breast.part2 <- prune(breast.part, cp = 0.016);
rpart.plot(breast.part2); # 剪枝以后的分类树图
b)进行交叉验证:由于数据的观测并不是太大(699)采取3折交叉验证 n = 699; zz1 = 1:n;
zz2 = rep(1:3, ceiling(699/3))[1:n]; set.seed(100); zz2 = sample(zz2, n); nmse = list(NULL, NULL); c <- breast_cancer_median; for (i in 1:3) {
data.train = c[-c(which(zz2 == i)), ]; data.test = c[c(zz2 == i), ];
d.train <- rpart(factor(btype) ~ ., data = data.train, method=\
table1 = table(predict(d.train, data.train, type = \ table2 = table(predict(d.train, data.test , type = \ nmse[[1]][i] = 1 - sum(diag(table1))/nrow(data.train); nmse[[2]][i] = 1 - sum(diag(table2))/sum(table2); cat(\第\折:\ cat(\训练集错误率:\
cat(\测试集错误率:\}
NMSE = array();
NMSE[1] = sum(nmse[[1]])/3; NMSE[2] = sum(nmse[[2]])/3;
cat(\训练集上的平均错误率为:\cat(\测试集上的平均错误率为:\
7
结果:
rpart method第 1 折:
训练集错误率: 0.04935622 测试集错误率: 0.05579399
rpart method第 2 折:
训练集错误率: 0.03433476 测试集错误率: 0.05150215
rpart method第 3 折:
训练集错误率: 0.04077253 测试集错误率: 0.04291845
rpart method训练集上的平均错误率为: 0.04148784
rpart method测试集上的平均错误率为: 0.05007153
2)神经网络 使用的包有nnet
a)使用中位数填补后的数据进行建模分析以及分析判错率 # 请先安装nnet程序包 library(nnet);
a <- nnet(factor(btype) ~ ., data = breast_cancer_median, size = 6, rang = 0.1, decay = 5e-4, maxit = 1000);
a.predict <- predict(a, data = breast_cancer_median, type = 'class'); table=table(a.predict, breast_cancer_median$btype);
# 计算错判率
pError=1 - sum(diag(table))/nrow(breast_cancer_median); cat(\分类的错判率pError为:\
结果显示全部判断正确 a.predict 2 4 2 458 0 4 0 241
nnet分类的错判率pError为: 0
b)使用三折交叉验证
n = 699; zz1 = 1:n;
zz2 = rep(1:3, ceiling(699/3))[1:n]; set.seed(100); zz2 = sample(zz2, n); nmse = list(NULL, NULL); c <- breast_cancer_median; for (i in 1:3) {
data.train = c[-c(which(zz2 == i)), ]; data.test = c[c(zz2 == i), ];
8
d.train <- nnet(factor(btype)~., data = data.train, size = 6, rang = 0.1, decay = 5e-4, maxit = 1000);
table1 = table(predict(d.train, data.train, type = \ table2 = table(predict(d.train, data.test, type = \ nmse[[1]][i] = 1 - sum(diag(table1))/nrow(data.train); nmse[[2]][i] = 1 - sum(diag(table2))/sum(table2); cat(\第\折:\
cat(\第\折训练集的错误率为:\
cat(\第\折测试集的错误率为:\}
NMSE = array();
NMSE[1] = sum(nmse[[1]])/3; NMSE[2] = sum(nmse[[2]])/3;
cat(\训练集上的平均错误率为:\cat(\测试集上的平均错误率为:\
结果:
nnet method第 1 折:
第 1 折训练集的错误率为: 0
第 1 折测试集的错误率为: 0.05150215
nnet method第 2 折:
第 2 折训练集的错误率为: 0.002145923 第 2 折测试集的错误率为: 0.08583691
nnet method第 3 折:
第 3 折训练集的错误率为: 0
第 3 折测试集的错误率为: 0.03862661
nnet method训练集上的平均错误率为: 0.0007153076 nnet method测试集上的平均错误率为: 0.05865522
3)支持向量机 使用的包有e1071,ggplot
a)ggplot这个包画图的功能很强大,就是可以使图输出到pdf,等好多形式的输出
可以拿出任意两个变量来画图给个直观的印象那个变量对分类影响较明显,例如使用一下两个变量来看,这里之举一个看。
# 绘制以bare_nuclei为横轴频数为中轴的直方图,请先安装ggplot2程序包 library(ggplot2); # 载入绘图函数报
c6 <- qplot(bare_nuclei, data = breast_cancer_median, colour = factor(btype)); ggsave(\ # 将图保存为PDF格式
9
b)使用中位数填补后的数据进行建模分析以及分析判错率
#请先安装e1071程序包 library(e1071);
s <- svm(factor(btype)~., data = breast_cancer_median); pre = predict(s, breast_cancer_median, type = 'class'); plot(pre ~ breast_cancer_median$btype);
table = table(pre, breast_cancer_median$btype); table; # 计算错判率
error <- 1 - sum(diag(table))/nrow(breast_cancer_median); cat(\分类的错判率error为:\
结果:
pre 2 4 2 446 5 4 12 236
SVM分类的错判率error为: 0.02432046
10
…… 此处隐藏:839字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [综合文档]应答器设备技术规范(征求意见稿)A1
- [综合文档]教师 2012年高考政治试题按考点分类汇
- [综合文档]保险公司的总经理助理竞职演说
- [综合文档]卫生应急大练兵大比武活动考试--题库(
- [综合文档]徐州经济技术开发区总体规划环境影响报
- [综合文档]汉语拼音表(带声调)
- [综合文档]二年级 上 思维训练( 1~18)
- [综合文档]特色学校五年发展规划
- [综合文档]机床经常出现报警“X1轴定位监控”
- [综合文档]《电子技术基础》21.§5—2、3、4 习题
- [综合文档]浙江省深化普通高中课程改革
- [综合文档]CRISP原理 - 图文
- [综合文档]2017年电大社会调查研究与方法形考答案
- [综合文档]浅析建筑施工安全毕业论文
- [综合文档]《回忆我的母亲》名师教案
- [综合文档]装饰装修工程监理规划
- [综合文档]三下乡心得体会-文艺
- [综合文档]柱计算长度系数 - 图文
- [综合文档]全流程思考,提高燃电系统热电转换率--
- [综合文档]2018年嘉定区中考物理一模含答案
- 433M车库门滚动码遥控器
- 8、架空线路施工规范
- 大学四年声乐学习的体会
- 新北师大版五年级数学上册《轴对称再认
- 部编版五年级上册语文第六单元小结复习
- 小学六年级英语形容词用法
- 第2课 抗美援朝保家卫国 课件01(岳麓版
- 2015年天津大学运筹学基础考研真题,考
- 微机计算机控制技术课后于海生(第2版)
- 安全教育实践活动
- Delphi程序设计教程_第1章_Delphi概述
- 第八讲 工业革命与启蒙运动
- 《中华人民共和国药典》2005年版二部勘
- 科粤版九年级化学2.3构成物质的微粒(1)
- 西师大版数学三年级下册《长方形、正方
- ch6_冒泡排序演示
- 第4章 冲裁模具设计
- 浙江中小民营企业员工流失论文[终稿]
- 再议有线数字电视市场营运模式
- 昆明供水工程监理大纲




