Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string representation of a numpy array with commas separating its elements

Tags:

python

numpy

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.

like image 596
martinako Avatar asked May 07 '13 16:05

martinako


People also ask

Why does my NumPy array have commas?

A parenthesized number followed by a comma denotes a tuple with one element. The trailing comma distinguishes a one-element tuple from a parenthesized n . In a dimension entry, instructs NumPy to choose the length that will keep the total number of array elements the same.

How do you divide each element in a NumPy array in Python?

divide() in Python. numpy. divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise).

Can you have a string in a NumPy array?

The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects. When the elements are numbers, they must all be of the same type. For example, they might be all integers or all floating point numbers.

How do you split the element of a given NumPy array with spaces?

To split the elements of a given array with spaces we will use numpy. char. split(). It is a function for doing string operations in NumPy.


2 Answers

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...

like image 96
mgilson Avatar answered Sep 21 '22 13:09

mgilson


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.

like image 31
K.Kit Avatar answered Sep 23 '22 13:09

K.Kit