Python print numpy array format

Python Print Numpy Array with Precision

Most of the time, the Numpy package is used for scientific computational data in arrays, which means that the length of the values can be massive, especially that of the floating-point values or values defined with scientific notation. To format the print result from these numpy arrays, the user can utilize the set_printoptions() method.

This post will explain how to use and format the output of your print statement up to a specific precision in Python. The content of this guide contains the following:

The set_printoptions() Method

The set_printoptions() method is used to define the default settings for the print statements when printing out the Numpy arrays. This set_printoptions() takes many different arguments. The general syntax of this method is defined below:

  • edgeitems: This argument defines the number of items to display on both ends in the output for each dimension of the array. All other items in between would not be displayed in the output.
  • precision: This argument defines the number of digits to display after the floating point
  • suppress: This argument takes in a boolean value and defines whether or not to display the value in scientific notation.

Let’s see how to print out the values of an array up to a specific precision.

How to Print Numpy Array With Precision Using set_printoptions() Method?

To demonstrate the use of the set_printoptions() method, start by first creating an array that contains floating point values with the following line of code:

setArray = numpy.array ( [ 1.2785 , 4.129837 , 0.112 , 65.2322331 ] )

After that, call the set_printoptions() method and, in the argument, define the precision value (3 for this post):

Lastly, print out the Numpy array with the help of the print() method:

When this code is executed, it will produce the following result on the terminal:

As you can observe in the output, the values were printed with only three digits after the floating point.

How to Print Scientific Values With Precision in Numpy Arrays?

Another thing is that the Numpy arrays can hold the values in the form of scientific notations, and when you want to print out those values with a set precision value, you can use the set_printoptions() method. To demonstrate this, simply start by importing the numpy package and creating an array with the following line:

Читайте также:  Rock paper scissors python code

setArray = numpy.array ( [ 1.3e-6, 1.2e-5, 1.1e-4 ] )

After that, simply use the set_printoptions() method and pass the argument “suppress=True” to convert the scientific notation to floating point notation, and pass the precision argument to specify the number of digits after the floating point:

Once that is done, simply print out the array onto the terminal using the print() method:

When this program is executed, it produces the following result on the terminal:

As you can see that you were able to get the print from a Numpy array with scientific notation values up to a specific precision.

Conclusion

To get the output/print of values from Numpy arrays up to a specific precision point, the user can utilize the set_printoptions() method. To do this, the user has to call the set_printoptions() method with the “precision” argument and specify the number of digits to be displayed after the decimal/floating point. Also, with the “suppress” argument, the user has the option to change the format of the scientific notation values to floating point values.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor’s degree in computer science.

Источник

How to Print an Array in Python

Print Arrays In Python

So before we get right into the topic, let us know a bit about Arrays in Python.

Python Arrays

Arrays are a collection of data elements of the same type under the same name. In Python, we can implement arrays using lists or the NumPy module. The NumPy module provides us with arrays of type ndarray (NumPy Array).

Further, an array can be multi-dimensional. As we know, the simplest form of multi-dimensional arrays is two-dimensional arrays. Hence, in this tutorial, we are going to be considering 1D as well as 2D Arrays.

Upskill 2x faster with Educative

Supercharge your skillset with Educative Python courses → use code: ASK15 to save 15%

Ways to Print an Array in Python

Now, let us look at some of the ways to print both 1D as well as 2D arrays in Python. Note: these arrays are going to be implemented using lists.

Directly printing using the print() method

We can directly pass the name of the array(list) containing the values to be printed to the print() method in Python to print the same.

But in this case, the array is printed in the form of a list i.e. with brackets and values separated by commas.

arr = [2,4,5,7,9] arr_2d = [[1,2],[3,4]] print("The Array is: ", arr) #printing the array print("The 2D-Array is: ", arr_2d) #printing the 2D-Array
The Array is: [2, 4, 5, 7, 9] The 2D-Array is: [[1, 2], [3, 4]]

Here, arr is a one-dimensional array. Whereas, arr_2d is a two-dimensional one. We directly pass their respective names to the print() method to print them in the form of a list and list of lists respectively.

Читайте также:  Replace first symbol in string java

Using for loops in Python

We can also print an array in Python by traversing through all the respective elements using for loops.

