How to format in numpy savetxt such that zeros are saved only as “0”

前端 未结 3 1196
难免孤独
难免孤独 2020-12-19 10:14

I am saving a numpy sparse array (densed) into a csv. The result is I have a 3GB csv. The problem is 95% of the cells are 0.0000. I used fmt=\'%5.4f\'

3条回答
  •  爱一瞬间的悲伤
    2020-12-19 10:52

    It would be much better if you saved only the non-zeros entries in your sparse matrix (m in the example below), you could achieve that doing:

    fname = 'row_col_data.txt'
    m = m.tocoo()
    a = np.vstack((m.row, m.col, m.data)).T
    header = '{0}, {1}'.format(*m.shape)
    np.savetxt(fname, a, header=header, fmt=('%d', '%d', '%5.4f'))
    

    and the sparse matrix can be recomposed doing:

    row, col, data = np.loadtxt(fname, skiprows=1, unpack=True)
    shape = map(int, open(fname).next()[1:].split(','))
    m = coo_matrix((data, (row, col)), shape=shape)
    

提交回复
热议问题