决策树与随机森林的R语言实现

1.用party包构建决策树

以iris数据集为例。

用ctree()建立决策树,用predict()对新数据进行预测。

训练集与测试集划分:

> str(iris)
'data.frame':	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> set.seed(1234)
> ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))
> trainData <- iris[ind==1,]
> testData <- iris[ind==2,]
用默认参数来建立决策树:

> library(party)
> myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
> iris_ctree <- ctree(myFormula, data=trainData)
> # check the prediction
> table(predict(iris_ctree), trainData$Species)
            
             setosa versicolor virginica
  setosa         40          0         0
  versicolor      0         37         3
  virginica       0          1        31
输出规则并绘制已构建好的决策树以便查看。

> print(iris_ctree)

	 Conditional inference tree with 4 terminal nodes

Response:  Species 
Inputs:  Sepal.Length, Sepal.Width, Petal.Length, Petal.Width 
Number of observations:  112 

1) Petal.Length <= 1.9; criterion = 1, statistic = 104.643
  2)*  weights = 40 
1) Petal.Length > 1.9
  3) Petal.Width <= 1.7; criterion = 1, statistic = 48.939
    4) Petal.Length <= 4.4; criterion = 0.974, statistic = 7.397
      5)*  weights = 21 
    4) Petal.Length > 4.4
      6)*  weights = 19 
  3) Petal.Width > 1.7
    7)*  weights = 32 
> plot(iris_ctree)
> # 图略
> plot(iris_ctree, type="simple")
> #图略
用测试集对构建好的决策树进行测试:

> # predict on test data
> testPred <- predict(iris_ctree, newdata = testData)
> table(testPred, testData$Species)
            
testPred     setosa versicolor virginica
  setosa         10          0    
  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,以下是一个简单的决策树随机森林R语言中进行2分类的代码示例: 决策树代码示例: ```R # 导入数据集 data(iris) # 安装和导入rpart包 install.packages("rpart") library(rpart) # 将Species列设置为因变量 iris$Species <- as.factor(iris$Species) # 将数据集分为训练集和测试集 trainIndex <- sample(1:nrow(iris), 0.7*nrow(iris)) trainData <- iris[trainIndex,] testData <- iris[-trainIndex,] # 创建决策树模型 treeModel <- rpart(Species ~ ., data=trainData, method="class") # 使用测试集进行预测 treePred <- predict(treeModel, testData, type="class") # 计算预测准确率 treeAccuracy <- sum(treePred == testData$Species) / nrow(testData) # 输出结果 cat("决策树预测准确率为:", treeAccuracy) ``` 随机森林代码示例: ```R # 导入数据集 data(iris) # 安装和导入randomForest包 install.packages("randomForest") library(randomForest) # 将Species列设置为因变量 iris$Species <- as.factor(iris$Species) # 将数据集分为训练集和测试集 trainIndex <- sample(1:nrow(iris), 0.7*nrow(iris)) trainData <- iris[trainIndex,] testData <- iris[-trainIndex,] # 创建随机森林模型 rfModel <- randomForest(Species ~ ., data=trainData, ntree=500) # 使用测试集进行预测 rfPred <- predict(rfModel, testData) # 计算预测准确率 rfAccuracy <- sum(rfPred == testData$Species) / nrow(testData) # 输出结果 cat("随机森林预测准确率为:", rfAccuracy) ``` 请注意,这只是一个简单的示例,实际应用中可能需要更多的数据预处理和调参才能取得更好的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值