How to suppress the use of scientific notations for small numbers using NumPy?


When working with NumPy arrays, you may encounter small numbers represented in scientific notation. Although this compact representation is advantageous, deciphering or comparing values can be arduous. This guide delves into four distinct techniques to abate scientific notation usage for diminutive numbers in NumPy arrays: employing numpy.vectorize alongside string formatting, utilizing numpy.ndarray.round, leveraging string formatting, and harnessing numpy.set_printoptions. Examples will elucidate these methodologies, discussing pros and cons, and providing an all-encompassing comprehension of each approach.

Method 1: Using numpy.vectorize with string formatting

numpy.vectorize function, when combined with string formatting, can suppress scientific notation in NumPy arrays. This approach is beneficial for applying specific formatting to each element, resulting in a new array of formatted strings.

Syntax

formatted_array = numpy.vectorize('{:.Nf}'.format)(array)

Here, N denotes the decimal places to retain, and '{:.Nf}' represents the string formatting syntax for floating-point numbers with N decimal places. The numpy.vectorize function applies the string formatting to each element.

Example

In the following example, we construct a NumPy array with diminutive figures and employ numpy.vectorize with string formatting to abate scientific notation.

import numpy as np

array = np.array([1e-10, 2e-10, 3e-10])
formatted_array = np.vectorize('{:.10f}'.format)(array)
print(formatted_array)

Output

['0.0000000001' '0.0000000002' '0.0000000003']

Advantages

  • Empowers specific formatting applications to each element.

  • Adaptable to alternative formatting requisites.

Disadvantages

  • Outputs an array of strings, unsuitable for further numerical operations.

Method 2: Using numpy.ndarray.round

numpy.ndarray.round function facilitates rounding NumPy array elements to a specified decimal place count, effectively abating scientific notation. This method is useful for retaining the output as a NumPy array with numerical values.

Syntax

rounded_array = array.round(N)

Here, N represents the decimal places to retain. The round function rounds each element to N decimal places.

Example

In the subsequent example, we create a NumPy array with diminutive figures and utilize numpy.ndarray.round to abate scientific notation.

import numpy as np

array = np.array([1e-10, 2e-10, 3e-10])
rounded_array = array.round(10)
print(rounded_array)

Output

[0.0000000001 0.0000000002 0.0000000003]

Advantages

  • Retains output as a NumPy array with numerical values.

  • Suitable for further numerical operations.

Disadvantages

  • Inadequate for specific string formatting requirements.

3: Using string formatting

String formatting facilitates the formatting of each element in a NumPy array, suppressing scientific notation. This methodology resembles using numpy.vectorize with string formatting but employs list comprehension.

Syntax

formatted_array = ['{:.Nf}'.format(x) for x in array]

Here, N denotes decimal places to retain, and '{:.Nf}' represents the string formatting syntax for floating-point numbers with N decimal places. List comprehension applies the string formatting to each element.

Example

In the subsequent example, we create a NumPy array with diminutive figures and leverage string formatting to abate scientific notation.

import numpy as np

array = np.array([1e-10, 2e-10, 3e-10])
formatted_array = ['{:.10f}'.format(x) for x in array]
print(formatted_array)

Output

['0.0000000001', '0.0000000002', '0.0000000003']

Advantages

  • Empowers specific formatting applications to each element.

  • Adaptable to alternative formatting requisites.

Disadvantages

  • Outputs a list of strings, unsuitable for further numerical operations.

Method 4: Using numpy.set_printoptions

numpy.set_printoptions function facilitates setting global printing options for NumPy arrays, including abating scientific notation. This approach is beneficial for modifying default printing behavior for all NumPy arrays in your code.

Syntax

np.set_printoptions(suppress=True, precision=N)

Here, suppress=True disables scientific notation, and precision=N sets the decimal places to retain.

Example

In the subsequent example, we create a NumPy array with diminutive figures and harness numpy.set_printoptions to abate scientific notation.

import numpy as np

np.set_printoptions(suppress=True, precision=10)
array = np.array([1e-10, 2e-10, 3e-10])
print(array)

Output

[0.0000000001 0.0000000002 0.0000000003]

Advantages

  • Alters default printing behavior for all NumPy arrays in your code.

  • Output remains a NumPy array with numerical values, suitable for further numerical operations.

Disadvantages

  • Impacts all NumPy arrays in your code, potentially undesired in specific cases.

  • May not be suitable if specific string formatting is required for the output.

Conclusion

In this guide, we examined how to abate scientific notation usage for minuscule figures in NumPy arrays using various techniques. We explored employing `numpy.vectorize` with string formatting, utilizing `numpy.ndarray.round`, leveraging string formatting, and harnessing `numpy.set_printoptions`. Each method offers a straightforward and efficient means to display NumPy arrays without scientific notation, depending on your specific requirements and preferences.

By comprehending the advantages and disadvantages of each method, you can select the most suitable approach for your specific needs.

Updated on: 28-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements