Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a numpy matrix in a text file - python

Suppose I am getting a numpy matrix from some calculation. Here is my numpy matrix 'result1'::

    result1=
    [[   1.         0.         0.         0.00375   -0.01072   -0.      -1000.     ]
     [   2.         3.         4.         0.        -0.004    750.         0.     ]
     [   3.         3.         0.         0.         0.      -750.      1000.     ]]

Now I want to write this matrix in a text file named 'result.txt'. For this, I wrote the following code::

np.savetxt('result.txt', result1, fmt='%.2e')

But it is giving me all the elements of the matrix in one row.

    1.00e+00 0.00e+00 0.00e+00 3.75e-03 -1.07e-02 -1.14e-13 -1.00e+032.00e+00 3.00e+00 4.00e+00 0.00e+00 -4.00e-03 7.50e+02 0.00e+003.00e+00 3.00e+00 0.00e+00 0.00e+00 0.00e+00 -7.50e+02 1.00e+03

I want to write the matrix in the text file in the proper matrix format. How can I do this? I used keyword newline='\n' or newline='',but the result is same.

Thanks in advance...

=======

This edited part is for @Warren

try this one:

>>> import numpy as np
>>> mat=np.matrix([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
>>> mat
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
>>> np.savetxt('text.txt',mat,fmt='%.2f')

in my text.txt file, I am getting:

1.00 2.00 3.004.00 5.00 6.007.00 8.00 9.00

like image 704
Nafees Avatar asked Mar 01 '14 18:03

Nafees


People also ask

How do you write NumPy array to text file in Python?

Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.

How do I append a NumPy array to a text file?

The NumPy module savetxt() method is used to append the Numpy array at the end of the existing csv file with a single format pattern fmt = %s. We will open CSV file in append mode and call the savetxt() method to writes rows with header. Second call the savetxt() method to append the rows.

How do you write an array to a text file in Python?

We can save an array to a text file using the numpy. savetxt() function. The function accepts several parameters, including the name of the file, the format, encoding format, delimiters separating the columns, the header, the footer, and comments accompanying the file.

How do I save a NumPy array to a file?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.


1 Answers

like Francesco Nazzaro's answer, but a little different to make sure the file can be opened successfully, try:

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mat = np.matrix(a)
with open('outfile.txt','wb') as f:
    for line in mat:
        np.savetxt(f, line, fmt='%.2f')
like image 169
DAYHU Avatar answered Oct 01 '22 09:10

DAYHU