suppose i have 2 numpy arrays as follows:
init = 100
a = np.append(init, np.zeros(5))
b = np.random.randn(5)
so a is of shape (6,) and b is of shape(5,). i would like to add (or perform some other operation, e.g. exponentiation) these together to obtain a new numpy array of shape (6,) whose first value of a (100) is the same and the remaining values are added together (in this case this will just look like appending 100 to b, but that is because it is a toy example initialized with zeroes. attempting to add as is, will produce:
a+b
ValueError: operands could not be broadcast together with shapes (6,) (5,)
is there a one-liner way to use broadcasting, or newaxis here to trick numpy into treating them as compatible shapes?
the desired output:
array([ 100. , 1.93947328, 0.12075821, 1.65319123, -0.29222052, -1.04465838])
numpy.random.seed(0)at the top and re run your code and re print your desired output? Becausenp.randomcreated random values. And your random values will be different from when I callnp.randomfrom my console.