Python:如何使用Python存储稀疏矩阵? [英] Python: how do you store a sparse matrix using python?

查看:372
本文介绍了Python:如何使用Python存储稀疏矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有使用稀疏矩阵的输出,我需要将此稀疏矩阵存储在硬盘中,我该怎么办?如果我应该创建一个数据库,那我该怎么办? 这是我的代码:

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?? 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()

我需要将此"mat"存储在文件中,并且应该使用坐标访问矩阵的值.

I need to store this "mat" in a file and should access the value of the matrix using the co-ordinates..

稀疏矩阵的结果是这样的:`稀疏矩阵的结果是这样的:

result of the sparse matrix is like this: `the result of sparse matrix are like this:

(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 into a file and read the same i'm getting 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

推荐答案

注意:该答案是针对现在提供代码的修订问题的.

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

您不应在函数中调用cPickle.dump().创建稀疏矩阵,然后将其内容转储到文件中.

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

尝试:

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()

这篇关于Python:如何使用Python存储稀疏矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