简体   繁体   中英

Numpy Value Error: operands could not be broadcast together with shapes (1,99) (100,)

I am attemping to define y equal to (x × 0.15) + N where N is a 100 element 1D array of random values chosen from a Gaussian distribution with mean 0.0 and standard deviation 0.5 but I keep getting the error code: ValueError: operands could not be broadcast together with shapes (1,99) (100,) . Any tips on how I can revise my code to make it work would be much appreciated, thanks.

N = np.random.normal (0, 0.5,size=(100))
y = (np.dot([x],0.15)) + N

It would be helpful if you included the x array in your example above. Python is complaining that (np.dot([x],0.15)) has dimensions np.shape(1,99) , which means that x has the same shape. You could x.flatten() to make it np.shape(99) , but then you still have the problem that x has 99 elements, and N has 100, and python does not know how to add two arrays of dissimilar length.

Assuming this is somehow wrong, and that x should have had 100 elements, you could just do

x = np.arange(100) # Example x-array, use your own in stead
N = np.random.normal(0, 0.5,size=x.shape)
y = x*0.15 + N

since all (np.dot([x],0.15)) does in this case is to multiply each element of x with 0.15, which much simpler written as the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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