R决策树预测

时间:2018-06-12 06:55:15

标签: r decision-tree party

我有这个数据集:

"Density","bodyfat","Age","stato"
1.0708,12.3,23,atletico
1.0853,6.1,22,atletico
1.0414,25.3,22,sopraMedia
1.0751,10.4,26,atletico
1.0414,25.3,22,sopraMedia
1.0321,10.4,26,atletico
1.0561,25.3,22,sopraMedia
1.0752,3.1,26,pesoMinimo
1.0987,26.2,22,obeso
1.0654,15.4,26,buonoStato
1.0321,16.9,22,buonoStato
1.0451,10.4,26,atletico
1.0924,27.3,22,obeso
1.0461,1.4,26,pesoMinimo
1.0155,25.3,22,sopraMedia
1.0112,10.4,26,atletico
1.0785,3.3,22,pesoMinimo
1.0776,28.1,26,obeso

我想要这个分类规则:

2 < bodyFat < 4 => pesoMinimo
6 < bodyFat < 13 => atletico
14 < bodyFat < 17 => buonoStato
18 < bodyFat < 25 => sopraMedia
bodyFat > 26 => obeso

我用于分类的代码是:

library(party)

#read data file
mydata= read.csv("/home/Bodyfat.csv")

# Check attributes of data
str(mydata)


set.seed(1234)
ind <- sample(2,nrow(mydata),replace=TRUE, prob= c(0.7,0.3))
trainData <- mydata[ind==1,]
testData <- mydata[ind==2,]


myFormula <- stato ~ bodyfat
albero <- ctree(myFormula, data=trainData)
table(predict(albero),trainData$stato)

print(albero)
plot(albero)

testData <- data.frame(Density=1.0515,bodyfat=30.7,Age=30,stato="")
testPred <- predict(albero,newdata= testData)
table(testPred,testData$stato)

我得到的结果不太好

我获得的第一次预测:     表(预测(ALBERO),trainData $ Stato)进行

                 atletico buonoStato obeso pesoMinimo sopraMedia
  atletico          5          2     3          2          3
  buonoStato        0          0     0          0          0
  obeso             0          0     0          0          0
  pesoMinimo        0          0     0          0          0
  sopraMedia        0          0     0          0          0

我获得第二次预测:

> table(testPred,testData$stato)

 testPred      
   atletico   1
   buonoStato 0
   obeso      0
   pesoMinimo 0
   sopraMedia 0

但是在NewData中我有bodyFat = 30.7 =&gt; “obeso”而非“atletico”。

为什么它无法正常工作?

DPUT:

> dput(mydata)
structure(list(Density = c(1.0708, 1.0853, 1.0414, 1.0751, 1.0414, 
1.0321, 1.0561, 1.0752, 1.0987, 1.0654, 1.0321, 1.0451, 1.0924, 
1.0461, 1.0155, 1.0112, 1.0785, 1.0776), bodyfat = c(12.3, 6.1, 
25.3, 10.4, 25.3, 10.4, 25.3, 3.1, 26.2, 15.4, 16.9, 10.4, 27.3, 
1.4, 25.3, 10.4, 3.3, 28.1), Age = c(23L, 22L, 22L, 26L, 22L, 
26L, 22L, 26L, 22L, 26L, 22L, 26L, 22L, 26L, 22L, 26L, 22L, 26L
), stato = structure(c(1L, 1L, 5L, 1L, 5L, 1L, 5L, 4L, 3L, 2L, 
2L, 1L, 3L, 4L, 5L, 1L, 4L, 3L), .Label = c("atletico", "buonoStato", 
"obeso", "pesoMinimo", "sopraMedia"), class = "factor")), .Names = c("Density", 
"bodyfat", "Age", "stato"), class = "data.frame", row.names = c(NA, 
 -18L))

0 个答案:

没有答案