形状不匹配 Python

Shape mismatch Python

所以我正在尝试创建一个 3D 数组,其中包含我自己定义的一些标准值(实际上是其他人,但这并不重要) 这是我的代码:

Tt = 120 # Total duration (sec)
delta = 0.001 # Time bin (sec)
T = Tt/delta # number of time bins
Ncells = 8;
Cmap = np.zeros([Ncells,Ncells,int(T)])
ExcInhID = np.expand_dims([1, -1, 1, -1, 1, -1, 1], axis =1)
a = list(range(0,int(T)))

for t in a:
    Cmap[:,:,t] = (-1)*np.identity(Ncells)
    Cmap[1:,0,t] = list(ExcInhID*np.ones([Ncells-1,1])*(1 - min(max(t-T/3,0)*(1/(T/3)),0)))
    CC5 = list(np.arange(0,Ncells)) ; del CC5[4];
    Cmap[CC5,4,t] = list(ExcInhID*np.ones([Ncells-1,1])*(min(max(t-T/3,0)*(1/(T/3)),0)))
    Cmap[6,2,t] = 1; Cmap[5,7,t] = -1; Cmap[1,5,t] = 1;

这在

之前有效
Cmap[CC5,4,t] = list(ExcInhID*np.ones([Ncells-1,1])*(min(max(t-T/3,0)*(1/(T/3)),0)))  Cmap[CC5,4,t] = list(ExcInhID*np.ones([Ncells-1,1])*(min(max(t-T/3,0)*(1/(T/3)),0)))

哪里出现错误:

ValueError: shape mismatch: value array of shape (7,1) could not be broadcast to indexing result of shape (7,)

这很奇怪,因为它在另一条线上确实有效:

 Cmap[1:,0,t] = list(ExcInhID*np.ones([Ncells-1,1])*(1 - min(max(t-T/3,0)*(1/(T/3)),0)))

我什么都试过了,不知道该怎么办了.. 大家有什么建议吗?

我不完全确定为什么您在第一个实例中没有出错而在第二个实例中只出错。但是,如果您按以下方式更改您的行,事情就会奏效。

来自

Cmap[1:,0,t] = list(ExcInhID*np.ones([Ncells-1,1])*(1 - min(max(t-T/3,0)*(1/(T/3)),0)))
....
Cmap[CC5,4,t] = list(ExcInhID*np.ones([Ncells-1,1])*(min(max(t-T/3,0)*(1/(T/3)),0)))

Cmap[1:,0,t] = np.stack(ExcInhID*np.ones([Ncells-1,1])*(1 - min(max(t-T/3,0)*(1/(T/3)),0)), axis=1)
....
Cmap[CC5,4,t] = np.stack(ExcInhID*np.ones([Ncells-1,1])*(min(max(t-T/3,0)*(1/(T/3)),0)), axis=1)