scipy.sparse.save_npz¶
- scipy.sparse.save_npz(file, matrix, compressed=True)[源代码]¶
使用将稀疏矩阵保存到文件
.npz
格式化。- 参数
- file字符串或类似文件的对象
将保存数据的文件名(字符串)或打开的文件(类似文件的对象)。如果文件是字符串,则
.npz
如果文件名不存在,将在文件名后附加扩展名。- matrix: spmatrix (format: ``csc``, ``csr``, ``bsr``, ``dia`` or coo``)
要保存的稀疏矩阵。
- compressed布尔值,可选
允许压缩文件。默认值:true
参见
scipy.sparse.load_npz
使用从文件加载稀疏矩阵
.npz
格式化。numpy.savez
将多个数组保存到
.npz
存档。numpy.savez_compressed
将多个数组保存到压缩的
.npz
存档。
示例
将稀疏矩阵存储到磁盘,然后重新加载:
>>> import scipy.sparse >>> sparse_matrix = scipy.sparse.csc_matrix(np.array([[0, 0, 3], [4, 0, 0]])) >>> sparse_matrix <2x3 sparse matrix of type '<class 'numpy.int64'>' with 2 stored elements in Compressed Sparse Column format> >>> sparse_matrix.toarray() array([[0, 0, 3], [4, 0, 0]], dtype=int64)
>>> scipy.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix) >>> sparse_matrix = scipy.sparse.load_npz('/tmp/sparse_matrix.npz')
>>> sparse_matrix <2x3 sparse matrix of type '<class 'numpy.int64'>' with 2 stored elements in Compressed Sparse Column format> >>> sparse_matrix.toarray() array([[0, 0, 3], [4, 0, 0]], dtype=int64)