动态矩阵使用Numpy

时间:2016-11-06 21:21:29

标签: python arrays numpy matrix

我正在研究HMM算法。由于我是python的新手,我想知道如何使用numpy动态更改矩阵的维度。

我通过numpy resize获得不一致的输出。

1    [[ 1.]]

 2   [[ 1.  1.]]
    [[ 2.  1.]]

  3  [[ 2.  1.  0.]
     [ 0.  0.  1.]]

   4 [[ 2.  1.  0.  0.]
     [ 0.  1.  0.  0.]
     [ 0.  0.  0.  1.]]

Here in fourth i should get output as 
     [[ 2.  1.  0.  0.]
     [ 0.  0.  1.  0.]
     [ 0.  0.  0.  1.]]

代码: -

   with open("SampleFile.tsv") as tsvfile:
        tsvreader = csv.reader(tsvfile, delimiter="\t")
        wordindex = 0
        postagindex = 0
        flag = 0
        for line in tsvreader:
            if len(line) > 0:
                if line[1] not in words:
                    wcount += 1
                    flag = 1
                    #wordlikelihoodmatrix.resize(poscount, wcount)
                    words.append(line[1])
                    wordindex = words.index(line[1])
                else:
                    wordindex = words.index(line[1])

                if line[2] not in poslist:
                    poscount += 1
                    flag = 1
                    #wordlikelihoodmatrix.resize(poscount, wcount)
                    poslist.append(line[2])
                    posindex = poslist.index(line[2])
                else:
                    posindex = poslist.index(line[2])

                if flag == 1:
                    wordlikelihoodmatrix.resize(poscount, wcount)
                    flag = 0

                wordlikelihoodmatrix[posindex][wordindex] +=1
                print wordlikelihoodmatrix;

在调整大小或任何其他方式获得预期输出而不使用numpy时,是否有人可以提出我出错的地方?

0 个答案:

没有答案