Hi All,
I have an 2D numeric array of x,y points eg [(1,3),(2,4),(5,6)] and I
would
like to remove all the points in the array that don't meet the min/max
criteria. I will have several thousand points. With lists I can do it
Thanks to Tim and Chris. Just what I was looking for!
I tested both along with some other tries that I had made.
For 10000 points -
G:\GPS\Python\GUI_Test\Filter>speed.py
time for is 0.022809
time for is 0.060303
time for is 0.055692
time for is 0.003652
time for is 0.003561
For 100 points -
G:\GPS\Python\GUI_Test\Filter>speed.py
time for is 0.000238
time for is 0.000784
time for is 0.000678
time for is 0.000376
time for is 0.000153
They scale slightly differently between Tim's and Chris' methods.
Thanks again,
Gordon Williams
Here is the code (since someone will ask):
'''Test the speed of different methods of getting points out of a list'''
import time
import Numeric as n
size= 100
maxNum=size/10.
#data
a= n.array(n.arange(0,maxNum,.05))
a.shape= (size,2)
#list
l= a.tolist()
(xMin,yMin)= (3,2)
(xMax,yMax)= (4,6)
def listComp(seq):
'''using list comprehension'''
return [(x,y) for x,y in seq if xMin < x xMin) & (seq[:,0] < xMax) & (seq[:,1] > yMin) &
(seq[:,1] < yMax)
return n.take(a,n.nonzero(valid))
#Tests
tests= [(listComp,l), (arraykludge,a),(arrayComp,a), (arrayTimH,a),
(arrayChrisB,a)]
for fun,seq in tests:
t=time.clock()
apply(fun,(seq,))
dt= time.clock()-t
print "time for %s is %f" %(str(fun),dt)
----- Original Message -----
From: "Gordon Williams"
To:
Sent: Thursday, February 27, 2003 2:05 PM
Subject: filtering numeric arrays
point
like
[(x,y) for x,y in seq if xMin < x
How do I get the same functionality and better speed using numeric. I
have
tried a bunch of things using compress and take but I am running up
against
a brick wall.
Any ideas?
Thanks
Gordon Williams