VEBP
VEBP

Reputation: 59

How to modify the quantity of decimals shown in the python terminal?

When I compute an operation and it is displayed in the python terminal (spyder) the numbers has to many decimals and make difficult (sometimes) read the result, for example

T = array([[ 5,  5,  8,  8,  7],
           [ 7,  8,  6,  9,  6],
           [ 6,  7,  8,  9,  8],
           [ 1,  7,  1,  7,  9],
           [ 4,  2,  3,  6, 10]])

print( T @ inv(t) )

in the terminal I have

[[ 1.00000000e+00  4.44089210e-16  0.00000000e+00 -2.49800181e-16
   3.33066907e-16]
 [ 8.88178420e-16  1.00000000e+00  0.00000000e+00  5.55111512e-17
   0.00000000e+00]
 [ 0.00000000e+00  4.44089210e-16  1.00000000e+00 -8.32667268e-17
   0.00000000e+00]
 [ 1.77635684e-15 -4.44089210e-16  0.00000000e+00  1.00000000e+00
   1.11022302e-16]
 [-8.88178420e-16 -4.44089210e-16  3.55271368e-15 -2.22044605e-16
   1.00000000e+00]]

and this is a simple Identity matrix. The question is

¿is it posible to reduce the number of decimals in the matrix to show me a more easy one? just like this

np.array([[1, 0, 0, 0, 0],
          [0, 1, 0, 0, 0],
          [0, 0, 1, 0, 0],
          [0, 0, 0, 1, 0],
          [0, 0, 0, 0, 1]])

or even better, not only just for the matrix but all numbers in the terminal

The option

'%.2f' % T @ inv(T)

give me the error

TypeError: only size-1 arrays can be converted to Python scalars

I'm talking about to change all displays in the terminal from a initial comand in the head of the script

Upvotes: 1

Views: 1046

Answers (1)

Ehsan
Ehsan

Reputation: 12407

Use this default function in numpy to control that:

np.set_printoptions(suppress=True)

if you need to show only specific number of digits use argument precision:

np.set_printoptions(precision=0, suppress=True)

You can check out more options here.

Upvotes: 1

Related Questions