How to append a tuple to a numpy array without it being preformed element-wise?

I agree with @user2357112 comment:

appending to NumPy arrays is catastrophically slower than appending to ordinary lists. It's an operation that they are not at all designed for

Here's a little benchmark:

# measure execution time
import timeit
import numpy as np


def f1(num_iterations):
    x = np.dtype((np.int32, (2, 1)))

    for i in range(num_iterations):
        x = np.append(x, (i, i))


def f2(num_iterations):
    x = np.array([(0, 0)])

    for i in range(num_iterations):
        x = np.vstack((x, (i, i)))


def f3(num_iterations):
    x = []
    for i in range(num_iterations):
        x.append((i, i))

    x = np.array(x)

N = 50000

print timeit.timeit('f1(N)', setup='from __main__ import f1, N', number=1)
print timeit.timeit('f2(N)', setup='from __main__ import f2, N', number=1)
print timeit.timeit('f3(N)', setup='from __main__ import f3, N', number=1)

I wouldn't use neither np.append nor vstack, I'd just create my python array properly and then use it to construct the np.array

EDIT

Here's the benchmark output on my laptop:

  • append: 12.4983000173
  • vstack: 1.60663705793
  • list: 0.0252208517006

[Finished in 14.3s]


You need to supply the shape to numpy dtype, like so:

x = np.dtype((np.int32, (1,2))) 
x = np.append(x,(2,3))

Outputs

array([dtype(('<i4', (2, 3))), 1, 2], dtype=object)

[Reference][1]http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html