473,287 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Numeric/Numarray equivalent to zip ?

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

Jul 19 '05 #1
5 1478
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #2
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

Jul 19 '05 #3
"Peter Otten" <__*******@web.de> 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

Jul 19 '05 #4
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
Jul 19 '05 #5
"Peter Otten" <__*******@web.de> 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[i] = arrays[i]
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

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Travis Oliphant | last post by:
Numarray is making great progress and is quite usable for many purposes. An idea that was championed by some is that the Numeric code base would stay static and be replaced entirely by Numarray. ...
7
by: Jive | last post by:
Here's my sitch: I use gnuplot.py at work, platform Win32. I want to upgrade to Python 2.4. Gnuplot.py uses extension module Numeric. Numeric is now "unsupported." The documentation says "If...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
1
by: proof | last post by:
a = range(1000000) b = * 3 b = , b)] This is rather slow in python and I thought that kind of things should be written using numeric or numarray. I tried to read trough manuals but it...
1
by: jelle | last post by:
#No rant intended I'm not at all confused wether I should learn an one of the advanced array modules, I'm slightly confused over which I should pick up. I'm impressed with the efforts of SciPy...
22
by: J | last post by:
Hi I hope the title of this message indicates my question. I am looking for basic array functionality in Python and it turns out that there are all these packages which are somehow related....
10
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
5
by: Erik Johnson | last post by:
I am just starting to explore doing some scientific type data analysis using Python, and am a little confused by the different incarnations of modules (e.g., try Google("Python numeric"). There...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.