Pretty-print of a NumPy array without scientific notation and with given precision
Here in this blog, we will understand how to make a numpy array free from scientific notation and with a given precision in Python. This tutorial is very helpful and easy to understand about the concept of modifying values with scientific notations and values with many digits after the decimal. This method is used to make the elements of a numpy array understandable to the readers and users.
NumPy array:
Below is an example of a NumPy array with arbitrary values, using the np.random.rand() method.
import numpy as np array=np.random.rand(10) array
Output:
In this example, we get an array without any scientific notation which is easy to read by readers.
NumPy array with scientific notation:
This is an example of a NumPy array with scientific notation created with the help of np.random.rand()
method.
import numpy as np array=np.random.rand(1000) array
Output:
In this example, we can see that the values have scientific notations which become difficult to read by the readers.
np.set_printoptions()
We use the np.set_printoptions()
method to set the precision of the values in an array and also to omit the scientific notations from the values in that array. There are two parameters that are assigned particular values to set the precision and exclude the scientific notations from the array. The two parameters are given below:
Precision:
The precision parameter decides the number of digits after the decimal. For example, if precision is set to a numerical value of 3, then all the values of that array will have digits up to 3 places after the decimal.
Suppress:
This parameter is set to the boolean function “True” to remove the scientific notations from each element of an array.
Example:
import numpy as np array=np.random.rand(1000) np.set_printoptions(precision=3,suppress=True) array
Output:
Leave a Reply