0

I have a two numpy.arrays, I want to get following result efficiently

1.add the element's of b to a's sub-array

    a=numpy.array([(1,2,3),(1,2,3)])
    b=numpy.array([0,0])
->
    c=[(0,1,2,3),(0,1,2,3)] 

code in a loop

a=numpy.array([(1,2,3),(1,2,3)])
b=numpy.array([(0,0)])
c=numpy.zeros(2 , 4)
idx=0
for x in a:
   c[idx]=(a[idx][0],a[idx][1],a[idx][2], b[idx])
   idx = idx+1

and
2. Get an 2-D array with dimension(a.dim*b.dim, 2) from two 1-D arrays

    a=numpy.array([(1,2)])
    b=numpy.array([(3,4)])
->
    c=[(1,3),(1,4),(2,3),(2,4)]

code in a loop

a=numpy.array([(1,2)])
b=numpy.array([(3,4)])
c=numpy.zeros(a.size*b.size , 2)
idx=0
for x in a:
    for y in b:
        c[idx]=(x,y)
        idx = idx+1
Samuel
  • 5,977
  • 14
  • 55
  • 77
  • Can you show some context? This is possible, and not that hard, but it may not be a good idea. – user2357112 Aug 28 '13 at 01:53
  • Okay, now it looks like you're asking two different questions. Can you give a precise definition of the result you're trying to achieve? – user2357112 Aug 28 '13 at 01:55
  • I want to these two method to get new array. And I don't want to make this in a loop . I this use loop to make is not efficient – Samuel Aug 28 '13 at 01:56
  • The two things you're trying to achieve look like entirely different things. If you think they're the same, it's not at all clear what you want to do. – user2357112 Aug 28 '13 at 01:57
  • Could you write a loop that achieves the effect that you're looking for, so it's clearer what you want? – user2357112 Aug 28 '13 at 01:58

2 Answers2

3

For the first problem, you can define b differently and use numpy.hstack:

a = numpy.array([(1,2,3),(1,2,3)])
b = numpy.array([[0],[0]])
numpy.hstack((b,a))

Regarding the second problem, I would probably use sza's answer and create the numpy array from that result, if necessary. That technique was suggested in an old Stack Overflow question.

Community
  • 1
  • 1
Paulo Almeida
  • 7,803
  • 28
  • 36
  • Because I have a big array `a`, and actually `b` has the same element. Can i don't alloc `b` the same size of `a` – Samuel Aug 28 '13 at 03:18
  • I'm not sure I understand what you mean, but you can probably use `numpy.empty_like`, `zeros_like` or `ones_like`. Look into `array.fill` too. – Paulo Almeida Aug 28 '13 at 03:32
  • Yes , That's what i mean. But I don't want to use `ones_like`, This will alloc a big memory. And all the elements of `b` are the same, So it's a little memory-wasting – Samuel Aug 28 '13 at 04:58
  • I see what you mean now. sza's answer is better for that problem too, then, just use a fixed `y`. – Paulo Almeida Aug 28 '13 at 05:02
  • But he also use the loop. I'm afraid it's not efficient. So I should make trade-off on it right? – Samuel Aug 28 '13 at 05:05
  • As far as I know yes, because numpy array stacking methods need arrays with the same dimensions along the axis being stacked. Test it and see what are the trade-offs. – Paulo Almeida Aug 28 '13 at 05:13
2

For the first one, you can do

>>> a=numpy.array([(1,2,3),(1,2,3)])
>>> b=numpy.array([0,0])
>>> [tuple(numpy.insert(x, 0, y)) for (x,y) in zip(a,b)]
[(0, 1, 2, 3), (0, 1, 2, 3)]

For the 2nd one, you can get the 2-D array like this

>>> a=numpy.array([(1,2)])
>>> b=numpy.array([(3,4)])
>>> import itertools
>>> c = list(itertools.product(a.tolist()[0], b.tolist()[0]))
[(1, 3), (1, 4), (2, 3), (2, 4)]
zs2020
  • 53,766
  • 29
  • 154
  • 219