jphollowed jphollowed - 3 months ago 6
Python Question

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

If I try

x = np.append(x, (2,3))


the tuple
(2,3)
does not get appended to the end of the array, rather
2
and
3
get appended individually, even if I originally declared
x
as

x = np.array([], dtype = tuple)


or

x = np.array([], dtype = (int,2))


What is the proper way to do this?

BPL BPL
Answer

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]

Comments