Python 使用省略号重塑numpy数组的问题
我正在尝试为一个项目复制以下批处理生成器。但是,我在重塑数据时遇到了问题。该函数的目标是获取[60003000]的数组,并将其重塑为[batch_size,1003000,1]的形状 我试图复制的功能代码Python 使用省略号重塑numpy数组的问题,python,arrays,keras,numpy-ndarray,ellipsis,Python,Arrays,Keras,Numpy Ndarray,Ellipsis,我正在尝试为一个项目复制以下批处理生成器。但是,我在重塑数据时遇到了问题。该函数的目标是获取[60003000]的数组,并将其重塑为[batch_size,1003000,1]的形状 我试图复制的功能代码 def gen(dict_files, aug=False): while True: record_name = random.choice(list(dict_files.keys())) batch_data = dict_files[record_name] a
def gen(dict_files, aug=False):
while True:
record_name = random.choice(list(dict_files.keys()))
batch_data = dict_files[record_name]
all_rows = batch_data['x']
for i in range(batch_size):
start_index = random.choice(range(all_rows.shape[0]-WINDOW_SIZE))
X = all_rows[start_index:start_index+WINDOW_SIZE, ...]
Y = batch_data['y'][start_index:start_index+WINDOW_SIZE]
X = np.expand_dims(X, 0)
Y = np.expand_dims(Y, -1)
Y = np.expand_dims(Y, 0)
yield X, Y
发电机输出X、Y:
X.shape=(batch_size, 100, 3000, 1)
Y.shape=(batch_size, 100, 1)
我的代码:
参数定义:
功能=阵列[60003000]&标签=阵列[6000,]
def generator(features, labels, batch_size):
##Define batch shapes
X_train_batch = np.zeros((batch_size,100, 3000, 1))
y_train_batch = np.zeros((batch_size,100, 1))
while True:
sample_index = random.choice(range(features.shape[0]))
sample_data = features[sample_index]
##Generating training batches
for i in range(batch_size):
start_index = random.choice(range(sample_data.shape[0]-100)) #pick random start point in signal (of length 3000timesteps)
X = sample_data[start_index:start_index + 100, ...] #record 100 timesteps in signal from rand start point
Y = labels[start_index:start_index + 100] #Record classification of X
#print(X.shape) #gives arrays of (100,), should be (100,3000)
##reshaping to input shape taken by model
X = np.expand_dims(X, 0)
Y = np.expand_dims(Y, -1)
Y = np.expand_dims(Y, 0)
##Collecting samples into correct batch size
#X_train_batch[i] = X
#y_train_batch[i] = Y
print(y_train_batch.shape) #gives (32,100,1) which is correct!
generator(features, labels, 32)
有人能解释一下这里的省略号(“…”)的功能吗X=all_行[开始索引:开始索引+窗口大小,…]
?根据我的理解,省略号放置了3000个时间步的样本_数据以给出输出(1003000),但我显然不理解某些东西,因为我无法在代码中获得相同的行为
接下来,我如何用代码复制gen输出的内容