0

I have the following numpy array:

matrix = numpy.zeros([30, 30], dtype = numpy.int32)

I later change the values of this array and save it to a file like so:

conf_mat = open("conf_mat.txt", "w")
conf_mat.write(str(matrix))

But the result in the file looks like this

[[    0     0     0     8   161     0    18     0     0     0     0     0
     13     0     1     0   140     2     0     0     8     0    14     0
      0     0     0     0     0     0]
 [    0     0     0    41    31     0    39     0     0     0     0     0
     44     0     0     0    21    39     0     0    39     0   105     0
      0     0     0     0     0     0]
 [    0     0     0    71   162     0   155     0     0     1     0     0
      6     0     0     0   350   110     0     0     8     0    21     0
      0     0     0     0     0     0]
 ...

As you can see 1 row of the matrix is separated into 3 lines in the document. How can I write the whole row of the matrix on just one row of the file?

PS: I don't know if it makes a difference, but I am using a remote linux server via ssh connection.

1
  • Are you sure each row isn't written in one line and your text editor just wraps around long lines? Your main issue is that you do str(matrix) which can be a hassle to read the file back wih all the square brackets. It's best to stick to numpy.loadtxt as other answers have suggested. Commented Feb 15, 2021 at 12:50

2 Answers 2

2

To save numpy arrays, use numpy.save (npy format) or numpy.savetxt (CSV/plaintext), and to load numpy.load or numpy.loadtxt, see https://numpy.org/devdocs/user/how-to-io.html and https://numpy.org/doc/stable/reference/routines.io.html

numpy.savetxt can only store 1D or 2D arrays, though. And to store the datas as integer values instead of exponential notation, use fmt='%d' as a keyword argument to numpy.savetxt, e.g. numpy.savetxt("foo", matrix, fmt='%d'). For more info on the format flags, please check https://numpy.org/doc/stable/reference/generated/numpy.savetxt.html

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

4 Comments

When I try .savetxt() the values look like this 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 .... How can I make them ints?
Pass fmt='%d' as a keyword argument
And one more question. Can I make it so that each value takes like 5 spaces so that the data is more ordered and each value is exactly above the value of the row below? Do you understand what I am talking about?
You can use fmt='%5d', to pad the number with spaces, where the 5 denotes the minimum width of the field. Numbers which take more space than that will still be expanded to have enough space. I added a link to the documentation of savetxt.
0

Slightly off-topic: you could also use pandas to save the numpy matrix:

>>> import pandas as pd
>>> import numpy as np
>>> pd.DataFrame(np.zeros([4, 2])).to_csv()
',0,1\n0,0.0,0.0\n1,0.0,0.0\n2,0.0,0.0\n3,0.0,0.0\n'
>>> 

(if you use to_csv(<filename>) it will be written to disk instead)

1 Comment

Thanks for the answer. I am not looking for csv format, but it could be useful for somebody else. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.