我使用 RWeka
包中的 J48()
函数,已经得到了 churn
数据集的 decision tree
。由于树非常大,我无法看到整个树。我想把它输出到文本文件中,但格式会发生变化。如何保存它并保留树的格式呢?
save(m2,file="thisexample.txt", ascii=TRUE)
m2
是存储 J48
树输出的 dataframe
。
回答:
I. 使用 iris
数据集和 RWeka
的 J48()
函数的示例。
library(RWeka) result = J48(Species~.,data=iris) result # J48 pruned tree # ------------------ # Petal.Width <= 0.6: setosa (50.0) # Petal.Width > 0.6 # | Petal.Width <= 1.7 # | | Petal.Length <= 4.9: versicolor (48.0/1.0) # | | Petal.Length > 4.9 # | | | Petal.Width <= 1.5: virginica (3.0) # | | | Petal.Width > 1.5: versicolor (3.0/1.0) # | Petal.Width > 1.7: virginica (46.0/1.0) # Number of Leaves : 5 # Size of the tree : 9
II. 使用 sink()
函数将其写入文本文件
sink("result.txt") print (result) sink()
III. 打开保存在当前工作目录中的 result.txt
文件。