稀疏的矩阵尺寸与常规矩阵大小

发布于 2025-01-21 10:22:52 字数 139 浏览 1 评论 0原文

我的常规矩阵具有对象大小416字节,当我使用为(,“ sparsematrix”)将其变成稀疏矩阵时,此稀疏矩阵的大小将达到1720字节。

正常吗?我们不应该期望稀疏矩阵比常规矩阵更小的存储尺寸吗?

非常感谢!

My regular matrix has object size 416 bytes, and when I use as(, "sparseMatrix") to turn it into sparse matrix, then the size for this sparse matrix goes up to 1720 bytes.

Is it normal? Shouldn't we expect a smaller storage size for the sparse matrix than the regular one?

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2025-01-28 10:22:52

矩阵是R的基本数据结构之一,并且可以用很少的元数据存储:它是一个值序列,每个维度仅为每个维度和数据类型。

sparsematrix对象中包含更多的元数据,因为您将在下面的示例中看到str()。最突出的是,对于每个非零值AN(x,y)位置除了值本身外,还存储了位置。如果您存储整数,则仅此一项就会导致记忆使用的三倍。仅当有许多零值时,这才能得到补偿,因为它们根本没有存储。

密集的示例

比较矩阵的零值:

> mat1 = matrix( sample(3*3), c(3, 3))
> smat1 <- as(mat1, "sparseMatrix")

> showMem(c('mat1', 'smat1'), bytes=T)
        size bytes
mat1   264 B   264
smat1 1.7 kB  1688

> mat1
     [,1] [,2] [,3]
[1,]    2    5    7
[2,]    8    6    1
[3,]    3    4    9

> str(mat1)
 int [1:3, 1:3] 2 8 3 5 6 4 7 1 9

> str(smat1)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:9] 0 1 2 0 1 2 0 1 2
  ..@ p       : int [1:4] 0 3 6 9
  ..@ Dim     : int [1:2] 3 3
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:9] 2 8 3 5 6 4 7 1 9
  ..@ factors : list()

或这样的矩阵的较大版本:

> mat2 = matrix( sample(1000*1000), c(1000, 1000))
> smat2 <- as(mat2, "sparseMatrix")

> showMem(c('mat2', 'smat2'), bytes=T)
       size    bytes
mat2   4 MB  4000216
smat2 12 MB 12005504

稀疏示例

在这里我们创建一个更稀疏的矩阵,只有6个零,只有3个值。我们可以看到Sparsematrix仅存储3个值。

> mat3 = matrix( sample(3*3)%%3%%2, c(3, 3))
> smat3 <- as(mat3, "sparseMatrix")

> showMem(c('mat3', 'smat3'), bytes=T)
        size bytes
mat3   344 B   344
smat3 1.6 kB  1560

> mat3
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    0    0    0
[3,]    1    0    1

> str(mat3)
 num [1:3, 1:3] 0 0 1 1 0 0 0 0 1

> str(smat3)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:3] 2 0 2
  ..@ p       : int [1:4] 0 1 2 3
  ..@ Dim     : int [1:2] 3 3
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:3] 1 1 1
  ..@ factors : list()

最后,Sparsematrix可以节省预期的内存:

> mat4 = matrix( sample(1000*1000)%%3%%2, c(1000, 1000))

> smat4 <- as(mat4, "sparseMatrix")

> table(mat4)
mat4
     0      1 
666666 333334 

> showMem(c('mat4', 'smat4'), bytes=T)
      size   bytes
mat4  8 MB 8000216
smat4 4 MB 4005512

matrix is one of the base data structures of R, and can be stored with very little metadata: it is a sequence of values with just a length for each dimension, and a data type.

A sparseMatrix object however contains more metadata, as you'll see with str() in the examples below. Most prominently, for each non-zero value an (x,y) position is stored in addition to the value itself. This alone will cause a threefold increase in memory use, if you're storing integers. This is only compensated when there are many zero values, as they are not stored at all.

Dense example

Compare for a matrix with no zero values:

> mat1 = matrix( sample(3*3), c(3, 3))
> smat1 <- as(mat1, "sparseMatrix")

> showMem(c('mat1', 'smat1'), bytes=T)
        size bytes
mat1   264 B   264
smat1 1.7 kB  1688

> mat1
     [,1] [,2] [,3]
[1,]    2    5    7
[2,]    8    6    1
[3,]    3    4    9

> str(mat1)
 int [1:3, 1:3] 2 8 3 5 6 4 7 1 9

> str(smat1)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:9] 0 1 2 0 1 2 0 1 2
  ..@ p       : int [1:4] 0 3 6 9
  ..@ Dim     : int [1:2] 3 3
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:9] 2 8 3 5 6 4 7 1 9
  ..@ factors : list()

Or a larger version of such a matrix:

> mat2 = matrix( sample(1000*1000), c(1000, 1000))
> smat2 <- as(mat2, "sparseMatrix")

> showMem(c('mat2', 'smat2'), bytes=T)
       size    bytes
mat2   4 MB  4000216
smat2 12 MB 12005504

Sparse example

Here we create a more sparse matrix, with 6 zeroes and only 3 values. We can see that the sparseMatrix only stores the 3 values.

> mat3 = matrix( sample(3*3)%%3%%2, c(3, 3))
> smat3 <- as(mat3, "sparseMatrix")

> showMem(c('mat3', 'smat3'), bytes=T)
        size bytes
mat3   344 B   344
smat3 1.6 kB  1560

> mat3
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    0    0    0
[3,]    1    0    1

> str(mat3)
 num [1:3, 1:3] 0 0 1 1 0 0 0 0 1

> str(smat3)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:3] 2 0 2
  ..@ p       : int [1:4] 0 1 2 3
  ..@ Dim     : int [1:2] 3 3
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:3] 1 1 1
  ..@ factors : list()

And finally a case where the sparseMatrix gives the expected memory savings:

> mat4 = matrix( sample(1000*1000)%%3%%2, c(1000, 1000))

> smat4 <- as(mat4, "sparseMatrix")

> table(mat4)
mat4
     0      1 
666666 333334 

> showMem(c('mat4', 'smat4'), bytes=T)
      size   bytes
mat4  8 MB 8000216
smat4 4 MB 4005512
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文