I have output using sparse matrix in python, I need to save this sparse matrix on my hard drive, how can I do this? if I have to create a database, then how do I do this? this is my code:
import nltk import cPickle import numpy from scipy.sparse import lil_matrix from nltk.corpus import wordnet as wn from nltk.corpus import brown f = open('spmatrix.pkl','wb') 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 cPickle.dump(mat,f,-1) 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.close()
I need to save this “mat” in a file and access the matrix value using coordinates.
the result of a sparse matrix is: "the result of a sparse matrix is as follows:
(173, 168) 2.0 (173, 169) 1.0 (173, 172) 1.0 (173, 237) 4.0 (174, 231) 1.0 (175, 141) 1.0 (176, 195) 1.0
but when I store it in a file and read the same thing, I get it like this:
(0, 68) 1.0 (0, 77) 1.0 (0, 95) 1.0 (0, 100) 1.0 (0, 103) 1.0 (0, 110) 1.0 (0, 112) 2.0 (0, 132) 1.0 (0, 133) 2.0 (0, 139) 1.0 (0, 146) 2.0 (0, 156) 1.0 (0, 157) 1.0 (0, 185) 1.0
python numpy sparse-matrix
Bhuvan raj
source share