scipy.sparse学习

来源:互联网 发布:国家电网南瑞集团知乎 编辑:程序博客网 时间:2024/05/18 00:11

①行压缩矩阵:scipy.sparse.csr_matrix(arg1, shape=None, dtype=None, copy=False)

构造方法:

1、通过csc_matrix(D)形式构造,其中D的维度必须小于等于2

In [1]: import  numpy as np   ...: from scipy.sparse import csr_matrix   ...: arr = np.array([[0,1,0,2,0],[1,1,0,2,0],[2,0,5,0,0]])   ...: b = csr_matrix(arr)   ...:In [2]: bOut[2]:<3x5 sparse matrix of type '<class 'numpy.int32'>'        with 7 stored elements in Compressed Sparse Row format>In [3]: type(b)Out[3]: scipy.sparse.csr.csr_matrix

csr_matrix对象属性:

In [4]: b.shapeOut[4]: (3, 5)In [5]: b.nnzOut[5]: 7In [6]: b.ndimOut[6]: 2In [7]: b.dataOut[7]: array([1, 2, 1, 1, 2, 2, 5], dtype=int32)In [8]: b.indicesOut[8]: array([1, 3, 0, 1, 3, 0, 2], dtype=int32)In [9]: b.indptrOut[9]: array([0, 2, 5, 7], dtype=int32)
nnz属性:稀疏矩阵非零元素个数

data属性:稀疏矩阵中元素

indices属性:稀疏矩阵非0元素对应的列索引值所组成数组

indptr属性:第一个元素0,之后每个元素表示稀疏矩阵中每行元素(非零元素)个数累计结果

2、通过csc_matrix((data, indices, indptr), [shape=(M, N)])形式构造

In [10]: import numpy as np    ...: from scipy.sparse import csr_matrix    ...: indptr = np.array([0,3,5,7,8])    ...: indices = np.array([0,2,4,1,3,2,4,0])    ...: data = np.array(range(1,9))    ...: csr_matrix((data,indices,indptr),shape=(4,5)).toarray()    ...:Out[10]:array([[1, 0, 2, 0, 3],       [0, 4, 0, 5, 0],       [0, 0, 6, 0, 7],       [8, 0, 0, 0, 0]])
结论:①indptr数组中最后一个元素等于data数组的长度 ②indptr数组长度减1等于矩阵的行数

③对于矩阵第i行其列索引编号:indices[indptr[i]:indptr[i+1]];对于矩阵第i行其索引列对应的数据:data[indptr[i]:indptr[i+1]]

data数组与最后稀疏矩阵转换成的数组的映射过程:

矩阵第一行:即i=0,索引编号为indices[indptr[0]:indptr[1]],其中indptr[0]=0,indptr[1]=3,则indices[indptr[0]:indptr[1]]=indices[0:3]=[0,2,4],说明矩阵的第一行的第1、3、5列为非0数据,该行其他列都为0

矩阵第一行的1、3、5列存放具体值:data[indptr[i]:indptr[i+1]]=data[0:3]=[1,2,3],表名第1列存放1、第3列存放2、第5列存放3

3、通过csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])形式构造,data表示矩阵元素,row_ind表示矩阵行编号,col_ind表示矩阵列编号

In [11]: import numpy as np    ...: from scipy.sparse import csr_matrix    ...: row = np.array([0,0,0,1,1,2,2,3])    ...: col = np.array([0,2,4,1,3,2,4,0])    ...: data = np.array(range(1,9))    ...: csr_matrix((data,(row,col)),shape=(4,5)).toarray()    ...:Out[11]:array([[1, 0, 2, 0, 3],       [0, 4, 0, 5, 0],       [0, 0, 6, 0, 7],       [8, 0, 0, 0, 0]], dtype=int32)
结论:row、col、data三数组长度相等,矩阵k行k列元素:a[row_ind[k], col_ind[k]] = data[k]

4、通过csr_matrix((M, N), [dtype])形式构建空矩阵

In [12]: import numpy as np    ...: from scipy.sparse import csr_matrix    ...: csr_matrix((4,3))    ...:Out[12]:<4x3 sparse matrix of type '<class 'numpy.float64'>'        with 0 stored elements in Compressed Sparse Row format>
5、通过csr_matrix(S)形式构建,其中S为矩阵

In [13]: import numpy as np    ...: from scipy.sparse import csr_matrix    ...: arr = np.array([0,1,0,2,3,0,5,6,0,1,0,2]).reshape(3,4)    ...: csr_matrix(np.matrix(arr))    ...:Out[13]:<3x4 sparse matrix of type '<class 'numpy.int32'>'        with 7 stored elements in Compressed Sparse Row format>



原创粉丝点击