Search code examples
pythonpython-3.xnumpyartificial-intelligence

How to insert numpy arrays of size 1 into an empty numpy array?


I am defining two NumPy arrays:

PREtrain_labels = np.asarray([inpLblsArray[0:80]])
train_labels = np.array([])
TRstore = 0
i = 0

while i < len(train_images):
  TRstore = np.asarray([PREtrain_labels.item(i)])
  np.append(train_labels, TRstore)
  i = i + 1

Here, I have a NumPy array PREtrain_labels which contains integers and was sliced from a larger NumPy array. I defined an empty array train_labels. My goal is to fill the empty NumPy array (train_labels) with the selected slice of integers in the array PREtrain_labels. But there is a catch, as I take each integer from the PREtrain_labels array, I want each integer to be placed inside another empty array which is called TRstore. Then, I want to take the TRstore NumPy array and place it inside the empty train_labels array. However, when I run the code and print the final train_labels array, it is empty.

How can possibly fix this? If np.append() is the wrong method to use, which one should I use? The code above will not run alone, so, I am simplifying my code below into a runnable version. Thanks in advance!

loopArr = np.array([1, 2, 3, 4, 5])
a = np.asarray([1, 2, 3, 4, 5])
target = np.array([])
store = 0
i = 0

while i < len(loopArr):
  store = np.asarray([a.item(i)])
  np.append(target, store)
  i = i + 1

print(target)

Solution

  • In [30]: res = np.array([])                                                                    
    In [31]: np.append(res, np.array(1))                                                           
    Out[31]: array([1.])
    In [32]: res                                                                                   
    Out[32]: array([], dtype=float64)
    

    Out[31} is a 1 element float array - because res is float. But note that res has not changed. np.append is a just badly conceived 'cover' for np.concatenate.

    concatenate is a bit pickier about the dimensions of the inputs

    In [33]: np.concatenate([ res, np.array(1)])                                                   
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-33-bb45ea1930d0> in <module>
    ----> 1 np.concatenate([ res, np.array(1)])
    
    <__array_function__ internals> in concatenate(*args, **kwargs)
    
    ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
    In [34]: np.concatenate([ res, np.array([1])])                                                 
    Out[34]: array([1.])
    

    This joins one (0,) shape array with a (1,) shape to produce a (1,). That [] array is nearly useless.

    concatenate takes a whole list of arrays (or even lists). Take advantage of that:

    In [35]: np.concatenate([np.array([1]), np.array([2]), [3], [1,2,3]])                          
    Out[35]: array([1, 2, 3, 1, 2, 3])
    

    But why are you doing append in a loop? Why not list appends? That works in-place and is relatively fast and error free. Why make life hard for yourself?