0

Based on this question: https://stackoverflow.com/a/8489498/9934156 I wanted to expand an existing numpy array and fill it with a variable amount of random columns.

My approach for this is:

target = 'data'
amount = 4

data = {'data': array([[1, 2],
   [5, 9],
   [4, 4],
   [8, 42],
   ...,


N = data[target].shape
biggerarr = np.random.rand(N[0], N[1]+amount)
existingArr = np.array(data[target])
biggerarr[:,:] = existingArr

data[target] = a

But no matter how I play arround with it, I always get either

ValueError: could not broadcast input array from shape (200,2) into shape (200,6)

Or when I do existingArr[:,:] = biggerarr

ValueError: could not broadcast input array from shape (200,6) into shape (200,2)

1 Answers1

0

When you try to execute biggerarr[:,:] = existingArr you have (200, 6) array on the left side and (200, 2) array on the right side. What you need to do is to set only part of biggerarr. This can be done by using the following sintax

biggerarr[:, :N[1]] = existingArr

This is a short version of writing biggerarr[0:-1, 0:N[1]] = existingArr. You can find more about it in this question

Another way of adding columns is to use concatenate. In this case you need to stack the arrays horizontally, so you should specify axis=1 for this to work.

randomArr = np.random.rand(N[0], amount)
biggerArr = np.concatenate((data[target], randomArr), axis=1)
fdermishin
  • 3,519
  • 3
  • 24
  • 45