Scipy: Key Explanations

I do not distinguish between coo_matrix, csr_matrixand csc_matrix.

The documentation mentions that coo_matrix is ​​not efficient for arithmetic operations, and we need to convert it to csror csc. I look more at matrix multiplication. And I did not understand what was going on behind the scenes, if I only had to coo_matrixconvert it to a matrix csror csv.

Also, if I have something like

A = array([[1,2,3,0,0,5],
        [5,0,0,1,2,0]])
print coo_matrix(A)

He is typing

  (0, 0)    1
  (0, 1)    2
  (0, 2)    3
  (0, 5)    5

which is great. but is there a way, I can directly enter my matrix as printed. Something like defining a COO matrix zero, and then start defining values coo_matrix, as is done in matlab.

Thank!

+5
source share
3 answers

. coo_matrix, csr_matrix csc_matrix - . coo_matrix - , , . , , , . . csr_matrix csc_matrix . , , csr csc 1-D , , ( csr), , . , .

. scipy.sparse.dok_matrix. . MATLAB, csr csc . :

>>> A = scipy.sparse.dok_matrix((5,5))
>>> A[2,3] = 7
>>> print A
  (2, 3)      7.0
+1

, - :

row  = np.array([0,0,0,0])
col  = np.array([0,1,2,5])
data = np.array([1,2,3,5])
coo_matrix((data,(row,col))).todense()

:

matrix([[1, 2, 3, 0, 0, 5]])

scipy

0

python scipy,

, .
:

  • , , DOK ( ), LIL ( ) COO ( ). .
  • , , CSR ( ) CSC ( ).

(COO)

COO (, , ). ( , ), . ,

(CSR)

(CSR) (CRS) M () , , . .

CSR m × n- M () (A, IA, JA). NNZ M. ( , .)

A NNZ M ( "-" ).

  • IA m + 1. :
    IA [0] = 0
    IA [i] = IA [i - 1] + ( (i - 1) - )

, m IA M, IA [m] NNZ, A, phantom M.
i- A [IA [i ]] A [IA [i + 1] - 1] ( ), .

JA M A , , NNZ.

For example, the matrix 0 0 0 0
5 8 0 0
0 0 3 0
0 6 0 0

is a 4 × 4 matrix with 4 nonzero elements, therefore

A = [5 8 3 6]
IA = [0 0 2 3 4]
JA = [0 1 2 1]

Source: https://en.wikipedia.org/wiki/Sparse_matrix

0
source

All Articles