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

前端 未结 7 1816
灰色年华
灰色年华 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 06:52

    Note: This answer is in response to the revise question that now provides code.

    You should not call cPickle.dump() in your function. Create the sparse matrix and then dump its contents to the file.

    Try:

    def markov(L):
       count=0
       c=len(text1)
       for i in range(0,c-2):
           h=L.index(text1[i])
           k=L.index(text1[i+1])
           mat[h,k]=mat[h,k]+1 #matrix
    
    
    text = [w for g in brown.categories() for w in brown.words(categories=g)]
    text1=text[1:500]
    arr=set(text1)
    arr=list(arr)
    mat=lil_matrix((len(arr),len(arr)))
    markov(arr)
    f = open('spmatrix.pkl','wb')
    cPickle.dump(mat,f,-1)
    f.close()
    

提交回复
热议问题