1

Problem

I want to concatenate multiple 2-dimensional numpy arrays having shape (1, N). As an example, let's say I want to concatenate multiple np.array([1, 1]).

My inefficient solution

A very inefficient way to do this is the following:

main_array = np.array([])
for i in range(0, 10):    
    if main_array.size == 0:
        # necessary step otherwise the first time the two arrays have different shapes
        main_array = np.expand_dims(np.array([1, 1]), 0)                                                          
    else:
        main_array = np.concatenate((main_array, np.expand_dims(np.array([1, 1]), 0)))

The result, as expected, is a numpy array having shape (10, 2).

Question

How can I make this operation more efficient?

EDIT:

In my example, I add 10 times the same array for the sake of simplicity. This is probably confusing since I am actually looking for an efficient way to add any array and not necessarily the same one.

2
  • 1
    np.vstack([list of arrays])? Commented Mar 7, 2021 at 12:01
  • Do one concatenate on the whole list of arrays. List comprehension or append is more efficient than repeated concatenates. Commented Mar 7, 2021 at 12:31

1 Answer 1

2

Use np.tile

Code

np.tile(np.array([1, 1]), (10, 1))
# Output: Same as OP
array([[1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1],
       [1, 1]])

Explanation

Function np.tile

Usage: np.tile(A, reps)

parameter A

Object which acts as a “tile” that gets copied and repeated

  • Numpy array
  • Python list
  • Python tuple

parameter reps

Indicates how to repeat the input array if reps is an integer then A is repeated n times, horizontally. If reps is a tuple (e.g. (r, c))

  • r is the number of repeats downwards and
  • c is the number of repeats across.

Adding Different Arrays Vertically

Using vstack

arr1 = np.array([1, 1])
arr2 = np.array([2, 2])
arr3 = np.array([3, 3])
arr = [arr1, arr2, arr3]
np.vstack(arr)

# Output
array([[1, 1],
       [2, 2],
       [3, 3]])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, you answer my question because in my example I am adding 10 times the same array. I am actually looking for a more general way to add different arrays, not necessarily the same array. This is not clear from my question, sorry.
@maurock--extended answer to show adding different arrays. Is this what you wanted?
Your method doesn't require the if-else statement or to increase the dimensionality, so it's much more efficient, thanks!
@maurock--glad to help. The primary improvement is to avoid Python for loops (very slow relative to using a built-in function that does the looping in C).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.