【问题标题】:How do you normalize the rows of a document term matrix in place in R?您如何在 R 中规范化文档术语矩阵的行?
【发布时间】:2015-06-04 22:02:56
【问题描述】:

我有一个名为 train_dtm 的 DocumentTermMatrix,我想标准化所有文档中术语频率的频率计数。我面临的问题是生成的矩阵也应该是 DocumentTermMatrix 类型,因为我想将归一化矩阵传递给 R 中 TopicModels 包的另一个方法 LDA。

下面是我使用的方法:

docs_dtm <- DocumentTermMatrix(docs)

现在,我希望对上述 documenttermmatrix 的行进行规范化。我什至尝试通过添加控制参数

docs_dtm <- DocumentTermMatrix(docs, control=list(weighting = function(x) weightTf(x, normalize=TRUE)))

但是上面的调用会抛出一个错误说

Error in weightTf(x, normalize=TRUE): unused argument (normalize = TRUE)

我已经编写了使用 apply() 方法规范化 train_dtm 值的方法,但它不返回 DocumentTermMatrix 类型的矩阵。

还有其他方法可以完成上述任务吗?

【问题讨论】:

  • 你有一个最小的工作示例吗?
  • 感谢您的回复。我在上面粘贴了我的代码。

标签: r tm topicmodels


【解决方案1】:

创建 dtm 后标准化:

docs_dtm_norm <- t(apply(docs_dtm, 1, function(x) x/sqrt(sum(x^2))))

【讨论】:

    【解决方案2】:

    您可以尝试直接传递加权参数吗,例如:

    docs_dtm <- DocumentTermMatrix(docs, control = list(weighting = weightTf, normalize = TRUE))
    

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 1970-01-01
      • 2018-11-26
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2018-04-29
      相关资源
      最近更新 更多