R中的稀疏(dgCMatrix)矩阵行归一化
我有一个很大的稀疏矩阵,称之为p:R中的稀疏(dgCMatrix)矩阵行归一化,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,我有一个很大的稀疏矩阵,称之为p: > str(P) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:7868093] 4221 6098 8780 10313 11102 14243 20570 22145 24468 24977 ... ..@ p : int [1:7357] 0 0 269 388 692 2434 3662 4179 4205
> str(P)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:7868093] 4221 6098 8780 10313 11102 14243 20570 22145 24468 24977 ...
..@ p : int [1:7357] 0 0 269 388 692 2434 3662 4179 4205 4256 ...
..@ Dim : int [1:2] 1303967 7356
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ x : num [1:7868093] 1 1 1 1 1 1 1 1 1 1 ...
..@ factors : list()
我希望行正常化(比如,使用L-2范数)。。。(利用矢量循环)直接方法类似于:
> row_normalized_P <- P / rowSums(P^2)
>row\u normalized\u P我想出了一个很好的解决方案(像往常一样,发布后大约15分钟:-/)
>row\u normalized\u P我想出了一个很好的解决方案(像往常一样,发布后大约15分钟:-/)
>row\u normalized\u P对于列规格化,请记住交换乘法顺序:P%*%Matrix::Diagonal(x=1/sqrt(Matrix::colSums(P^2))
对于列规格化,请记住交换乘法顺序:P%*%Matrix::Diagonal(x=1/sqrt(Matrix::colSums(P^2))
> row_normalized_P <- Matrix::Diagonal(x = 1 / sqrt(Matrix::rowSums(P^2))) %*% P