A simple example
>library(partykit)
> partykit:::.list.rules.party(ctree(Petal.Length~.,data=iris))
2
"Petal.Width <= 0.6"
6
"Petal.Width > 0.6 & Sepal.Length <= 6.2 & Petal.Width <= 1.3 & Sepal.Length <= 5.5"
7
"Petal.Width > 0.6 & Sepal.Length <= 6.2 & Petal.Width <= 1.3 & Sepal.Length > 5.5"
....
For example, in the second rule, the two occurrences of Sepal.Length
can be consolidated into Sepal.Length<=5.5
So, is there a way to consolidate the rules?
In the plot of the tree below, on the way to node 6 (the node whose rules you reference in your question) we first keep only points with Petal.Width
> 0.6. But even then node 6 doesn't include all remaining points with Sepal.Length
<= 5.5, but only those that also have Petal.Width
<= 1.3. In other words, there's an intervening Petal.Width
split between the two Sepal.Length
splits, so the first Sepal.Length
split isn't redundant.
m1 = ctree(Petal.Length~.,data=iris)
plot(m1)
There my be a more efficient way, but this functions may give you what you want:
consolidate_rules <- function(tree){
split.vars <- colnames(tree$node$info$criterion)
split <- partykit:::.list.rules.party(tree)
new.split <- c()
for(i.split in seq_along(split)) {
for (i.split.var in split.vars) {
x0 <- split[i.split]
x1 <- strsplit(x0, " & ")
x2 <- grep(i.split.var, x1[[1]], value = TRUE)
x3l <- strsplit(grep("<=", x2, value = TRUE), " <= ") # lower than
x3g <- strsplit(grep(">", x2, value = TRUE), " > ") # greater
x3e <- strsplit(grep(" %in% ", x2, value = TRUE), "%in%") # elements
x4 <- c()
if (length(x3e) != 0) {
b <- sapply(x3e, "[[", 2)
b1 <- gsub('"', '', b)
b2 <- gsub("[c( )]", "", b1)
b3 <- gsub("(NA,)|(,NA)", "", b2)
b4 <- unique(strsplit(paste0(b3, collapse = ","), ",")[[1]])
x4 <- paste0(i.split.var, ' %in% c("',
paste0(b4, collapse = '", "'),'")')
}
if (length(x3l) != 0) {
x4 <- paste0(i.split.var, " <= ",
min(as.numeric(sapply(x3l, "[[", 2))))
}
if (length(x3g) != 0) {
x4 <- paste0(x4, ifelse(length(x4) > 0 ," & ",""),
i.split.var, " > ",
max(as.numeric(sapply(x3g, "[[", 2))))
}
tmp <- paste0(if(!is.null(new.split[i.split]) &&
!is.na(new.split[i.split]) &
length(x4) >0) {" & "}, x4)
new.split[i.split] <-
paste0(if(!is.null(new.split[i.split]) &&
!is.na(new.split[i.split])) {new.split[i.split]},
tmp)
rm(x0, x1, x2, x3l, x3g, x3e, x4)
}
}
names(new.split) <- names(split)
return(new.split)
}
You can call the function with:
ct <- ctree(Petal.Length~.,data=iris)
consolidate_rules(ct)
For the node 6 the result looks like this:
6
"Sepal.Length <= 5.5 & Petal.Width <= 1.3 & Petal.Width > 0.6"
As the result is "just" a string with the rules, I don't know if you can use it in the same way as a .list.rules.party
object.
But i hope this mioght help you.
A simpler version:
"Petal.Width > 0.6 & Sepal.Length <= 6.2 & Petal.Width <= 1.3 & Sepal.Length <= 5.5" %>%
str_split(' & ') %>% unlist() %>% str_split(' ') %>%
lapply(function(x) data.frame(var = x[1], cond = x[2], value = tail(x, -2) %>% paste(collapse = ' '))) %>% bind_rows() %>%
group_by(var, cond) %>%
filter(
if (str_detect(unique(cond), '<')) 1:n() == which.min(as.numeric(value))
else if (str_detect(unique(cond), '>')) 1:n() == which.max(as.numeric(value))
else 1:n() == which.min(str_count(value, ','))
) %>%
apply(1, paste, collapse = ' ') %>% paste(collapse = ' & ')
[1] "Petal.Width > 0.6 & Petal.Width <= 1.3 & Sepal.Length <= 5.5"
It works by splitting the rule using &
as marker and then split again each element (eg.: Petal.Width > 0.6
) into its three components (eg, the variable Petal.Width
, the condition >
and the value 0.6
). I make everything into a dataframe, group by variable and condition and then choose the right element according to the condition. Finally I collapse first by row and then again in one string.
I come up with it today, so I didn't test it thoroughly yet but should work. It requires the dplyr
and stringr
packages. Note that this code works on a single rule, but you can use it with vectors of strings with sapply()
.
来源:https://stackoverflow.com/questions/44296667/consolidate-party-rules