带有插入符号问题的 Text2Vec 分类
Text2Vec classification with caret problems
一些上下文:
我一直在研究 text2vec
包和 caret
的文本多 class class 化问题。计划是使用 text2vec
来构建文档术语矩阵、修剪词汇和各种预处理的东西,然后用 caret
尝试不同的模型,但我无法像训练时那样得到结果,插入符号抛出一些如下所示的错误:
+ Fold02.Rep1: cost=0.25
predictions failed for Fold01.Rep1: cost=0.25 Error in as.vector(data) :
no method for coercing this S4 class to a vector
所有折叠和重复都会发生这种情况。我认为将 text2vec
生成的文档术语矩阵转换为向量时会出现问题,因为插入符号需要进行一些计算,但老实说我不确定,这是这个问题的主要原因。
使用的代码(有一些跳过的部分)如下所示。请注意,我向 caret
提供 text2vec
returns 的文档术语矩阵的直接结果,但我不完全确定这是否正确。
library(text2vec)
library(caret)
data("movie_review")
train = movie_review[1:4000, ]
test = movie_review[4001:5000, ]
it <- itoken(train$review, preprocess_function = tolower, tokenizer = word_tokenizer)
vocab <- create_vocabulary(it, stopwords = tokenizers::stopwords())
pruned_vocab <- prune_vocabulary(vocab, term_count_min = 10, doc_proportion_max = 0.5, doc_proportion_min = 0.001)
vectorizer <- vocab_vectorizer(pruned_vocab)
it = itoken(train$review, tokenizer = word_tokenizer, ids = train$id)
dtm_train = create_dtm(it, vectorizer)
it = itoken(test$review, tokenizer = word_tokenizer, ids = test$id)
dtm_test = create_dtm(it, vectorizer)
ctrl.svm.1 <- trainControl(method="repeatedcv",
number=10,
repeats=5,
summaryFunction = multiClassSummary,
verboseIter = TRUE)
fit.svm.1 <- train(x = dtm_train, y= as.factor(train$sentiment),
method="svmLinear2",
metric="Accuracy",
trControl = ctrl.svm.1,
scale = FALSE, verbose = TRUE)
正如我所说,问题出现在启动 train() 函数时。
dtm_train 对象属于 class:
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
结构如下所示:
str(dtm_train)
> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:368047] 2582 2995 3879 3233 2118 2416 2468 2471 3044 3669 ...
..@ p : int [1:6566] 0 0 3 4 4 10 10 14 14 22 ...
..@ Dim : int [1:2] 4000 6565
..@ Dimnames:List of 2
.. ..$ : chr [1:4000] "5814_8" "2381_9" "7759_3" "3630_4" ...
.. ..$ : chr [1:6565] "floriane" "lil" "elm" "kolchak" ...
..@ x : num [1:368047] 1 1 1 1 1 1 2 2 1 3 ...
..@ factors : list()
我做错了什么?为什么插入符号无法处理此类数据,如果在文档中它暗示能够处理此类数据?
Í如果你把你的 S4 class dtm_train 变成一个简单的矩阵,代码就可以工作了。
fit.svm.1 <- train(x = as.matrix(dtm_train), y= as.factor(train$sentiment),
method="svmLinear2",
metric="Accuracy",
trControl = ctrl.svm.1,
scale = FALSE, verbose = TRUE)
不要忘记为您的 dtm_test 做同样的事情,否则预测功能也会报错。
pred <- predict(fit.svm.1, newdata = as.matrix(dtm_test)
一些上下文:
我一直在研究 text2vec
包和 caret
的文本多 class class 化问题。计划是使用 text2vec
来构建文档术语矩阵、修剪词汇和各种预处理的东西,然后用 caret
尝试不同的模型,但我无法像训练时那样得到结果,插入符号抛出一些如下所示的错误:
+ Fold02.Rep1: cost=0.25
predictions failed for Fold01.Rep1: cost=0.25 Error in as.vector(data) :
no method for coercing this S4 class to a vector
所有折叠和重复都会发生这种情况。我认为将 text2vec
生成的文档术语矩阵转换为向量时会出现问题,因为插入符号需要进行一些计算,但老实说我不确定,这是这个问题的主要原因。
使用的代码(有一些跳过的部分)如下所示。请注意,我向 caret
提供 text2vec
returns 的文档术语矩阵的直接结果,但我不完全确定这是否正确。
library(text2vec)
library(caret)
data("movie_review")
train = movie_review[1:4000, ]
test = movie_review[4001:5000, ]
it <- itoken(train$review, preprocess_function = tolower, tokenizer = word_tokenizer)
vocab <- create_vocabulary(it, stopwords = tokenizers::stopwords())
pruned_vocab <- prune_vocabulary(vocab, term_count_min = 10, doc_proportion_max = 0.5, doc_proportion_min = 0.001)
vectorizer <- vocab_vectorizer(pruned_vocab)
it = itoken(train$review, tokenizer = word_tokenizer, ids = train$id)
dtm_train = create_dtm(it, vectorizer)
it = itoken(test$review, tokenizer = word_tokenizer, ids = test$id)
dtm_test = create_dtm(it, vectorizer)
ctrl.svm.1 <- trainControl(method="repeatedcv",
number=10,
repeats=5,
summaryFunction = multiClassSummary,
verboseIter = TRUE)
fit.svm.1 <- train(x = dtm_train, y= as.factor(train$sentiment),
method="svmLinear2",
metric="Accuracy",
trControl = ctrl.svm.1,
scale = FALSE, verbose = TRUE)
正如我所说,问题出现在启动 train() 函数时。 dtm_train 对象属于 class:
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
结构如下所示:
str(dtm_train)
> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:368047] 2582 2995 3879 3233 2118 2416 2468 2471 3044 3669 ...
..@ p : int [1:6566] 0 0 3 4 4 10 10 14 14 22 ...
..@ Dim : int [1:2] 4000 6565
..@ Dimnames:List of 2
.. ..$ : chr [1:4000] "5814_8" "2381_9" "7759_3" "3630_4" ...
.. ..$ : chr [1:6565] "floriane" "lil" "elm" "kolchak" ...
..@ x : num [1:368047] 1 1 1 1 1 1 2 2 1 3 ...
..@ factors : list()
我做错了什么?为什么插入符号无法处理此类数据,如果在文档中它暗示能够处理此类数据?
Í如果你把你的 S4 class dtm_train 变成一个简单的矩阵,代码就可以工作了。
fit.svm.1 <- train(x = as.matrix(dtm_train), y= as.factor(train$sentiment),
method="svmLinear2",
metric="Accuracy",
trControl = ctrl.svm.1,
scale = FALSE, verbose = TRUE)
不要忘记为您的 dtm_test 做同样的事情,否则预测功能也会报错。
pred <- predict(fit.svm.1, newdata = as.matrix(dtm_test)