【发布时间】:2015-05-27 03:41:36
【问题描述】:
我正在尝试创建一个随机森林多类分类器。但是,当我执行我的代码时,我在调用 RandomForest 时收到以下错误:
结果$training_classes 中的错误: $ 运算符对原子向量无效 调用:randomForest ... eval -> model.frame -> model.frame.default -> eval -> eval 执行停止
这是我的代码:
#!/usr/bin/Rscript
library(randomForest);
library(tm)
training_classes <- readLines("incidents_training_classes.txt",)
training_data <- readLines("incidents_training_words.txt")
doc.vec <- VectorSource(training_data)
doc.corpus <- Corpus(doc.vec)
summary(doc.corpus)
doc.corpus <- tm_map(doc.corpus, removeNumbers)
doc.corpus <- tm_map(doc.corpus, removeWords, stopwords("english"))
DTM <- DocumentTermMatrix(doc.corpus, control=list(weighting=weightTfIdf,minWordLength=3))
DTM.common <- removeSparseTerms(DTM,.99)
# Convert DTM to a data frame
capture.output(DTM.dataframe <- as.data.frame(inspect(DTM.common))) -> .null
class.dataframe <- as.data.frame(training_classes, stringsAsFactors=TRUE)
# Merge predictor variables and class labels
results <- cbind(DTM.dataframe,class.dataframe)
forest.rf <- randomForest( results$training_classes ~ .,importance=TRUE, data = results, ntree = 500);
结果数据框具有预期的尺寸。我倒在了谷歌上,没有任何运气。
非常感谢任何帮助。
【问题讨论】:
-
R
formula(正确使用)中很少有“$”。str(results)显示什么? -
这是我的结构调用的顶部:
The metadata consists of 2 tag-value pairs and a data frame Available tags are: create_date creator Available variables in the data frame are: MetaID 'data.frame': 8271 obs. of 813 variables: $ able : num 0 0 0.0171 0 0 ... $ accept : num 0 0 0 0 0 ... $ access : num 0 0 0 0 0 0 0 0 0 0 ... $ accessing : num 0 0 0 0 0 0 0 0 0 0 ... $ account : num 0 0 0 0 0 0 0 0 0 0 ... -
这里是 str() 调用的底部:
$ yes : num 0 0 0 0 0 0 0 0 0 0 ... $ yesterday : num 0 0 0 0 0 0 0 0 0 0 ... $ yet : num 0 0 0 0 0 0 0 0 0 0 ... $ training_classes: Factor w/ 8 levels "B","C","D","G",..: 8 7 8 7 2 5 7 8 7 2 ... -
在我看来,“结果”对象比数据框更复杂。它不适合作为 randomForest 数据参数的参数。
-
is.data.frame(result) 的输出为:[TRUE]。我之前没有使用过文档术语矩阵。我通常使用 sed/awk 等来清理数据,然后将文本作为表格读取。在这种情况下,我使用的是 DTM。据我所知,DTM 不能包含类标签。
标签: r