Output numpy.ndarray as csr file

101 Views Asked by At

I'm trying to output a numpy ndarray as a CSR file (this is an intermediate stage, I'm trying to use a program that requires CSR format as input).

So far, I've tried using scipy.sparse.coo_matrix() and writing out to an ijv file with the following code:

pca_coo = scipy.sparse.coo_matrix(pca_result)
with open(project + '/matrix/for_knn.jiv', 'w') as f:
    for row, col, val in zip(pca_coo.row, pca_coo.col, pca_coo.data):
        t= f.write("{}\t{}\t{}\n".format(row, col, val))

The file produced by the above code causes the program downstream to segfault.

I'm assuming at this point that the problem is in the format of the output, but I haven't been able to locate the issue.

Edit: Answered below.

1

There are 1 best solutions below

0
On

A friend helped me with the following:

def write_csr(C, outputname):
    """
    writes out a csr matrix to a file (for use with l2knng).
    C = csr matrix
    outputName = output file name
    """
    with open(outputname, 'a') as OUTFILE:
        for i in range(0,C.shape[0]):
            sub = C[i,]
            outstr = ""
            for j in range(0,sub.size):
                outstr += " " + str(sub.indices[j]+1) + " " + str(sub.data[j])
            outstr += "\n"
            _ = OUTFILE.write(outstr)

Seems to work well.