Irfan Dary
Irfan Dary

Reputation: 33

make a full matrix output in python

I have a matrix with 236 x 97 dimension. When I print the matrix in Python its output isn't complete, having ....... in the middle of matrix.

I tried to write the matrix to a test file, but the result is exactly same. I can't post the screenshot because my reputation is not enough, and won't appear correctly if I choose another markup option. Can anyone solve this?


 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

Upvotes: 2

Views: 1784

Answers (2)

DilithiumMatrix
DilithiumMatrix

Reputation: 18637

The problem is that you're specifically saving the str representation to a file with this line:

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

Which explicitly casts it to a string (%s) --- generating the abridged version you're seeing.

There are lots of ways to write the entire matrix to output, one easy option would be to use numpy.savetxt, for example:

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

Upvotes: 1

albert
albert

Reputation: 8593

Assuming your matrix is an numpy array you can use matrix.tofile(<options>) to write the array to a file as documented 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 = ' ')

Upvotes: 1

Related Questions