1

I have an N by 6 by f numpy array. I would like to be able to write this as a binary file and then read it by another python script into an array of the same dimensions.

To test this, I do the following (based on this question):

import numpy as np

farray=np.array([[3.14, 2.7, 0.0, -1.0, 1.1],[3.14, 2.7, 0.0, -1.0, 1.1]])
testf = open('test','wb')
farray.tofile(testf)
testf.close()

arraytest = np.fromfile('test')

At which point arraytest.shape is (10,). How can I make sure it is (2,5) instead without having to know 2 and 5 in advance to reshape the array? Can I somehow encode this information in the binary file as you would do with \n in a regular text file?

1 Answer 1

3

Why not use np.save & np.load?

import numpy as np
farray=np.array([[3.14, 2.7, 0.0, -1.0, 1.1],[3.14, 2.7, 0.0, -1.0, 1.1]])
np.save('nums.npy', farray)
loaded_farray = np.load('nums.npy')
print(loaded_farray)

[[ 3.14 2.7 0. -1. 1.1 ] [ 3.14 2.7 0. -1. 1.1 ]]

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

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.