我想解析一些真实的数据到一个.mat
对象来加载我的matlab脚本。Python创建一个空的稀疏矩阵
我收到此错误:
TypeError: 'coo_matrix' object does not support item assignment
我发现coo_matrix。但是,我无法为其分配值。
的data.txt
10 45
11 12
4 1
我想获得尺寸100×100 的稀疏矩阵。并指定1对
Mat(10, 45) = 1
Mat(11, 12) = 1
Mat(4, 1) = 1
CODE
import numpy as np
from scipy.sparse import coo_matrix
def pdata(pathToFile):
M = coo_matrix(100, 100)
with open(pathToFile) as f:
for line in f:
s = line.split()
x, y = [int(v) for v in s]
M[x, y] = 1
return M
if __name__ == "__main__":
M = pdata('small.txt')
任何建议吗?
'coo_matrix'获取数据参数。检查它的文档。 – hpaulj