Python

Python Print Numpy Array with Precision

Numpy is a Python package that is used to do scientific computations. It offers high-performance multidimensional arrays as well as the tools needed to work with them. A NumPy array is a tuple of positive integers that indexes a grid of values (of the same type). Numpy arrays are quick and simple to grasp, and they allow users to do calculations across vast arrays.

NumPy has a wide range of methods that can be used in various situations. Set_printoptions() is an example of a numerical range-based function. The set_printoptions() function in Python is used to control how floating-point numbers, arrays, and other NumPy objects are printed. The set_printoptions() method will be discussed in-depth and with examples in this article.

What Is the Set_printoptions() Method in Python?

We can get custom printing options with the numpy.set_printoptions() method of Python, such as setting the precisions of floating values.

To display each entry in the array with precise digits of precision, call numpy.set_printoptions (precision=None, suppress=None). Set suppress to True to disable scientific notation when it is presented. NumPy uses up to 8 digits of precision by default, and scientific notation is not suppressed.

What Is the Syntax of Set_printoptions() Method?

The set_printoptions() method’s syntax is given below.

The set_printoptions() method has the following parameters in its syntax.

  • precision: The default value for this parameter is 8, which reflects the number of digits of precision.
  • threshold: Instead of full repr, this reflects the total amount of array members that trigger summarization. This is an optional field with a value of 1000 as the default.
  • edgeitems: This reflects the total number of array objects at the start and the end of each dimension. This is a three-digit field that is optional.
  • suppress: A Boolean value is required. If True, the function will always use fixed-point notation to output floating-point integers. The numbers that are equal to zero in the present precision will print as zero in this situation; when the absolute value of the smallest is <1e-4 or the ratio of the largest absolute value to the minimum is >1e3, the scientific notation is used if False. This is also an optional parameter with the value False as the default.

Now that you have a basic grasp of the set_printoptions method’s syntax and operation, it’s time to look at some examples. The provided examples will show you how to use the set_printoptions() method to print numpy arrays with precision.

Example 1

To help you understand how to use the set_printoptions() function below is an example program. The arange and set_printoptions functions from the numpy module are used in the code below. After that, we used a precision value of 5, a threshold value of 5, an edgeitems value of 4, and a suppress value of True to implement the set_printoptions() function.

Our code’s printing option is configured with this command. We used the arange() function to build an array object ‘arr’ containing integers ranging from 1 to 11 in the second final line of the code. Finally, the array object ‘arr’ has been printed.

from numpy import set_printoptions, arange
set_printoptions(precision=5, threshold=5, edgeitems=4, suppress=True)
arr = arange(12)
print(arr)

As you can see, the integers 1 to 11 are printed using the above-mentioned program code.

Example 2

Another NumPy sample code to construct an array with scientific notation numbers can be found here. We set the precision value to 8 in this example and printed the array in this code. Let’s just have a look at each line of the code one by one. This way, you’ll have a better understanding of what this code performs.

We began by importing the numpy module, which is required to build and run this program code. Following that, we constructed the array and saved it in the variable ‘n.’ Following that, we printed the message ‘Precision value is set to 8′ to benefit the readers’ understanding. After that, we used the set_printoptions() method to set the precision to 8 and print the array in the same way.

import numpy as np
n = np.array([1.3e-6, 1.2e-5, 1.1e-4])
print("Precision value is set to 8:")
np.set_printoptions(suppress=True, precision=8)
print(n)

The typed message is displayed first, followed by the array values, which are presented according to the set precision, which in our case is 8.

Example 3

We’ve created a NumPy program code to display NumPy array elements of floating values with specified precision in the third and final example of this post.

The numpy module is imported first in the program code, and an array (named arr) is generated with the various floating values. These include [0.56448929, 0.12343222, 0.5643783, 0.8764567, 0.34567826, 0.34562654, 0.23452456, 0.86342567, 0.09423526, 0.25617865], 0.34567826, 0.34562654, 0.23452456, 0.86342567, 0.09423526, 0.25617865]. Following that, the message (Precision value is set to 4) is displayed, informing the readers of the specified value of precision.

Finally, the precision value is passed to the set_printoptions() function, and the array is updated and presented.

import numpy as np
arr =np.array([ 0.56448929, 0.12343222, 0.5643783, 0.8764567, 0.34567826, 0.34562654,
0.23452456, 0.86342567, 0.09423526, 0.25617865])
print("Precision value is set to 4:")
np.set_printoptions(precision=4)
print(arr)

The message and precise array values are displayed in the output image. See the image below.

Conclusion

The set_printoptions() function of Python was covered in this post. It is often used by programmers to modify the printing of Numpy arrays. Here you’ll find all the details as well as sample programs that you may use on your own. This will make it easy for you to comprehend the entire issue. This article contains all you need to know, from definition to syntax to examples. If you’re new to this notion and need a step-by-step guide to getting started, go no further than this article.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content