京公网安备 11010802034615号
经营许可证编号:京B2-20210330
1.准备数据
[plain] view plain copy
> install.packages("tree")
> library(tree)
> library(ISLR)
> attach(Carseats)
> High=ifelse(Sales<=8,"No","Yes") //set high values by sales data to calssify
> Carseats=data.frame(Carseats,High) //include the high data into the data source
> fix(Carseats)
2.生成决策树
[plain] view plain copy
> tree.carseats=tree(High~.-Sales,Carseats)
> summary(tree.carseats)
[plain] view plain copy
//output training error is 9%
Classification tree:
tree(formula = High ~ . - Sales, data = Carseats)
Variables actually used in tree construction:
[1] "ShelveLoc" "Price" "Income" "CompPrice" "Population"
[6] "Advertising" "Age" "US"
Number of terminal nodes: 27
Residual mean deviance: 0.4575 = 170.7 / 373
Misclassification error rate: 0.09 = 36 / 400
3. 显示决策树
[plain] view plain copy
> plot(tree . carseats )
> text(tree .carseats ,pretty =0)
4.Test Error
[plain] view plain copy
//prepare train data and test data
//We begin by using the sample() function to split the set of observations sample() into two halves, by selecting a random subset of 200 observations out of the original 400 observations.
> set . seed (1)
> train=sample(1:nrow(Carseats),200)
> Carseats.test=Carseats[-train,]
> High.test=High[-train]
//get the tree model with train data
> tree. carseats =tree (High~.-Sales , Carseats , subset =train )
//get the test error with tree model, train data and predict method
//predict is a generic function for predictions from the results of various model fitting functions.
> tree.pred = predict ( tree.carseats , Carseats .test ,type =" class ")
> table ( tree.pred ,High. test)
High. test
tree. pred No Yes
No 86 27
Yes 30 57
> (86+57) /200
[1] 0.715
5.决策树剪枝
[plain] view plain copy
/**
Next, we consider whether pruning the tree might lead to improved results. The function cv.tree() performs cross-validation in order to cv.tree() determine the optimal level of tree complexity; cost complexity pruning is used in order to select a sequence of trees for consideration.
For regression trees, only the default, deviance, is accepted. For classification trees, the default is deviance and the alternative is misclass (number of misclassifications or total loss).
We use the argument FUN=prune.misclass in order to indicate that we want the classification error rate to guide the cross-validation and pruning process, rather than the default for the cv.tree() function, which is deviance.
If the tree is regression tree,
> plot(cv. boston$size ,cv. boston$dev ,type=’b ’)
*/
> set . seed (3)
> cv. carseats =cv. tree(tree .carseats ,FUN = prune . misclass ,K=10)
//The cv.tree() function reports the number of terminal nodes of each tree considered (size) as well as the corresponding error rate(dev) and the value of the cost-complexity parameter used (k, which corresponds to α.
> names (cv. carseats )
[1] " size" "dev " "k" " method "
> cv. carseats
$size //the number of terminal nodes of each tree considered
[1] 19 17 14 13 9 7 3 2 1
$dev //the corresponding error rate
[1] 55 55 53 52 50 56 69 65 80
$k // the value of the cost-complexity parameter used
[1] -Inf 0.0000000 0.6666667 1.0000000 1.7500000
2.0000000 4.2500000
[8] 5.0000000 23.0000000
$method //miscalss for classification tree
[1] " misclass "
attr (," class ")
[1] " prune " "tree. sequence "
[plain] view plain copy
//plot the error rate with tree node size to see whcih node size is best
> plot(cv. carseats$size ,cv. carseats$dev ,type=’b ’)
/**
Note that, despite the name, dev corresponds to the cross-validation error rate in this instance. The tree with 9 terminal nodes results in the lowest cross-validation error rate, with 50 cross-validation errors. We plot the error rate as a function of both size and k.
*/
> prune . carseats = prune . misclass ( tree. carseats , best =9)
> plot( prune . carseats )
> text( prune .carseats , pretty =0)
//get test error again to see whether the this pruned tree perform on the test data set
> tree.pred = predict ( prune . carseats , Carseats .test , type =" class ")
> table ( tree.pred ,High. test)
High. test
tree. pred No Yes
No 94 24
Yes 22 60
> (94+60) /200
[1] 0.77
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
CDA数据分析师 出品 作者:李诗怡 1. VLOOKUP:按行跨区域找数据(最常用) 作用:在指定的表格或数据区域的第一列里找目标值, ...
2026-10-23数据敏感度不是天赋,也不是“看一眼数字就能发现问题”的直觉。对市场专员来说,它更接近一套可练习的工作习惯:看到投放ROI、 ...
2026-10-23市场营销专员转数据分析,不是从零重来。你已有的渠道投放、活动执行、线索跟进经验,本身就是分析工作的原料——缺的不是业务理 ...
2026-10-23先说结论:市场岗位并非必须考数据分析证书。招聘方更常看的是你能否把渠道效果、活动ROI、线索转化这些业务问题讲清楚,证书只 ...
2026-10-23市场分析师的职场知识提升,最怕两件事:一是把“学工具”当成学习本身,Excel、SQL、Python轮着学一遍,回到工作里还是不知道怎 ...
2026-10-23市场分析师的通用技能可以归为五块:业务与市场理解、调研与竞品分析、用户与渠道数据分析、指标与分析表达、工具操作。它们不是 ...
2026-10-23市场分析师的核心技能,可以先用一个框架回答:懂业务与市场、能取数与处理数据、会指标与分析方法、能把洞察讲清楚。这四块是共 ...
2026-10-23市场策划不是“会写文案、能办活动”这么简单。它本质上是一个用创意和资源撬动业务结果的岗位:先理解市场和人群,再形成方案, ...
2026-10-23社群运营不是“拉群、发通知、维护气氛”这么简单。它真正要对的结果是:用户进得来、留得住、愿意互动、最终产生转化或复购。因 ...
2026-10-23营销人的能力提升计划,最容易做成一张课程清单:报名几门课、读完几本书、考一个证,年底回头看,工作方式却没变。问题不在努力 ...
2026-10-23人力资源专员的职场价值,不由“做了多少事”决定,而由“解决什么问题、留下什么成果”决定。同样做招聘、算考勤、办入离职,有 ...
2026-10-23产品经理的职场价值,不主要由会画原型、会写文档决定,而由能否用业务结果证明自己的判断与推动决定。同一岗位的人价值差距大, ...
2026-10-23运营专员的能力提升,最容易走偏成两件事:一是把市面上所有工具都学一遍,二是把沟通、协作这类通用能力当成唯一答案。更有效的 ...
2026-10-23市场总监的沟通与管理,难点通常不在话术,而在三件事能否同时成立:向上用结论和数据对齐目标,平级用共同指标换取协同,向下用 ...
2026-10-23市场分析师的沟通失分,往往不是“嘴笨”,而是三件事没做好:结论没提炼出来、指标口径没对齐、建议没有落到决策上。所以提升沟 ...
2026-10-23社群运营做久了,很容易陷入一种忙碌:每天在群里发内容、回消息、做活动,热闹的时候很热闹,可一到月底复盘,说不清哪些动作带 ...
2026-10-23品牌运营的技能提升,最容易走偏的一步是先报课、先学工具。更有效的顺序是:先沿“品牌定位与策略—内容与传播—渠道与活动执行 ...
2026-10-23营销人员想赢得客户信任,单靠话术和关系维护越来越不够。客户在签约前会追问:你懂我的业务吗?方案里的数字怎么来的?承诺的效 ...
2026-10-23APP运营专员的能力提升,容易陷入两种误区:一是把“会用工具”当成能力全部,学了一堆Excel函数和SQL语法,回到岗位仍说不清一 ...
2026-10-23社群运营的沟通难点,不在于“会不会说话”,而在于同一件事要对三类人说清楚:对用户要安抚情绪、给出确定答复;对产品、活动、 ...
2026-10-23