R语言 回归决策树
决策树是机器学习中的一种算法,它使用决策作为特征,以树状结构的形式表示结果。它是一种常用的工具,用于直观地表示算法做出的决定。决策树同时使用分类和回归。回归树用于因变量是连续的时候,而分类树用于因变量是分类的时候。例如,确定/预测性别是分类的一个例子,而根据发动机功率预测汽车的里程数是回归的一个例子。在这篇文章中,让我们讨论在R编程中使用回归的决策树的语法和实现。
在R中的实现
在R编程中, rpart() 函数存在于 rpart 包中。使用 rpart() 函数,可以在R中建立决策树。
语法:
rpart(formula, data, method)
参数:
formula: 表示拟合模型所依据的公式
data: 表示数据框架
method: 表示创建决策树的方法。”anova “用于回归,”class “作为分类的方法。
例1:
在这个例子中,让我们使用回归决策树来预测萼片的宽度。
第1步: 安装所需的软件包
# Install the package
install.packages("rpart")
第2步: 加载软件包
# Load the package
library(rpart)
第3步: 为回归的决策树拟合模型
# Create decision tree using regression
fit <- rpart(Sepal.Width ~ Sepal.Length +
Petal.Length + Petal.Width + Species,
method = "anova", data = iris)
第4步: 绘制树形图
# Output to be present as PNG file
png(file = "decTreeGFG.png", width = 600,
height = 600)
# Plot
plot(fit, uniform = TRUE,
main = "Sepal Width Decision
Tree using Regression")
text(fit, use.n = TRUE, cex = .7)
# Saving the file
dev.off()
第5步: 打印决策树模型
# Print model
print(fit)
第6步: 预测萼片宽度
# Create test data
df <- data.frame (Species = 'versicolor',
Sepal.Length = 5.1,
Petal.Length = 4.5,
Petal.Width = 1.4)
# Predicting sepal width
# using testing data and model
# method anova is used for regression
cat("Predicted value:\n")
predict(fit, df, method = "anova")
输出
n= 150
node), split, n, deviance, yval
* denotes terminal node
1) root 150 28.3069300 3.057333
2) Species=versicolor, virginica 100 10.9616000 2.872000
4) Petal.Length=4.05 84 7.3480950 2.945238
10) Petal.Width< 1.95 55 3.4920000 2.860000
20) Sepal.Length=6.35 19 0.6242105 2.963158 *
11) Petal.Width>=1.95 29 2.6986210 3.106897
22) Petal.Length=5.25 22 2.0277270 3.168182 *
3) Species=setosa 50 7.0408000 3.428000
6) Sepal.Length=5.05 22 1.7859090 3.713636 *
Predicted value:
1
2.805556
例2:
在这个例子中,让我们用决策树来预测mpg值的回归。
第1步: 安装所需的软件包
# Install the package
install.packages("rpart")
第2步: 加载软件包
# Load the package
library(rpart)
第3步: 为回归的决策树拟合模型
# Create decision tree using regression
fit <- rpart(mpg ~ disp + hp + cyl,
method = "anova", data = mtcars )
第4步: 绘制树形图
# Output to be present as PNG file
png(file = "decTree2GFG.png", width = 600,
height = 600)
# Plot
plot(fit, uniform = TRUE,
main = "MPG Decision Tree using Regression")
text(fit, use.n = TRUE, cex = .6)
# Saving the file
dev.off()
第5步: 打印决策树模型
# Print model
print(fit)
第6步: 使用测试数据集预测mpg值
# Create test data
df <- data.frame (disp = 351, hp = 250,
cyl = 8)
# Predicting mpg using testing data and model
cat("Predicted value:\n")
predict(fit, df, method = "anova")
输出
n= 32
node), split, n, deviance, yval
* denotes terminal node
1) root 32 1126.04700 20.09062
2) cyl>=5 21 198.47240 16.64762
4) hp>=192.5 7 28.82857 13.41429 *
5) hp< 192.5 14 59.87214 18.26429 *
3) cyl< 5 11 203.38550 26.66364 *
Predicted value:
1
13.41429
决策树的优点
- 考虑所有可能的决策: 决策树考虑所有可能的决策来创建问题的结果。
- 易于使用: 有了分类和回归技术,它很容易用于任何类型的问题,并进一步创建预测和解决该问题。
- 没有缺失值的问题: 有缺失值的数据集没有问题,不会影响决策树的建立。
决策树的劣势
- 需要更多时间: 决策树需要更多时间来计算大数据集。
- 学习能力差: 决策树的学习能力不强。随机森林方法被用于更好的学习。