簡體   English   中英

如何在R中將“ RWeka”決策樹轉換為“方”樹?

[英]How do I convert an “RWeka” decision tree into a “party” tree in R?

我正在R中使用RWeka軟件包,以使用“ M5P”將M5'樹適合數據集。 然后,我想將生成的樹轉換為“派對”樹,以便可以訪問變量的重要性。 我遇到的問題是,在沒有出現以下錯誤的情況下,我似乎無法使as.party函數as.party工作:

"Error: all(sapply(split, head, 1) %in% c("<=", ">")) is not TRUE"

僅當我在for循環中應用函數時才會出現此錯誤,但是當我運行5倍交叉驗證時,for循環是必需的。

下面是我一直在運行的代碼:

n <- nrow(data)

k <- 5

indCV <- sample( rep(1:k,each=ceiling(n/k)), n)


for(i in 1:k){

#Training data is for all the observations where indCV is not equal to i

training_data <- data.frame(x[-which(indCV==i),])

training_response <- y[-which(indCV==i)]

#Test the data on the fifth of the data where the observation indices are equal to i

test_data <- x[which(indCV==i),]

test_response <- y[which(indCV==i)]

#Fit a pruned model to the training data

fit <- M5P(training_response~., data=training_data, control=Weka_control(N=TRUE))

#Convert to party

p <- as.party(fit)
}

RWeka軟件包提供了一個將M5P樹轉換為party對象的示例。 如果您運行example("M5P", package = "RWeka")那么example("M5P", package = "RWeka")可視化實際上是由partykit繪制的。 運行示例之后,請參見plot(m3)as.party(m3)

但是,盡管對於J48您可以獲得完整的constparty對象,但對於M5P並非如此。 在后一種情況下,樹結構本身可以轉換為party但是節點內的線性模型轉換為lm對象並非完全簡單。 因此,如果要使用參與party表示來計算僅取決於樹結構的度量(例如,用於拆分的變量,拆分數,拆分點等),則可以這樣做。 但是,如果您要計算依賴於模型或預測的度量(例如均方誤差等),那么party類將無濟於事。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM