6

I have some data as numpy 2D array list-

array([[ 0.62367947],
       [ 0.95427859],
       [ 0.97984112],
       [ 0.7025228 ],
       [ 0.86436385],
       [ 0.71010739],
       [ 0.98748138],
       [ 0.75198057]])

array([[-1.,  1., -1.],    
       [-1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1., -1.,  1.],
       [-1., -1., -1.],
       [ 1.,  1., -1.],
       [ 1., -1., -1.],
       [-1., -1.,  1.]])

And I want to save them in a txt file so that they look like

0.62367947    -1 1 -1    
0.95427859    -1 1 1    
0.97984112     1 1 1

Can someone help me how I can do it using numpy savetxt

2
  • 1
    What error are you getting? Commented May 1, 2013 at 12:11
  • Can you include the line where you actually use savetxt? Commented May 1, 2013 at 12:13

1 Answer 1

6
import numpy as np
R = np.array([[0.62367947],
              [0.95427859],
              [0.97984112],
              [0.7025228],
              [0.86436385],
              [0.71010739],
              [0.98748138],
              [0.75198057]])

phase = np.array([[-1., 1., -1.],
                  [-1., 1., 1.],
                  [1., 1., 1.],
                  [1., -1., 1.],
                  [-1., -1., -1.],
                  [1., 1., -1.],
                  [1., -1., -1.],
                  [-1., -1., 1.]])

np.savetxt('R2.txt', np.hstack([R, phase]), fmt=['%0.8f','%g','%g','%g'])    

yields

0.62367947 -1 1 -1
0.95427859 -1 1 1
0.97984112 1 1 1
0.70252280 1 -1 1
0.86436385 -1 -1 -1
0.71010739 1 1 -1
0.98748138 1 -1 -1
0.75198057 -1 -1 1

np.hstack stacks arrays horizontally. Since R and phase are both 2-dimensional, np.hstack([R, phase]) yields

In [137]: np.hstack([R,phase])
Out[137]: 
array([[ 0.62367947, -1.        ,  1.        , -1.        ],
       [ 0.95427859, -1.        ,  1.        ,  1.        ],
       [ 0.97984112,  1.        ,  1.        ,  1.        ],
       [ 0.7025228 ,  1.        , -1.        ,  1.        ],
       [ 0.86436385, -1.        , -1.        , -1.        ],
       [ 0.71010739,  1.        ,  1.        , -1.        ],
       [ 0.98748138,  1.        , -1.        , -1.        ],
       [ 0.75198057, -1.        , -1.        ,  1.        ]])

Passing this 2D array to np.savetxt gives you the desired result.

Sign up to request clarification or add additional context in comments.

5 Comments

@ unutbu - well, the answer you gave is working for one set of R2 and phase value. But I have 8 sets of R2 and phase values. And I have to use the append to have them in a list. So then when I am trying to save them using your code- it says- array dimensions must agree except for d_0
Collect all your R2 and phase values into a 2-dimensional array. Then call np.savetxt once for the entire 2D array.
array([[ 0.62367947], [ 0.95427859], [ 0.97984112], [ 0.7025228 ], [ 0.86436385], [ 0.71010739], [ 0.98748138], [ 0.75198057]]) array([[-1., 1., -1.], [-1., 1., 1.], [ 1., 1., 1.], [ 1., -1., 1.], [-1., -1., -1.], [ 1., 1., -1.], [ 1., -1., -1.], [-1., -1., 1.]]) I made them 2D arrays. Still I got the same error msg. pls have a look.
Please edit your original post with the code you are using to generate these arrays, how you are calling np.savetxt, and what you want the output to look like. I appreciate that you posted runnable code, but it would be nice if you pared it down to just the relevant part.
thanx for your explanation. Now I understand well and it is according to my result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.