堆叠多个数组与多维python

  • 本文关键字:python 数组 python arrays numpy
  • 更新时间 :
  • 英文 :


我试图在python中创建和堆栈多个多维数组,我似乎无法得到正确的。

我:

y_0 = np.random.uniform(-1.0,1.0, size=(1,1,s_conn.weights.shape[0],1))
y_1 = np.random.uniform(-500.0, 500.0, size=(1,1,s_conn.weights.shape[0],1))
y_2 = np.random.uniform(-50.,50., size=(1,1,s_conn.weights.shape[0],1))
y_3 = np.random.uniform(-6.0, 6.0, size=(1,1,s_conn.weights.shape[0],1))
y_4 = np.random.uniform(-20.0, 20.0, size=(1,1,s_conn.weights.shape[0],1))
y_5 = np.random.uniform(-500.,500., size=(1,1,s_conn.weights.shape[0],1))

其中s.conn为NxN矩阵。

并且每个数组都有dim: (1,1,10,1)我需要的是一个形状为:(1,6,10,1)

的数组我怎么得到它?我试过np。堆栈和手动创建和重塑,但我不断得到奇怪/不正确的结果。

如果你能给我一点建议我会很感激的。

js

np。Stack沿着新的轴连接数组序列,因此它将为您的矩阵创建一个不希望的维度,考虑使用np.concatenate。

np.concatenate([y_0,y_1,y_2,y_3,y_4,y_5],axis = 1)

注意,您还可以使用Array.squeeze()来删除不需要的维度(在使用np时可能出现)。在不使用重塑的情况下保存,例如,使用

可以获得相同的结果
np.stack([y_0,y_1,y_2,y_3,y_4,y_5],axis = 1).squeeze(axis=2)