1

I am creating inside a for loop in each iteration of it a numpy array of size 20x30x30x3. I want to concatenate all of those numpy arrays into a bigger one. If the iteration steps are 100 then the numpy array I want should be2000x30x30x3. I tried to do with lists:

new_one_arr1_list = []
new_one_arr2_list = []
all_arr1 = np.array([])
for item in one_arr1: # 100 iterations
    item = np.reshape(item, (1, 30, 30, 3))
    new_one_arr1 = np.repeat(item, 20, axis=0)

    all_arr1 = np.concatenate(([all_arr1 , new_one_arr1 ]))
    ind = np.random.randint(one_arr2.shape[0], size=(20,))
    new_one_arr2= one_arr1[ind]

    new_one_arr1_list.append(new_one_arr1)
    new_one_arr2_list.append(new_one_arr2)

In each iteration step new_one_arr1 and new_one_arr2 they have size 20x30x30x3. In the end when I am converting new_one_arr1_list and new_one_arr2_list and the size it is 100x20x30x30x3. How can I have 2000x30x30x3 in the end in a numpy array?

EDIT: I tried to use concatenate to add the arrays within a numpy array all_arr1 using: all_arr1= np.concatenate(([all_arr1, new_one_arr1])) however, I received the message:

ValueError: all the input arrays must have same number of dimensions

3
  • you use // for comment instead of #. This code can't compile. Commented Oct 17, 2018 at 11:11
  • Yes I add this comment just here (as a clarification). Commented Oct 17, 2018 at 11:12
  • 2
    Appending to a list in a loop is easier, and faster, than repeated concatenate., Commented Oct 17, 2018 at 12:35

2 Answers 2

2

In order to create the concatenation and work around the error, I initialized the array with None and tested if it is None in the loop. Thereby you do not have to worry about not fitting dimensions. However, i created some arrays for the ones you did only describe and ended up with a final dimesion of (400, 30, 30, 3). This fits in here, since 20*20 = 400. Hope this helps for you solution.

new_one_arr1_list = []
new_one_arr2_list = []
one_arr1 = np.ones((20,30,30,3))
one_arr2 = np.ones((20,30,30,3))
all_arr1 = None
count = 0
for item in one_arr1: # 100 iterations
    item = np.reshape(item, (1, 30, 30, 3))
    new_one_arr1 = np.repeat(item, 20, axis=0)

    # print(all_arr1.shape, new_one_arr1.shape)
    if all_arr1 is None:
        all_arr1 = new_one_arr1
    else:
        all_arr1 = np.concatenate(([all_arr1 , new_one_arr1 ]), axis=0)
    ind = np.random.randint(one_arr2.shape[0], size=(20,))
    new_one_arr2= one_arr1[ind]

    new_one_arr1_list.append(new_one_arr1)
    new_one_arr2_list.append(new_one_arr2)
    count += 1
print(count)
all_arr1.shape
Sign up to request clarification or add additional context in comments.

1 Comment

I guess so. Depends on your goals mate
2

Use np.concatenate operation given in the documenation: https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html

Don't concatenate in the first iteration, it'll raise dimension error, just copy it during the first iter. For the remaining iterations keep concatenating.

new_one_arr1_list = []
new_one_arr2_list = []
all_arr1 = np.array([])
firstIteration = True
for item in one_arr1: # 100 iterations


    item = np.reshape(item, (1, 30, 30, 3))
    new_one_arr1 = np.repeat(item, 20, axis=0)

    if firstIteration:
        all_arr1 = new_one_arr1
        firstIteration=False
    else:
        all_arr1 = np.concatenate(([all_arr1 , new_one_arr1 ]))
    ind = np.random.randint(one_arr2.shape[0], size=(20,))
    new_one_arr2= one_arr1[ind]

    new_one_arr1_list.append(new_one_arr1)
    new_one_arr2_list.append(new_one_arr2)

5 Comments

How can I use it inside the for-loop? I need for example to add an empty array in the first loop.
I tried to do something like all_arr1= np.array([]) before the for loop and then within the for loop all_arr1= np.concatenate(([all_arr1, new_one_arr1])), however, i am getting error all the input arrays must have same number of dimensions .
initialize all_arr1 before the for loop. I mean run an iteration first separately then iterate for remaining 99.
Sorry I did not get the point. You mean to concatenate the numpy arrays within the list?
In the first iteration don't concatenate. Do the below. all_arr1 = new_one_arr1. In the remaining iterations keep concatenating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.