SciPy Learning in Python--random sparse matrices and operations

Source: Internet
Author: User
Tags random seed

1. generate a random sparse matrix :

The functions of generating random sparse matrices in scipy are as follows:

scipy.sparse.rand(m,n,density,format,dtype,random_state)
    • 1

Parameter description:

Parameters meaning
M,n An integer; a row and column that represents a matrix
Density A real type; the sparsity of a matrix.
Format STR type; the type of the matrix; such as format= ' COO '
Dtype Dtype a type that returns a matrix value
Ranom_state {numpy.random.randomstate,int}; optional random seed; if empty, default Numpy.random

Example

The code is as follows:

Importscipy as Spyn=4m=4density=0.5Matrixformat='COO'B=spy.sparse.rand (m,n,density=density,format=matrixformat,dtype=None)Print(B)>>> (1, 1) 0.0687198939788  (3, 3) 0.141328654998(0,3) 0.944468193258  (2, 3) 0.598652789611(0,2) 0.0629165518906  (2, 0) 0.624087894456  (1, 2) 0.309460820898  (2, 2) 0.731375305002

2. Operation of sparse matrices:

Importscipy as Spyn=4m=4Row=spy.array ([0,0,0,1,1,3,3]) Col=spy.array ([0,0,1,2,3,2,3]) value=spy.array ([1,2,1,8,1,3,5])Print('Customize the sparse matrix to generate a CSC format :')#the ' COO ' format matrix cannot perform some of the following actionsA=spy.sparse.csc_matrix ((Value, (Row,col)), shape=(n,m))Print('non-sparse representation of sparse matrices ...')Print(A.todense ())Print('non-0 element corresponding coordinate of sparse matrix ...') Nonzero=A.nonzero ()Print(nonzero)Print('outputs a non-0 element corresponding to the row and column coordinates ...')Print(nonzero[0])Print(nonzero[1])Print('output line I not 0 value ...') I=2Print(A[i,:])Print('output column J not 0 value ...') J=2Print(A[:,j])Print('the output coordinate is (I,J) the corresponding value ...')Print(A[i,j])

The output results are as follows:

Customize the sparse matrix to generate a CSC format: Non-sparse representation of sparse matrices ... [[3 18 13 51, 1, 3, 3], dtype=int32), array ([0, 1, 2, 3, 2, 3], dtype=1 1 3 3 1 2 3 2 3] Output line I non-0 value ... Output column J not 0 value  ... (1, 0)        8  (3, 0)        3 The output coordinate is (I,J) the corresponding value ... 0
Note: For more information, see docs.scipy.org

SciPy Learning in Python--random sparse matrices and operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.