R中的决策树预测使用id来影响预测

时间:2018-04-21 21:25:44

标签: r decision-tree

我正在使用C50库试图预测未来几年的出勤率'毕业然而我的树显示ID作为预测的一部分!当我拿出它时,我的树只变成一个节点(级别)..任何建议都将受到高度赞赏

数据集(JSON)的一部分:

{"id":"50","name":"James Charlie","faculty":"Science","degree":"Bachelor degree","course":"Sport Science","attend":"No","year":"2016"},

完整数据集/学生对象:git repo

R脚本:

con=dbConnect(MySQL(), user = 'root', password = '', dbname='students', host = 'localhost') dbListTables(con) Student <- dbReadTable(con, 'students') rows <- nrow(Student)

    Student$attend <- as.factor(Student$attend) Student$year <- as.factor(Student$year) 
Student$faculty <- as.factor(Student$faculty) 
Student$course <- as.factor(Student$course) 
Student 
dim(Student) 
summary(Student)

str(Student)
    Student <- Student[-2]
    dim(Student)

str(Student)

set.seed(1234)
Student_rand <- Student[order(runif(719)), ] #randomize the data
Student_train <- Student_rand[1:400, ] #split data/train data to predect the test
Student_test  <- Student_rand[401:719, ] #validation for train prediction
summary(Student_train)


prop.table(table(Student_train$attend))#propability for prediction
prop.table(table(Student_test$attend))

Student_model <- C5.0(Student_train[,-5],Student_train$attend)

summary(Student_model)

Student_model
summary(Student_model)
jpeg("tree.jpg")
plot(Student_model)
dev.off()

Student_model$predictors
Student_model$trials
Student_model$tree
summary(Student_model)

Student_pred <- predict(Student_model, Student_test,type="class")

table(Student_test$attend ,Student_pred)
CrossTable(Student_pred, Student_test$attend,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('predicted default', 'actual default'))

最后是树: enter image description here

**我尝试的第一件事就是删除了id,我收到了以下错误:

partysplit出错(varid = as.integer(i),index = index,info = k,prob = NULL)*

  

:'index'的最小值不等于1另外:警告   message:in min(index,na.rm = TRUE):没有非缺失参数   分钟;返回Inf

*

然后我尝试并添加了一个随机列,导致预测使用该随机列作为推断.. **

1 个答案:

答案 0 :(得分:1)

复制结构并分配给Student。保留name和id关联的原始数据帧显示有很多hte name变量的重复:

str(Student[2])
#'data.frame':  724 obs. of  1 variable:
# $ name: chr  "Jill Austin" "David Beckham" "Chris Evans" "Sheldon Cooper" ...
length(table(Student[2]))
#[1] 201

然后我看了前165个ID中的重复,如果id小于165,它们的概率非常低:

length(table(Student[1:164, 2]))
[1] 163

因此,定义一个标记重复的变量:

 Student$IsRepeated <- ave( Student$name, Student$name, FUN=length) > 1

然后事实证明&#34; name.repeatingness&#34;考虑到其他预测因素后,与出勤率有关。

> with( Student, table( attend, IsRepeated ) )
      IsRepeated
attend FALSE TRUE
   No     50  259
   Yes    59  356   # so nothing dramatic here, but try other predictors as well

首先我查看输出:

 with( Student, table(attend, year, IsRepeated , faculty) )

有点长,所以我注意到科学与工程小组有些不同:

  with( Student, table(attend, year, IsRepeated , fac_EorS=faculty %in% c("Engineering", "Science") ) )
, , IsRepeated = FALSE, fac_EorS = FALSE

      year
attend 2015 2016 2017 2018
   No     0    0    0   10
   Yes    0    0    0   16

, , IsRepeated = TRUE, fac_EorS = FALSE

      year
attend 2015 2016 2017 2018
   No     9    9    9  131
   Yes   37   17   17  113

, , IsRepeated = FALSE, fac_EorS = TRUE

      year
attend 2015 2016 2017 2018
   No     0    0    1   39
   Yes    1    0    0   42

, , IsRepeated = TRUE, fac_EorS = TRUE

      year
attend 2015 2016 2017 2018
   No    34   34   33    0   # also shows how the `date` became the 2nd split
   Yes   45   32   32   63