1

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.

1
  • Try looking at entropy[edges.data == 1.0].shape and you will see that it is different. I am only guessing at your intentions, but you may want to also mask data.data using edges.data == 1.0, as described below Commented Apr 30, 2014 at 20:46

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.