【问题标题】:R: how to map test data into lsa space created by training dataR:如何将测试数据映射到由训练数据创建的 lsa 空间
【发布时间】:2016-08-25 05:16:26
【问题描述】:

我正在尝试使用 LSA 进行文本分析。我在 StackOverflow 上阅读了许多其他关于 LSA 的帖子,但我还没有找到与我的类似的帖子。如果你知道有一个和我类似的,请把我重定向到它!非常感谢!

这是我创建的示例数据的可重现代码:

创建样本数据训练和测试集

sentiment = c(1,1,0,1,0,1,0,0,1,0)
length(sentiment) #10
text = c('im happy', 'this is good', 'what a bummer X(', 'today is kinda okay day for me', 'i somehow messed up big time', 
         'guess not being promoted is not too bad :]', 'stayhing home is boring :(', 'kids wont stop crying QQ', 'warriors are legendary!', 'stop reading my tweets!!!')
train_data = data.table(as.factor(sentiment), text)
> train_data
    sentiment                                text
 1:  1                                   im happy
 2:  1                               this is good
 3:  0                           what a bummer X(
 4:  1             today is kinda okay day for me
 5:  0               i somehow messed up big time
 6:  1 guess not being promoted is not too bad :]
 7:  0                 stayhing home is boring :(
 8:  0                   kids wont stop crying QQ
 9:  1                    warriors are legendary!
10:  0                  stop reading my tweets!!!

sentiment = c(0,1,0,0)
text = c('running out of things to say...', 'if you are still reading, good for you!', 'nothing ended on a good note today', 'seriously sleep deprived!! >__<')
test_data = data.table(as.factor(sentiment), text)
> train_data
   sentiment                                    text
1:         0         running out of things to say...
2:         1 if you are still reading, good for you!
3:         0      nothing ended on a good note today
4:         0         seriously sleep deprived!! >__<

训练数据集的预处理

corpus.train = Corpus(VectorSource(train_data$text))

为训练集创建一个术语文档矩阵

tdm.train = TermDocumentMatrix(
  corpus.train,
  control = list(
    removePunctuation = TRUE,
    stopwords = stopwords(kind = "en"),
    stemming = function(word) wordStem(word, language = "english"),
    removeNumbers = TRUE, 
    tolower = TRUE,
    weighting = weightTfIdf)
)

转换成矩阵(供以后使用)

train_matrix = as.matrix(tdm.train)

使用训练数据创建一个 lsa 空间

lsa.train = lsa(tdm.train, dimcalc_share())

设置维度#(我在这里随机选择了一个,因为数据太小,无法创建肘形)

k = 6

将训练矩阵投影到新的 LSA 空间中

projected.train = fold_in(docvecs = train_matrix, LSAspace = lsa.train)[1:k,]

将以上投影数据转换成矩阵

projected.train.matrix = matrix(projected.train, 
                                nrow = dim(projected.train)[1],
                                ncol = dim(projected.train)[2])

训练随机森林模型(不知何故,这个步骤不再适用于这个小样本数据......但没关系,在这个问题上不会是一个大问题;但是,如果你也可以帮助我解决这个错误,那太棒了!我尝试用谷歌搜索这个错误,但它没有修复......)

trcontrol_rf = trainControl(method = "boot", p = .75, trim = T)
model_train_caret = train(x = t(projected.train.matrix), y = train_data$sentiment, method = "rf", trControl = trcontrol_rf)

测试数据集的预处理

基本上我在重复我对训练数据集所做的一切,除了我没有使用测试集来创建自己的 LSA 空间

corpus.test = Corpus(VectorSource(test_data$text))

为测试集创建术语文档矩阵

tdm.test = TermDocumentMatrix(
  corpus.test,
  control = list(
    removePunctuation = TRUE,
    stopwords = stopwords(kind = "en"),
    stemming = function(word) wordStem(word, language = "english"),
    removeNumbers = TRUE, 
    tolower = TRUE,
    weighting = weightTfIdf)
)

转换成矩阵(供以后使用)

test_matrix = as.matrix(tdm.test)

将测试矩阵投影到经过训练的 LSA 空间(这里是问题所在)

projected.test = fold_in(docvecs = test_matrix, LSAspace = lsa.train)

但我会得到一个错误: crossprod(docvecs, LSAspace$tk) 中的错误:参数不一致

我没有找到关于这个错误的任何有用的谷歌搜索结果......(谷歌QQ只有一个搜索结果页面) 任何帮助深表感谢!谢谢!

【问题讨论】:

    标签: r projection lsa


    【解决方案1】:

    在构建 LSA 模型时,您使用的是训练数据的词汇。但是,当您为测试数据构建 TermDocumentMatrix 时,您使用的是测试数据的词汇表。 LSA 模型只知道如何处理根据训练数据的词汇表列出的文档。

    解决此问题的一种方法是创建测试 TDM,并将 dictionary 设置为训练数据的词汇表:

    tdm.test = TermDocumentMatrix(
        corpus.test,
        control = list(
            removeNumbers = TRUE, 
            tolower = TRUE,
            stopwords = stopwords("en"),
            stemming = TRUE,
            removePunctuation = TRUE,
            weighting = weightTfIdf,
            dictionary=rownames(tdm.train)
        )
    )
    

    【讨论】:

    • 嗨,瑞安,非常感谢您的帮助!我确实尝试过使用字典,但我最终得到了一个非常小的测试集(我猜其余的都被过滤掉了)。我该如何解决这个问题?
    • TermDoucmentMatrix 转换(带或不带字典)对您的测试数据大小没有任何影响。也许您的代码中的其他地方存在错误。也许您可以更新您的示例?
    • 啊好吧!我错误地认为我得到了一套更小的套装;我实际上遇到了一个错误,但不知何故它不再存在了。不过非常感谢!
    猜你喜欢
    • 2020-03-28
    • 2017-08-27
    • 2021-12-08
    • 1970-01-01
    • 2019-10-23
    • 2017-02-20
    • 2017-04-07
    • 2020-05-05
    • 2016-10-04
    相关资源
    最近更新 更多