Print NumPy Array without scientific notation in Python

In this article, we will learn how to print a numpy.array without scientific notation.

Table Of Contents

What is scientific notation?

Scientific notation is a format of displaying very large or very small numbers. NumPy uses this scientific notation intead of actual number while printing.

For very small numbers,

0.000000321 is represented as 3.21 X 10^-7
In python scientific notation it is represented as 3.21e-07

For Large numbers,

Advertisements
123456789.1 is represented as 1.23456789 X 10^8
In python scientific notation it is represented as 1.23456789e+08

Given a NumPy array we need to print the array without scientific notation.

There are multiple ways to print the array without scientific notation. Lets discuss all the methods one by one with proper approach and a working code example.

1. Using set_printoptions() function and supress argument.

The numpy module has a set_printoptions() function. The set_printoptions() function is used to set how the arrays, floating-point numbers, NumPy objects are to be displayed. By default if the numbers are very big or very small then the array will be represented using the scientific notation. Now by using passing the supress as True, we can remove the scientific notation and print the array.

Syntax of set_printoptions() function

numpy.set_printoptions(Supress = True)
  • Parameters:
    • suppress : bool, optional
      • If True, always print the floating point numbers using fixed point notation. So, numbers equal to zero in the current precision will be printed as zero. If False, then the scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False.
  • Returns:
    • None.

Approach:

  1. Import numpy library and create numpy array.
  2. Pass the supress value as True to the set_printoptions() method.
  3. Print the Array, The entire array will be displayed without scientific notation.

Source Code

import numpy as np

# creating a numpy array
arr = np.array([1, 2, 3, 4, 5, 1000.1])

print("Array with scientific notation", arr)

# Removing the scientific notation
np.set_printoptions(suppress=True)

print("Array without scientific notation", arr)

Output:

Array with scientific notation [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]
Array without scientific notation [   1.     2.     3.     4.     5.  1000.1]

2. Using printoptions() function and supress argument.

The numpy module has a printoptions() function, and it is used to set how the arrays, floating-point numbers, NumPy objects are to be displayed. By default if the numbers are very big or very small then the array will be represented using the scientific notation. Now by using passing the supress as True in the printoptions(), we can remove the scientific notation and print the array.

Syntax of printoptions() function

numpy.printoptions(Supress = True)
  • Parameters:
    • suppress : bool, optional
      • If True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will print as zero. If False, then scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False.
  • Returns:
    • None.

Approach:

  1. Import numpy library and create numpy array.
  2. Pass the supress value as True to the printoptions() method.
  3. Print the Array, The entire array will be displayed without scientific notation.

Source Code

import numpy as np

# Creating a numpy array

arr = np.array([1,2,3,4,5,1000.1])

print("Array with scientific notation", arr)

# Removing the scientific notation
np.printoptions(suppress=True)

print("Array without scientific notation", arr)

Output:

Array with scientific notation [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]
Array without scientific notation [   1.     2.     3.     4.     5.  1000.1]

3. Using set_printoptions() function and formatter argument.

The numpy module has a set_printoptions() function, and it is used to set how the arrays, floating-point numbers, NumPy objects are to be displayed. By default if the numbers are very big or very small then the array will be represented using the scientific notation. Using set_printoptions() we can surpress the scientific notation.

Syntax of set_printoptions() function

numpy.set_printoptions(formatter = dict)
  • Parameters:
    • formatter : the keys in the dictionary should indicate the type that the respective formatting function applies to.
  • Returns:
    • None.

Examples

np.set_printoptions( formatter = {'all':lambda x: str(x)} )

This will change all types of elements into string. Here callable is the lamba function.

np.set_printoptions( formatter={'int_kind':lambda x: str(x)} )

This will change int types of elements into string. Here callable is the lamba function.

Approach:

  1. Import numpy library and create numpy array.
  2. Pass the formatter to the set_printoptions() method.
  3. Print the Array, The entire array will be displayed without scientific notation.

Source Code

import numpy as np

# creating a numpy array
arr = np.array([1,2,3,4,5,1000.1])

print("Array with scientific notation",arr)

# Removing the scientific notation
np.set_printoptions(formatter={'all':lambda x: str(x)})

print("Array without scientific notation",arr)

Output:

Array with scientific notation [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]
Array without scientific notation [   1.     2.     3.     4.     5.  1000.1]

For the above method, what if we mention a type that the elements in the array does not belong to. In that case, it will not format those elements.

import numpy as np

# creating a numpy array
arr = np.array([1,2,3,4,5,1000.1])
print("Array with scientific notation",arr)

# Removing the scientific notation
np.set_printoptions(formatter={'bool':lambda x: str(x)})

print("result =",arr)

Output:

Array with scientific notation [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]
result = [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]

4. Using array2string() function and suppress_small argument.

The numpy module has a array2string() function, and it returns a string representation of an array.

Syntax of array2string() function

numpy.array2string(arr, suppress_small)
  • Parameters:
    • arr = The input array
    • suppress_small = bool, optional. Represent numbers “very close” to zero as zero; default is False.
  • Returns:
    • It Returns a string representation of an array.

Approach:

  1. Import numpy library and create numpy array.
  2. Pass the array, suppress_small=True to the array2string() method.
  3. Print the Array, The entire array will be displayed without scientific notation.

Source Code

import numpy as np
import sys

# Creating a numpy array
arr = np.array([1,2,3,4,5,1000.1])
print("Array with scientific notation", arr)

# Removing the scientific notation
arr = np.array2string(arr, suppress_small=True)

print("Array without scientific notation", arr)

Output:

Array with scientific notation [1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 1.0001e+03]
Array without scientific notation [   1.     2.     3.     4.     5.  1000.1]

Summary

Great! you made it. We have discussed all possible methods to print a numpy.array without scientific notation. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top