我在 R 中有一个稀疏矩阵
我现在希望对 R 执行非负矩阵分解
data.txt 是我使用 python 创建的文本文件,它由 3 列组成,其中第一列指定行号,第二列指定列号,第三列指定值
数据.txt
1 5 10
3 2 5
4 6 9
原始 data.txt 包含 164009 行,这是 250000x250000 稀疏矩阵的数据我使用了 NMF 库,我正在做
x=scan('data.txt',what=list(integer(),integer(),numeric()))
library('Matrix')
R=sparseMatrix(i=x[[1]],j=x[[2]],x=x[[3]])
res<-nmf(R,3)
它给了我一个错误:Error in function (classes, fdef, mtable): unable to find an inherited method for function nmf, for signature "dgCMAtrix", "missing", "missing"
谁能帮我弄清楚我做错了什么?
最佳答案
第一个问题是您向 nmf 提供了一个 dgCMatrix。
> class(R)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
帮助在这里:
help(nmf)
请参阅方法部分。它需要一个实数矩阵。由于条目的数量,使用 as.matrix 进行强制可能对您没有多大帮助。
现在,即使使用您的示例数据,对矩阵的强制也不够写:
> nmf(as.matrix(R))
Error: NMF::nmf : when argument 'rank' is not provided, argument 'seed' is required to inherit from class 'NMF'. See ?nmf.
让我们给它一个排名:
> nmf(as.matrix(R),2)
Error in .local(x, rank, method, ...) :
Input matrix x contains at least one null row.
确实如此:
> R
4 x 6 sparse Matrix of class "dgCMatrix"
[1,] . . . . 10 .
[2,] . . . . . .
[3,] . . 5 . . .
[4,] . . . . . 9
关于r - 在 R 中执行非负矩阵分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255336/