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
数据分析咨询请扫描二维码
在当今以数据为导向的商业环境中,数据分析师的角色变得越来越重要。无论是揭示消费者行为的趋势,还是优化企业运营的效率,数据 ...
2024-11-17金融数学是一门充满挑战和机遇的专业,它将数学、统计学和金融学的知识有机结合,旨在培养能够运用数学和统计方法解决复杂金融市 ...
2024-11-16在信息时代的浪潮中,大数据已成为推动创新的重要力量。无论是在商业、医疗、金融,还是在日常生活中,大数据扮演的角色都愈发举 ...
2024-11-16随着大数据技术的迅猛发展,数据已经成为现代商业、科技乃至生活各个方面的重要资产。大数据专业的毕业生在这一变革背景下,拥有 ...
2024-11-15随着大数据技术的迅猛发展,数据已经成为现代商业、科技乃至生活各个方面的重要资产。大数据专业的毕业生在这一变革背景下,拥有 ...
2024-11-15在快速演变的数字时代,数据分析已成为多个行业的核心驱动力。无论你是刚刚踏入数据分析领域,还是寻求进一步发展的专业人士,理 ...
2024-11-15Python作为一种通用编程语言,以其简单易学、功能强大等特点,成为众多领域的核心技术驱动者。无论是初学者还是有经验的编程人员 ...
2024-11-15在当今数据驱动的世界中,数据分析已成为许多行业的基础。无论是商业决策,产品开发,还是市场策略优化,数据分析都扮演着至关重 ...
2024-11-15数据分析作为现代商业和研究领域不可或缺的一部分,吸引了越来越多的初学者。然而,自学数据分析的过程中,初学者常常会遇到许多 ...
2024-11-15在当今的数据驱动世界中,机器学习方法在数据挖掘与分析中扮演着核心角色。这些方法通过从数据中学习模式和规律来构建模型,实现 ...
2024-11-15随着数据在各个行业的重要性日益增加,数据分析师在商业和技术领域的角色变得至关重要。其核心职责之一便是通过数据可视化,将复 ...
2024-11-15数据分析师的职责不仅仅局限于解析数据和得出结论,更在于将这些复杂的信息转换为清晰、易懂且具有影响力的沟通。良好的沟通能力 ...
2024-11-15数字化转型是企业提升竞争力和实现可持续发展的关键路径。面对快速变化的市场环境,以及技术的飞速发展,企业在数字化转型过程中 ...
2024-11-15CDA数据分析师认证:CDA认证分为三个等级:Level Ⅰ、Level Ⅱ和Level Ⅲ,每个等级的报考条件如下: Le ...
2024-11-14自学数据分析可能是一条充满挑战却又令人兴奋的道路。随着数据在现代社会中的重要性日益增长,掌握数据分析技能不仅能提升你的就 ...
2024-11-14数据分析相关职业选择 数据分析领域正在蓬勃发展,为各种专业背景的人才提供了丰富的职业机会。从初学者到有经验的专家,每个人 ...
2024-11-14数据挖掘与分析在金融行业的使用 在当今快速发展的金融行业中,数据挖掘与分析的应用愈发重要,成为驱动行业变革和提升竞争力的 ...
2024-11-14学习数据挖掘需要掌握哪些技能 数据挖掘是一个不断发展的领域,它结合了统计学、计算机科学和领域专业知识,旨在从数据中提取有 ...
2024-11-14统计学作为一门基于数据的学科,其广泛的应用领域和多样的职业选择,使得毕业生拥有丰厚的就业前景。无论是在政府还是企业,统计 ...
2024-11-14在当今高速发展的技术环境下,企业正在面临前所未有的机遇和挑战。数字化转型已成为企业保持竞争力和应对市场变化的必由之路。要 ...
2024-11-13