numpys tostring/fromstring --- what do I need to specify to restore the array

2024/10/10 4:28:22

Given a raw binary representation of a numpy array, what is the complete set of metadata needed to unambiguously restore the array?

For example,

>>> np.fromstring( np.array([42]).tostring())
array([  2.07507571e-322])

which is to be expected (with a hindsight, at least): here the I haven't told fromstring to expect ints, so it goes with the default float.

But it seems to me that just specifying the dtype=np.float64 or similar may or may not be sufficient. For example,

>>> a = np.array([42.])
>>> a.dtype
dtype('float64')
>>> a.dtype.byteorder
'='

which the docs tell me means 'native order'. Meaning, it's going to be interpreted differently on a big-endian and little-endian machines --- or am I missing something simple?

Answer

sys.byteorder gives the endianness of the machine.


However, as @J.F.Sebastain, @seberg and @jorgeca have suggested, np.savez is a better way to go. The help docstring shows

import io
content = io.BytesIO()
np.savez(content, x=x, y=y)
content.seek(0)

which means you could save the string content to an sqlite database.

Then, when you SELECT this string from the database, it can be re-converted into numpy arrays with

data = np.load(content)
https://en.xdnf.cn/q/69934.html

Related Q&A

How to limit width of column headers in Pandas

How can I limit the column width within Pandas when displaying dataframes, etc? I know about display.max_colwidth but it doesnt affect column names. Also, I do not want to break the names up, but rath…

Django + Auth0 JWT authentication refusing to decode

I am trying to implement Auth0 JWT-based authentication in my Django REST API using the django-rest-framework. I know that there is a JWT library available for the REST framework, and I have tried usin…

How to measure the angle between 2 lines in a same image using python opencv?

I have detected a lane boundary line which is not straight using hough transform and then extracted that line separately. Then blended with another image that has a straight line. Now I need to calcula…

How to modify variables in another python file?

windows 10 - python 3.5.2Hi, I have the two following python files, and I want to edit the second files variables using the code in the first python file.firstfile.pyfrom X.secondfile import *def edit(…

SciPy optimizer ignores one of the constraints

I am trying to solve an optimization problem where I need to create a portfolio that with a minimum tracking error from benchmark portfolio and its subject to some constraints:import scipy.optimize as …

How can one use HashiCorp Vault in Airflow?

I am starting to use Apache Airflow and I am wondering how to effectively make it use secrets and passwords stored in Vault. Unfortunately, search does not return meaningful answers beyond a yet-to-be-…

List all words in a dictionary that start with user input

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string?Ex: User: "abd" Program:abdicate, abdomen, abduct..…

Python version of C#s conditional operator (?)

I saw this question but it uses the ?? operator as a null check, I want to use it as a bool true/false test.I have this code in Python:if self.trait == self.spouse.trait:trait = self.trait else:trait…

Python String Replace Error

I have a python script that keeps returning the following error:TypeError: replace() takes at least 2 arguments (1 given)I cannot for the life of me figure out what is causing this.Here is part of my c…

How to run two modules at the same time in IDLE

I am working on a super simple socket program and I have code for the client and code for the server. How do I run both these .py files at the same time to see if they work ?