How to recover a numpy array from npy.gz file

前端 未结 2 1677
谎友^
谎友^ 2021-01-12 05:55

I\'ve saved a number of numpy objects with the following code:

f = gzip.GzipFile(\'/some/path/file.npy.gz\', \"w\")
np.save(file=f, arr=np.rint(trimmed).asty         


        
相关标签:
2条回答
  • 2021-01-12 06:04

    You've been doing this somehow by your means, but you may use numpy functions instead to save and load objects rather than using other functions.

    You can save desired array by using save() where array_obj is your array which you wish to save.

    array_file = open('array.npy', 'wb')
    numpy.save(array_file, array_obj)
    

    Then, you may retrieve the desired array as following.

    array_file = open('array.npy', 'rb')
    array_obj = numpy.load(array_file)
    

    Use accordingly, hope it helps!

    0 讨论(0)
  • 2021-01-12 06:05

    If it works to save to a gzip file, it might also work to read from one. load is the counterpart to save:

    In [193]: import gzip
    In [194]: f = gzip.GzipFile('file.npy.gz', "w")
    In [195]: np.save(f, np.arange(100))
    In [196]: f.close()
    
    In [200]: f = gzip.GzipFile('file.npy.gz', "r")
    In [201]: np.load(f)
    Out[201]: 
    array([ 0,  1,  2,  3,  4,  .... 98, 99])
    

    There is also a savez(compressed) that saves multiple arrays to a zip archive.

    0 讨论(0)
提交回复
热议问题