array manipulation without for loops

Sheldon shejo284 at gmail.com
Sun Jun 25 19:34:42 CEST 2006


Hi Alex,

I will code this in a little while and get back to you. Terrific! I saw
this function but I skipped over it without realizing what it could do.

The Numeric doc is not very good and I am just getting into Python so
your book sounds great especially since it covers Numeric. I will look
into it when I get back to work tomorrow.

Bye for now,
Sheldon

Alex Martelli wrote:

> Sheldon <shejo284 at gmail.com> wrote:
>
> > Alex,
> >
> > I am using Numeric and have created 3 arrays: zero((1215,1215),Float)
> > Two arrays are compared and one is used to hold the mean difference
> > between the two compared arrays. Then I compare 290 or 340 pairs of
> > arrays. I know that memory is a problem and that is why I don't open
> > all of these arrays at the same time. I cannot install Numpy due to my
> > working conditions. Sorry I should have made it clear that is was
> > Numeric I was working with.
>
> It's OK, even if the hard-core numeric-python people are all
> evangelizing for migration to numpy (for reasons that are of course
> quite defensible!), I think it's quite OK to stick with good old Numeric
> for the moment (and that's exactly what I do for my own personal use!).
>
> So, anyway, I'll assume you mean your 1215 x 1215 arrays were created by
> calling Numeric.zeros, not "zero" (with no trailing s) which is a name
> that does not exists in Numeric.
>
> Looking back to your original post, let's say that you have two such
> arrays, a and b, both 1215x1215 and of Numeric.Float type, and the
> entries of each array are all worth 1, 2, or 255 (that's how I read your
> original post; if that's not the case, please specify).  We want to
> write a function that alters both a and b, specifically setting to 255
> all entries in each array whose corresponding entries are 255 in the
> other array.
>
> Now that's pretty easy -- for example:
>
> import Numeric
>
> def equalize(a, b, v=255):
>     Numeric.putmask(a, b==v, v)
>     Numeric.putmask(b, a==v, v)
>
> if __name__ == '__main__':
>     a = Numeric.zeros((5,5), Numeric.Float)
>     b = Numeric.zeros((5,5), Numeric.Float)
>     a[1,2]=a[2,1]=b[3,4]=b[0,2]=255
>     a[3,0]=a[0,0]=1
>     b[0,3]=b[4,4]=2
>     print "Before:"
>     print a
>     print b
>     equalize(a, b)
>     print "After:"
>     print a
>     print b
>
>
> brain:~/pynut alex$ python ab.py
> Before:
> [[   1.    0.    0.    0.    0.]
>  [   0.    0.  255.    0.    0.]
>  [   0.  255.    0.    0.    0.]
>  [   1.    0.    0.    0.    0.]
>  [   0.    0.    0.    0.    0.]]
> [[   0.    0.  255.    2.    0.]
>  [   0.    0.    0.    0.    0.]
>  [   0.    0.    0.    0.    0.]
>  [   0.    0.    0.    0.  255.]
>  [   0.    0.    0.    0.    2.]]
> After:
> [[   1.    0.  255.    0.    0.]
>  [   0.    0.  255.    0.    0.]
>  [   0.  255.    0.    0.    0.]
>  [   1.    0.    0.    0.  255.]
>  [   0.    0.    0.    0.    0.]]
> [[   0.    0.  255.    2.    0.]
>  [   0.    0.  255.    0.    0.]
>  [   0.  255.    0.    0.    0.]
>  [   0.    0.    0.    0.  255.]
>  [   0.    0.    0.    0.    2.]]
> brain:~/pynut alex$
>
> Of course I'm using tiny arrays here, for speed of running and ease of
> display and eyeball-checking, but everything should work just as well in
> your case.  Care to check and let us know?
>
> Numeric has pretty good documentation (numpy's is probably even better,
> but it is not available for free, so I don't know!), and if you don't
> find that documentation sufficient you might want to have a look to my
> book "Python in a Nutshell" which devotes a chapter to Numeric (it also
> is not available for free, but you can get a subscription to O'Reilly's
> Safari online-books repository, which is free for the first two weeks,
> and lets you look at many books including Python in a Nutshell -- if you
> don't want to pay monthly subscription fees, make sure you cancel your
> trial subscription before two weeks have passed!!!).
>
> I strongly recommend that, in some way or other, you DO get a taste of
> the huge amount of functionality that Numeric provides for you -- with
> the size of computational tasks you're talking about, an investment of
> 2-3 hours spent becoming deeply familiar with everything Numeric offers
> may well repay itself in savings of ten times as much execution time,
> and what other investments offer such ROI as 1000%?-)
> 
> 
> Alex




More information about the Python-list mailing list