string representation of a numpy array with commas separating its elements

PythonNumpy

Python Problem Overview


I have a numpy array, for example:

points = np.array([[-468.927,  -11.299,   76.271, -536.723],
                   [-429.379, -694.915, -214.689,  745.763],
                   [   0.,       0.,       0.,       0.   ]])

if I print it or turn it into a string with str() I get:

print w_points
[[-468.927  -11.299   76.271 -536.723]
 [-429.379 -694.915 -214.689  745.763]
 [   0.       0.       0.       0.   ]]

I need to turn it into a string that prints with separating commas while keeping the 2D array structure, that is:

[[-468.927,  -11.299,   76.271, -536.723],
 [-429.379, -694.915, -214.689,  745.763],
 [   0.,       0.,       0.,       0.   ]]

Does anybody know an easy way of turning a numpy array to that form of string?

I know that .tolist() adds the commas but the result loses the 2D structure.

Python Solutions


Solution 1 - Python

Try using repr

>>> import numpy as np
>>> points = np.array([[-468.927,  -11.299,   76.271, -536.723],
...                    [-429.379, -694.915, -214.689,  745.763],
...                    [   0.,       0.,       0.,       0.   ]])
>>> print(repr(points))
array([[-468.927,  -11.299,   76.271, -536.723],
       [-429.379, -694.915, -214.689,  745.763],
       [   0.   ,    0.   ,    0.   ,    0.   ]])

If you plan on using large numpy arrays, set np.set_printoptions(threshold=np.nan) first. Without it, the array representation will be truncated after about 1000 entries (by default).

>>> arr = np.arange(1001)
>>> print(repr(arr))
array([   0,    1,    2, ...,  998,  999, 1000])

Of course, if you have arrays that large, this starts to become less useful and you should probably analyze the data some way other than just looking at it and there are better ways of persisting a numpy array than saving it's repr to a file...

Solution 2 - Python

Now, in numpy 1.11, there is numpy.array2string:

In [279]: a = np.reshape(np.arange(25, dtype='int8'), (5, 5))

In [280]: print(np.array2string(a, separator=', '))
[[ 0,  1,  2,  3,  4],
 [ 5,  6,  7,  8,  9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]]

Comparing with repr from @mgilson (shows "array()" and dtype):

In [281]: print(repr(a))
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]], dtype=int8)

P.S. Still need np.set_printoptions(threshold=np.nan) for large array.

Solution 3 - Python

The function you are looking for is np.set_string_function. source

What this function does is let you override the default __str__ or __repr__ functions for the numpy objects. If you set the repr flag to True, the __repr__ function will be overriden with your custom function. Likewise, if you set repr=False, the __str__ function will be overriden. Since print calls the __str__ function of the object, we need to set repr=False.

For example:

np.set_string_function(lambda x: repr(x), repr=False)
x = np.arange(5)
print(x)

will print the output

array([0, 1, 2, 3, 4])

A more aesthetically pleasing version is

np.set_string_function(lambda x: repr(x).replace('(', '').replace(')', '').replace('array', '').replace("       ", ' ') , repr=False)

print(np.eye(3))

which gives

[[1., 0., 0.],
 [0., 1., 0.],
 [0., 0., 1.]]

Hope this answers your question.

Solution 4 - Python

Another way to do it, which is particularly helpful when an object doesn't have a _repr_() method, is to employ Python's pprint module (which has various formatting options). Here is what that looks like, by example:

>>> import numpy as np
>>> import pprint
>>>
>>> A = np.zeros(10, dtype=np.int64)
>>>
>>> print(A)
[0 0 0 0 0 0 0 0 0 0]
>>>
>>> pprint.pprint(A)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmartinakoView Question on Stackoverflow
Solution 1 - PythonmgilsonView Answer on Stackoverflow
Solution 2 - PythonK.KitView Answer on Stackoverflow
Solution 3 - PythonvaragrawalView Answer on Stackoverflow
Solution 4 - PythonNYCeyesView Answer on Stackoverflow