R_Studio(决策树算法)鸢尾花卉数据集Iris是一类多重变量分析的数据集【精】

  鸢尾花卉数据集Iris是一类多重变量分析的数据集

  通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类

  针对iris数据集实践决策树算法(C4.5、C5.0),并用交叉矩阵评估模型

  

  iris数据RStudio系统自带

  

  

Gary<-iris
#建立决策树模型,来预测鸢尾花的种类
#重命名变量名,将预测鸢尾花卉转换为class 通过前四个变量预测class属于哪一个类
Gary.names<-c('sepal length','sepal width','petal length','petal width', 'class')
names(Gary)<-Gary.names
#查看维度 150条数据 5维变量
dim(Gary)
#str()查看数据框中每个变量的属性
str(Gary)
#summary()提供最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻辑型向量的频数统计
summary(Gary)
#设定生成随机数的种子,种子是为了让结果具有重复性
set.seed(1)
#将数据集拆分为训练集和测试集,拆分比例为0.75
index<-sample(nrow(Gary),0.75*nrow(Gary),replace = F)
train<-Gary[index,]
test<-Gary[-index,]
library(C50)
#训练数据用于建立决策树模型
#测试集用于模型评估
mod<-C5.0(train[,-5],train[,5])
summary(mod)
#预测模型
pre1<-predict(mod,newdata=test,type='class')
tab<-table(pre1,test$class)
tab
sum(diag(tab))/sum(tab)
Gary.Script

实现过程

  将数据保存并重命名变量名

Gary<-iris

Gary.names<-c('sepal length','sepal width','petal length','petal width', 'class')

names(Gary)<-Gary.names

  dim():查看数据的维度 

  str():查看数据框中每个变量的属性

  summary():提供最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻辑型向量的频数统计

> dim(Gary)        #150条数据 5维变量
[1] 150   5

> str(Gary)        #查看了前四个数据框的属性值
'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 ...
 $ class       : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

> summary(Gary)
  sepal length    sepal width     petal length    petal width           class   
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  

  设定生成随机数的种子,种子是为了让结果具有重复性

  set.seed()只对运行该命令后的第一次随机产生结果有效(伪随机)

set.seed(1)

  将数据集拆分为训练集和测试集,拆分比例为0.75

index<-sample(nrow(Gary),0.75*nrow(Gary),replace = F)
train<-Gary[index,]
test<-Gary[-index,]

  加载C50包

  C5.0算法则是C4.5算法的商业版本,较C4.5算法提高了运算效率,它加入了boosting算法,使该算法更加智能化

library(C50)

  训练数据用于建立决策树模型

  测试集用于模型评估

  C5.0(x, y, ...):其中x指定自变量(数据框或矩阵的形式),y指定因变量

> C5.0(train[,-5],train[,5])

Call:
C5.0.default(x = train[, -5], y = train[, 5])

Classification Tree        分类树
Number of samples: 112     样本树
Number of predictors: 4     预测树

Tree size: 3   树高:3  
Non-standard options: attempt to group attributes    #尝试分组属性

  

> summary(mod)

Call:
C5.0.default(x = train[, -5], y = train[, 5])


C5.0 [Release 2.07 GPL Edition]      
-------------------------------

Class specified by attribute `outcome'         #"class"属性指定的类

Read 112 cases (5 attributes) from undefined.data  #112例(5读取从undefined.data属性)

Decision tree:                #决策树:

petal length <= 1.9: setosa (38)          #花瓣长度≤1.9:setosa(38)
petal length
> 1.9:                  #花瓣长度>1.9: :...petal width <= 1.6: versicolor (38/1)     #petal宽度:≤1.6:云芝(38/1) petal width > 1.6: virginica (36/2)      #花瓣宽度>1.6:锦葵(36/2) Evaluation on training data (112 cases):      #评估在训练数据(112例): Decision Tree                 #决策树 ---------------- Size Errors                 #尺寸错误    3 3( 2.7%) <<              #3 3(2.7 %)<< (a) (b) (c) <-classified as      ---- ---- ---- 38 (a): class setosa    37 2 (b): class versicolor 1 34 (c): class virginica Attribute usage:                    #属性 100.00% petal length                #100%的花瓣长度 66.07% petal width                 #占66.07%花瓣宽度 Time: 0.0 secs

  预测鸢尾花卉属于哪一类

  用交叉矩阵评估模型

> pre1<-predict(mod,newdata=test,type='class')
> tab<-table(pre1,test$class)
> tab
            
pre1         setosa versicolor virginica
  setosa         12          0         0
  versicolor      0         11         3
  virginica       0          0        12

 

  预测了38组数据,其中35组数据正确,3组数据预测出错

  对角线上的数据实际值和预测值相同,非对角线上的值为预测错误的值

  评估模型(预测)的正确率

sum(diag(tab))/sum(tab)
[1] 0.9210526

    diag(x = 1, nrow, ncol) 

    diag(x) <- value 

  解析: 

    x:一个矩阵,向量或一维数组,或不填写。 

    nrow, ncol:可选 行列。 

    value :对角线的值,可以是一个值或一个向量

 

 

(如需转载学习,请标明出处)
原文地址:https://www.cnblogs.com/1138720556Gary/p/9820039.html