楼主: stonechat
2385 3

[程序分享] R code for Generate Rules for randomForest [推广有奖]

  • 0关注
  • 0粉丝

已卖:16份资源

博士生

48%

还不是VIP/贵宾

-

威望
0
论坛币
94 个
通用积分
6.2205
学术水平
1 点
热心指数
1 点
信用等级
0 点
经验
4105 点
帖子
76
精华
0
在线时间
534 小时
注册时间
2010-8-5
最后登录
2025-3-2

楼主
stonechat 发表于 2014-12-18 14:25:30 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
#**************************
#return the rules of a tree
#**************************
getConds<-function(tree){
  #store all conditions into a list
  conds<-list()
  #start by the terminal nodes and find previous conditions
  id.leafs<-which(tree$status==-1)
          j<-0
          for(i in id.leafs){
                j<-j+1
                prevConds<-prevCond(tree,i)
                conds[[j]]<-prevConds$cond
                while(prevConds$id>1){
                  prevConds<-prevCond(tree,prevConds$id)
                  conds[[j]]<-paste(conds[[j]]," & ",prevConds$cond)
                  if(prevConds$id==1){
                        conds[[j]]<-paste(conds[[j]]," => ",tree$prediction[i])
        break()
      }
    }

  }

  return(conds)
}

#**************************
#find the previous conditions in the tree
#**************************
prevCond<-function(tree,i){
  if(i %in% tree$right_daughter){
                id<-which(tree$right_daughter==i)
                cond<-paste(tree$split_var[id],">",tree$split_point[id])
          }
          if(i %in% tree$left_daughter){
    id<-which(tree$left_daughter==i)
                cond<-paste(tree$split_var[id],"<",tree$split_point[id])
  }

  return(list(cond=cond,id=id))
}

#remove spaces in a word
collapse<-function(x){
  x<-sub(" ","_",x)

  return(x)
}


data(iris)
require(randomForest)
mod.rf <- randomForest(Species ~ ., data=iris)
tree<-getTree(mod.rf, k=1, labelVar=TRUE)
#rename the name of the column
colnames(tree)<-sapply(colnames(tree),collapse)
rules<-getConds(tree)
print(rules)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:randomForest generate Forest random Rules return

已有 1 人评分学术水平 热心指数 收起 理由
ryoeng + 1 + 1 对论坛有贡献

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

沙发
DM小菜鸟 发表于 2015-1-15 20:38:30
感谢分享

藤椅
引擎子 发表于 2015-1-17 10:54:21
谢谢分享

板凳
mlz_1987 发表于 2015-7-15 17:13:34
菜鸟求教啊,这程序我复制过去没法运行,有哪位整理好没,跪求啊

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 08:21