scipy.sparse学习

scipy.sparse是scipy中的稀疏矩阵操作模块,在网上查找资料时下面这几个比较好:

1、官网

https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html

 class scipy.sparse.csr_matrix(arg1, shape=None, dtype=None, copy=False)[source]

    Compressed Sparse Row matrix

    This can be instantiated in several ways:

        csr_matrix(D)
            with a dense matrix or rank-2 ndarray D
        csr_matrix(S)
            with another sparse matrix S (equivalent to S.tocsr())
        csr_matrix((M, N), [dtype])
            to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype=’d’.
        csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
            where data, row_ind and col_ind satisfy the relationship a[row_ind[k], col_ind[k]] = data[k].
        csr_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays.

从官网上可以看到,稀疏矩阵能实例操作中方法中说最后一种是标准操作,但是啦,我怎么感觉最后一种却他妈比较费劲呀感觉官网也没说啥清楚

2、一篇解说比较好的blogs

https://blog.csdn.net/kancy110/article/details/73930106

下面是我做的一个示例这也是官网自带的例子:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import numpy as np 
from scipy.sparse import csr_matrix

indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
a=csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
print a

"""
输出:
[[1 0 2]
 [0 0 3]
 [4 5 6]]
"""
#根据官方的解说:
#对于第i行其所在列的索引为indices[indptr[i]:indptr[i+1]]
#对于第i行其所在列的值为data[indptr[i]:indptr[i+1]]
#总行数=len(indptr)-1
#据此我们来分析分析上面这个怎么转化
#下面以i表示行索引,j表示列索引,s=indptr[i],e=indptr[i+1]
##当i=0,s=indptr[0]=0;e=indptr[1]=2,则:
#j=indices[s:e]=[0,2] ==> a[0,0]=data[0]=1,a[0,2]=data[1]=2

##当i=1,s=indptr[1]=2;e=indptr[2]=3,则:
#j=indices[s:e]=[2] ==> a[1,2]=data[2]=3

##当i=2,s=indptr[2]=3;e=indptr[3]=6,则:
#j=indices[s:e]=[0,1,2] ==> a[2,0]=data[3]=4,a[2,1]=data[4]=5,a[2,2]=data[5]=6

b=csr_matrix(a)
print b

"""
输出:
  (0, 0)	1
  (0, 2)	2
  (1, 2)	3
  (2, 0)	4
  (2, 1)	5
  (2, 2)	6
"""

个人还是比较喜欢: csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])这种风格的稀疏矩阵转化

猜你喜欢

转载自blog.csdn.net/lingyunxianhe/article/details/81156133