Justin
Justin

Reputation: 43

How slicing numpy load file is loaded into memory

If I want to load a portion of a file using numpy.load, I use slicing as:

np.load('myfile.npy')[start:end].

Does this guarantee that this portion from the file, i.e., [start:end], is only loaded into to memory or does it load the entire file first then slice it?

Thanks,

Upvotes: 0

Views: 527

Answers (1)

user2357112
user2357112

Reputation: 282026

That loads the whole thing. If you don't want to load the whole thing, you could mmap the file and only copy the part you want:

part = numpy.load('myfile.npy', mmap_mode='r')[start:end].copy()

Upvotes: 2

Related Questions