Python: how do you store a sparse matrix using python?

前端 未结 7 1828
灰色年华
灰色年华 2021-02-06 06:17

I have got an output using sparse matrix in python, i need to store this sparse matrix in my hard disk, how can i do it? if i should create a database then how should i do?? thi

7条回答
  •  广开言路
    2021-02-06 07:07

    Depending on the size of the sparse matrix, I tend to just use cPickle to pickle the array:

    import cPickle
    f = open('spmatrix.pkl','wb')
    cPickle.dump(your_matrix,f,-1)
    f.close()
    

    If I'm dealing with really large datasets then I tend to use netcdf4-python

    Edit:

    To then access the file again you would:

    f = open('spmatrix.pkl','rb') # open the file in read binary mode
    # load the data in the .pkl file into a new variable spmat
    spmat = cPickle.load(f) 
    f.close()
    

提交回复
热议问题