在python中创建一个完整的矩阵输出

时间:2015-06-06 03:17:01

标签: python python-2.7 matrix python-2.x

我有一个236 x 97维的矩阵。当我用Python打印矩阵时,它的输出不完整,矩阵中间有.......

我尝试将矩阵写入测试文件,但结果完全相同。 我无法发布屏幕截图,因为我的声誉不够,如果我选择其他标记选项,则无法正常显示。 任何人都可以解决这个问题吗?

 def build(self):
    self.keys = [k for k in self.wdict.keys() if len(self.wdict[k]) > 1]
    self.keys.sort()
    self.A = zeros([len(self.keys), self.dcount])
    for i, k in enumerate(self.keys):
        for d in self.wdict[k]:
            self.A[i,d] += 1

 def printA(self):
    outprint = open('outputprint.txt','w')
    print 'Here is the weighted matrix'
    print self.A
    outprint.write('%s' % self.A)
    outprint.close()
    print self.A.shape

2 个答案:

答案 0 :(得分:1)

问题在于您专门将str表示法保存到具有此行的文件中:

  

outprint.write('%s' % self.A)

明确地将其转换为字符串(%s)---生成您正在查看的删节版本。

有很多方法可以将整个矩阵写入输出,一个简单的选择是使用numpy.savetxt,例如:

import numpy
numpy.savetxt('outputprint.txt', self.A)

答案 1 :(得分:1)

假设您的矩阵是一个numpy数组,您可以使用matrix.tofile(<options>)将数组写入文件,如文档here所示:

#!/usr/bin/env python
# coding: utf-8

import numpy as np

# create a matrix of random numbers and desired dimension
a = np.random.rand(236, 97)

# write matrix to file
a.tofile('output.txt', sep = ' ')