Printing `numpy.ndarray` on one line

When using scipy / numpy, I get information that I store in numpy.ndarray

>>> a
array([[ 0.15555605,  0.51031528,  0.84580176,  0.06722675],
       [ 0.60556045,  0.62721023, -0.48979983, -0.04152777],
       [-0.78044785,  0.58837543, -0.21146041, -0.13568023],
       [ 0.        ,  0.        ,  0.        ,  1.        ]])
>>> print(a)
[[ 0.15555605  0.51031528  0.84580176  0.06722675]
 [ 0.60556045  0.62721023 -0.48979983 -0.04152777]
 [-0.78044785  0.58837543 -0.21146041 -0.13568023]
 [ 0.          0.          0.          1.        ]]

      

How do I print the result on one line?

I've already checked:

>>> numpy.get_printoptions()
{'precision': 8, 'threshold': 1000, 'edgeitems': 3, 'linewidth': 75, 'suppress': False, 'nanstr': 'nan', 'infstr': 'inf', 'formatter': None}

      

But even setting it linewidth

to 1000 doesn't change that. Is there a way to change the displayed format of this type?

Is it possible to also add a comma between each number (like displaying an array, but no surrounding array(...)

)?

+3


source to share


2 answers


To print numpy.array

in one line, you can convert it to a list with your built-in functionnumpy.tolist()

Example:

import numpy as np

arr = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9)))

      

Simple array printing:



print(arr)
[[1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]]

      

Compared to numpy.tolist()

:

print(array.tolist())
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

      

+7


source


NumPy provides several ways to customize printing, for example np.array2string

.

For this answer, I'll assume you have an array like this:

>>> import numpy as np
... arr = np.array([[ 0.15555605,  0.51031528,  0.84580176,  0.06722675],
...                 [ 0.60556045,  0.62721023, -0.48979983, -0.04152777],
...                 [-0.78044785,  0.58837543, -0.21146041, -0.13568023],
...                 [ 0.        ,  0.        ,  0.        ,  1.        ]])

      

  • If you want to display all elements, you need to set threshold

    to np.inf

    .
  • If you want to have ,

    both a separator, you can set separator

    to ','

    .

However, it has no way to remove line breaks, just

  • max_line_width

    which gives the number of characters printed on one line for the innermost dimension. So it works for 1D arrays when you install max_line_width=np.inf

    , but it doesn't work out of the box for ND arrays.


Fortunately, it returns a string that can be manipulated, for example by deleting all lines:

>>> np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=',').replace('\n', '')
'[[ 0.15555605, 0.51031528, 0.84580176, 0.06722675], [ 0.60556045, 0.62721023,-0.48979983,-0.04152777], [-0.78044785, 0.58837543,-0.21146041,-0.13568023], [ 0.        , 0.        , 0.        , 1.        ]]'

      

Or use a regular expression to remove all whitespace:

>>> import re
>>> re.sub(r'\s+', '', np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=','))
'[[0.15555605,0.51031528,0.84580176,0.06722675],[0.60556045,0.62721023,-0.48979983,-0.04152777],[-0.78044785,0.58837543,-0.21146041,-0.13568023],[0.,0.,0.,1.]]'

      

Consistently they are not really "short" and they are also slower than converting to list

with .tolist()

and then to string, but this is probably a good alternative, especially if you want to customize the printed output without creating a (potentially huge) unnecessary list ...

+1


source







All Articles