Numeric/Numarray equivalent to zip ?

Discussion in 'Python' started by George Sakkis, Apr 30, 2005.

  1. What's the fastest and most elegant equivalent of zip() in
    Numeric/Numarray between two equally sized 1D arrays ? That is, how to
    combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I
    expect the fastest and the most elegant idiom to be identical, as it is
    usually the case in this excellent library, but if not, both would be
    useful to know. Thanks,

    George
    George Sakkis, Apr 30, 2005
    #1
    1. Advertising

  2. George Sakkis

    Robert Kern Guest

    George Sakkis wrote:
    > What's the fastest and most elegant equivalent of zip() in
    > Numeric/Numarray between two equally sized 1D arrays ? That is, how to
    > combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I
    > expect the fastest and the most elegant idiom to be identical, as it is
    > usually the case in this excellent library, but if not, both would be
    > useful to know. Thanks,


    Look at combining concatenate(), reshape(), and transpose(). In Scipy, I
    would use hstack() and vstack().

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Apr 30, 2005
    #2
    1. Advertising

  3. George Sakkis

    Peter Otten Guest

    George Sakkis wrote:

    > What's the fastest and most elegant equivalent of zip() in
    > Numeric/Numarray between two equally sized 1D arrays ? That is, how to
    > combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I
    > expect the fastest and the most elegant idiom to be identical, as it is
    > usually the case in this excellent library, but if not, both would be
    > useful to know. Thanks,


    >>> import Numeric as nu
    >>> a = nu.array(range(3))
    >>> nu.array([a, a])

    array([[0, 1, 2],
    [0, 1, 2]])
    >>> nu.transpose(nu.array([a, a]))

    array([[0, 0],
    [1, 1],
    [2, 2]])

    Or am I missing something? As to speed, it seems to be the fastest to
    write...

    Peter
    Peter Otten, May 1, 2005
    #3
  4. "Peter Otten" <> wrote:

    > George Sakkis wrote:
    >
    > > What's the fastest and most elegant equivalent of zip() in
    > > Numeric/Numarray between two equally sized 1D arrays ? That is, how

    to
    > > combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I
    > > expect the fastest and the most elegant idiom to be identical, as

    it is
    > > usually the case in this excellent library, but if not, both would

    be
    > > useful to know. Thanks,

    >
    > >>> import Numeric as nu
    > >>> a = nu.array(range(3))
    > >>> nu.array([a, a])

    > array([[0, 1, 2],
    > [0, 1, 2]])
    > >>> nu.transpose(nu.array([a, a]))

    > array([[0, 0],
    > [1, 1],
    > [2, 2]])
    >
    > Or am I missing something? As to speed, it seems to be the fastest to
    > write...


    Though not the fastest to execute; using concatenate instead of
    initializing an array from a list [a,a] is more than 2,5 time faster in
    my system (~4.6 vs 11.8 usec per loop according to timeit.py), and it's
    not harder either. One difference is that the equivalent expression for
    concatenate expects arrays of shape (1,len(a)) instead of 1D arrays os
    shape (len(a),):

    >>> a = reshape(range(5), (1,5))
    >>> a

    array([ [0, 1, 2, 3, 4]])
    >>> concatenate((a,a))

    array([[0, 1, 2, 3, 4],
    [0, 1, 2, 3, 4]])


    George
    George Sakkis, May 1, 2005
    #4
  5. George Sakkis

    Peter Otten Guest

    George Sakkis wrote:

    > Though not the fastest to execute; using concatenate instead of
    > initializing an array from a list [a,a] is more than 2,5 time faster in
    > my system (~4.6 vs 11.8 usec per loop according to timeit.py), and it's
    > not harder either.


    That surprises me. I would expect essentially the same amount of
    data-shuffling.

    > One difference is that the equivalent expression for
    > concatenate expects arrays of shape (1,len(a)) instead of 1D arrays os
    > shape (len(a),):


    If you want to start out with 1D arrays, just reorder the operations:

    >>> a = array(range(5))
    >>> reshape(concatenate((a, a)), (2, 5))

    array([[0, 1, 2, 3, 4],
    [0, 1, 2, 3, 4]])
    >>>


    Peter
    Peter Otten, May 1, 2005
    #5
  6. "Peter Otten" <> wrote:
    > George Sakkis wrote:
    >
    > > Though not the fastest to execute; using concatenate instead of
    > > initializing an array from a list [a,a] is more than 2,5 time

    faster in
    > > my system (~4.6 vs 11.8 usec per loop according to timeit.py), and

    it's
    > > not harder either.

    >
    > That surprises me. I would expect essentially the same amount of
    > data-shuffling.


    Here are some timing comparisons of four versions I tried. The first
    three work on 1D arrays directly and the fourth on 2D row arrays (i.e.
    shape (1,len(a))):

    from Numeric import *

    # 11.5 usec/loop
    def ziparrays_1(*arrays):
    return array(arrays)

    # 8.1 usec/loop
    def ziparrays_2(*arrays):
    a = zeros((len(arrays),len(arrays[0])))
    for i in xrange(len(arrays)):
    a = arrays
    return a

    # 13.6 usec/loop
    def ziparrays_3(*arrays):
    return reshape(concatenate(arrays), (len(arrays),len(arrays[0])))

    # 4.6 usec/loop
    def ziparrays_4(*arrays):
    return concatenate(arrays)


    So if one has the choice, it's better to start with 2D arrays instead
    of 1D. Comparing versions 3 and 4, it's surprising that reshape takes
    twice as much as concatenate.

    George
    George Sakkis, May 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Schumacher

    Help req: IDL to Python, numarray-Numeric

    Ray Schumacher, Aug 28, 2004, in forum: Python
    Replies:
    2
    Views:
    475
  2. Matt Feinstein

    Matplotlib question-- Numeric or numarray?

    Matt Feinstein, Apr 8, 2005, in forum: Python
    Replies:
    1
    Views:
    408
    John Hunter
    Apr 8, 2005
  3. proof
    Replies:
    1
    Views:
    315
    Robert Kern
    Sep 9, 2005
  4. jelle
    Replies:
    1
    Views:
    276
    Robert Kern
    Oct 21, 2005
  5. J
    Replies:
    22
    Views:
    621
    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=
    Jan 23, 2006
Loading...

Share This Page