简体   繁体   English

形状不匹配的Python

[英]Shape mismatch Python

So I'm trying to create a 3D array with some standard values defined by myself (actually other people, but that doesn't matter) This is my code: 因此,我试图用自己(实际上是其他人,但是没关系)定义的一些标准值创建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;

This does work until 这一直有效直到

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)))

Where I get the error: 我在哪里得到错误:

ValueError: shape mismatch: value array of shape (7,1) could not be broadcast to indexing result of shape (7,) ValueError:形状不匹配:形状(7,1)的值数组无法广播到形状(7,)的索引结果

Which is weird since it did work on the other line over here: 这很奇怪,因为它确实在这里的另一行上起作用:

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

I've tried everything and don't know what to do anymore.. Does any of you have suggestions? 我已经尝试了一切,现在不知道该怎么办。.你们有建议吗?

I am not exactly sure why you did not get error on the first instance and got only in the second. 我不完全确定为什么您没有在第一次出现错误而仅在第二次出现错误。 But if you change your lines as below, things would work. 但是,如果您按以下方式更改行,则一切正常。

From

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)))

to

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)

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

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