您绝对应该使用
numpy.save,您仍然可以在内存中使用它:
>>> import io>>> import numpy as np>>> import zlib>>> f = io.BytesIO()>>> arr = np.random.rand(100, 100)>>> np.save(f, arr)>>> compressed = zlib.compress(f.getvalue())
要解压缩,请逆向执行此过程:
>>> np.load(io.BytesIO(zlib.decompress(compressed)))array([[ 0.80881898, 0.50553303, 0.03859795, ..., 0.05850996, 0.9174782 , 0.48671767], [ 0.79715979, 0.81465744, 0.93529834, ..., 0.53577085, 0.59098735, 0.22716425], [ 0.49570713, 0.09599001, 0.74023709, ..., 0.85172897, 0.05066641, 0.10364143], ..., [ 0.89720137, 0.60616688, 0.62966729, ..., 0.6206728 , 0.96160519, 0.69746633], [ 0.59276237, 0.71586014, 0.35959289, ..., 0.46977027, 0.46586237, 0.10949621], [ 0.8075795 , 0.70107856, 0.81389246, ..., 0.92068768, 0.38013495, 0.21489793]])>>>
如您所见,它与我们之前保存的内容匹配:
>>> arrarray([[ 0.80881898, 0.50553303, 0.03859795, ..., 0.05850996, 0.9174782 , 0.48671767], [ 0.79715979, 0.81465744, 0.93529834, ..., 0.53577085, 0.59098735, 0.22716425], [ 0.49570713, 0.09599001, 0.74023709, ..., 0.85172897, 0.05066641, 0.10364143], ..., [ 0.89720137, 0.60616688, 0.62966729, ..., 0.6206728 , 0.96160519, 0.69746633], [ 0.59276237, 0.71586014, 0.35959289, ..., 0.46977027, 0.46586237, 0.10949621], [ 0.8075795 , 0.70107856, 0.81389246, ..., 0.92068768, 0.38013495, 0.21489793]])>>>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)