0

I want to take a simple, single column file containing floats into multiple numpy arrays that are each created from the same number of lines.

So, for example, if the file has 180 lines, I'd like to create 3 numpy arrays, one containing data from lines 1-60, 2nd from 61 to 120, and 3rd from 121 to 180.

I was attempting to use the np.fromfile() function, but this doesn't seem to be able to respond to file pointers (as in, seek to 1st line, read 60 lines, then seek to 61, read 60, ...). Does anyone know how this can be done efficiently?

1 Answer 1

2
arr1, arr2, arr3 = np.loadtxt(path).reshape(3,-1)

Just read all the data into an array, then reshape the array to have 3 rows. The -1 in the call to reshape will be replaced by reshape by whatever number makes sense. For example, if the array has length 180, then upon reshaping, the array will have 60 columns. Note that the length of the original array must be exactly divisible by 3.

Alternatively,

arr1, arr2, arr3 = np.array_split(np.loadtxt(path), 3)

This is a bit more robust since array_split will split the array into 3 parts even if the length of the original array is not exactly divisible by 3.

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.