Python 创建稀疏矩阵

1.coo_matrix
在这里插入图片描述

>>> import scipy.sparse as ss
>>> import numpy as np
>>> import random
>>> row = [2,2,3,2]   #行
>>> col = [3,4,2,3]   #列
>>> data = [1,1,1,1]  #要填充的数据
>>> c = ss.coo_matrix((data,(row,col)),shape=(5,6))
>>> print(c.toarray())
[[0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 2 1 0]
 [0 0 1 0 0 0]
 [0 0 0 0 0 0]]
>>>

稍微需要注意的一点是,用coo_matrix创建矩阵的时候,相同的行列坐标可以出现多次。矩阵被真正创建完成以后,相应的坐标值会加起来得到最终的结果。

posted @ 2019-10-14 15:31  Philtell  阅读(1340)  评论(0编辑  收藏  举报