gpt4 book ai didi

r - 如何将 DocumentTermMatrix(tm 包)转换为 R 中的稀疏矩阵?

转载 作者:行者123 更新时间:2023-12-03 18:03:28 24 4
gpt4 key购买 nike

我用过 tm包装和 DocumentTermMatrix创建一个 DocumentTermMatrix现在我想将其转换为备用矩阵以输出到 glmnet函数来自 glmnet包裹。

关于如何做到这一点的任何想法?
对象如下所示:

> str(yy)
List of 6
$ i : int [1:13864810] 2 2 2 2 2 2 2 2 2 2 ...
$ j : int [1:13864810] 320 334 339 346 347 348 355 360 362 363 ...
$ v : num [1:13864810] 1 1 1 1 1 1 1 1 1 1 ...
$ nrow : int 709678
$ ncol : int 371
$ dimnames:List of 2
..$ Docs : chr [1:709678] "1" "2" "3" "4" ...
..$ Terms: chr [1:371] "declarative_" "declarative_0" "declarative_0zc" "declarative_0zd" ...
- attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
- attr(*, "weighting")= chr [1:2] "term frequency" "tf"
> class(yy)
[1] "DocumentTermMatrix" "simple_triplet_matrix"

这是唯一的方法吗?
sparseYY <- sparseMatrix( i = yy$i, j=yy$j, x =yy$v)

最佳答案

只需使用 as.matrix转换为 稀疏矩阵 :

> dtm_matrix <- as.matrix(dtm)
> class(dtm_matrix)
[1] "matrix"

关于r - 如何将 DocumentTermMatrix(tm 包)转换为 R 中的稀疏矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com