Python稀疏矩阵文件大小相同

时间:2016-11-03 03:01:08

标签: python numpy scipy sparse-matrix

我正在使用numpy来保存一组约7300个稀疏矩阵。每个稀疏矩阵具有相同的行数但具有不同的列数。写入文件的数据是矩阵中的非零值,每个非零值的位置以及矩阵的形状。然而,每个文件在磁盘上的大小完全相同。

这是我用来保存和加载文件的代码。我很乐意根据需要提供更多代码和/或输出信息。

import numpy as np
import scipy.sparse as sp

# insert other code here

def saveSparseGraph(graph, fn):
    """
    Try to save the graph using numpy.save

    Inputs:
    - graph: the csr_matrix to save
    - fn: the filename base (no extensions)

    Returns: none
    """
    np.savez(fn, data=graph.data, indices=graph.indices, indptr=graph.indptr, shape=graph.shape)
    print "Saved the files"


def loadSparseGraph(fn):
    """
    Try to load the previously saved graph

    Inputs:
    - fn: the file name/path base (no extensions)

    Returns: 
    - the loaded sparse matrix
    """
    loader = np.load(fn+".npz")
    print "Sparse graph loaded"
    return sp.csr_matrix((loader['data'], loader['indices'], loader['indptr']), shape=loader['shape'])

从保存的文件加载数据并查看它时,它似乎都在那里。 (我说“出现”因为我不知道原始数据的全部范围,因为矩阵很大。)

是否有任何理由相同的文件大小才有意义?

0 个答案:

没有答案
相关问题