2

I need to create an empty numpy array of a shape (?, 10, 10, 3). ? means I don't know how many elements will be inserted. Then I have many numpy arrays of a shape (1, 10, 10, 3) which I want to be inserted to the prepared array one by one, so the ? mark will be increasing with inserted elements.

I'm trying all variations of numpy array methods like empty, insert, concatenate, append... but I'm not able to achieve this. Could you please give me a hand with this?

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
T.Poe
  • 1,949
  • 6
  • 28
  • 59
  • 3
    Don't do that; NumPy arrays are fixed-size, and trying to extend them dynamically requires making a new array every time, with horribly expensive, quadratic runtime costs. – user2357112 Dec 06 '17 at 18:33
  • If you have some idea on the max possible length along the first axis, simply pre-allocate with `out = (M,10,10,3)`, with `M` being that max number and index and assign at each iteration : `out[i] = ..` keeping the count of iterations. At the end, simply slice out `out[:C]` with `C`being that last iteration number/count. – Divakar Dec 06 '17 at 18:39
  • Thanks, I didn't know that. So I did it by removing the first single-dimensional entry of the inserted element, appended it to a regular python array and that made it a NumPy array. It works that way, not sure about effectiveness. – T.Poe Dec 06 '17 at 18:43
  • If you *really* have to do something like this, don't accumulate into an array, accumulate into a plain `list` object using `my_list.append(array_x)`, then, do `final_array = np.array(my_list)` – juanpa.arrivillaga Dec 06 '17 at 18:43
  • @T.Poe what do you mean by a "regular python array"? Do you mean a *list*? – juanpa.arrivillaga Dec 06 '17 at 18:44
  • 1
    @juanpa.arrivillaga Yes I meant a list, sry. I did it the way you suggested. – T.Poe Dec 06 '17 at 18:48
  • Are you just trying to create a 4 dimensional numpy array ? – IMCoins Dec 06 '17 at 20:35

1 Answers1

1

Using np.append works:

import numpy as np

mat = np.empty((0,10,10,3))

array = np.random.rand(1,10,10,3)

mat = np.append(mat, array, axis=0)
mat = np.append(mat, array, axis=0)

print(mat.shape)

>>>(2,10,10,3)
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • Thanks. But what about this "horribly expensive, quadratic runtime costs" as someone mentioned in the comments under the question? Is it true or not? – T.Poe Dec 06 '17 at 21:19
  • I believe it is. You'd be better off appending to a list and then form the array if you can. I was just answering your question! – Jacques Gaudin Dec 06 '17 at 21:32
  • Ok, so I just use the list list appending, thanks again. – T.Poe Dec 06 '17 at 21:33
  • If `mat` and `array` match in dimensions, I prefer to use `concatenate`. `np.append` looks too much like `list.append`, confusing beginners in many ways. – hpaulj Dec 06 '17 at 21:33