Numpy

Fill an array with a loop

Filling a list in the loop

Sometimes you may want to create an array from a loop with unknown size at the beginning of the code execution, that’s not possible with arrays directly as far as i know. So to achieve this we fill a list instead:

myList = []
for i in range(0,final_length):
    myList.append(some_value_or_vector)

It is important that all the appended values have the same dimensions and type. Matrices, scalars and vectors are allowed here.

Converting a list to an array

To convert a list into a numpy array that can be used with TF and other libraries, do the following:

myArray = np.array(myList)

If it is important for you to get only a certain dimension from the list into te array, you can use addressing as such:

myArray = np.array(myList)[x,y,z...]

Where x, y and z are integers or ranges like (start:end).

Concatenate arrays

When there is the need to join some arrays into one big array the command bellow can be used:

myBigArray = np.concatenate((array1,array2,...))

Warning

Numpy’s ‘concatenate’ MUST RECEIVE a tuple, that is to say the double parenthesis ((array1,array2)) are NOT redundant!