I have a NumPy
ndarray
to which I would like to add row/column headers.
The data is actually 7x12x12, but I can represent it like this: >
I am not aware of any method to add headers to the matrix (even though I would find it useful). What I would do is to create a small class that prints the object for me, overloading the __str__
function.
Something like this:
class myMat:
def __init__(self, mat, name):
self.mat = mat
self.name = name
self.head = ['a','b','c','d','e','f']
self.sep = ','
def __str__(self):
s = "%s%s"%(self.name,self.sep)
for x in self.head:
s += "%s%s"%(x,self.sep)
s = s[:-len(self.sep)] + '\n'
for i in range(len(self.mat)):
row = self.mat[i]
s += "%s%s"%(self.head[i],self.sep)
for x in row:
s += "%s%s"%(str(x),self.sep)
s += '\n'
s = s[:-len(self.sep)-len('\n')]
return s
Then you could just easily print them with the headers, using the following code:
print myMat(A,'A')
print myMat(B,'B')