[Numpy-discussion] general newbie question about applying an arbitrary function to all elements in a numpy array
michaeladamkatz at yahoo.com
Tue May 3 20:34:48 EDT 2011
Robert, thanks for these quick replies.
So, it sounds like the main thing is for me to get better at thinking
"declaratively" in terms of these array operations, instead of the "imperative"
way I normally think about processing arrays in C++.
I did look at Numexpr, and that's in the direction I had in mind. It's not quite
what I was thinking, in terms of being able to use arbitrary if/else statements
and function calls, but it certainly seems to let you at least express your
computations in a natural way without having to worry so much about the number
of temp arrays being created.
From: Robert Kern <robert.kern at gmail.com>
To: Discussion of Numerical Python <numpy-discussion at scipy.org>
Sent: Tue, May 3, 2011 5:14:06 PM
Subject: Re: [Numpy-discussion] general newbie question about applying an
arbitrary function to all elements in a numpy array
On Tue, May 3, 2011 at 18:58, Michael Katz <michaeladamkatz at yahoo.com> wrote:
> So I was trying to find a "pure numpy" solution for this. I then learned
> about fancy indexing and boolean indexing, and saw that I could do boolean
> array version:
> mapped_colors = np.zeros(unmapped_colors.shape, dtype=np.uint32) + gray
> mapped_colors[unmapped_colors==BNA_LAND_FEATURE_CODE] = green
> mapped_colors[unmapped_colors==BNA_WATER_FEATURE_CODE] = blue
> and in fact I could do "fancy indexing" version:
> color_array = np.array((green, blue, gray), dtype=np.uint32)
> colors = color_array[unmapped_colors]
> The boolean array version is pretty simple, but makes three "passes" over
> the data.
> The fancy indexing version is simple and one pass, but it relies on the fact
> that my constant values happen to be nice to use as array indexes. And in
> fact to use it in actual code I'd need to do one or more other passes to
> check unmapped_colors for any indexes < 0 or > 2.
Also, still not *quite* as general as you might like, but sufficient
for the problem as stated:
colors = color_array[np.clip(unmapped_colors, 0, 2)]
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
NumPy-Discussion mailing list
NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion