I use the following code to show the element names in a npz file. But it requires loading the file completely, which can be slow especially when the file is large. Is there a way to extract the element names without having to load the file completely?
x = numpy.load(file)
for k in x.iterkeys():
print k
Without reading the entire file into memory, you can access small fragments of large files on disk by using mmap
[memmap documentation]. Default is r+
(Open existing file for reading and writing).
My test code below uses the NpzFile files
attribute [NpzFile documentation], and 'mnist.npz' test data [mnist.npz link], everything seems to be pretty fast in Python 3.6:
>>> import numpy as np
>>> x = np.load('mnist.npz', mmap_mode='r')
>>> for k in x.files:
... print(k)
...
x_test
x_train
y_train
y_test
>>>
Kindly check the linked numpy.memmap for more.
Edit:
print(x.files)
seems to work fine too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With