对新问题的回答:
import numpy as np
from scipy import sparse, io
A = sparse.eye(5, format='csr') * np.pi
np.set_printoptions(precision=16, linewidth=1000)
with open('matrix.txt', 'a') as f:
for row in A:
f.write(str(row.toarray()[0]))
f.write('\n')
# [3.141592653589793 0. 0. 0. 0. ]
# [0. 3.141592653589793 0. 0. 0. ]
# [0. 0. 3.141592653589793 0. 0. ]
# [0. 0. 0. 3.141592653589793 0. ]
# [0. 0. 0. 0. 3.141592653589793]
并加上开始/结束括号:
import numpy as np
from scipy import sparse, io
A = sparse.eye(5, format='csr') * np.pi
np.set_printoptions(precision=16, linewidth=1000)
with open('matrix.txt', 'a') as f:
for i, row in enumerate(A):
f.write('[' if (i == 0) else ' ')
f.write(str(row.toarray()[0]))
f.write(']' if (i == A.shape[0] - 1) else '\n')
# [[3.141592653589793 0. 0. 0. 0. ]
# [0. 3.141592653589793 0. 0. 0. ]
# [0. 0. 3.141592653589793 0. 0. ]
# [0. 0. 0. 3.141592653589793 0. ]
# [0. 0. 0. 0. 3.141592653589793]]
您可能需要根据您的数据调整set_printoptions
个.
回答原始问题,这不要求矩阵写得密集.
Harwell-Boeing format是纯文本:
import numpy as np
from scipy import sparse, io
A = sparse.eye(3, format='csr') * np.pi
# Default title 0
# 3 1 1 1
# RUA 3 3 3 0
# (40I2) (40I2) (3E25.16)
# 1 2 3 4
# 1 2 3
# 3.1415926535897931E+00 3.1415926535897931E+00 3.1415926535897931E+00
io.hb_write('matrix.txt', A) # saves as matrix.txt
A2 = io.hb_read('matrix.txt')
assert not (A2 != A).nnz # efficient check for equality
Matrix Market:
io.mmwrite('matrix', A) # saves as matrix.mtx
# %%MatrixMarket matrix coordinate real symmetric
# %
# 3 3 3
# 1 1 3.141592653589793e+00
# 2 2 3.141592653589793e+00
# 3 3 3.141592653589793e+00
A2 = io.mmread('matrix')
assert not (A2 != A).nnz
如果你想要一个更简单的format,尽管它涉及更多的代码:
import numpy as np
from scipy import sparse
A = sparse.eye(10, format='csr')*np.pi
np.savetxt('data.txt', A.data)
np.savetxt('indices.txt', A.indices, fmt='%i')
np.savetxt('indptr.txt', A.indptr, fmt='%i')
加载:
data = np.loadtxt('data.txt')
indices = np.loadtxt('indices.txt', dtype=np.int32)
indptr = np.loadtxt('indptr.txt', dtype=np.int32)
A2 = sparse.csr_matrix((data, indices, indptr))
assert not (A2 != A).nnz
但重要的是,你需要保存的只是csr_matrix
的data
、indices
和indptr
属性.