Numpy is a Python bundle that’s used to do scientific computations. It provides high-performance multidimensional arrays in addition to the instruments wanted to work with them. A NumPy array is a tuple of constructive integers that indexes a grid of values (of the identical sort). Numpy arrays are fast and easy to understand, they usually permit customers to do calculations throughout huge arrays.

NumPy has a variety of strategies that can be utilized in numerous conditions. Set_printoptions() is an instance of a numerical range-based perform. The set_printoptions() perform in Python is used to manage how floating-point numbers, arrays, and different NumPy objects are printed. The set_printoptions() methodology will likely be mentioned in-depth and with examples on this article.

What Is the Set_printoptions() Methodology in Python?

We are able to get customized printing choices with the numpy.set_printoptions() methodology of Python, comparable to setting the precisions of floating values.

To show every entry within the array with exact digits of precision, name numpy.set_printoptions (precision=None, suppress=None). Set suppress to True to disable scientific notation when it’s offered. NumPy makes use of as much as 8 digits of precision by default, and scientific notation will not be suppressed.

What Is the Syntax of Set_printoptions() Methodology?

The set_printoptions() methodology’s syntax is given beneath.

The set_printoptions() methodology has the next parameters in its syntax.

  • precision: The default worth for this parameter is 8, which displays the variety of digits of precision.
  • threshold: As an alternative of full repr, this displays the full quantity of array members that set off summarization. That is an elective area with a price of 1000 because the default.
  • edgeitems: This displays the full variety of array objects initially and the top of every dimension. This can be a three-digit area that’s elective.
  • suppress: A Boolean worth is required. If True, the perform will at all times use fixed-point notation to output floating-point integers. The numbers which might be equal to zero within the current precision will print as zero on this state of affairs; when absolutely the worth of the smallest is <1e-4 or the ratio of the biggest absolute worth to the minimal is >1e3, the scientific notation is used if False. That is additionally an elective parameter with the worth False because the default.

Now that you’ve a primary grasp of the set_printoptions methodology’s syntax and operation, it’s time to take a look at some examples. The offered examples will present you how one can use the set_printoptions() methodology to print numpy arrays with precision.

Instance 1

That will help you perceive how one can use the set_printoptions() perform beneath is an instance program. The arange and set_printoptions features from the numpy module are used within the code beneath. After that, we used a precision worth of 5, a threshold worth of 5, an edgeitems worth of 4, and a suppress worth of True to implement the set_printoptions() perform.

Our code’s printing choice is configured with this command. We used the arange() perform to construct an array object ‘arr’ containing integers starting from 1 to 11 within the second last line of the code. Lastly, 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)

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

Instance 2

One other NumPy pattern code to assemble an array with scientific notation numbers will be discovered right here. We set the precision worth to eight on this instance and printed the array on this code. Let’s simply take a look at every line of the code one after the other. This fashion, you’ll have a greater understanding of what this code performs.

We started by importing the numpy module, which is required to construct and run this program code. Following that, we constructed the array and saved it within the variable ‘n.’ Following that, we printed the message ‘Precision worth is ready to eight′ to profit the readers’ understanding. After that, we used the set_printoptions() methodology to set the precision to eight and print the array in the identical method.

import numpy as np
n = np.array([1.3e-6, 1.2e-5, 1.1e-4])
print(“Precision worth is ready to eight:”)
np.set_printoptions(suppress=True, precision=8)

The typed message is displayed first, adopted by the array values, that are offered in keeping with the set precision, which in our case is 8.

Instance 3

We’ve created a NumPy program code to show NumPy array parts of floating values with specified precision within the third and last instance of this submit.

The numpy module is imported first in this system code, and an array (named arr) is generated with the assorted floating values. These embrace [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 worth is ready to 4) is displayed, informing the readers of the required worth of precision.

Lastly, the precision worth is handed to the set_printoptions() perform, and the array is up to date and offered.

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 worth is ready to 4:”)

The message and exact array values are displayed within the output picture. See the picture beneath.


The set_printoptions() perform of Python was coated on this submit. It’s typically utilized by programmers to change the printing of Numpy arrays. Right here you’ll discover all the small print in addition to pattern packages that you could be use by yourself. This may make it simple so that you can comprehend the whole subject. This text comprises all you want to know, from definition to syntax to examples. For those who’re new to this notion and want a step-by-step information to getting began, go no additional than this text.

Did you want this text?
Share it on any of the next social media channels beneath to offer us your vote. Your suggestions helps us enhance.