Python

Python Print Numpy Array with Precision

Most of the time, the Numpy package is used for scientific computational data in arrays, which means that the length of the values can be massive, especially that of the floating-point values or values defined with scientific notation. To format the print result from these numpy arrays, the user can utilize the set_printoptions() method.

This post will explain how to use and format the output of your print statement up to a specific precision in Python. The content of this guide contains the following:

The set_printoptions() Method

The set_printoptions() method is used to define the default settings for the print statements when printing out the Numpy arrays. This set_printoptions() takes many different arguments. The general syntax of this method is defined below:

numpy.set_printoptions(edgeitems, precision, suppress, ...)

 

 

In the syntax:

  • edgeitems: This argument defines the number of items to display on both ends in the output for each dimension of the array. All other items in between would not be displayed in the output.
  • precision: This argument defines the number of digits to display after the floating point
  • suppress: This argument takes in a boolean value and defines whether or not to display the value in scientific notation.

Let’s see how to print out the values of an array up to a specific precision.

How to Print Numpy Array With Precision Using set_printoptions() Method?

To demonstrate the use of the set_printoptions() method, start by first creating an array that contains floating point values with the following line of code:

import numpy

setArray = numpy.array([1.2785,4.129837,0.112,65.2322331])

 

After that, call the set_printoptions() method and, in the argument, define the precision value (3 for this post):

numpy.set_printoptions(precision=3)

 

Lastly, print out the Numpy array with the help of the print() method:

print(setArray)

 

When this code is executed, it will produce the following result on the terminal:

As you can observe in the output, the values were printed with only three digits after the floating point.

How to Print Scientific Values With Precision in Numpy Arrays?

Another thing is that the Numpy arrays can hold the values in the form of scientific notations, and when you want to print out those values with a set precision value, you can use the set_printoptions() method. To demonstrate this, simply start by importing the numpy package and creating an array with the following line:

import numpy

setArray = numpy.array([1.3e-6, 1.2e-5, 1.1e-4])

 

After that, simply use the set_printoptions() method and pass the argument “suppress=True” to convert the scientific notation to floating point notation, and pass the precision argument to specify the number of digits after the floating point:

numpy.set_printoptions(precision=7, suppress=True)

 

Once that is done, simply print out the array onto the terminal using the print() method:

print(setArray)

 

When this program is executed, it produces the following result on the terminal:

As you can see that you were able to get the print from a Numpy array with scientific notation values up to a specific precision.

Conclusion

To get the output/print of values from Numpy arrays up to a specific precision point, the user can utilize the set_printoptions() method. To do this, the user has to call the set_printoptions() method with the “precision” argument and specify the number of digits to be displayed after the decimal/floating point. Also, with the “suppress” argument, the user has the option to change the format of the scientific notation values to floating point values.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.