numpy array2string applied on huge array, skips central values, ( ... in the middle )

2024/10/2 14:30:47

I have array of size (3, 3, 19, 19), which I applied flatten to get array of size 3249.

I had to write these values to file along with some other data, so I did following to get the array in string.

np.array2string(arr.flatten(), separator=', ', suppress_small=False)

However when I checked the content of the files after write, I noticed that I have ,... , in the middle of the array as following

[ 0.09720755, -0.1221265 , 0.08671697, ..., 0.01460444, 0.02018792,0.11455765]

How can I get string of array with all the elements, so I can potentially get all data to a file?

Answer

As far as I understand array2string, it's just for returning a "nice" string representation of the array.

numpy.ndarray.tofile might be a better option for your purposes - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html. It should write the full contents of the array to the given file.

with open("test.bin", "wb") as f:arr.flatten().tofile(f)

And you can of course read it back with numpy.fromfile - https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html.

with open("test.bin", "rb") as f:arr = numpy.fromfile(f)
https://en.xdnf.cn/q/70846.html

Related Q&A

save password as salted hash in mongodb in users collection using python/bcrypt

I want to generate a salted password hash and store it in MongoDB collection called users, like this:users_doc = { "username": "James","password": "<salted_hash_pa…

Get the min of [0, x] element wise for a column

I need to compute a column where the value is the result of a vectorized operation over other columns: df["new_col"] = df["col1"] - min(0,df["col2"])It turned out, however…

Virtual column in QTableView?

Im started to learning Qt4 Model/View Programming and I have beginner question.I have simple application which show sqlite table in QTableView:class Model(QtSql.QSqlTableModel):def __init__(self, paren…

Python httplib2, AttributeError: set object has no attribute items

Im playing with the Python library httplib2. The following is my code. import urllib.parse import httplib2httplib2.debuglevel = 1http = httplib2.Http()url = "http://login.sina.com.cn/hd/signin.php…

Atom IDE autocomplete-python not working

I have just installed the Atom IDE and the package autocomplete-python (on Windows). But the package is not working. Do I have to make any setting changes? (I have disabled autocomplete-plus and autoc…

Multiple instances of a class being overwritten at the same time? (Python)

Heres a very simple code I made to demonstrate the problem Im encountering. Whats happening here is that Im creating two different instances of the same class but changing an attribute of one will cha…

how to store binary file recieved by Flask into postgres

I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route(/upload, methods=[POST]) def upload_file():def allowed_file(f):return Truefile …

How can I kill a single shot QtCore.QTimer in PyQt4?

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call t…

How to convert list of lists to a set in python so I can compare to other sets?

I have a list users_with_invites_ids_list, formed by loop where I append values to the list, in python that looks like this:...[ObjectId(55119e14bf2e4e010d8b48f2)], [ObjectId(54624128bf2e4e5e558b5a52)]…

How can I create an ODBC connection to SAS?

Im writing a program that needs to access SAS data. Ive downloaded the ODBC drivers for SAS and installed them, but I need to be able to create ODBC connections on the fly, programmatically. The foll…