conditional array indexing

I have spent over an hour now trying to figure out what I thought would be a really basic conditional array assignment. I am very new to python and am in need of help. I have a 2D integer array of land cover classes and a 1D array with a roughness value for each class. I am trying to create a new 2D array containing a map of the roughness values. I am trying the following code: roughness = np.array(landcover.shape,dtype='f') for i in range(np.min(landcover),np.max(landcover)): roughness[landcover == i] = z0_legend[i] Alas it tells me that I can only have a 1D boolean array index. What is the easiest way around this without having to loop through each row of the 2D array. I need to keep this vectorized as much as possible as these arrays are very large. Thanks, Bryan

On Mon, Feb 14, 2011 at 16:21, Bryan Woods <bwoods@aer.com> wrote:
I have spent over an hour now trying to figure out what I thought would be a really basic conditional array assignment. I am very new to python and am in need of help.
I have a 2D integer array of land cover classes and a 1D array with a roughness value for each class. I am trying to create a new 2D array containing a map of the roughness values.
roughness = z0_legend[landcover] -- Robert Kern "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

Thanks for your reply. Unfortunately it is not working that simply. This tells me that "only integer arrays with one element can be converted to an index" roughness and landcover are identically shaped 2D arrays. z0_legend in a 1D look-up table. I assume the error here comes from trying to index a 1D array with a 2D array. On 2/14/11 5:24 PM, Robert Kern wrote:
On Mon, Feb 14, 2011 at 16:21, Bryan Woods<bwoods@aer.com> wrote:
I have spent over an hour now trying to figure out what I thought would be a really basic conditional array assignment. I am very new to python and am in need of help.
I have a 2D integer array of land cover classes and a 1D array with a roughness value for each class. I am trying to create a new 2D array containing a map of the roughness values. roughness = z0_legend[landcover]

On Mon, Feb 14, 2011 at 16:39, Bryan Woods <bwoods@aer.com> wrote:
Thanks for your reply. Unfortunately it is not working that simply.
This tells me that "only integer arrays with one element can be converted to an index"
roughness and landcover are identically shaped 2D arrays. z0_legend in a 1D look-up table. I assume the error here comes from trying to index a 1D array with a 2D array.
Make sure that z0_legend is a numpy array, not a list. -- Robert Kern "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

On 2/14/11 2:39 PM, Bryan Woods wrote:
Thanks for your reply. Unfortunately it is not working that simply.
This tells me that "only integer arrays with one element can be converted to an index"
Examples, example, examples! I think this is what you want: In [15]: land_cover Out[15]: array([[4, 2, 0, 4], [0, 2, 1, 1], [1, 1, 4, 2]]) In [16]: z0_legend Out[16]: array([ 3.4, 5.2, 1.3, 4.2, 6.4]) In [17]: roughness = z0_legend[land_cover] In [18]: roughness Out[18]: array([[ 6.4, 1.3, 3.4, 6.4], [ 3.4, 1.3, 5.2, 5.2], [ 5.2, 5.2, 6.4, 1.3]]) -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (4)
-
Bryan Woods
-
Christopher Barker
-
Pauli Virtanen
-
Robert Kern