我目前正在处理 R 的问题。我想在数据集上应用分类树,但结果似乎是错误的,因为我已经使用 Weka 解决了相同的问题,并且得到了不同的结果。
我得到了一个包含在 csv 文件中的数据集,如下所示:
age,menopause,tumor.size,inv.nodes,node.caps,deg.malig,breast,breast.quad,irradiat,class
40-49,premeno,15-19,0-2,yes,3,right,left_up,no,recurrence-events
50-59,ge40,15-19,0-2,no,1,right,central,no,no-recurrence-events
50-59,ge40,35-39,0-2,no,2,left,left_low,no,recurrence-events
40-49,premeno,35-39,0-2,yes,3,right,left_low,yes,no-recurrence-events
40-49,premeno,30-34,3-5,yes,2,left,right_up,no,recurrence-events
这是脚本:
#Open r file
cancer = read.csv("cancer.csv")
#Data Exploration
summary(cancer)
str(cancer)
#Divide into test and train sets
set.seed(1234)
ind <- sample(2, nrow(cancer), replace=TRUE, prob=c(0.7, 0.3))
trainData <- cancer[ind==1,]
testData <- cancer[ind==2,]
#Build the model
library(party)
cancermodel <- class ~ age + menopause + tumor.size + inv.nodes + node.caps + deg.malig + breast + breast.quad + irradiat
cancertree <- ctree(cancermodel,trainData)
table(predict(cancertree),trainData$class)
#Draw tree
plot(cancertree, type="simple")
#Testset
testPred <- predict(cancertree, newdata = testData)
table(testPred, testData$class)