我使用numpy的1.9.0和记忆inneficiency
np.loadtxt()和
np.genfromtxt()似乎是直接关系到它们是基于临时表来存储数据的事实:
- 看到这里的
np.loadtxt()
- 和这里的
np.genfromtxt()
通过预先了解
shape数组的大小,您可以想到一个文件读取器,该读取器将通过使用相应的数据存储数据来消耗非常接近理论内存量(在这种情况下为3.2
GB)的内存
dtype:
def read_large_txt(path, delimiter=None, dtype=None): with open(path) as f: nrows = sum(1 for line in f) f.seek(0) ncols = len(f.next().split(delimiter)) out = np.empty((nrows, ncols), dtype=dtype) f.seek(0) for i, line in enumerate(f): out[i] = line.split(delimiter) return out
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)