Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SciPy Sparse Matrix

SciPy Sparse Matrix

A sparse matrix refers to a matrix in which the vast majority of values are zero in numerical analysis. Conversely, if most elements are non-zero, the matrix is dense.

Large sparse matrices frequently appear when solving linear models in science and engineering.

In the figure above, the left side is a sparse matrix containing many zero elements, while the right side is a dense matrix whose elements are mostly non-zero.

Here is a simple example:

The sparse matrix above contains only 9 non-zero elements and 26 zero elements. Its sparsity is 74% and its density is 26%.

SciPy's scipy.sparse module provides functions for working with sparse matrices.

We mainly use the following two types of sparse matrices:

In this chapter we mainly use CSR matrices.

CSR Matrix

We can create a CSR matrix by passing an array to the scipy.sparse.csr_matrix() function.

Example

Create a CSR matrix.

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

0

,

0

,

0

,

0

,

0

,

1

,

1

,

0

,

2

]

)

print

(

csr_matrix

(

arr

)

)

The output of the code above is:



  (0, 5)        1

  (0, 6)        1

  (0, 8)        2

Result analysis:

CSR Matrix Methods

We can use the data attribute to view the stored data (excluding zero elements):

Example

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

[

0

,

0

,

0

]

,

[

0

,

0

,

1

]

,

[

1

,

0

,

2

]

]

)

print

(

csr_matrix

(

arr

)

.

data

)

The output of the code above is:



[1 1 2]

Use the count_nonzero() method to count the total number of non-zero elements:

Example

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

[

0

,

0

,

0

]

,

[

0

,

0

,

1

]

,

[

1

,

0

,

2

]

]

)

print

(

csr_matrix

(

arr

)

.

count_nonzero

(

)

)

The output of the code above is:



3

Use the eliminate_zeros() method to remove zero elements from the matrix:

Example

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

[

0

,

0

,

0

]

,

[

0

,

0

,

1

]

,

[

1

,

0

,

2

]

]

)

mat

=

csr_matrix

(

arr

)

mat.

eliminate_zeros

(

)

print

(

mat

)

The output of the code above is:



  (1, 2)    1

  (2, 0)    1

  (2, 2)    2

Use the sum_duplicates() method to remove duplicates:

Example

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

[

0

,

0

,

0

]

,

[

0

,

0

,

1

]

,

[

1

,

0

,

2

]

]

)

mat

=

csr_matrix

(

arr

)

mat.

sum_duplicates

(

)

print

(

mat

)

The output of the code above is:



  (1, 2)    1

  (2, 0)    1

  (2, 2)    2

To convert CSR to CSC, use the tocsc() method:

Example

import

numpy

as

np

from

scipy.

sparse

import

csr_matrix

arr

=

np.

array

(

[

[

0

,

0

,

0

]

,

[

0

,

0

,

1

]

,

[

1

,

0

,

2

]

]

)

newarr

=

csr_matrix

(

arr

)

.

tocsc

(

)

print

(

newarr

)



  (2, 0)    1

  (1, 2)    1

  (2, 2)    2