1

This is probably overkill for my problem, but I am curious about the answer.

I have a matrix of np.float32 values that I want to put into some code. It's 50x3 so I want to just put it in the source directly - it's not something that will change often. It's a little sensitive to rounding, so I want to encode the data exactly if possible. Is there a good way to do this in the source while also preserving the matrix format?

I'm thinking something along the lines of putting [[0xC45B36F3, ...],...] and somehow encoding that to a np.float32.

3 Answers 3

2

If you chose to encode the integer values, you could then do:

int_data = np.array([[0xC45B36F3, ...],...], dtype=np.uint32)
floats = int_data.view(np.float32)
Sign up to request clarification or add additional context in comments.

Comments

2

Why don't you use the numpy.save builtin? I've tried it with several float32 inputs and the numpy.load had no rounding issues.

Or is there any reason why you can't or don't want to do it that way?

1 Comment

Each row is associated to a separate set of data, so I wanted to have a matrix in the source so it would be easy to see changes per dataset.
1

You can convert the matrix m in bytes :

m.tobytes()  

Then you paste the bytes it in your code :

data=b'\x34f\xd3.......paste the 4*50*3 bytes here ......\x12'

After you can reconstruct the exact matrix :

m=matrix(frombuffer(data,float32).reshape(50,3)) 

without any loss.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.