【发布时间】:2018-07-03 17:34:14
【问题描述】:
我正在尝试为学校项目运行 randomForest。我正在尝试构建一个测试分类器,它根据一些文本预测一个类别(列标签)。
目前我被困住了,因为我的文档术语矩阵似乎有问题。 这是错误:
> rfmodel <- randomForest(df$label, data = events_dtm)
Error in if (n == 0) stop("data (x) has 0 rows") :
argument is of length zero
这就是代码当前的样子。数据具有代表性。
library(tidyverse)
library(tidytext)
library(stringr)
library(caret)
library(tm)
library(dplyr)
library(randomForest)
text = c("this is a random text",
"another rnd text",
"hi there",
"not so rnd",
"what's that?",
"kinda boring",
"this is a random text",
"another rnd text",
"hi there",
"not so rnd",
"what's that?",
"kinda boring")
label = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
df <- data.frame(text= text, label=label)
df$label <- as.factor(df$label)
df$text <- as.character(df$text)
df$ID <- seq.int(nrow(df))
df <- df[1:5,]
as_tibble(df) %>%
mutate(text = as.character(text)) -> type
data("stop_words")
type %>%
unnest_tokens(output = word, input = text) %>%
anti_join(stop_words) %>%
mutate(word = SnowballC::wordStem(word)) -> type_tokens
type_tokens %>%
count(ID, word) %>%
cast_dtm(document = ID, term = word, value = n,
weighting = weightTfIdf) -> type_dtm
print(type_dtm)
rfmodel <- randomForest(df$label, data = type_dtm)
print(rfmodel)
dfT <- data.frame(text= text)
dfT$ID <- seq.int(nrow(dfT))
as_tibble(dfT) %>%
mutate(text = as.character(text)) -> typeT
typeT %>%
unnest_tokens(output = word, input = text) -> typeT
typeT %>%
count(ID, word) %>%
cast_dtm(document = ID, term = word, value = n,
weighting = weightTfIdf) -> typeT
pred_test <- predict(rfmodel, newdata = dfT, type = "class")
print(pred_test)
由于我对随机森林和 R 都很陌生,因此可能存在概念上的错误。 知道如何解决这个问题吗?
【问题讨论】:
标签: r text-mining random-forest