Esempio n. 1
0
def save_sparse_matrix(data, fmt, filepath):
    """
    Save a scipy sparse matrix in the specified format. Row and column
    indices will be converted to 1-indexed if you specify a plain text
    format (tsv, csv, mm). Note that zero entries are guaranteed to be
    saved in tsv or csv format.

    Parameters
    ----------
    data : scipy sparse matrix to save
    fmt : str
        Specifies the file format to write:
        - tsv
        - csv
        - mm  (MatrixMarket)
        - npz (save as npz archive of numpy arrays)
        - fsm (mrec.sparse.fast_sparse_matrix)
    filepath : str
        The file to load.
    """
    if fmt == 'tsv':
        m = data.tocoo()
        with open(filepath, 'w') as out:
            for u, i, v in izip(m.row, m.col, m.data):
                print >> out, '{0}\t{1}\t{2}'.format(u + 1, i + 1, v)
    elif fmt == 'csv':
        m = data.tocoo()
        with open(filepath, 'w') as out:
            for u, i, v in izip(m.row, m.col, m.data):
                print >> out, '{0},{1},{2}'.format(u + 1, i + 1, v)
    elif fmt == 'mm':
        mmwrite(filepath, data)
    elif fmt == 'npz':
        savez(data.tocoo(), filepath)
    elif fmt == 'fsm':
        fast_sparse_matrix(data).save(filepath)
    else:
        raise ValueError('unknown output format: {0}'.format(fmt))
Esempio n. 2
0
def save_sparse_matrix(data,fmt,filepath):
    """
    Save a scipy sparse matrix in the specified format. Row and column
    indices will be converted to 1-indexed if you specify a plain text
    format (tsv, csv, mm). Note that zero entries are guaranteed to be
    saved in tsv or csv format.

    Parameters
    ----------
    data : scipy sparse matrix to save
    fmt : str
        Specifies the file format to write:
        - tsv
        - csv
        - mm  (MatrixMarket)
        - npz (save as npz archive of numpy arrays)
        - fsm (mrec.sparse.fast_sparse_matrix)
    filepath : str
        The file to load.
    """
    if fmt == 'tsv':
        m = data.tocoo()
        with open(filepath,'w') as out:
            for u,i,v in izip(m.row,m.col,m.data):
                print >>out,'{0}\t{1}\t{2}'.format(u+1,i+1,v)
    elif fmt == 'csv':
        m = data.tocoo()
        with open(filepath,'w') as out:
            for u,i,v in izip(m.row,m.col,m.data):
                print >>out,'{0},{1},{2}'.format(u+1,i+1,v)
    elif fmt == 'mm':
        mmwrite(filepath,data)
    elif fmt == 'npz':
        savez(data.tocoo(),filepath)
    elif fmt == 'fsm':
        fast_sparse_matrix(data).save(filepath)
    else:
        raise ValueError('unknown output format: {0}'.format(fmt))
Esempio n. 3
0
def save_sparse_matrix(data,fmt,filepath):
    """
    Save a scipy sparse matrix in the specified format.  Row and column
    indices will be converted to 1-indexed if you specify a plain text
    format (tsv, csv, mm).

    Parameters
    ----------
    data : scipy sparse matrix to save
    fmt : str
        Specifies the file format to write:
        - tsv
        - csv
        - mm  (MatrixMarket)
        - npz (save as npz archive of numpy arrays)
        - fsm (mrec.sparse.fast_sparse_matrix)
    filepath : str
        The file to load.
    """
    if fmt == 'tsv':
        row,col = data.nonzero()
        out = open(filepath,'w')
        for u,i,v in izip(row,col,data.data):
            print >>out,'{0}\t{1}\t{2}'.format(u+1,i+1,v)
    elif fmt == 'csv':
        row,col = data.nonzero()
        out = open(filepath,'w')
        for u,i,v in izip(row,col,data.data):
            print >>out,'{0},{1},{2}'.format(u+1,i+1,v)
    elif fmt == 'mm':
        mmwrite(filepath,data)
    elif fmt == 'npz':
        return savez(data,filepath)
    elif fmt == 'fsm':
        fast_sparse_matrix.load(dataset).save(filepath)
    else:
        raise ValueError('unknown output format: {0}'.format(fmt))