24

Im using python 2.7 and am attempting a forcasting on some random data from 1.00000000 to 3.0000000008. There are approx 196 items in my array and I get the error

ValueError: operands could not be broadcast together with shape (2) (50)

I do not seem to be able to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated.

Here is the code I am using that generates this error

nsample = 50
sig = 0.25
x1 = np.linspace(0,20, nsample)
X = np.c_[x1, np.sin(x1), (x1-5)**2, np.ones(nsample)]
beta = masterAverageList
y_true = ((X, beta))
y = y_true + sig * np.random.normal(size=nsample)
ali_m
  • 71,714
  • 23
  • 223
  • 298

1 Answers1

23

If X and beta do not have the same shape as the second term in the rhs of your last line (i.e. nsample), then you will get this type of error. To add an array to a tuple of arrays, they all must be the same shape.

I would recommend looking at the numpy broadcasting rules.

ali_m
  • 71,714
  • 23
  • 223
  • 298
JoshAdel
  • 66,734
  • 27
  • 141
  • 140
  • My mistake if it changes anything in my array there is 168 items the shape is currently (1, 168) (168, 4). So do you mean identical the shape of both should be (1, 168) (1, 168)? – The Spiteful Octopus Aug 08 '12 at 17:44
  • Oops I edited my code befor posting that. But have switched it back to how it was the shape is actually (1,168)(50,4). A I supposed to get them to be both (1,168)(1,168) or (50,4)(50,4)? – The Spiteful Octopus Aug 08 '12 at 17:52
  • I can't really answer what size your arrays should be. They just need to be the same in order to add them in the way that you are attempting to. – JoshAdel Aug 08 '12 at 21:07
  • 1
    The data will change but not the size so Is there a way to do it as they are?, I'm starting to think this topic is still a little to advanced for a noob such as myself ;) – The Spiteful Octopus Aug 09 '12 at 01:17