arr = [2,4,5,7,9] arr_2d = [[1,2],[3,4]] #printing the array print("The Array is : ") for i in arr: print(i, end = ' ') #printing the 2D-Array print("\nThe 2D-Array is:") for i in arr_2d: for j in i: print(j, end=" ") print()
The Array is : 2 4 5 7 9 The 2D-Array is: 1 2 3 4

In the code above we traverse through the elements of a 1D as well as a 2D Array using for loops and print the corresponding elements in our desired form.

Ways to print NumPy Array in Python

As mentioned earlier, we can also implement arrays in Python using the NumPy module. The module comes with a pre-defined array class that can hold values of same type.

These NumPy arrays can also be multi-dimensional. So, let us see how can we print both 1D as well as 2D NumPy arrays in Python.

Using print() method

Similar to the case of arrays implemented using lists, we can directly pass NumPy array name to the print() method to print the arrays.

import numpy as np arr_2d = np.array([[21,43],[22,55],[53,86]]) arr = np.array([1,2,3,4]) print("Numpy array is: ", arr) #printing the 1d numpy array print("Numpy 2D-array is: ", arr_2d) #printing the 2d numpy array
Numpy array is: [1 2 3 4] Numpy 2D-array is: [[21 43] [22 55] [53 86]]

Here, arr and arr_2d are one 1D and one 2D NumPy arrays respectively. We pass their names to the print() method and print both of them. Note: this time also the arrays are printed in the form of NumPy arrays with brackets.

Using for loops

Again, we can also traverse through NumPy arrays in Python using loop structures. Doing so we can access each element of the array and print the same. This is another way to print an array in Python.

Look at the example below carefully.

import numpy as np arr = np.array([11,22,33,44]) arr_2d = np.array([[90,20],[76,45],[44,87],[73,81]]) #printing the numpy array print("The Numpy Array is : ") for i in arr: print(i, end = ' ') #printing the numpy 2D-Array print("\nThe Numpy 2D-Array is:") for i in arr_2d: for j in i: print(j, end=" ") print()
The Numpy Array is : 11 22 33 44 The Numpy 2D-Array is: 90 20 76 45 44 87 73 81

Here also we print the NumPy array elements in our desired way(without brackets) by accessing the elements of the 1D and 2D array individually.

Conclusion

So in this tutorial, we learned how to print an array in Python. I hope you now have a clear understanding of the topic. For any further question related to the topic, feel free to use the comments.

References

Источник

Numpy – Print Array With Commas

The Numpy library in Python comes with a number of useful functions to work with and manipulate arrays. In this tutorial, we will look at how to print a numpy array with elements separated by commas with the help of some examples.

Читайте также:  Filtering function in php

Printing a Numpy Array

First, let’s see what we get when we directly print a numpy array.

import numpy as np # create numpy array ar = np.array([1, 2, 3, 4, 5]) # print the array print(ar)

Here, we created a numpy array of five numbers and then printed it using the print() function. You can see that the elements are printed with a space between them.

How to print a numpy array with commas?

print numpy array with commas

You can use the numpy array2string() function to print a numpy array with elements separated by commas. Pass the array and the separator you want to use (“,” in our case) to the array2string() function.

The following is the syntax –

# print numpy array with "," separator print(np.array2string(ar, separator=","))

It returns a string representation of the array with the given separator.

Let’s now apply this function to the above array such that its elements are separated by commas on printing.

# print array with "," as separator print(np.array2string(ar, separator=","))

The array elements are separated by commas.

If the printed array looks cluttered, you can specify , (a comma followed by a space) as the separator.

# print array with ", " as separator print(np.array2string(ar, separator=", "))

The elements now look less cluttered.

Use the repr() function

Alternatively, you can use the Python built-in repr() function to print a numpy array with commas. The repr() function is used to print the string representation of an object in Python.

The array elements are separated by commas. Note that the output is not exactly the same as what we got with the array2string() function.

Summary – Print Numpy Array with Comma as a Separator

In this tutorial, we looked at how to print a numpy array with a comma as a separator. The following are the steps mentioned in this tutorial –

  • Create a numpy array (skip this step if you already have a numpy array to operate on).
  • Use the numpy array2string() function to get a string representation of the array with the desired separator (a comma in our case).
    There are alternative methods as well, such as using the repr() function.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

Оцените статью