我需要将一个非常“高”的两列数组写入文本文件,而且速度非常慢。我发现如果我将数组重塑为更宽的数组,写入速度会快得多。例如
import time
import numpy as np
dataMat1 = np.random.rand(1000,1000)
dataMat2 = np.random.rand(2,500000)
dataMat3 = np.random.rand(500000,2)
start = time.perf_counter()
with open('test1.txt','w') as f:
np.savetxt(f,dataMat1,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
start = time.perf_counter()
with open('test2.txt','w') as f:
np.savetxt(f,dataMat2,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
start = time.perf_counter()
with open('test3.txt','w') as f:
np.savetxt(f,dataMat3,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
Run Code Online (Sandbox Code Playgroud)
在三个数据矩阵中元素数量相同的情况下,为什么最后一个比其他两个更耗时?有没有办法加快写入“高”数据数组的速度?
我有一个多维数组与numpy保存,只想部分加载一些维度,因为数组非常大.
我怎么能以简单的方式做到这一点?
编辑:上下文简单而基本:
你有5 Gb数组保存numpy.save.但是,您只需要访问数组的某些部分A[:,:]而无需在内存中加载5gb.
答案是:h5py用于部分保存/加载数据:这里代码示例:
import sys
import h5py
def main():
data = read()
if sys.argv[1] == 'x':
x_slice(data)
elif sys.argv[1] == 'z':
z_slice(data)
def read():
f = h5py.File('/tmp/test.hdf5', 'r')
return f['seismic_volume']
def z_slice(data):
return data[:,:,0]
def x_slice(data):
return data[0,:,:]
Run Code Online (Sandbox Code Playgroud) 我正在尝试将瓶颈值保存到新创建的hdf5文件中。瓶颈值成批出现(120,10,10, 2048)。单独保存一个批处理将占用超过16个演出,而python似乎在冻结该批处理。根据最近的发现(请参阅更新,看来hdf5占用大内存是可以的,但是冻结的部分似乎是一个小故障。
我只是想保存前两批用于测试目的,而只保存训练数据集(再一次,这是一次测试运行),但是我什至不能超过第一批。它只会在第一批中停顿,并且不会循环到下一个迭代。如果我尝试检查hdf5,资源管理器将变慢,Python将冻结。如果我尝试杀死Python(即使不检查hdf5文件),Python也无法正确关闭,并且会强制重启。
以下是相关的代码和数据:
总数据点约为90,000 ish,分120个批次发布。
Bottleneck shape is (120,10,10,2048)
Run Code Online (Sandbox Code Playgroud)
所以我要保存的第一批是 (120,10,10,2048)
这是我尝试保存数据集的方式:
with h5py.File(hdf5_path, mode='w') as hdf5:
hdf5.create_dataset("train_bottle", train_shape, np.float32)
hdf5.create_dataset("train_labels", (len(train.filenames), params['bottle_labels']),np.uint8)
hdf5.create_dataset("validation_bottle", validation_shape, np.float32)
hdf5.create_dataset("validation_labels",
(len(valid.filenames),params['bottle_labels']),np.uint8)
#this first part above works fine
current_iteration = 0
print('created_datasets')
for x, y in train:
number_of_examples = len(train.filenames) # number of images
prediction = model.predict(x)
labels = y
print(prediction.shape) # (120,10,10,2048)
print(y.shape) # (120, 12)
print('start',current_iteration*params['batch_size']) # 0
print('end',(current_iteration+1) * params['batch_size']) # 120
hdf5["train_bottle"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] …Run Code Online (Sandbox Code Playgroud) 我想保存一个dict或数组。
我尝试了 withnp.save和 with pickle,发现前者总是花费更少的时间。
我的实际数据要大得多,但为了演示目的,我在这里只展示了一小部分:
import numpy as np
#import numpy.array as array
import time
import pickle
b = {0: [np.array([0, 0, 0, 0])], 1: [np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]), np.array([0, 0, 1, 0]), np.array([0, 0, 0, 1]), np.array([-1, 0, 0, 0]), np.array([ 0, -1, 0, 0]), np.array([ 0, 0, -1, 0]), np.array([ 0, 0, 0, -1])], 2: [np.array([2, 0, 0, 0]), np.array([1, 1, 0, 0]), np.array([1, 0, 1, 0]), np.array([1, …Run Code Online (Sandbox Code Playgroud)