sparse 的几种矩阵类型都可以使用。来自sparse.csr_matrix 文档:http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
>>> row = array([0,0,1,2,2,2])
>>> col = array([0,2,2,0,1,2])
>>> data = array([1,2,3,4,5,6])
>>> csr_matrix( (data,(row,col)), shape=(3,3) ).todense()
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
coo_matrix 也接受这个(data,(row,col)) 输入
Matlab 提供了一种创建稀疏矩阵的类似方法,包括添加重复条目的业务(请参阅coo_matrix 文档底部的注释)。
我想你想构建
row = array([0,0,0, 1,1,1,2,2,2,....])
col = array([1,43,243, 2,34,276,,...])
# col = col-1 # to change to 0 based indexing?
data = array([1,1,1,...])
例如:
np.array(indices).ravel()
# array([ 1, 43, 243, 2, 34, 276])
np.arange(2).repeat(3)
# array([0, 0, 0, 1, 1, 1])
np.ones(6)
# array([ 1., 1., 1., 1., 1., 1.])