0

I have the following code:

import numpy as np
from numpy import matrix

MM = matrix([[1.00,0,0],[0,1.50,0],[0,0,2.00]])
M = np.array(MM)
print("matrix M: \n", M)

which output looks like this:

matrix M: 
 [[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]

This is very aesthetic, but I want it to look more organized. Something like this hopefully:

[1.  0.   0.]
[0.  1.5  0.]
[0.  0.   2.]
Georgy
  • 12,464
  • 7
  • 65
  • 73
joacokp
  • 29
  • 6
  • You can create a custom 'to_string()' method. Regretfully, python print in this fashion the n-dimensional array..... – Liuk Dec 18 '19 at 14:55
  • 1
    You can fix the alignment of the first line of `M` in the output with `print("matrix M: \n", M, sep='')`. But you'll still have the outer square brackets. – Warren Weckesser Dec 18 '19 at 14:58
  • @WarrenWeckesser right, but it won't remove the double square brackets like OP apparently wants – DeepSpace Dec 18 '19 at 15:06
  • 1
    After having read the answers I'm now not sure if I should've edited the title. What is your exact question? How to remove the brackets? How to align the values? How to remove the space before the matrix? – Georgy Dec 18 '19 at 15:24
  • I'd suggest a test case that involves a wider range of values. For example a '12.3' in one row could mess up the row-by-row formatting. A big plus for the default matrix format is that it keeps columns aligned. Getting rid of the outer set of [] might not be worth the effort. A `np.matrix` is always 2d, but other numpy arrays may be 1d or 3d. – hpaulj Dec 18 '19 at 16:49

5 Answers5

2

If you print M on its own it actually looks as you expect.

The reason why the first line gets messed up is because print inserts a space between its arguments by default. You can suppress that by providing sep=''.

See https://docs.python.org/3/library/functions.html#print for reference.

Or better yet, omit \n from the first print argument and use sep='\n' instead:

>>> print("matrix M:", M, sep='\n')
matrix M: 
[[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

Pretty close:

'\n'.join('[{}]'.format(' '.join(str(n) for n in row)) for row in M)

Outputs

[1.0 0.0 0.0]
[0.0 1.5 0.0]
[0.0 0.0 2.0]

This removes the indentation and the double square brackets.

However, with very large matrices this might be slow due to the nested for loops.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

The exact desired output is this:

print('\n'.join('[{}]'.format('. '.join(str(float(n)) for n in row)) for row in M))

[1.0. 0.0. 0.0]
[0.0. 1.5. 0.0]
[0.0. 0.0. 2.0]
seralouk
  • 30,938
  • 9
  • 118
  • 133
0

As describes here Pretty print 2D Python list , a very nice option is the following:

s = [[str(e) for e in row] for row in M]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print('\n'.join(table))

1.0     0.0     0.0
0.0     1.5     0.0
0.0     0.0     2.0
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
0

Depending on how much extra code/complexity you want for a solution here, you can do a few things. You can use a custom function in the print statement if you really want fine-grained control over the output (using code offered up in some of the other proposed solutions), but a less cumbersome approach that still gives you a decent amount of flexibility is to use the array2string function already included in Numpy.

A few examples using your scenario:

  • Without any arguments:
>>> print(np.array2string(a=M))
[[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]
  • Using the separator argument to add a bit of padding around elements:
>>> print(np.array2string(a=M, separator='   '))
[[1.    0.    0. ]
 [0.    1.5   0. ]
 [0.    0.    2. ]]
  • If the leading and trailing brackets are bothersome, you can go a bit further with the print statement (array2string doesn't give you a way to change this) itself, doing a bit of manipulation to account for the character spacing with the brackets being removed:
>>> print(' ' + np.array2string(a=M, separator='   ')[1:-1])
 [1.    0.    0. ]
 [0.    1.5   0. ]
 [0.    0.    2. ]

Note that the last scenario still leaves a space at the beginning of each line. You could use regexes to clean this up.

I realize that this may not be exactly what you are looking for in terms of output, but I thought I might offer up a lighter weight alternative that gets you mostly there.

geekmuse
  • 360
  • 1
  • 5