Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 676
pretzlstyle Avatar asked Aug 16 '16 21:08

pretzlstyle


People also ask

Can you put tuples in a NumPy array?

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

How do I append an element to an array in NumPy?

You can add a NumPy array element by using the append() method of the NumPy module. The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above. The axis is an optional integer along which define how the array is going to be displayed.

Is element wise operation possible in NumPy?

Addition, subtraction, multiplication, and division of arguments(NumPy arrays) element-wise. First array elements raised to powers from second array, element-wise. Return element-wise remainder of division.


2 Answers

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]

like image 60
BPL Avatar answered Oct 02 '22 15:10

BPL


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

like image 40
Rahul Madhavan Avatar answered Oct 02 '22 17:10

Rahul Madhavan