简体   繁体   English

从错误的numpy形状重塑功能广播

[英]Reshape function broadcasting from wrong numpy shape

I am trying to reshape a numpy.array from shape (4,4) to shape (2,2,2,2). 我正在尝试将numpy.array从形状(4,4)重塑为形状(2,2,2,2)。 The error I am receiving is: 我收到的错误是:

ValueError: could not broadcast input array from shape (2,2,2,2) into shape (4,4).

This makes me think I have the shapes backwards, but after checking that is not the case. 这使我认为我的形状是向后的,但是经过检查后情况并非如此。

I have a user defined function that uses np.reshape to reshape an array to a certain shape if it is not already that shape. 我有一个用户定义的函数,该函数使用np.reshape将数组重新整形为某个形状(如果还不是该形状)。 I have tried eliminating the user defined function and using only np.reshape, but it returned the same error. 我尝试消除用户定义的函数,仅使用np.reshape,但是它返回了相同的错误。 What am I missing? 我想念什么?

The reshape function: 重塑功能:

def reshape(matrix, ports, modes):
    shape = (ports, ports, modes, modes)
    if(np.shape(matrix) != shape): #reshape if necessary
        return np.reshape(matrix, shape)
    else:
        return matrix

Where I call this function: 我在哪里调用此函数:


def plot(S, F, ports, modes, x_range, y_range, title, f_units, 
         multi_modal = True):
    data = {} #create dictionary to store S-parameters
    if(not multi_modal): #if we want average
        for f in range(0, len(F)): #iterate through frequencies
            print(np.shape(S[f]))
            S[f] = reshape(S[f], ports, modes)

In this instance, ports = 2 and modes = 2. 在这种情况下,端口= 2,模式= 2。

S[f] is a np.array of shape (4,4): S[f]是形状为(4,4)的np.array:

[[ 1.00000000e+00+0.00000000e+00j -1.02728868e-19+1.64952184e-22j
  -1.37762998e-20+2.40441793e-24j -4.18063430e-24-1.18287261e-21j]
 [ 0.00000000e+00-0.00000000e+00j -1.00000000e+00+1.22464680e-16j
   3.03393173e-26-1.77961140e-24j  1.57277027e-25+2.06062998e-23j]
 [-1.95100984e-27+3.66506948e-24j  2.38762635e-25+1.48052807e-22j
   1.00000000e+00+0.00000000e+00j  2.90518731e-20+1.33913685e-17j]
 [-3.47614015e-25-4.08540212e-23j -3.30653510e-21+2.87402660e-23j
   1.77338192e-21+2.27000073e-19j -1.00000000e+00+1.22464680e-16j]]

Why is it returning the error: 为什么返回错误:

ValueError: could not broadcast input array from shape (2,2,2,2) into shape (4,4)

when it should be reshaping from (4,4) to (2,2,2,2)? 什么时候应该将其从(4,4)更改为(2,2,2,2)?

This would do 这会做

def plot(S, F, ports, modes, x_range, y_range, title, f_units, 
         multi_modal = True):
    data = {} #create dictionary to store S-parameters
    if(not multi_modal): #if we want average
        for f in range(0, len(F)): #iterate through frequencies
            print(np.shape(S[f]))
            S_reshaped = reshape(S[f], ports, modes)

If you need to store your results you can create an empty list and then append the reshaped array to it. 如果需要存储结果,则可以创建一个空列表,然后将重新排列的数组附加到该列表中。

def plot(S, F, ports, modes, x_range, y_range, title, f_units, 
         multi_modal = True):
    data = {} #create dictionary to store S-parameters
    S_reshaped=[]
    if(not multi_modal): #if we want average
        for f in range(0, len(F)): #iterate through frequencies
            print(np.shape(S[f]))
            S_reshaped.append(reshape(S[f], ports, modes))

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

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