4

I have three arrays:

a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.array([7,8])

I want to do something like this:

np.savetxt('data.txt',np.array(a,b,c))

or just

np.savetxt('data.txt',(a,b,c))

but I get

 TypeError: float argument required, not numpy.ndarray

I've circumvented this issue by doing something like

np.savetxt('data.txt',np.array([a[0],a[1],...,c[2]]))

but this is not very satisfying (especially because the array sizes can change) and also plots all the values in a single column rather than a single row and multiple columns (how I want it).

The issue seems to be that the arrays are of different dimension. The kind of output I'd ideally like using the above example is:

   1 2 3 4 5 6 7 8

Does anyone know a nicer way of doing this?

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
user1654183
  • 3,553
  • 6
  • 20
  • 31
  • possible duplicate of [How to write a multidimensional array to a text file?](http://stackoverflow.com/questions/3685265/how-to-write-a-multidimensional-array-to-a-text-file) – jamylak May 07 '13 at 11:15

4 Answers4

4

You can try to concatenate them also with numpy.r_

np.savetxt('data.txt',np.r_[a,b,c])
Francesco Montesano
  • 7,562
  • 2
  • 38
  • 62
1

Your code works just fine for me:

$ python
Python 2.7.3 (default, Nov  7 2012, 22:09:53) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> c = np.array([7,8,9])
>>> np.savetxt('data.txt', (a,b,c))
>>> ^D
$ cat data.txt
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00
4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00
7.000000000000000000e+00 8.000000000000000000e+00 9.000000000000000000e+00

If that isn't working for you, I suppose your NumPy may be too old. In that case, here's another way, which at the same time transposes the way the data are written to the file, which may be interested as well:

np.savetxt('data.txt', np.column_stack((a,b,c)))
John Zwinck
  • 207,363
  • 31
  • 261
  • 371
  • John - thanks for your response. I made a mistake in my initial question. The 'minimal' test case I provided was a little too minimal. As you say the original code I provided worked. The update explains the error I've been getting better, hopefully. – user1654183 May 07 '13 at 11:33
0

I realised one possible way is just to np.hstack((a,b,c)) before sending to np.savetxt. I'm not sure there will be a 'nicer' way in this case.

user1654183
  • 3,553
  • 6
  • 20
  • 31
0

i think this is what you want.

>>>a=np.array([1,2,3])
>>>b=np.array([4,5,6])
>>>c=np.array([7,8,9])
>>>np.savetxt('data.txt',(list(a)+list(b)+list(c)),fmt='%s',delimiter=',',newline=' ')
>>>!cat data.txt
1 2 3 4 5 6 7 8 9
user2179627
  • 197
  • 1
  • 2
  • 13
  • or as per Francescos idea, you can concat arrays and do something like this np.savetxt('data.txt',np.r_[a,b,c],fmt='%s',delimiter=',',newline=' ') – user2179627 May 07 '13 at 15:04