PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

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

 August 10, 2022     arrays, decimal, matrix, numpy, python     No comments   

Issue

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


Solution

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.



Answered By - Ehsan
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing