How to store multiple numpy 1d arrays with different lengths and print them

I got multiple numpy (X,) arrays from a loop and would like to store them. I know that I could add them to a regular python list

A = []
for i in range(10):
  A.append(some_numpy_array)

      

However, this solution does not look elegant and cannot be parsed with numpy.savetxt.

numpy.savetxt("out.txt", A)

      

Is there any other solution?

0


source to share


1 answer


So you have a list of arrays

In [6]: alist = [np.arange(i) for i in range(3,7)]
In [7]: alist
Out[7]: 
[array([0, 1, 2]),
 array([0, 1, 2, 3]),
 array([0, 1, 2, 3, 4]),
 array([0, 1, 2, 3, 4, 5])]

      

What's ridiculous about this? You can wrap it in an array of objects

In [8]: arr=np.array(alist)
In [9]: arr
Out[9]: 
array([array([0, 1, 2]), array([0, 1, 2, 3]), array([0, 1, 2, 3, 4]),
       array([0, 1, 2, 3, 4, 5])], dtype=object)

      

This loses list methods like append

but gains type arrays reshape

. And the array math has to resort to python level iteration, equivalent to the speed of a list. And there's one big gotcha. All sublists are the same length, the result is a 2d int array, not a single object.

savetxt

given a list, will turn it into an array like I did and then try to write "rows". savetxt

designed to produce CSV output - neat rows and columns with delimiter. This display doesn't make sense with this list, does it?



In [11]: np.savetxt('test.txt',alist,fmt='%s',delimiter=',')
In [12]: cat test.txt
[0 1 2]
[0 1 2 3]
[0 1 2 3 4]
[0 1 2 3 4 5]

      

This wrote the list / array as a 1d array with common string formatting for each line. Effectively

In [13]: for row in alist:
    ...:     print(row)
    ...:     
[0 1 2]
[0 1 2 3]
[0 1 2 3 4]
[0 1 2 3 4 5]

      

You can iterate savetxt

over an open file, writing one array at a time:

In [18]: with open('test.txt','wb') as f:
    ...:     for row in alist:
    ...:         np.savetxt(f, [row], fmt='%5d',delimiter=',')      
In [19]: cat test.txt
    0,    1,    2
    0,    1,    2,    3
    0,    1,    2,    3,    4
    0,    1,    2,    3,    4,    5

      

People face the same problem when trying to write 3D arrays or other arrays that don't fit the simple 2d numeric array model. You don't need to use savetxt

to write text to a file. Plain Python has sufficient tools to do this.

0


source







All Articles