繁体   English   中英

使用多个数组python更改列表的形状

[英]Change the shape of list with multiple arrays python

最初,我有一个名为voltage的字典,其中每个形状有 206 个数组(25,3,1) 我已使用以下代码将字典转换为数组列表

temp = []
input= []

for value, key in voltage.items():
    temp = [key,value]
    input.append(temp)

input是我的列表的名称。 当我检查列表的形状时,我得到(1,206,2)

np.shape(input) - gives (1,206,2)

我想将列表的形状更改为(206, 25,3,1) 当我将input传递给以下模型时,我收到一个错误(附在下面)。

K = Input(shape=(25,3,1))

x = Conv2D(16,(2,2), strides=2, activation='relu', data_format='channels_last', padding='same')(K)   
x = BatchNormalization()(x)

x = Conv2D(32,(2,2), activation='relu', data_format='channels_last' , padding='same')(x)  
x = BatchNormalization()(x)

x = Flatten()(x)

x = Dense(320, activation='relu')(x)
x = Dense(160, activation='relu')(x)
x = Dense(80, activation='relu')(x)
x = Dense(1)(x)

model = Model(K,x)

model.compile(optimizer='adam',loss='mse')

model.fit(x=input, y=output, steps_per_epoch=None, epochs=1000)

ValueError:检查输入时出错:预期 input_5 有 4 个维度,但得到的数组具有 (1, 206, 2) 形状

希望得到一些帮助。

您可以执行以下操作:

import numpy as np

d = {1: np.random.rand(25, 3, 1),
2: np.random.rand(25, 3, 1),
3: np.random.rand(25, 3, 1),
4: np.random.rand(25, 3, 1)}

arr = np.empty((len(d.keys()), *d[1].shape))

for i, k in enumerate(d.keys()):
    arr[i] = d[k]

print(arr.shape)  # (4, 25, 3, 1)

创建一个所需大小的空numpy数组,然后将 dict, volatge , 值附加到所需位置。

如果您的键不是常规的(意味着 1、4、6 等),您可以创建一个映射,将arr的第一个条目映射到voltage值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM