ValueError: operands could not be broadcast together with shapes (15,1) (256,256) (256,256)

username: A_Shamsadini
cooked: <p>Hi<br>
Why I have this error in this line" result[i,:,:,0] = np.where(type_map == 1, inst_map, result[i,:,:,0])"?<br>
Please help me<br>
Traceback (most recent call last):<br>
File “transform_for_metrics.py”, line 54, in <br>
result[i,:,:,0] = np.where(type_map == 1, inst_map, result[i,:,:,0])<br>
File “&lt;<strong>array_function</strong> internals&gt;”, line 6, in where<br>
ValueError: operands could not be broadcast together with shapes (15,1) (256,256) (256,256)</p>
<p>path_to_folder = “/content/drive/MyDrive/0.9-0.1/outputs/mat/”</p>
<p>file_format = “.mat”</p>
<h1>
<a name="read-all-mat-or-npy-files-from-directory-1" class="anchor" href="#read-all-mat-or-npy-files-from-directory-1"></a>read all .mat or .npy files from directory</h1>
<p>files = []</p>
<p>for i in os.listdir(path_to_folder):</p>
<pre><code>if file_format == ".npy":

    if i.endswith('.npy'):

        files.append(np.load(path_to_folder + "/" + i))

if file_format == ".mat": #and i != "fold3_148.mat":

    if i.endswith('.mat'):

        mat_dict = sio.loadmat(path_to_folder + "/" + i)

        # add file number to dictionary (file name should be in format "fold3_105.mat" with 105 as the file number)

        #print(mat_dict)

        x = i.split(".") 

        

        #x = x[0].split("_")

        x = int(x[0])

        

        mat_dict['filenumber'] = x

        files.append(mat_dict)

        #print("successfullly appended file " + i)
</code></pre>
<h1>
<a name="sort-files-numerically-by-file-number-2" class="anchor" href="#sort-files-numerically-by-file-number-2"></a>sort files numerically by file number</h1>
<p>files_sorted = sorted(files, key=lambda k: k[‘filenumber’])</p>
<h1>
<a name="transform-format-3" class="anchor" href="#transform-format-3"></a>transform format</h1>
<p>result = np.zeros((len(files_sorted), 256, 256, 6))</p>
<p>for i, pred in enumerate(files_sorted):</p>
<pre><code>if file_format ==  ".npy":

    inst_map = pred[:,:,0]

    type_map = pred[:,:,1]

if file_format ==  ".mat":

    inst_map = pred['inst_map']

    type_map = pred['inst_type']



if(np.any(type_map==1)):

    result[i,:,:,0] = np.where(type_map == 1, inst_map, result[i,:,:,0])

if(np.any(type_map==2)):

    result[i,:,:,1] = np.where(type_map == 2, inst_map, result[i,:,:,1])

if(np.any(type_map==3)):

    result[i,:,:,2] = np.where(type_map == 3, inst_map, result[i,:,:,2])

if(np.any(type_map==4)):

    result[i,:,:,3] = np.where(type_map == 4, inst_map, result[i,:,:,3])

if(np.any(type_map==5)):</code></pre>

username: ptrblck
cooked: <p>The error is caused by a shape mismatch in <code>np.where</code> as seen here:</p>
<pre><code class="lang-python">a = np.random.randn(15, 1) &gt; 0.
b = np.random.randn(256, 256)
c = np.random.randn(256, 256)
np.where(a, b, c)
# &gt; operands could not be broadcast together with shapes (15,1) (256,256) (256,256) 
</code></pre>
<p>Check the <a href="https://numpy.org/doc/stable/reference/generated/numpy.where.html">docs</a> of this call and make sure <code>b</code> and <code>c</code> are broadcastable to <code>a</code>.</p>