Domanda

I'm receiving this error with the code below, but I didn't figure out why.

I printed my objects shapes and they are the same.

def plot_edges_entropy(data, edges, file_path):
    plt.figure()
    
    entropy = numpy.zeros((data.rows, data.cols), float)
    print entropy.shape, edges.data.shape, data.data.shape
    entropy[edges.data == 1.0] = data.data

    fig = plt.imshow(entropy, extent=[0, data.cols, data.rows, 0], cmap='hot_r', vmin=0, vmax=1, interpolation='nearest')
    plt.colorbar(fig)
    plt.savefig(file_path + '.png')
    plt.close()

def __init__(self, open_image=False):
    """
    The Data constructor
    """
    self.data = misc.imread('../TestImages/brain_noisy.jpg', flatten=True)
    self.data /= 255.0
    x, y = self.data.shape
    self.rows = x
    self.cols = y

    if not open_image:
        self.data = numpy.zeros((self.rows, self.cols), float)

Print statement:

(211, 256) (211, 256) (211, 256)

Error:

entropy[edges.data == 1.0] = data.data

ValueError: array is not broadcastable to correct shape

If I try to assign a simple value, it works:

 entropy[edges.data == 1.0] = 100

What's is the problem? I can assign a ndarray to another with respect to some condition?

Thank you in advance.

È stato utile?

Soluzione

Unless edges.data == 1.0 everywhere, this will not work, because you are trying to set it with the full data.data. Are you possibly intending the following?

entropy[edges.data == 1.0] = data.data[edges.data == 1.0]

Altri suggerimenti

You're assigning to a subset of entropy, specifically the subset for which edges.data == 1.

So you need to make sure that data.data has the same shape as the subset to which you're assigning.

To check, try printing: entropy[edges.data == 1.0].shape

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top