Python ValueError: operands could not be broadcast together with shapes

13,134

You are doing component wise product. Either make those things matrices or use:

 R1 = np.dot(Ur, np.dot(diag(SR), VrT))

or use something like:

Ur, Sr, VrT = map(np.asmatrix, (Ur, diag(Sr), Vrt))
R1 = Ur * Sr * VrT

Which is much cleaner if you do a lot of matrix products (like in this line), otherwise arrays are typically preferable since they are the base type. If you prefer you can of course also just call np.asmatrix on each itself.

Share:
13,134

Related videos on Youtube

Chris C
Author by

Chris C

Updated on September 15, 2022

Comments

  • Chris C
    Chris C over 1 year

    I am doing SVD and when I try to run my code I get the following error:

    ValueError: operands could not be broadcast together with shapes (375, 375) (375, 500)

    I am using an image with size (500, 375)

    Here is my code:

    from PIL import Image
    from Image import new
    from numpy import *
    import numpy as np
    from scipy.linalg import svd
    
    im = Image.open("lake.tif")
    pix = im.load()
    im.show()
    r, g, b = im.split()
    R = np.array(r.getdata())
    R.shape = (500, 375)
    Ur, Sr, VrT = svd(R.T, full_matrices=False)
    R1 = Ur * diag(Sr) * VrT
    
  • Pierre GM
    Pierre GM over 11 years
    you should probably describe how to transform a ndarray into a matrix and explain that for matrix objects, multiplication works as expected...