首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将DocumentTermMatrix转换为dgTMatrix

将DocumentTermMatrix转换为dgTMatrix
EN

Stack Overflow用户
提问于 2018-04-14 20:12:44
回答 1查看 969关注 0票数 3

我试图通过AssociatedPress的LDA实现从tm-package运行text2vec数据集。

我面临的问题是数据类型的不兼容性:AssociatedPress是一个tm::DocumentTermMatrix,而后者又是slam::simple_triplet_matrix的一个子类。然而,text2vec期望输入xtext2vec::lda$fit_transform(x = ...)Matrix::dgTMatrix

因此,我的问题是:有没有办法强迫DocumentTermMatrix接受text2vec接受的东西?

最小(失败)示例:

代码语言:javascript
复制
library('tm')
library('text2vec')

data("AssociatedPress", package="topicmodels")

dtm <- AssociatedPress[1:10, ]

lda_model = LDA$new(
  n_topics = 10,
  doc_topic_prior = 0.1,
  topic_word_prior = 0.01
)

doc_topic_distr =
  lda_model$fit_transform(
    x = dtm,
    n_iter = 1000,
    convergence_tol = 0.001,
    n_check_convergence = 25,
    progressbar = FALSE
  )

...which给出:

基::rowSums(x,na.rm = na.rm,dims = dims,.):'x‘必须是至少两个维度的数组

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-15 08:51:21

答案在@Dmitriy提供的duplicate中。但是它并没有提到它来自于基本包Matrix

由于我没有安装topicmodels,所以我将使用包含在tm包中的crude数据集。原则是一样的。

代码语言:javascript
复制
library(tm)
data("crude")

dtm <- DocumentTermMatrix(crude,
                          control = list(weighting =
                                           function(x)
                                             weightTfIdf(x, normalize =
                                                           FALSE),
                                         stopwords = TRUE))

# transform into a sparseMatrix dgcMatrix
m <-  Matrix::sparseMatrix(i=dtm$i, 
                           j=dtm$j, 
                           x=dtm$v, 
                           dims=c(dtm$nrow, dtm$ncol),
                           dimnames = dtm$dimnames)
str(m)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:1890] 6 1 18 6 6 5 9 12 9 5 ...
  ..@ p       : int [1:1201] 0 1 2 3 4 5 6 8 9 11 ...
  ..@ Dim     : int [1:2] 20 1200
  ..@ Dimnames:List of 2
  .. ..$ Docs : chr [1:20] "127" "144" "191" "194" ...
  .. ..$ Terms: chr [1:1200] "\"(it)" "\"demand" "\"expansion" "\"for" ...
  ..@ x       : num [1:1890] 4.32 4.32 4.32 4.32 4.32 ...
  ..@ factors : list()

其余代码:

代码语言:javascript
复制
library(text2vec)

lda_model <- LDA$new(
  n_topics = 10,
  doc_topic_prior = 0.1,
  topic_word_prior = 0.01
)

doc_topic_distr <-
  lda_model$fit_transform(
    x = m,
    n_iter = 1000,
    convergence_tol = 0.001,
    n_check_convergence = 25,
    progressbar = FALSE
  )

INFO [2018-04-15 10:40:00] iter 25 loglikelihood = -32949.882
INFO [2018-04-15 10:40:00] iter 50 loglikelihood = -32901.801
INFO [2018-04-15 10:40:00] iter 75 loglikelihood = -32922.208
INFO [2018-04-15 10:40:00] early stopping at 75 iteration
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49835762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档